function acf_get_valid_options_page($page = '') { // allow for string if (empty($page)) { $page = array('page_title' => __('Options', 'acf'), 'menu_title' => __('Options', 'acf'), 'menu_slug' => 'acf-options'); } elseif (is_string($page)) { $page_title = $page; $page = array('page_title' => $page_title, 'menu_title' => $page_title); } // defaults $page = wp_parse_args($page, array('page_title' => '', 'menu_title' => '', 'menu_slug' => '', 'capability' => 'edit_posts', 'parent_slug' => '', 'position' => false, 'icon_url' => false, 'redirect' => true, 'post_id' => 'options', 'autoload' => false, 'update_button' => __('Update', 'acf'))); // ACF4 compatibility $migrate = array('title' => 'page_title', 'menu' => 'menu_title', 'slug' => 'menu_slug', 'parent' => 'parent_slug'); foreach ($migrate as $old => $new) { if (!empty($page[$old])) { $page[$new] = acf_extract_var($page, $old); } } // page_title (allows user to define page with just page_title or title) if (empty($page['menu_title'])) { $page['menu_title'] = $page['page_title']; } // menu_slug if (empty($page['menu_slug'])) { $page['menu_slug'] = 'acf-options-' . sanitize_title($page['menu_title']); } // return return $page; }
function add_field_group($field_group) { // validate $field_group = acf_get_valid_field_group($field_group); // don't allow overrides if (acf_is_local_field_group($field_group['key'])) { return; } // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // add field group $this->groups[$field_group['key']] = $field_group; // add fields foreach ($fields as $field) { // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['key']; } // add field group reference //$field['field_group'] = $field_group['key']; // add field $this->add_field($field); } }
function update_field_group($field_group) { // vars $path = acf_get_setting('save_json'); $file = $field_group['key'] . '.json'; // remove trailing slash $path = untrailingslashit($path); // bail early if dir does not exist if (!is_writable($path)) { //error_log( 'ACF failed to save field group to .json file. Path does not exist: ' . $path ); return; } // load fields $fields = acf_get_fields($field_group); // prepare fields $fields = acf_prepare_fields_for_export($fields); // add to field group $field_group['fields'] = $fields; // extract field group ID $id = acf_extract_var($field_group, 'ID'); // write file $f = fopen("{$path}/{$file}", 'w'); fwrite($f, acf_json_encode($field_group)); fclose($f); }
function relationships_import_json_field_groups() { // tell ACF NOT to save to JSON add_filter('acf/settings/save_json', 'lc_import_json_save_no_point', 99); // Remove previous field groups in DB $args = array('post_type' => 'acf-field-group', 'post_status' => 'any', 'posts_per_page' => -1); $query = new WP_Query($args); foreach ($query->posts as $acf_group) { wp_delete_post($acf_group->ID, true); } // Find local JSON directory $dir = new DirectoryIterator(dirname(__FILE__) . '/json'); foreach ($dir as $file) { if (!$file->isDot() && 'json' == $file->getExtension()) { $json = json_decode(file_get_contents($file->getPathname()), true); // What follows is basically a copy of import() in ACF admin/settings-export.php // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // save field group $field_group = acf_update_field_group($field_group); // add to ref $ref[$field_group['key']] = $field_group['ID']; // add to order $order[$field_group['ID']] = 0; // add fields foreach ($fields as $field) { // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } elseif (isset($ref[$field['parent']])) { $field['parent'] = $ref[$field['parent']]; } // add field menu_order if (!isset($order[$field['parent']])) { $order[$field['parent']] = 0; } $field['menu_order'] = $order[$field['parent']]; $order[$field['parent']]++; // save field $field = acf_update_field($field); } } } } }
function acf_sync_import_json_field_groups($directory) { $dir = new DirectoryIterator($directory); foreach ($dir as $file) { if (!$file->isDot() && $file->getExtension() == 'json') { $json = json_decode(file_get_contents($file->getPathname()), true); // What follows is basically a copy of import() in ACF admin/settings-export.php // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // save field group $field_group = acf_update_field_group($field_group); // add to ref $ref[$field_group['key']] = $field_group['ID']; // add to order $order[$field_group['ID']] = 0; // add fields foreach ($fields as $field) { // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } elseif (isset($ref[$field['parent']])) { $field['parent'] = $ref[$field['parent']]; } // add field menu_order if (!isset($order[$field['parent']])) { $order[$field['parent']] = 0; } $field['menu_order'] = $order[$field['parent']]; $order[$field['parent']]++; // save field $field = acf_update_field($field); } } } } }
function acf_location_rules_values_page_grandparent($choices) { // this code is copied directly from // render_location_values() // case "page" $groups = acf_get_grouped_posts(array('post_type' => 'page')); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { $posts = acf_extract_var($groups, $group_title); foreach (array_keys($posts) as $post_id) { $posts[$post_id] = acf_get_post_title($posts[$post_id]); } $choices = $posts; } } // end of copy from ACF return $choices; }
/** * Import (overwrite) field groups into DB * @param string $params['name'] * @param string $params['group'] * @param string $params['old_value'] The previous settings data * @param string $params['new_value'] The new settings data */ function acf_pull($params) { $field_group = $params['new_value']; if ($existing_group = acf_get_field_group($field_group['key'])) { $field_group['ID'] = $existing_group['ID']; $existing_fields = acf_get_fields($existing_group); // Remove fields foreach ($existing_fields as $field) { wp_delete_post($field['ID'], true); } } // extract fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // save field group $field_group = acf_update_field_group($field_group); // add to ref $ref[$field_group['key']] = $field_group['ID']; // add to order $order[$field_group['ID']] = 0; // add fields foreach ($fields as $index => $field) { // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } else { if (isset($ref[$field['parent']])) { $field['parent'] = $ref[$field['parent']]; } } // add field menu_order if (!isset($order[$field['parent']])) { $order[$field['parent']] = 0; } $field['menu_order'] = $order[$field['parent']]; $order[$field['parent']]++; // save field $field = acf_update_field($field); // add to ref $ref[$field['key']] = $field['ID']; } }
static function get_json() { if (!function_exists('acf_get_field_group')) { echo 'You need ACF activated to use this screen'; exit; } $keys = isset($_GET['generate-template-id']) ? array($_GET['generate-template-id']) : array(); //print_r($keys); //exit; //$keys = $_GET['acf_export_keys']; //$keys = array('group_55e23ad63ecc3'); //$keys = array('group_55d38b033048e'); //$keys = array('group_55d26a506a990'); // validate if (empty($keys)) { return false; } // vars $json = array(); // construct JSON foreach ($keys as $key) { // load field group $field_group = acf_get_field_group($key); // validate field group if (empty($field_group)) { continue; } // load fields $field_group['fields'] = acf_get_fields($field_group); // prepare fields $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']); // extract field group ID $id = acf_extract_var($field_group, 'ID'); // add to json array $json[] = $field_group; } // return return $json; }
function current_screen() { // validate screen if (!acf_is_screen('edit-acf-field-group')) { return; } // customize post_status global $wp_post_statuses; // modify publish post status $wp_post_statuses['publish']->label_count = _n_noop('Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', 'acf'); // reorder trash to end $wp_post_statuses['trash'] = acf_extract_var($wp_post_statuses, 'trash'); // check stuff $this->check_duplicate(); $this->check_sync(); // actions add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); add_action('admin_footer', array($this, 'admin_footer')); // columns add_filter('manage_edit-acf-field-group_columns', array($this, 'field_group_columns'), 10, 1); add_action('manage_acf-field-group_posts_custom_column', array($this, 'field_group_columns_html'), 10, 2); }
function render_field($field) { // convert value to array $field['value'] = acf_force_type_array($field['value']); // add empty value (allows '' to be selected) if (empty($field['value'])) { $field['value'][''] = ''; } // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // vars $atts = array('id' => $field['id'], 'class' => $field['class'], 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // hidden input if ($field['ui']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value']))); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); } // ui if ($field['ui']) { $atts['disabled'] = 'disabled'; $atts['class'] .= ' acf-hidden'; } // multiple if ($field['multiple']) { $atts['multiple'] = 'multiple'; $atts['size'] = 5; $atts['name'] .= '[]'; } // special atts foreach (array('readonly', 'disabled') as $k) { if (!empty($field[$k])) { $atts[$k] = $k; } } // vars $els = array(); $choices = array(); // loop through values and add them as options if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { $els[] = array('type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => in_array($k2, $field['value'])); $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { $els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => in_array($k, $field['value'])); $choices[] = $k; } } } // prepende orphans /* if( !empty($field['value']) ) { foreach( $field['value'] as $v ) { if( empty($v) ) { continue; } if( !in_array($v, $choices) ) { array_unshift( $els, array( 'type' => 'option', 'value' => $v, 'label' => $v, 'selected' => true ) ); } } } */ // null if ($field['allow_null']) { array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -')); } // html echo '<select ' . acf_esc_attr($atts) . '>'; // construct html if (!empty($els)) { foreach ($els as $el) { // extract type $type = acf_extract_var($el, 'type'); if ($type == 'option') { // get label $label = acf_extract_var($el, 'label'); // validate selected if (acf_extract_var($el, 'selected')) { $el['selected'] = 'selected'; } // echo echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { // echo echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } echo '</select>'; }
function duplicate_field($field) { // vars $sub_fields = array(); if (!empty($field['layouts'])) { // loop through layouts foreach ($field['layouts'] as $layout) { // extract sub fields $extra = acf_extract_var($layout, 'sub_fields'); // merge if (!empty($extra)) { $sub_fields = array_merge($sub_fields, $extra); } } // foreach } // if // save field to get ID $field = acf_update_field($field); // duplicate sub fields acf_duplicate_fields($sub_fields, $field['ID']); // return return $field; }
function render_field($field) { // Change Field into a select $field['type'] = 'select'; $field['ui'] = 1; $field['ajax'] = 1; $field['choices'] = array(); // load posts $posts = $this->get_posts($field['value'], $field); if ($posts) { foreach (array_keys($posts) as $i) { // vars $post = acf_extract_var($posts, $i); // append to choices $field['choices'][$post->ID] = $this->get_post_title($post, $field); } } // render acf_render_field($field); }
function acf_order_by_search($array, $search) { // vars $weights = array(); $needle = strtolower($search); // add key prefix foreach (array_keys($array) as $k) { $array['_' . $k] = acf_extract_var($array, $k); } // add search weight foreach ($array as $k => $v) { // vars $weight = 0; $haystack = strtolower($v); $strpos = strpos($haystack, $needle); // detect search match if ($strpos !== false) { // set eright to length of match $weight = strlen($search); // increase weight if match starts at begining of string if ($strpos == 0) { $weight++; } } // append to wights $weights[$k] = $weight; } // sort the array with menu_order ascending array_multisort($weights, SORT_DESC, $array); // remove key prefix foreach (array_keys($array) as $k) { $array[substr($k, 1)] = acf_extract_var($array, $k); } // return return $array; }
function render_field_select($field) { // Change Field into a select $field['type'] = 'select'; $field['ui'] = 1; $field['ajax'] = 1; $field['choices'] = array(); // value if (!empty($field['value'])) { // get terms $terms = $this->get_terms($field['value'], $field['taxonomy']); // set choices if (!empty($terms)) { foreach (array_keys($terms) as $i) { // vars $term = acf_extract_var($terms, $i); // append to choices $field['choices'][$term->term_id] = $this->get_term_title($term, $field); } } } // render select acf_render_field($field); }
function format_value($value, $post_id, $field) { // ACF4 null if ($value === 'null') { return false; } // bail early if no value if (empty($value)) { return $value; } // get posts $value = $this->get_posts($value, $field); // set choices foreach (array_keys($value) as $i) { // vars $post = acf_extract_var($value, $i); // convert $post to permalink if (is_object($post)) { $post = get_permalink($post); } // append back to $value $value[$i] = $post; } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
function render_field($field) { // vars $values = array(); $atts = array('id' => $field['id'], 'class' => "acf-relationship {$field['class']}", 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-s' => '', 'data-post_type' => '', 'data-taxonomy' => '', 'data-paged' => 1); // Lang if (defined('ICL_LANGUAGE_CODE')) { $atts['data-lang'] = ICL_LANGUAGE_CODE; } // data types $field['post_type'] = acf_force_type_array($field['post_type']); $field['taxonomy'] = acf_force_type_array($field['taxonomy']); // post_types $post_types = array(); if (!empty($field['post_type'])) { $post_types = $field['post_type']; } else { $post_types = acf_get_post_types(); } $post_types = acf_get_pretty_post_types($post_types); // taxonomies $taxonomies = array(); if (!empty($field['taxonomy'])) { // get the field's terms $term_groups = acf_force_type_array($field['taxonomy']); $term_groups = acf_decode_taxonomy_terms($term_groups); // update taxonomies $taxonomies = array_keys($term_groups); } elseif (!empty($field['post_type'])) { // loop over post types and find connected taxonomies foreach ($field['post_type'] as $post_type) { $post_taxonomies = get_object_taxonomies($post_type); // bail early if no taxonomies if (empty($post_taxonomies)) { continue; } foreach ($post_taxonomies as $post_taxonomy) { if (!in_array($post_taxonomy, $taxonomies)) { $taxonomies[] = $post_taxonomy; } } } } else { $taxonomies = acf_get_taxonomies(); } // terms $term_groups = acf_get_taxonomy_terms($taxonomies); // update $term_groups with specific terms if (!empty($field['taxonomy'])) { foreach (array_keys($term_groups) as $taxonomy) { foreach (array_keys($term_groups[$taxonomy]) as $term) { if (!in_array($term, $field['taxonomy'])) { unset($term_groups[$taxonomy][$term]); } } } } // width for select filters $width = array('search' => 0, 'post_type' => 0, 'taxonomy' => 0); if (!empty($field['filters'])) { $width = array('search' => 50, 'post_type' => 25, 'taxonomy' => 25); foreach (array_keys($width) as $k) { if (!in_array($k, $field['filters'])) { $width[$k] = 0; } } // search if ($width['search'] == 0) { $width['post_type'] = $width['post_type'] == 0 ? 0 : 50; $width['taxonomy'] = $width['taxonomy'] == 0 ? 0 : 50; } // post_type if ($width['post_type'] == 0) { $width['taxonomy'] = $width['taxonomy'] == 0 ? 0 : 50; } // taxonomy if ($width['taxonomy'] == 0) { $width['post_type'] = $width['post_type'] == 0 ? 0 : 50; } // search if ($width['post_type'] == 0 && $width['taxonomy'] == 0) { $width['search'] = $width['search'] == 0 ? 0 : 100; } } ?> <div <?php acf_esc_attr_e($atts); ?> > <div class="acf-hidden"> <input type="hidden" name="<?php echo $field['name']; ?> " value="" /> </div> <?php if ($width['search'] > 0 || $width['post_type'] > 0 || $width['taxonomy'] > 0) { ?> <div class="filters"> <ul class="acf-hl"> <?php if ($width['search'] > 0) { ?> <li style="width:<?php echo $width['search']; ?> %;"> <div class="inner"> <input class="filter" data-filter="s" placeholder="<?php _e("Search...", 'acf'); ?> " type="text" /> </div> </li> <?php } ?> <?php if ($width['post_type'] > 0) { ?> <li style="width:<?php echo $width['post_type']; ?> %;"> <div class="inner"> <select class="filter" data-filter="post_type"> <option value=""><?php _e('Select post type', 'acf'); ?> </option> <?php foreach ($post_types as $k => $v) { ?> <option value="<?php echo $k; ?> "><?php echo $v; ?> </option> <?php } ?> </select> </div> </li> <?php } ?> <?php if ($width['taxonomy'] > 0) { ?> <li style="width:<?php echo $width['taxonomy']; ?> %;"> <div class="inner"> <select class="filter" data-filter="taxonomy"> <option value=""><?php _e('Select taxonomy', 'acf'); ?> </option> <?php foreach ($term_groups as $k_opt => $v_opt) { ?> <optgroup label="<?php echo $k_opt; ?> "> <?php foreach ($v_opt as $k => $v) { ?> <option value="<?php echo $k; ?> "><?php echo $v; ?> </option> <?php } ?> </optgroup> <?php } ?> </select> </div> </li> <?php } ?> </ul> </div> <?php } ?> <div class="selection acf-cf"> <div class="choices"> <ul class="acf-bl list"></ul> </div> <div class="values"> <ul class="acf-bl list"> <?php if (!empty($field['value'])) { // get posts $posts = acf_get_posts(array('post__in' => $field['value'], 'post_type' => $field['post_type'])); // set choices if (!empty($posts)) { foreach (array_keys($posts) as $i) { // vars $post = acf_extract_var($posts, $i); ?> <li> <input type="hidden" name="<?php echo $field['name']; ?> []" value="<?php echo $post->ID; ?> " /> <span data-id="<?php echo $post->ID; ?> " class="acf-rel-item"> <?php echo $this->get_post_title($post, $field); ?> <a href="#" class="acf-icon small dark" data-name="remove_item"><i class="acf-sprite-remove"></i></a> </span> </li><?php } } } ?> </ul> </div> </div> </div> <?php }
function prepare_field_for_import($field) { // var $extra = array(); // sub fields if ($field['type'] == 'repeater') { // extract sub fields $sub_fields = acf_extract_var($field, 'sub_fields'); // reset field setting $field['sub_fields'] = array(); if (!empty($sub_fields)) { foreach (array_keys($sub_fields) as $i) { // extract sub field $sub_field = acf_extract_var($sub_fields, $i); // attributes $sub_field['parent'] = $field['key']; // append to extra $extra[] = $sub_field; } } } elseif ($field['type'] == 'flexible_content') { // extract layouts $layouts = acf_extract_var($field, 'layouts'); // reset field setting $field['layouts'] = array(); // validate layouts if (!empty($layouts)) { // loop over layouts foreach (array_keys($layouts) as $i) { // extract layout $layout = acf_extract_var($layouts, $i); // get valid layout (fixes ACF4 export code bug undefined index 'key') if (empty($layout['key'])) { $layout['key'] = uniqid(); } // extract sub fields $sub_fields = acf_extract_var($layout, 'sub_fields'); // validate sub fields if (!empty($sub_fields)) { // loop over sub fields foreach (array_keys($sub_fields) as $j) { // extract sub field $sub_field = acf_extract_var($sub_fields, $j); // attributes $sub_field['parent'] = $field['key']; $sub_field['parent_layout'] = $layout['key']; // append to extra $extra[] = $sub_field; } } // append to layout $field['layouts'][] = $layout; } } } // extra if (!empty($extra)) { array_unshift($extra, $field); return $extra; } // return return $field; }
function psp_update_sub_field($selector, $value, $post_id = false) { // filter post_id // $post_id = acf_get_valid_post_id( $post_id ); // vars $field = false; $name = ''; // within a have_rows loop if (is_string($selector)) { // loop over global data if (!empty($GLOBALS['acf_field'])) { foreach ($GLOBALS['acf_field'] as $row) { // add to name $name .= "{$row['name']}_{$row['i']}_"; // override $post_id $post_id = $row['post_id']; } } // get sub field $field = get_sub_field_object($selector); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // append name $name .= $field['name']; // update name $field['name'] = $name; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = get_field_object($parent_name, $post_id, false, false); // add to name $name .= "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // save return acf_update_value($value, $post_id, $field); }
function ajax_query() { // options $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // vars $r = array(); $args = array(); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // editable roles $editable_roles = get_editable_roles(); if (!empty($field['role'])) { foreach ($editable_roles as $role => $role_info) { if (!in_array($role, $field['role'])) { unset($editable_roles[$role]); } } } // search if ($options['s']) { // append to $args $args['search'] = '*' . $options['s'] . '*'; // add filter to modify search colums add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 1); } // filters $args = apply_filters('acf/fields/user/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/user/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/user/query/key=' . $field['key'], $args, $field, $options['post_id']); // get users $users = get_users($args); if (!empty($users) && !empty($editable_roles)) { foreach ($editable_roles as $role => $role_info) { // vars $this_users = array(); $this_json = array(); // loop over users foreach (array_keys($users) as $key) { if (in_array($role, $users[$key]->roles)) { // extract user $user = acf_extract_var($users, $key); // append to $this_users $this_users[$user->ID] = ucfirst($user->display_name) . ' (' . $user->user_login . ')'; } } // bail early if no users for this role if (empty($this_users)) { continue; } // order by search if (!empty($args['s'])) { $this_users = acf_order_by_search($this_users, $args['s']); } // append to json foreach (array_keys($this_users) as $user_id) { // add to json $this_json[] = array('id' => $user_id, 'text' => $this_users[$user_id]); } // add as optgroup or results if (count($editable_roles) == 1) { $r = $this_json; } else { $r[] = array('text' => translate_user_role($role_info['name']), 'children' => $this_json); } } } // return JSON echo json_encode($r); die; }
function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // disable field to allow clone fields to appear selectable acf_disable_filter('clone'); // options $options = acf_parse_args($_POST, array('post_id' => 0, 'paged' => 0, 's' => '', 'title' => '', 'fields' => array())); // vars $results = array(); $s = false; $i = -1; $limit = 20; $range_start = $limit * ($options['paged'] - 1); // 0, 20, 40 $range_end = $range_start + ($limit - 1); // 19, 39, 59 // search if ($options['s'] !== '') { // strip slashes (search may be integer) $s = wp_unslash(strval($options['s'])); } // load groups $field_groups = acf_get_field_groups(); $field_group = false; // bail early if no field groups if (empty($field_groups)) { die; } // move current field group to start foreach (array_keys($field_groups) as $j) { // check ID if ($field_groups[$j]['ID'] !== $options['post_id']) { continue; } // extract field group and move to start $field_group = acf_extract_var($field_groups, $j); // field group found, stop looking break; } // if field group was not found, this is a new field group (not yet saved) if (!$field_group) { $field_group = array('ID' => $options['post_id'], 'title' => $options['title'], 'key' => ''); } // move current field group to start of list array_unshift($field_groups, $field_group); // loop foreach ($field_groups as $field_group) { // vars $fields = false; $data = array('text' => $field_group['title'], 'children' => array()); // get fields if ($field_group['ID'] == $options['post_id']) { $fields = $options['fields']; } else { $fields = acf_get_fields($field_group); $fields = acf_prepare_fields_for_import($fields); } // bail early if no fields if (!$fields) { continue; } // populate children $children = array(); $children[] = $field_group['key']; foreach ($fields as $field) { $children[] = $field['key']; } // loop foreach ($children as $child) { // bail ealry if no key (fake field group or corrupt field) if (!$child) { continue; } // vars $text = false; // bail early if is search, and $text does not contain $s if ($s !== false) { // get early $text = $this->get_clone_setting_choice($child); // search if (stripos($text, $s) === false) { continue; } } // $i $i++; // bail early if $i is out of bounds if ($i < $range_start || $i > $range_end) { continue; } // load text if ($text === false) { $text = $this->get_clone_setting_choice($child); } // append $data['children'][] = array('id' => $child, 'text' => $text); } // bail early if no children // - this group contained fields, but none shown on this page if (empty($data['children'])) { continue; } // append $results[] = $data; // end loop if $i is out of bounds // - no need to look further if ($i > $range_end) { break; } } // return acf_send_ajax_results(array('results' => $results, 'limit' => $limit)); }
<?php // vars $json = acf_extract_var($args, 'json'); ?> <div class="wrap acf-settings-wrap"> <h2><?php _e("Add-ons", 'acf'); ?> </h2> <div class="add-ons-list acf-cf"> <?php if (!empty($json)) { ?> <?php foreach ($json as $addon) { $addon = acf_parse_args($addon, array("title" => "", "slug" => "", "description" => "", "thumbnail" => "", "url" => "", "btn" => __("Download & Install", 'acf'), "btn_color" => "")); ?> <div class="acf-box add-on add-on-<?php echo $addon['slug']; ?> "> <div class="thumbnail"> <a target="_blank" href="<?php echo $addon['url'];
function update_sub_field($selector, $value, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // vars $field = false; // within a have_rows loop if (is_string($selector)) { // get current row $row = acf_get_row(); // override $post_id $post_id = $row['post_id']; // get sub field $field = get_sub_field_object($selector, false, false); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // update name $field['name'] = "{$row['name']}_{$row['i']}_{$field['name']}"; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = acf_maybe_get_field($parent_name, $post_id); // add to name $name = "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // delete if ($value === null) { return acf_delete_value($post_id, $field); } // update return acf_update_value($value, $post_id, $field); }
function acf_get_sub_field($selector, $field) { // sub fields if ($field['type'] == 'repeater') { // extract sub fields $sub_fields = acf_extract_var($field, 'sub_fields'); if (!empty($sub_fields)) { foreach ($sub_fields as $sub_field) { if ($sub_field['name'] == $selector || $sub_field['key'] == $selector) { // return return $sub_field; } // if } // foreach } // if } elseif ($field['type'] == 'flexible_content') { // vars $layouts = acf_extract_var($field, 'layouts'); $current = get_row_layout(); if (!empty($layouts)) { foreach ($layouts as $layout) { // skip layout if the current layout key does not match if ($current && $current !== $layout['name']) { continue; } // extract sub fields $sub_fields = acf_extract_var($layout, 'sub_fields'); if (!empty($sub_fields)) { foreach ($sub_fields as $sub_field) { if ($sub_field['name'] == $selector || $sub_field['key'] == $selector) { // return return $sub_field; } // if } // foreach } // if } // foreach } // if } // if // return return false; }
function duplicate_field($field) { // get sub fields $sub_fields = acf_extract_var($field, 'sub_fields'); // save field to get ID $field = acf_update_field($field); // duplicate sub fields acf_duplicate_fields($sub_fields, $field['ID']); // return return $field; }
function get_json() { // validate if (empty($_POST['acf_export_keys'])) { return false; } // vars $json = array(); // construct JSON foreach ($_POST['acf_export_keys'] as $key) { // load field group $field_group = acf_get_field_group($key); // validate field group if (empty($field_group)) { continue; } // load fields $field_group['fields'] = acf_get_fields($field_group); // prepare fields $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']); // extract field group ID $id = acf_extract_var($field_group, 'ID'); // add to json array $json[] = $field_group; } // return return $json; }
function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '')); // vars $r = array(); $args = array(); // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // editable roles $editable_roles = get_editable_roles(); if (!empty($field['role'])) { foreach ($editable_roles as $role => $role_info) { if (!in_array($role, $field['role'])) { unset($editable_roles[$role]); } } } // search if ($options['s']) { // append to $args $args['search'] = '*' . $options['s'] . '*'; // add reference $this->field = $field; // add filter to modify search colums add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 3); } // filters $args = apply_filters("acf/fields/user/query", $args, $field, $options['post_id']); $args = apply_filters("acf/fields/user/query/name={$field['_name']}", $args, $field, $options['post_id']); $args = apply_filters("acf/fields/user/query/key={$field['key']}", $args, $field, $options['post_id']); // get users $users = get_users($args); if (!empty($users) && !empty($editable_roles)) { foreach ($editable_roles as $role => $role_info) { // vars $this_users = array(); $this_json = array(); // loop over users foreach (array_keys($users) as $key) { if (in_array($role, $users[$key]->roles)) { // extract user $user = acf_extract_var($users, $key); // append to $this_users $this_users[$user->ID] = $this->get_result($user, $field, $options['post_id']); } } // bail early if no users for this role if (empty($this_users)) { continue; } // order by search if (!empty($args['s'])) { $this_users = acf_order_by_search($this_users, $args['s']); } // append to json foreach (array_keys($this_users) as $user_id) { // add to json $this_json[] = array('id' => $user_id, 'text' => $this_users[$user_id]); } // add as optgroup or results if (count($editable_roles) == 1) { $r = $this_json; } else { $r[] = array('text' => translate_user_role($role_info['name']), 'children' => $this_json); } } } // return return $r; }
function get_valid_field_group($field_group) { // global global $wpdb; // vars $v = 5; // add missing 'key' (v5.0.0) if (empty($field_group['key'])) { // update version $v = 4; // add missing key $field_group['key'] = empty($field_group['id']) ? uniqid('group_') : 'group_' . $field_group['id']; } // extract options (v5.0.0) if (!empty($field_group['options'])) { $options = acf_extract_var($field_group, 'options'); $field_group = array_merge($field_group, $options); } // location rules changed to groups (v5.0.0) if (!empty($field_group['location']['rules'])) { // extract location $location = acf_extract_var($field_group, 'location'); // reset location $field_group['location'] = array(); // vars $group = 0; $all_or_any = $location['allorany']; // loop over rules if (!empty($location['rules'])) { foreach ($location['rules'] as $rule) { // sperate groups? if ($all_or_any == 'any') { $group++; } // add to group $field_group['location'][$group][] = $rule; } } // reset keys $field_group['location'] = array_values($field_group['location']); } // some location rules have changed (v5.0.0) if (!empty($field_group['location'])) { // param changes $param_replace = array('taxonomy' => 'post_taxonomy', 'ef_media' => 'attachment', 'ef_taxonomy' => 'taxonomy', 'ef_user' => 'user_role', 'user_type' => 'current_user_role'); // remove conflicting param if ($v == 5) { unset($param_replace['taxonomy']); } // loop over location groups foreach (array_keys($field_group['location']) as $i) { // extract group $group = acf_extract_var($field_group['location'], $i); // bail early if group is empty if (empty($group)) { continue; } // loop over group rules foreach (array_keys($group) as $j) { // extract rule $rule = acf_extract_var($group, $j); // migrate param if (isset($param_replace[$rule['param']])) { $rule['param'] = $param_replace[$rule['param']]; } // category / taxonomy terms are saved differently if ($rule['param'] == 'post_category' || $rule['param'] == 'post_taxonomy') { if (is_numeric($rule['value'])) { $term_id = $rule['value']; $taxonomy = $wpdb->get_var($wpdb->prepare("SELECT taxonomy FROM {$wpdb->term_taxonomy} WHERE term_id = %d LIMIT 1", $term_id)); $term = get_term($term_id, $taxonomy); // update rule value $rule['value'] = "{$term->taxonomy}:{$term->slug}"; } } // append rule $group[$j] = $rule; } // foreach // append group $field_group['location'][$i] = $group; } // foreach } // if // change layout to style (v5.0.0) if (!empty($field_group['layout'])) { $field_group['style'] = acf_extract_var($field_group, 'layout'); } // change no_box to seamless (v5.0.0) if ($field_group['style'] === 'no_box') { $field_group['style'] = 'seamless'; } //return return $field_group; }
function acf_write_json_field_group($field_group) { // vars $path = acf_get_setting('save_json'); $file = $field_group['key'] . '.json'; // remove trailing slash $path = untrailingslashit($path); // bail early if dir does not exist if (!is_writable($path)) { return false; } // prepare for export $id = acf_extract_var($field_group, 'ID'); $field_group = acf_prepare_field_group_for_export($field_group); // add modified time $field_group['modified'] = get_post_modified_time('U', true, $id, true); // write file $f = fopen("{$path}/{$file}", 'w'); fwrite($f, acf_json_encode($field_group)); fclose($f); // return return true; }
function render_location_value($options) { // vars $options = wp_parse_args($options, array('group_id' => 0, 'rule_id' => 0, 'value' => null, 'param' => null)); // vars $choices = array(); // some case's have the same outcome if ($options['param'] == "page_parent") { $options['param'] = "page"; } switch ($options['param']) { /* * Basic */ case "post_type": // all post types except attachment $exclude = array('attachment'); $choices = acf_get_post_types($exclude); $choices = acf_get_pretty_post_types($choices); break; case "user_type": global $wp_roles; $choices = $wp_roles->get_names(); if (is_multisite()) { $choices['super_admin'] = __('Super Admin'); } break; /* * Post */ /* * Post */ case "post": // get post types $exclude = array('page', 'attachment'); $post_types = acf_get_post_types($exclude); // get posts grouped by post type $groups = acf_get_posts(array('post_type' => $post_types)); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); // override post data foreach (array_keys($posts) as $post_id) { // update $posts[$post_id] = acf_get_post_title($posts[$post_id]); } // append to $choices $choices[$group_title] = $posts; } } break; case "post_category": $terms = acf_get_taxonomy_terms('category'); if (!empty($terms)) { $choices = array_pop($terms); } break; case "post_format": $choices = get_post_format_strings(); break; case "post_status": $choices = array('publish' => __('Publish', 'acf'), 'pending' => __('Pending Review', 'acf'), 'draft' => __('Draft', 'acf'), 'future' => __('Future', 'acf'), 'private' => __('Private', 'acf'), 'inherit' => __('Revision', 'acf'), 'trash' => __('Trash', 'acf')); break; case "post_taxonomy": $choices = acf_get_taxonomy_terms(); // unset post_format if (isset($choices['post_format'])) { unset($choices['post_format']); } break; /* * Page */ /* * Page */ case "page": // get posts grouped by post type $groups = acf_get_posts(array('post_type' => 'page')); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); // override post data foreach (array_keys($posts) as $post_id) { // update $posts[$post_id] = acf_get_post_title($posts[$post_id]); } // append to $choices $choices = $posts; } } 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_parent": // refer to "page" break; case "page_template": $choices = array('default' => __("Default Template", 'acf')); $templates = get_page_templates(); foreach ($templates as $k => $v) { $choices[$v] = $k; } break; /* * User */ /* * User */ case "user_role": global $wp_roles; $choices = array_merge(array('all' => __('All', 'acf')), $wp_roles->get_names()); break; case "user_form": $choices = array('all' => __('All', 'acf'), 'edit' => __('Add / Edit', 'acf'), 'register' => __('Register', 'acf')); break; /* * Forms */ /* * Forms */ case "attachment": $choices = array('all' => __('All', 'acf')); break; case "taxonomy": $choices = array_merge(array('all' => __('All', 'acf')), acf_get_taxonomies()); // unset post_format if (isset($choices['post_format'])) { unset($choices['post_format']); } break; case "comment": $choices = array('all' => __('All', 'acf')); break; case "widget": global $wp_widget_factory; $choices = array('all' => __('All', 'acf')); if (!empty($wp_widget_factory->widgets)) { foreach ($wp_widget_factory->widgets as $widget) { $choices[$widget->id_base] = $widget->name; } } break; } // allow custom location rules $choices = apply_filters('acf/location/rule_values/' . $options['param'], $choices); // create field acf_render_field(array('type' => 'select', 'prefix' => "acf_field_group[location][{$options['group_id']}][{$options['rule_id']}]", 'name' => 'value', 'value' => $options['value'], 'choices' => $choices)); }
<?php // vars $field = acf_extract_var($args, 'field'); $groups = acf_extract_var($field, 'conditional_logic'); $disabled = empty($groups) ? 1 : 0; // UI needs at least 1 conditional logic rule if (empty($groups)) { $groups = array(array()); } ?> <tr data-name="conditional_logic" class="acf-field"> <td class="acf-label"> <label><?php _e("Conditional Logic", 'acf'); ?> </label> </td> <td class="acf-input"> <?php acf_render_field(array('type' => 'radio', 'name' => 'conditional_logic', 'prefix' => $field['prefix'], 'value' => $disabled ? 0 : 1, 'choices' => array(1 => __("Yes", 'acf'), 0 => __("No", 'acf')), 'layout' => 'horizontal')); ?> <div class="location-groups" <?php if ($disabled) { ?> style="display:none;"<?php } ?> > <?php