<?php
class acf_field_group
{
var $settings;
function __construct()
{
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_filter('acf/get_field_groups', array($this, 'get_field_groups'), 1, 1);
add_filter('acf/field_group/get_fields', array($this, 'get_fields'), 5, 2);
add_filter('acf/field_group/get_location', array($this, 'get_location'), 5, 2);
add_filter('acf/field_group/get_options', array($this, 'get_options'), 5, 2);
add_filter('acf/field_group/get_next_field_id', array($this, 'get_next_field_id'), 5, 1);
add_filter('name_save_pre', array($this, 'name_save_pre'));
add_action('save_post', array($this, 'save_post'));
add_action('wp_ajax_acf/field_group/render_options', array($this, 'ajax_render_options'));
add_action('wp_ajax_acf/field_group/render_location', array($this, 'ajax_render_location'));
}
function get_field_groups( $array )
{
$found = false;
$cache = wp_cache_get( 'field_groups', 'acf', false, $found );
if( $found )
{
return $cache;
}
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'acf',
'orderby' => 'menu_order title',
'order' => 'asc',
'suppress_filters' => false,
));
if( $posts ) { foreach( $posts as $post ) {
$array[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'menu_order' => $post->menu_order,
);
}}
wp_cache_set( 'field_groups', $array, 'acf' );
return $array;
}
function get_fields( $fields, $post_id )
{
global $wpdb;
if( !empty($fields) )
{
return $fields;
}
$rows = $wpdb->get_results( $wpdb->prepare("SELECT meta_key FROM $wpdb->postmeta WHERE post_id = %d AND meta_key LIKE %s", $post_id, 'field_%'), ARRAY_A);
if( $rows )
{
foreach( $rows as $row )
{
$field = apply_filters('acf/load_field', false, $row['meta_key'], $post_id );
$fields[ $field['order_no'] ] = $field;
}
ksort( $fields );
}
return $fields;
}
function get_location( $location, $post_id )
{
if( !empty($location) )
{
return $location;
}
$groups = array();
$group_no = 0;
$rules = get_post_meta($post_id, 'rule', false);
if( is_array($rules) )
{
foreach( $rules as $rule )
{
$rule = maybe_unserialize($rule);
if( !isset($rule['group_no']) )
{
$rule['group_no'] = $group_no;
if( get_post_meta($post_id, 'allorany', true) == 'any' )
{
$group_no++;
}
}
$groups[ $rule['group_no'] ][ $rule['order_no'] ] = $rule;
rules
ksort( $groups[ $rule['group_no'] ] );
}
groups
ksort( $groups );
}
fields
return $groups;
}
function get_options( $options, $post_id )
{
if( !empty($options) )
{
return $options;
}
$options = array(
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array(),
);
$position = get_post_meta($post_id, 'position', true);
if( $position )
{
$options['position'] = $position;
}
$layout = get_post_meta($post_id, 'layout', true);
if( $layout )
{
$options['layout'] = $layout;
}
$hide_on_screen = get_post_meta($post_id, 'hide_on_screen', true);
if( $hide_on_screen )
{
$hide_on_screen = maybe_unserialize($hide_on_screen);
$options['hide_on_screen'] = $hide_on_screen;
}
return $options;
}
function validate_page()
{
global $pagenow, $typenow;
$return = false;
if( in_array( $pagenow, array('post.php', 'post-new.php') ) )
{
if( $typenow == "acf" )
{
$return = true;
}
}
return $return;
}
function admin_enqueue_scripts()
{
if( ! $this->validate_page() ) { return; }
$this->settings = apply_filters('acf/get_info', 'all');
wp_dequeue_script( 'autosave' );
wp_enqueue_script(array(
'acf-field-group',
));
wp_enqueue_style(array(
'acf-global',
'acf-field-group',
));
do_action('acf/field_group/admin_enqueue_scripts');
add_action('admin_head', array($this, 'admin_head'));
}
function admin_head()
{
global $wp_version, $post;
$l10n = array(
'move_to_trash' => __("Move to trash. Are you sure?", 'acf'),
'checked' => __("checked", 'acf'),
'no_fields' => __("No toggle fields available", 'acf'),
'title' => __("Field group title is required", 'acf'),
'copy' => __("copy", 'acf'),
'or' => __("or", 'acf'),
'fields' => __("Fields", 'acf'),
'parent_fields' => __("Parent fields", 'acf'),
'sibling_fields' => __("Sibling fields", 'acf'),
'hide_show_all' => __("Hide / Show All", 'acf')
);
?>
<script type="text/javascript">
(function($) {
acf.post_id = <?php echo $post->ID; ?>;
acf.nonce = "<?php echo wp_create_nonce( 'acf_nonce' ); ?>";
acf.admin_url = "<?php echo admin_url(); ?>";
acf.ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
acf.wp_version = "<?php echo $wp_version; ?>";
acf.l10n = <?php echo json_encode( $l10n ); ?>;
})(jQuery);
</script>
<?php
do_action('acf/field_group/admin_head');
add_meta_box('acf_fields', __("Fields", 'acf'), array($this, 'html_fields'), 'acf', 'normal', 'high');
add_meta_box('acf_location', __("Location", 'acf'), array($this, 'html_location'), 'acf', 'normal', 'high');
add_meta_box('acf_options', __("Options", 'acf'), array($this, 'html_options'), 'acf', 'normal', 'high');
add_filter('screen_settings', array($this, 'screen_settings'), 10, 1);
}
function html_fields()
{
include( $this->settings['path'] . 'core/views/meta_box_fields.php' );
}
function html_location()
{
include( $this->settings['path'] . 'core/views/meta_box_location.php' );
}
function html_options()
{
include( $this->settings['path'] . 'core/views/meta_box_options.php' );
}
function screen_settings( $current )
{
$current .= '<h5>' . __("Fields", 'acf') . '</h5>';
$current .= '<div class="show-field_key">' . __("Show Field Key:", 'acf');
$current .= '<label class="show-field_key-no"><input checked="checked" type="radio" value="0" name="show-field_key" />' . __("No", 'acf') . '</label>';
$current .= '<label class="show-field_key-yes"><input type="radio" value="1" name="show-field_key" />' . __("Yes", 'acf') . '</label>';
$current .= '</div>';
return $current;
}
function ajax_render_options()
{
$options = array(
'field_key' => '',
'field_type' => '',
'post_id' => 0,
'nonce' => ''
);
$options = array_merge($options, $_POST);
if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
{
die(0);
}
if( ! $options['field_type'] )
{
die(0);
}
$options['field_key'] = str_replace("fields[", "", $options['field_key']);
$options['field_key'] = str_replace("][type]", "", $options['field_key']) ;
$field = array(
'type' => $options['field_type'],
'name' => $options['field_key']
);
do_action('acf/create_field_options', $field );
die();
}
function ajax_render_location( $options = array() )
{
$defaults = array(
'group_id' => 0,
'rule_id' => 0,
'value' => null,
'param' => null,
);
$is_ajax = false;
if( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'acf_nonce') )
{
$is_ajax = true;
}
if( $is_ajax )
{
$options = array_merge($defaults, $_POST);
}
else
{
$options = array_merge($defaults, $options);
}
$choices = array();
if($options['param'] == "page_parent")
{
$options['param'] = "page";
}
switch($options['param'])
{
case "post_type":
$choices = apply_filters('acf/get_post_types', array(), array('attachment'));
break;
case "page":
$post_type = 'page';
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => $post_type,
'orderby' => 'menu_order title',
'order' => 'ASC',
'post_status' => 'any',
'suppress_filters' => false,
'update_post_meta_cache' => false,
));
if( $posts )
{
into hierachial order!
if( is_post_type_hierarchical( $post_type ) )
{
$posts = get_page_children( 0, $posts );
}
foreach( $posts as $page )
{
$title = '';
$ancestors = get_ancestors($page->ID, 'page');
if($ancestors)
{
foreach($ancestors as $a)
{
$title .= '- ';
}
}
$title .= apply_filters( 'the_title', $page->post_title, $page->ID );
if($page->post_status != "publish")
{
$title .= " ($page->post_status)";
}
$choices[ $page->ID ] = $title;
}
}
break;
case "page_type" :
$choices = array(
'front_page' => __("Front Page", 'acf'),
'posts_page' => __("Posts Page", 'acf'),
'top_level' => __("Top Level Page (parent of 0)", 'acf'),
'parent' => __("Parent Page (has children)", 'acf'),
'child' => __("Child Page (has parent)", 'acf'),
);
break;
case "page_template" :
$choices = array(
'default' => __("Default Template", 'acf'),
);
$templates = get_page_templates();
foreach($templates as $k => $v)
{
$choices[$v] = $k;
}
break;
case "post" :
$post_types = get_post_types();
unset( $post_types['page'], $post_types['attachment'], $post_types['revision'] , $post_types['nav_menu_item'], $post_types['acf'] );
if( $post_types )
{
foreach( $post_types as $post_type )
{
$posts = get_posts(array(
'numberposts' => '-1',
'post_type' => $post_type,
'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'),
'suppress_filters' => false,
));
if( $posts)
{
$choices[$post_type] = array();
foreach($posts as $post)
{
$title = apply_filters( 'the_title', $post->post_title, $post->ID );
if($post->post_status != "publish")
{
$title .= " ($post->post_status)";
}
$choices[$post_type][$post->ID] = $title;
}
}
}
}
break;
case "post_category" :
$terms = get_terms( 'category', array( 'hide_empty' => false ) );
if( !empty($terms) ) {
foreach( $terms as $term ) {
$choices[ $term->term_id ] = $term->name;
}
}
break;
case "post_format" :
$choices = get_post_format_strings();
break;
case "post_status" :
$choices = array(
'publish' => __( 'Published', 'acf'),
'pending' => __( 'Pending Review', 'acf'),
'draft' => __( 'Draft', 'acf'),
'future' => __( 'Future', 'acf'),
'private' => __( 'Private', 'acf'),
'inherit' => __( 'Revision', 'acf'),
'trash' => __( 'Trash', 'acf'),
);
break;
case "user_type" :
global $wp_roles;
$choices = $wp_roles->get_names();
if( is_multisite() )
{
$choices['super_admin'] = __('Super Admin', 'acf');
}
break;
case "taxonomy" :
$choices = array();
$simple_value = true;
$choices = apply_filters('acf/get_taxonomies_for_select', $choices, $simple_value);
break;
case "ef_taxonomy" :
$choices = array('all' => __('All', 'acf'));
$taxonomies = get_taxonomies( array('public' => true), 'objects' );
foreach($taxonomies as $taxonomy)
{
$choices[ $taxonomy->name ] = $taxonomy->labels->name;
}
if( isset($choices['post_format']) )
{
unset( $choices['post_format']) ;
}
break;
case "ef_user" :
global $wp_roles;
$choices = array_merge( array('all' => __('All', 'acf')), $wp_roles->get_names() );
break;
case "ef_media" :
$choices = array('all' => __('All', 'acf'));
break;
}
$choices = apply_filters( 'acf/location/rule_values/' . $options['param'], $choices );
do_action('acf/create_field', array(
'type' => 'select',
'name' => 'location[' . $options['group_id'] . '][' . $options['rule_id'] . '][value]',
'value' => $options['value'],
'choices' => $choices,
));
?
if( $is_ajax )
{
die();
}
}
function name_save_pre($name)
{
if( !isset($_POST['post_type']) || $_POST['post_type'] != 'acf' )
{
return $name;
}
if( !$_POST['post_title'] )
{
$_POST['post_title'] = 'Unnamed Field Group';
}
$name = 'acf_' . sanitize_title($_POST['post_title']);
return $name;
}
function save_post($post_id)
{
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}
if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'field_group') )
{
return $post_id;
}
if( wp_is_post_revision($post_id) )
{
return $post_id;
}
$dont_delete = array();
if( isset($_POST['fields']) && is_array($_POST['fields']) )
{
$i = -1;
unset( $_POST['fields']['field_clone'] );
foreach( $_POST['fields'] as $key => $field )
{
$i++;
$field['order_no'] = $i;
$field['key'] = $key;
do_action('acf/update_field', $field, $post_id );
$dont_delete[] = $field['key'];
}
}
unset( $_POST['fields'] );
$keys = get_post_custom_keys($post_id);
foreach( $keys as $key )
{
if( strpos($key, 'field_') !== false && !in_array($key, $dont_delete) )
{
do_action('acf/delete_field', $post_id, $key);
}
}
if( isset($_POST['location']) && is_array($_POST['location']) )
{
delete_post_meta( $post_id, 'rule' );
$_POST['location'] = array_values( $_POST['location'] );
foreach( $_POST['location'] as $group_id => $group )
{
if( is_array($group) )
{
$group = array_values( $group );
foreach( $group as $rule_id => $rule )
{
$rule['order_no'] = $rule_id;
$rule['group_no'] = $group_id;
add_post_meta( $post_id, 'rule', $rule );
}
}
}
unset( $_POST['location'] );
}
if( isset($_POST['options']) && is_array($_POST['options']) )
{
update_post_meta($post_id, 'position', $_POST['options']['position']);
update_post_meta($post_id, 'layout', $_POST['options']['layout']);
update_post_meta($post_id, 'hide_on_screen', $_POST['options']['hide_on_screen']);
}
unset( $_POST['options'] );
}
}
new acf_field_group();
?>