<?php
function get_field_reference( $field_name, $post_id ) {
$found = false;
$cache = wp_cache_get( 'field_reference/post_id=' . $post_id . '/name=' . $field_name, 'acf', false, $found );
if( $found )
{
return $cache;
}
$return = '';
if( is_numeric($post_id) )
{
$return = get_post_meta($post_id, '_' . $field_name, true);
}
elseif( strpos($post_id, 'user_') !== false )
{
$temp_post_id = str_replace('user_', '', $post_id);
$return = get_user_meta($temp_post_id, '_' . $field_name, true);
}
else
{
$return = get_option('_' . $post_id . '_' . $field_name);
}
wp_cache_set( 'field_reference/post_id=' . $post_id . '/name=' . $field_name, $return, 'acf' );
return $return;
}
function get_field_objects( $post_id = false, $options = array() ) {
global $wpdb;
$post_id = apply_filters('acf/get_post_id', $post_id );
$field_key = '';
$value = array();
if( is_numeric($post_id) )
{
$keys = $wpdb->get_col($wpdb->prepare(
"SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d and meta_key LIKE %s AND meta_value LIKE %s",
$post_id,
'_%',
'field_%'
));
}
elseif( strpos($post_id, 'user_') !== false )
{
$user_id = str_replace('user_', '', $post_id);
$keys = $wpdb->get_col($wpdb->prepare(
"SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d and meta_key LIKE %s AND meta_value LIKE %s",
$user_id,
'_%',
'field_%'
));
}
else
{
$keys = $wpdb->get_col($wpdb->prepare(
"SELECT option_value FROM $wpdb->options WHERE option_name LIKE %s",
'_' . $post_id . '_%'
));
}
if( is_array($keys) )
{
foreach( $keys as $key )
{
$field = get_field_object( $key, $post_id, $options );
if( !is_array($field) )
{
continue;
}
$value[ $field['name'] ] = $field;
}
}
if( empty($value) )
{
return false;
}
return $value;
}
function get_fields( $post_id = false, $format_value = true ) {
$options = array(
'load_value' => true,
'format_value' => $format_value
);
$fields = get_field_objects( $post_id, $options );
if( is_array($fields) )
{
foreach( $fields as $k => $field )
{
$fields[ $k ] = $field['value'];
}
}
return $fields;
}
function get_field( $field_key, $post_id = false, $format_value = true ) {
$return = false;
$options = array(
'load_value' => true,
'format_value' => $format_value
);
$field = get_field_object( $field_key, $post_id, $options);
if( is_array($field) )
{
$return = $field['value'];
}
return $return;
}
function get_field_object( $field_key, $post_id = false, $options = array() ) {
acf()->include_3rd_party();
$post_id = apply_filters('acf/get_post_id', $post_id );
$field = false;
$orig_field_key = $field_key;
$defaults = array(
'load_value' => true,
'format_value' => true,
);
$options = array_merge($defaults, $options);
if( substr($field_key, 0, 6) !== 'field_' )
{
$field_key = get_field_reference( $field_key, $post_id );
}
if( substr($field_key, 0, 6) === 'field_' )
{
$field = apply_filters('acf/load_field', false, $field_key );
}
if( !$field )
{
$field = array(
'type' => 'text',
'name' => $orig_field_key,
'key' => 'field_' . $orig_field_key,
);
$field = apply_filters('acf/load_field', $field, $field['key'] );
}
if( $options['load_value'] )
{
$field['value'] = apply_filters('acf/load_value', false, $post_id, $field);
if( $options['format_value'] )
{
$field['value'] = apply_filters('acf/format_value_for_api', $field['value'], $post_id, $field);
}
}
return $field;
}
function the_field( $field_name, $post_id = false ) {
$value = get_field($field_name, $post_id);
if( is_array($value) )
{
$value = @implode(', ', $value);
}
echo $value;
}
function have_rows( $field_name, $post_id = false ) {
$depth = 0;
$row = array();
$new_parent_loop = false;
$new_child_loop = false;
$_post_id = $post_id;
$post_id = apply_filters('acf/get_post_id', $post_id );
if( empty($GLOBALS['acf_field']) )
{
reset_rows( true );
$new_parent_loop = true;
}
else
{
$row = end( $GLOBALS['acf_field'] );
$prev = prev( $GLOBALS['acf_field'] );
if( $post_id != $row['post_id'] )
{
if( $prev && $prev['post_id'] == $post_id )
{
reset_rows();
}
elseif( empty($_post_id) && isset($row['value'][ $row['i'] ][ $field_name ]) )
{
$new_child_loop = true;
}
else
{
$new_parent_loop = true;
}
}
elseif( $field_name != $row['name'] )
{
if( $prev && $prev['name'] == $field_name && $prev['post_id'] == $post_id )
{
reset_rows();
}
elseif( isset($row['value'][ $row['i'] ][ $field_name ]) )
{
$new_child_loop = true;
}
else
{
$new_parent_loop = true;
}
}
}
if( $new_parent_loop )
{
$f = get_field_object( $field_name, $post_id );
$v = $f['value'];
unset( $f['value'] );
$GLOBALS['acf_field'][] = array(
'name' => $field_name,
'value' => $v,
'field' => $f,
'i' => -1,
'post_id' => $post_id,
);
}
elseif( $new_child_loop )
{
$f = acf_get_child_field_from_parent_field( $field_name, $row['field'] );
$v = $row['value'][ $row['i'] ][ $field_name ];
$GLOBALS['acf_field'][] = array(
'name' => $field_name,
'value' => $v,
'field' => $f,
'i' => -1,
'post_id' => $post_id,
);
}
$row = end( $GLOBALS['acf_field'] );
if( is_array($row['value']) && array_key_exists( $row['i']+1, $row['value'] ) )
{
return true;
}
reset_rows();
return false;
}
function the_row() {
$depth = count( $GLOBALS['acf_field'] ) - 1;
$GLOBALS['acf_field'][ $depth ]['i']++;
$value = $GLOBALS['acf_field'][ $depth ]['value'];
$i = $GLOBALS['acf_field'][ $depth ]['i'];
return $value[ $i ];
}
function reset_rows( $hard_reset = false ) {
if( $hard_reset )
{
$GLOBALS['acf_field'] = array();
}
else
{
$depth = count( $GLOBALS['acf_field'] ) - 1;
unset( $GLOBALS['acf_field'][$depth] );
$GLOBALS['acf_field'] = array_values($GLOBALS['acf_field']);
}
return true;
}
function has_sub_field( $field_name, $post_id = false ) {
$r = have_rows( $field_name, $post_id );
if( $r )
{
the_row();
}
return $r;
}
function has_sub_fields( $field_name, $post_id = false )
{
return has_sub_field( $field_name, $post_id );
}
function get_sub_field( $field_name ) {
if( empty($GLOBALS['acf_field']) )
{
return false;
}
$row = end( $GLOBALS['acf_field'] );
value
if( isset($row['value'][ $row['i'] ][ $field_name ]) )
{
return $row['value'][ $row['i'] ][ $field_name ];
}
false
return false;
}
function the_sub_field($field_name)
{
$value = get_sub_field($field_name);
if(is_array($value))
{
$value = implode(', ', $value);
}
echo $value;
}
function get_sub_field_object( $child_name )
{
if( empty($GLOBALS['acf_field']) )
{
return false;
}
$depth = count( $GLOBALS['acf_field'] ) - 1;
$parent = $GLOBALS['acf_field'][$depth]['field'];
return acf_get_child_field_from_parent_field( $child_name, $parent );
}
function acf_get_child_field_from_parent_field( $child_name, $parent )
{
$return = false;
if( isset($parent['sub_fields']) && is_array($parent['sub_fields']) )
{
foreach( $parent['sub_fields'] as $child )
{
if( $child['name'] == $child_name || $child['key'] == $child_name )
{
$return = $child;
break;
}
$grand_child = acf_get_child_field_from_parent_field( $child_name, $child );
if( $grand_child )
{
$return = $grand_child;
break;
}
}
}
elseif( isset($parent['layouts']) && is_array($parent['layouts']) )
{
foreach( $parent['layouts'] as $layout )
{
$child = acf_get_child_field_from_parent_field( $child_name, $layout );
if( $child )
{
$return = $child;
break;
}
}
}
return $return;
}
$GLOBALS['acf_register_field_group'] = array();
function register_field_group( $array )
{
if( !isset($array['id']) )
{
$array['id'] = uniqid();
}
if( !isset($array['options']['hide_on_screen']) && isset($array['options']['show_on_page']) )
{
$show_all = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author');
$array['options']['hide_on_screen'] = array_diff($show_all, $array['options']['show_on_page']);
unset( $array['options']['show_on_page'] );
}
if( isset($array['location']['rules']) )
{
$groups = array();
$group_no = 0;
if( is_array($array['location']['rules']) )
{
foreach( $array['location']['rules'] as $rule )
{
$rule['group_no'] = $group_no;
if( $array['location']['allorany'] == 'any' )
{
$group_no++;
}
$groups[ $rule['group_no'] ][ $rule['order_no'] ] = $rule;
ksort( $groups[ $rule['group_no'] ] );
}
ksort( $groups );
}
$array['location'] = $groups;
}
$GLOBALS['acf_register_field_group'][] = $array;
}
add_filter('acf/get_field_groups', 'api_acf_get_field_groups', 2, 1);
function api_acf_get_field_groups( $return )
{
if( empty($GLOBALS['acf_register_field_group']) )
{
return $return;
}
foreach( $GLOBALS['acf_register_field_group'] as $acf )
{
$return[] = array(
'id' => $acf['id'],
'title' => $acf['title'],
'menu_order' => $acf['menu_order'],
);
}
foreach( $return as $key => $row )
{
$menu_order[ $key ] = $row['menu_order'];
$title[ $key ] = $row['title'];
}
if(isset($menu_order))
{
array_multisort($menu_order, SORT_ASC, $title, SORT_ASC, $return);
}
return $return;
}
add_filter('acf/field_group/get_fields', 'api_acf_field_group_get_fields', 1, 2);
function api_acf_field_group_get_fields( $fields, $post_id )
{
if( !empty($GLOBALS['acf_register_field_group']) )
{
foreach( $GLOBALS['acf_register_field_group'] as $acf )
{
if( $acf['id'] == $post_id )
{
foreach( $acf['fields'] as $f )
{
$fields[] = apply_filters('acf/load_field', $f, $f['key']);
}
break;
}
}
}
return $fields;
}
add_filter('acf/load_field', 'api_acf_load_field', 1, 2);
function api_acf_load_field( $field, $field_key )
{
if( !empty($GLOBALS['acf_register_field_group']) )
{
foreach( $GLOBALS['acf_register_field_group'] as $acf )
{
if( !empty($acf['fields']) )
{
foreach( $acf['fields'] as $f )
{
if( $f['key'] == $field_key )
{
$field = $f;
break;
}
}
}
}
}
return $field;
}
add_filter('acf/field_group/get_location', 'api_acf_field_group_get_location', 1, 2);
function api_acf_field_group_get_location( $location, $post_id )
{
if( !empty($GLOBALS['acf_register_field_group']) )
{
foreach( $GLOBALS['acf_register_field_group'] as $acf )
{
if( $acf['id'] == $post_id )
{
$location = $acf['location'];
break;
}
}
}
return $location;
}
add_filter('acf/field_group/get_options', 'api_acf_field_group_get_options', 1, 2);
function api_acf_field_group_get_options( $options, $post_id )
{
if( !empty($GLOBALS['acf_register_field_group']) )
{
foreach( $GLOBALS['acf_register_field_group'] as $acf )
{
if( $acf['id'] == $post_id )
{
$options = $acf['options'];
break;
}
}
}
return $options;
}
function get_row_layout()
{
$value = get_sub_field('acf_fc_layout');
return $value;
}
function acf_shortcode( $atts )
{
extract( shortcode_atts( array(
'field' => "",
'post_id' => false,
), $atts ) );
if( !$field || $field == "" )
{
return "";
}
$value = get_field( $field, $post_id );
if( is_array($value) )
{
$value = @implode( ', ', $value );
}
return $value;
}
add_shortcode( 'acf', 'acf_shortcode' );
function acf_form_head()
{
vars
global $post_id;
if( isset($_POST['acf_nonce']) && wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
$post_id = $_POST['post_id'];
$post_id = apply_filters('acf/pre_save_post', $post_id);
do_action('acf/save_post', $post_id);
if(isset($_POST['return']))
{
wp_redirect($_POST['return']);
exit;
}
}
wp_enqueue_style(array(
'colors-fresh'
));
do_action('acf/input/admin_enqueue_scripts');
add_action('wp_head', 'acf_form_wp_head');
}
function acf_form_wp_head()
{
do_action('acf/input/admin_head');
}
function acf_form( $options = array() )
{
global $post;
$defaults = array(
'post_id' => false,
'field_groups' => array(),
'form' => true,
'form_attributes' => array(
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => __("Update", 'acf'),
'updated_message' => __("Post updated", 'acf'),
);
$options = array_merge($defaults, $options);
foreach( $options as $k => $v )
{
if( is_array($v) )
{
$options[ $k ] = array_merge($defaults[ $k ], $options[ $k ]);
}
}
$options['post_id'] = apply_filters('acf/get_post_id', $options['post_id'] );
$options['form_attributes']['class'] .= 'acf-form';
if( empty($options['field_groups']) )
{
groups
$filter = array(
'post_id' => $options['post_id']
);
if( strpos($options['post_id'], 'user_') !== false )
{
$user_id = str_replace('user_', '', $options['post_id']);
$filter = array(
'ef_user' => $user_id
);
}
elseif( strpos($options['post_id'], 'taxonomy_') !== false )
{
$taxonomy_id = str_replace('taxonomy_', '', $options['post_id']);
$filter = array(
'ef_taxonomy' => $taxonomy_id
);
}
$options['field_groups'] = array();
$options['field_groups'] = apply_filters( 'acf/location/match_field_groups', $options['field_groups'], $filter );
}
if(isset($_GET['updated']) && $_GET['updated'] == 'true' && $options['updated_message'])
{
echo '<div id="message" class="updated"><p>' . $options['updated_message'] . '</p></div>';
}
if( $options['form'] ): ?>
<form <?php if($options['form_attributes']) {foreach($options['form_attributes'] as $k => $v) {echo $k . '="' . $v .'" '; }} ?>>
<?php endif; ?>
<div style="display:none">
<script type="text/javascript">
acf.o.post_id = <?php echo is_numeric($options['post_id']) ? $options['post_id'] : '"' . $options['post_id'] . '"'; ?>;
</script>
<input type="hidden" name="acf_nonce" value="<?php echo wp_create_nonce( 'input' ); ?>" />
<input type="hidden" name="post_id" value="<?php echo $options['post_id']; ?>" />
<input type="hidden" name="return" value="<?php echo $options['return']; ?>" />
<?php wp_editor('', 'acf_settings'); ?>
</div>
<div id="poststuff">
<?php
echo $options['html_before_fields'];
$acfs = apply_filters('acf/get_field_groups', array());
if( is_array($acfs) ) { foreach( $acfs as $acf ) {
if( !in_array( $acf['id'], $options['field_groups'] ) )
{
continue;
}
$acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
$fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
echo '<div id="acf_' . $acf['id'] . '" class="postbox acf_postbox ' . $acf['options']['layout'] . '">';
echo '<h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
echo '<div class="inside">';
do_action('acf/create_fields', $fields, $options['post_id']);
echo '</div></div>';
}}
echo $options['html_after_fields'];
?>
<?php if( $options['form'] ): ?>
<!-- Submit -->
<div class="field">
<input type="submit" value="<?php echo $options['submit_value']; ?>" />
</div>
<!-- / Submit -->
<?php endif; ?>
</div><!-- <div id="poststuff"> -->
<?php if( $options['form'] ): ?>
</form>
<?php endif;
}
function update_field( $field_key, $value, $post_id = false )
{
$post_id = apply_filters('acf/get_post_id', $post_id );
$options = array(
'load_value' => false,
'format_value' => false
);
$field = get_field_object( $field_key, $post_id, $options);
if( $field['type'] == 'repeater' )
{
$value = acf_convert_field_names_to_keys( $value, $field );
}
elseif( $field['type'] == 'flexible_content' )
{
if( $field['layouts'] )
{
foreach( $field['layouts'] as $layout )
{
$value = acf_convert_field_names_to_keys( $value, $layout );
}
}
}
do_action('acf/update_value', $value, $post_id, $field );
return true;
}
function delete_field( $field_name, $post_id )
{
do_action('acf/delete_value', $post_id, $field_name );
}
function create_field( $field )
{
do_action('acf/create_field', $field );
}
function acf_convert_field_names_to_keys( $value, $field )
{
if( !isset($field['sub_fields']) )
{
return $value;
}
$sub_fields = array();
if( $field['sub_fields'] )
{
foreach( $field['sub_fields'] as $sub_field )
{
$sub_fields[ $sub_field['name'] ] = $sub_field;
}
}
if( is_array($value) )
{
foreach( $value as $row_i => $row)
{
if( $row )
{
foreach( $row as $sub_field_name => $sub_field_value )
{
if( !isset($sub_fields[ $sub_field_name ]) )
{
continue;
}
$sub_field = $sub_fields[ $sub_field_name ];
$sub_field_value = acf_convert_field_names_to_keys( $sub_field_value, $sub_field );
$value[$row_i][ $sub_field['key'] ] = $sub_field_value;
unset( $value[$row_i][$sub_field_name] );
}
}
}
}
return $value;
}
function acf_force_type_array( $var ) {
if( is_array($var) ) {
return $var;
}
if( empty($var) && !is_numeric($var) ) {
return array();
}
if( is_string($var) ) {
return explode(', ', $var);
}
return array( $var );
}
function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {
if( !function_exists('wp_get_split_term') || empty($terms) ) {
return $terms;
}
$is_array = is_array($terms);
$terms = acf_force_type_array( $terms );
$terms = array_map('intval', $terms);
foreach( $terms as $i => $term_id ) {
$new_term_id = wp_get_split_term($term_id, $taxonomy);
if( $new_term_id ) {
$terms[ $i ] = $new_term_id;
}
}
if( !$is_array ) {
$terms = $terms[0];
}
return $terms;
}
function reset_the_repeater_field()
{
}
function the_repeater_field($field_name, $post_id = false)
{
return has_sub_field($field_name, $post_id);
}
function the_flexible_field($field_name, $post_id = false)
{
return has_sub_field($field_name, $post_id);
}
function acf_filter_post_id( $post_id )
{
return apply_filters('acf/get_post_id', $post_id );
}
?>