<?php
class acf_field_functions
{
function __construct()
{
add_filter('acf/load_value', array($this, 'load_value'), 5, 3);
add_action('acf/update_value', array($this, 'update_value'), 5, 3);
add_action('acf/delete_value', array($this, 'delete_value'), 5, 2);
add_action('acf/format_value', array($this, 'format_value'), 5, 3);
add_action('acf/format_value_for_api', array($this, 'format_value_for_api'), 5, 3);
add_filter('acf/load_field', array($this, 'load_field'), 5, 3);
add_action('acf/update_field', array($this, 'update_field'), 5, 2);
add_action('acf/delete_field', array($this, 'delete_field'), 5, 2);
add_action('acf/create_field', array($this, 'create_field'), 5, 1);
add_action('acf/create_field_options', array($this, 'create_field_options'), 5, 1);
add_filter('acf/load_field_defaults', array($this, 'load_field_defaults'), 5, 1);
}
function load_value($value, $post_id, $field)
{
$found = false;
$cache = wp_cache_get( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], 'acf', false, $found );
if( $found )
{
return $cache;
}
$value = false;
if( is_numeric($post_id) )
{
$v = get_post_meta( $post_id, $field['name'], false );
if( isset($v[0]) )
{
$value = $v[0];
}
}
elseif( strpos($post_id, 'user_') !== false )
{
$post_id = str_replace('user_', '', $post_id);
$v = get_user_meta( $post_id, $field['name'], false );
if( isset($v[0]) )
{
$value = $v[0];
}
}
else
{
$v = get_option( $post_id . '_' . $field['name'], false );
if( !is_null($value) )
{
$value = $v;
}
}
if( $value === false )
{
if( isset($field['default_value']) && $field['default_value'] !== "" )
{
$value = $field['default_value'];
}
}
$value = maybe_unserialize($value);
foreach( array('type', 'name', 'key') as $key )
{
$value = apply_filters('acf/load_value/' . $key . '=' . $field[ $key ], $value, $post_id, $field);
}
wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' );
return $value;
}
function format_value( $value, $post_id, $field )
{
$value = apply_filters('acf/format_value/type=' . $field['type'], $value, $post_id, $field);
return $value;
}
function format_value_for_api( $value, $post_id, $field )
{
$value = apply_filters('acf/format_value_for_api/type=' . $field['type'], $value, $post_id, $field);
return $value;
}
function update_value( $value, $post_id, $field )
{
$value = stripslashes_deep($value);
foreach( array('key', 'name', 'type') as $key )
{
$value = apply_filters('acf/update_value/' . $key . '=' . $field[ $key ], $value, $post_id, $field);
}
if( is_numeric($post_id) )
{
update_metadata('post', $post_id, $field['name'], $value );
update_metadata('post', $post_id, '_' . $field['name'], $field['key']);
}
elseif( strpos($post_id, 'user_') !== false )
{
$user_id = str_replace('user_', '', $post_id);
update_metadata('user', $user_id, $field['name'], $value);
update_metadata('user', $user_id, '_' . $field['name'], $field['key']);
}
else
{
$value = stripslashes_deep($value);
$this->update_option( $post_id . '_' . $field['name'], $value );
$this->update_option( '_' . $post_id . '_' . $field['name'], $field['key'] );
}
wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' );
}
function update_option( $option = '', $value = false, $autoload = 'no' ) {
$deprecated = '';
$return = false;
if( get_option($option) !== false )
{
$return = update_option( $option, $value );
}
else
{
$return = add_option( $option, $value, $deprecated, $autoload );
}
return $return;
}
function delete_value( $post_id, $key )
{
if( is_numeric($post_id) )
{
delete_post_meta( $post_id, $key );
delete_post_meta( $post_id, '_' . $key );
}
elseif( strpos($post_id, 'user_') !== false )
{
$post_id = str_replace('user_', '', $post_id);
delete_user_meta( $post_id, $key );
delete_user_meta( $post_id, '_' . $key );
}
else
{
delete_option( $post_id . '_' . $key );
delete_option( '_' . $post_id . '_' . $key );
}
wp_cache_delete( 'load_value/post_id=' . $post_id . '/name=' . $key, 'acf' );
}
function load_field( $field, $field_key, $post_id = false )
{
if( !$field )
{
$field = wp_cache_get( 'load_field/key=' . $field_key, 'acf' );
}
if( !$field )
{
global $wpdb;
$sql = $wpdb->prepare("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $field_key);
if( $post_id )
{
$sql .= $wpdb->prepare("AND post_id = %d", $post_id);
}
$rows = $wpdb->get_results( $sql, ARRAY_A );
if( !empty($rows) )
{
$row = $rows[0];
if( defined('ICL_LANGUAGE_CODE') && !$post_id )
{
$wpml_post_id = icl_object_id($row['post_id'], 'acf', true, ICL_LANGUAGE_CODE);
foreach( $rows as $r )
{
if( $r['post_id'] == $wpml_post_id )
{
$row = $r;
}
}
}
field if it is not in a trashed field group
if( get_post_status( $row['post_id'] ) != "trash" )
{
$field = $row['meta_value'];
$field = maybe_unserialize( $field );
$field = maybe_unserialize( $field );
$field['field_group'] = $row['post_id'];
}
}
}
$field = apply_filters('acf/load_field_defaults', $field);
foreach( array('type', 'name', 'key') as $key )
{
$field = apply_filters('acf/load_field/' . $key . '=' . $field[ $key ], $field);
}
wp_cache_set( 'load_field/key=' . $field_key, $field, 'acf' );
return $field;
}
function load_field_defaults( $field )
{
if( !is_array($field) )
{
$field = array();
}
$defaults = array(
'key' => '',
'label' => '',
'name' => '',
'_name' => '',
'type' => 'text',
'order_no' => 1,
'instructions' => '',
'required' => 0,
'id' => '',
'class' => '',
'conditional_logic' => array(
'status' => 0,
'allorany' => 'all',
'rules' => 0
),
);
$field = array_merge($defaults, $field);
$field = apply_filters( 'acf/parse_types', $field );
specific defaults
$field = apply_filters('acf/load_field_defaults/type=' . $field['type'] , $field);
if( !$field['class'] )
{
$field['class'] = $field['type'];
}
if( !$field['id'] )
{
$id = $field['name'];
$id = str_replace('][', '_', $id);
$id = str_replace('fields[', '', $id);
$id = str_replace('[', '-', $id);
$id = str_replace(']', '', $id);
$field['id'] = 'acf-field-' . $id;
}
if( !$field['_name'] )
{
$field['_name'] = $field['name'];
}
if( !empty($field['conditional_logic']['rules']) )
{
$field['conditional_logic']['rules'] = array_values($field['conditional_logic']['rules']);
}
return $field;
}
function update_field( $field, $post_id )
{
$field = apply_filters('acf/update_field/type=' . $field['type'], $field, $post_id );
wp_cache_delete( 'load_field/key=' . $field['key'], 'acf' );
update_post_meta( $post_id, $field['key'], $field );
}
function delete_field( $post_id, $field_key )
{
wp_cache_delete( 'load_field/key=' . $field_key, 'acf' );
delete_post_meta($post_id, $field_key);
}
function create_field( $field )
{
$field = apply_filters('acf/load_field_defaults', $field);
do_action('acf/create_field/type=' . $field['type'], $field);
if( $field['conditional_logic']['status'] )
{
$field['conditional_logic']['field'] = $field['key'];
?>
<script type="text/javascript">
(function($) {
if( typeof acf !== 'undefined' )
{
acf.conditional_logic.items.push(<?php echo json_encode($field['conditional_logic']); ?>);
}
})(jQuery);
</script>
<?php
}
}
function create_field_options($field)
{
$field = apply_filters('acf/load_field_defaults', $field);
do_action('acf/create_field_options/type=' . $field['type'], $field);
}
}
new acf_field_functions();
?>