function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '')); // vars $r = array(); $args = array('hide_empty' => false); // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // search if ($options['s']) { $args['search'] = $options['s']; } // filters $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']); // get terms $terms = get_terms($field['taxonomy'], $args); // sort into hierachial order! if (is_taxonomy_hierarchical($field['taxonomy'])) { // get parent $parent = acf_maybe_get($args, 'parent', 0); $parent = acf_maybe_get($args, 'child_of', $parent); // this will fail if a search has taken place because parents wont exist if (empty($args['search'])) { $terms = _get_term_children($parent, $terms, $field['taxonomy']); } } /// append to r foreach ($terms as $term) { // add to json $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id'])); } // return return $r; }
function ajax_layout_title() { // options $options = acf_parse_args($_POST, array('post_id' => 0, 'i' => 0, 'field_key' => '', 'nonce' => '', 'layout' => '', 'acf' => array())); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // vars $layout = false; foreach ($field['layouts'] as $k => $layout) { if ($layout['name'] === $options['layout']) { break; } } // bail ealry if no layout if (!$layout) { die; } // value // this flexible content field may be a sub field so it is important to // loop though all $_POST data to find thi's field's row value $value = $options['acf']; while (is_array($value)) { // get first key $k = key($value); // update value $value = array_pop($value[$k]); // stop looking if we have found the correct field's value if ($k === $options['field_key']) { break; } } // title $title = $this->get_layout_title($field, $layout, $options['i'], $value); // echo echo $title; die; }
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 = acf_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)); // 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 ajax_query() { // options $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => '')); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // vars $r = array(); $s = false; // search if ($options['s'] !== '') { // search may be integer $s = strval($options['s']); // strip slashes $s = wp_unslash($s); } // loop through choices if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { // if searching, but doesn't exist if ($s !== false && stripos($v, $s) === false) { continue; } // append $r[] = array('id' => $k, 'text' => strval($v)); } } // return JSON echo json_encode($r); die; }
function ajax_upgrade() { // options $options = acf_parse_args($_POST, array('version' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { wp_send_json_error(); } // vars $path = acf_get_path("admin/updates/{$options['version']}.php"); // load version if (!file_exists($path)) { wp_send_json_error(); } // load any errors / feedback from update ob_start(); // include include $path; // get feedback $feedback = ob_get_clean(); // update successful update_option('acf_version', $options['version']); // check for relevant updates. If none are found, update this to the plugin version $updates = acf_get_updates(); if (empty($updates)) { update_option('acf_version', acf_get_setting('version')); } // return wp_send_json_success(array('feedback' => $feedback)); }
function acf_get_valid_field($field = false) { // $field must be an array if (!is_array($field)) { $field = array(); } // bail ealry if field_name exists (only run this function once) if (!empty($field['_valid'])) { return $field; } // defaults $field = acf_parse_args($field, array('ID' => 0, 'key' => '', 'label' => '', 'name' => '', 'prefix' => '', 'type' => 'text', 'value' => null, 'menu_order' => 0, 'instructions' => '', 'required' => 0, 'id' => '', 'class' => '', 'conditional_logic' => 0, 'parent' => 0, '_name' => '', '_input' => '', '_valid' => 0)); // _name $field['_name'] = $field['name']; // translate foreach (array('label', 'instructions') as $s) { $field[$s] = __($field[$s]); } // field specific defaults $field = apply_filters("acf/get_valid_field", $field); $field = apply_filters("acf/get_valid_field/type={$field['type']}", $field); // field is now valid $field['_valid'] = 1; // return return $field; }
function ajax_query() { // options $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => '')); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // vars $r = array(); // strip slashes $options['s'] = wp_unslash($options['s']); if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { // search if ($options['s'] && stripos($v, $options['s']) === false) { continue; } // append $r[] = array('id' => $k, 'text' => strval($v)); } } // return JSON echo json_encode($r); die; }
function acf_get_valid_field_group($field_group = false) { // parse in defaults $field_group = acf_parse_args($field_group, array('ID' => 0, 'key' => '', 'title' => '', 'fields' => array(), 'location' => array(), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => array())); // filter $field_group = apply_filters('acf/get_valid_field_group', $field_group); // return return $field_group; }
function update_user_setting() { // options $options = acf_parse_args($_POST, array('name' => '', 'value' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce') || empty($options['name'])) { die('0'); } // upadte setting acf_update_user_setting($options['name'], $options['value']); // return die('1'); }
function set_data($data) { // defaults $data = acf_parse_args($data, array('post_id' => 0, 'nonce' => 'post', 'validation' => 1, 'ajax' => 0)); // update $this->data = $data; // enqueue uploader if page allows AJAX fields to appear if ($data['ajax']) { add_action($this->admin_footer, 'acf_enqueue_uploader', 1); } // return return $data; }
function ajax_query() { // options $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) { die; } // vars $r = array(); $args = array('hide_empty' => false); // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // search if ($options['s']) { $args['search'] = $options['s']; } // filters $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']); // get terms $terms = get_terms($field['taxonomy'], $args); // sort into hierachial order! if (is_taxonomy_hierarchical($field['taxonomy'])) { // this will fail if a search has taken place because parents wont exist if (empty($args['search'])) { $terms = _get_term_children(0, $terms, $field['taxonomy']); } } /// append to r foreach ($terms as $term) { // add to json $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id'])); } // return JSON echo json_encode($r); die; }
function ajax_search() { // options $args = acf_parse_args($_POST, array('s' => '', 'nonce' => '', 'width' => 0, 'height' => 0)); // width and height if (!$args['width']) { $args['width'] = $this->default_values['width']; } if (!$args['height']) { $args['height'] = $this->default_values['height']; } // validate if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { die; } // get oembed echo $this->wp_oembed_get($args['s'], $args['width'], $args['height']); // die 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)); }
<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']; ?> "> <img src="<?php echo $addon['thumbnail']; ?>
function acf_form($args = array()) { // vars $url = acf_get_current_url(); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp')); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => 'post', 'class' => '', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = acf_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // attributes $args['form_attributes']['class'] .= ' acf-form'; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // uploader (always set incase of multiple forms on the page) acf_update_setting('uploader', $args['uploader']); // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <!-- Submit --> <div class="acf-form-submit"> <input type="submit" class="button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> <span class="acf-spinner"></span> </div> <!-- / Submit --> </form> <?php } }
function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // defaults $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1)); // vars $results = array(); $args = array(); $s = false; $is_search = false; // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // search if ($options['s'] !== '') { // strip slashes (search may be integer) $s = wp_unslash(strval($options['s'])); // update vars $args['s'] = $s; $is_search = true; } // load field $field = acf_get_field($options['field_key']); if (!$field) { die; } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // filters $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']); // add archives to $results if ($field['allow_archives'] && $args['paged'] == 1) { $archives = array(); $archives[] = array('id' => home_url(), 'text' => home_url()); foreach ($args['post_type'] as $post_type) { // vars $archive_link = get_post_type_archive_link($post_type); // bail ealry if no link if (!$archive_link) { continue; } // bail early if no search match if ($is_search && stripos($archive_link, $s) === false) { continue; } // append $archives[] = array('id' => $archive_link, 'text' => $archive_link); } // append $results[] = array('text' => __('Archives', 'acf'), 'children' => $archives); } // get posts grouped by post type $groups = acf_get_grouped_posts($args); // loop if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); // data $data = array('text' => $group_title, 'children' => array()); // convert post objects to post titles foreach (array_keys($posts) as $post_id) { $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search); } // order posts by search if ($is_search && empty($args['orderby'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]); } // append to $results $results[] = $data; } } // return acf_send_ajax_results(array('results' => $results, 'limit' => $args['posts_per_page'])); }
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 ajax_add_term() { // vars $args = acf_parse_args($_POST, array('nonce' => '', 'field_key' => '', 'term_name' => '', 'term_parent' => '')); // verify nonce if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { die; } // load field $field = acf_get_field($args['field_key']); if (!$field) { die; } // vars $taxonomy_obj = get_taxonomy($field['taxonomy']); $taxonomy_label = $taxonomy_obj->labels->singular_name; // validate cap // note: this situation should never occur due to condition of the add new button if (!current_user_can($taxonomy_obj->cap->manage_terms)) { echo '<p><strong>' . __("Error", 'acf') . '.</strong> ' . sprintf(__('User unable to add new %s', 'acf'), $taxonomy_label) . '</p>'; die; } // save? if ($args['term_name']) { // exists if (term_exists($args['term_name'], $field['taxonomy'])) { wp_send_json_error(array('error' => sprintf(__('%s already exists', 'acf'), $taxonomy_label))); } // insert $extra = array(); if ($args['term_parent']) { $extra['parent'] = $args['term_parent']; } $data = wp_insert_term($args['term_name'], $field['taxonomy'], $extra); // error? if (is_wp_error($data)) { wp_send_json_error(array('error' => $data->get_error_message())); } // ancestors $prefix = ''; $ancestors = get_ancestors($data['term_id'], $field['taxonomy']); if (!empty($ancestors)) { $prefix = str_repeat('- ', count($ancestors)); } // success wp_send_json_success(array('message' => sprintf(__('%s added', 'acf'), $taxonomy_label), 'term_id' => $data['term_id'], 'term_name' => $args['term_name'], 'term_label' => $prefix . $args['term_name'], 'term_parent' => $args['term_parent'])); } ?> <form method="post"><?php acf_render_field_wrap(array('label' => 'Name', 'name' => 'term_name', 'type' => 'text')); if (is_taxonomy_hierarchical($field['taxonomy'])) { $choices = array(); $choices2 = $this->get_choices(array('field_key' => $field['key'])); if ($choices2) { foreach ($choices2 as $v) { $choices[$v['id']] = $v['text']; } } acf_render_field_wrap(array('label' => 'Parent', 'name' => 'term_parent', 'type' => 'select', 'allow_null' => 1, 'ui' => 0, 'choices' => $choices)); } ?> <p class="acf-submit"><button class="acf-button blue" type="submit"><?php _e("Add", 'acf'); ?> </button><i class="acf-loading"></i><span></span></p></form><?php // die die; }
function get_field_groups() { // options $options = acf_parse_args($_POST, array('nonce' => '', 'post_id' => 0, 'ajax' => 1)); // vars $r = array(); $nonce = acf_extract_var($options, 'nonce'); // verify nonce if (!wp_verify_nonce($nonce, 'acf_nonce')) { die; } // get field groups $field_groups = acf_get_field_groups($options); // loop through field groups and build $r if (!empty($field_groups)) { foreach ($field_groups as $field_group) { // vars $class = 'acf-postbox ' . $field_group['style']; // load fields $fields = acf_get_fields($field_group); // get field HTML ob_start(); // render if ($field_group['label_placement'] == 'left') { ?> <table class="acf-table"> <tbody> <?php acf_render_fields($options['post_id'], $fields, 'tr', $field_group['instruction_placement']); ?> </tbody> </table> <?php } else { acf_render_fields($options['post_id'], $fields, 'div', $field_group['instruction_placement']); } $html = ob_get_clean(); // get style $style = acf_get_field_group_style($field_group); // append to $r $r[] = array('key' => $field_group['key'], 'title' => $field_group['title'], 'html' => $html, 'style' => $style, 'class' => $class); } } // return wp_send_json_success($r); }
function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'post_type' => '', 'taxonomy' => '', 'lang' => false, 'field_key' => '', 'paged' => 1)); // vars $r = array(); $args = array(); // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // WPML if ($options['lang']) { global $sitepress; if (!empty($sitepress)) { $sitepress->switch_lang($options['lang']); } } // update $args if (!empty($options['post_type'])) { $args['post_type'] = acf_force_type_array($options['post_type']); } elseif (!empty($field['post_type'])) { $args['post_type'] = acf_force_type_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // update taxonomy $taxonomies = array(); if (!empty($options['taxonomy'])) { $term = acf_decode_taxonomy_term($options['taxonomy']); // append to $args $args['tax_query'] = array(array('taxonomy' => $term['taxonomy'], 'field' => 'slug', 'terms' => $term['term'])); } elseif (!empty($field['taxonomy'])) { $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // append to $args $args['tax_query'] = array(); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // search if ($options['s']) { $args['s'] = $options['s']; } // filters $args = apply_filters('acf/fields/relationship/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $args, $field, $options['post_id']); // get posts grouped by post type $groups = acf_get_grouped_posts($args); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); $titles = array(); // data $data = array('text' => $group_title, 'children' => array()); foreach (array_keys($posts) as $post_id) { // override data $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']); } // order by search if (!empty($args['s'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]); } // append to $r $r[] = $data; } // optgroup or single $post_types = acf_force_type_array($args['post_type']); // add as optgroup or results if (count($post_types) == 1) { $r = $r[0]['children']; } } // return return $r; }
function ajax_get_sort_order() { // vars $r = array(); $order = 'DESC'; $args = acf_parse_args($_POST, array('ids' => 0, 'sort' => 'date', 'field_key' => '', 'nonce' => '')); // validate if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { wp_send_json_error(); } // reverse if ($args['sort'] == 'reverse') { $ids = array_reverse($args['ids']); wp_send_json_success($ids); } if ($args['sort'] == 'title') { $order = 'ASC'; } // find attachments (DISTINCT POSTS) $ids = get_posts(array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => 'any', 'post__in' => $args['ids'], 'order' => $order, 'orderby' => $args['sort'], 'fields' => 'ids')); // success if (!empty($ids)) { wp_send_json_success($ids); } // failure wp_send_json_error(); }
function ajax_move_field() { // disable JSON to avoid conflicts between DB and JSON acf_disable_local(); $args = acf_parse_args($_POST, array('nonce' => '', 'field_id' => 0, 'field_group_id' => 0)); // verify nonce if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { die; } // confirm? if ($args['field_id'] && $args['field_group_id']) { // vars $field = acf_get_field($args['field_id']); $field_group = acf_get_field_group($args['field_group_id']); // update parent $field['parent'] = $field_group['ID']; // remove conditional logic $field['conditional_logic'] = 0; // update field acf_update_field($field); $v1 = $field['label']; $v2 = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; echo '<p><strong>' . __('Move Complete.', 'acf') . '</strong></p>'; echo sprintf(__('The %s field can now be found in the %s field group', 'acf'), $v1, $v2) . '</p>'; echo '<a href="#" class="acf-button blue acf-close-popup">' . __("Close Window", 'acf') . '</a>'; die; } // get all field groups $field_groups = acf_get_field_groups(); $choices = array(); if (!empty($field_groups)) { foreach ($field_groups as $field_group) { if ($field_group['ID']) { $choices[$field_group['ID']] = $field_group['title']; } } } // render options $field = acf_get_valid_field(array('type' => 'select', 'name' => 'acf_field_group', 'choices' => $choices)); echo '<p>' . __('Please select the destination for this field', 'acf') . '</p>'; echo '<form id="acf-move-field-form">'; // render acf_render_field_wrap($field); echo '<button type="submit" class="acf-button blue">' . __("Move Field", 'acf') . '</button>'; echo '</form>'; // die die; }
function get_ajax_query($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1)); // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // vars $results = array(); $args = array(); $s = false; $is_search = false; // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // search if ($options['s'] !== '') { // strip slashes (search may be integer) $s = wp_unslash(strval($options['s'])); // update vars $args['s'] = $s; $is_search = true; } // post_type if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // taxonomy if (!empty($field['taxonomy'])) { // vars $terms = acf_decode_taxonomy_terms($field['taxonomy']); // append to $args $args['tax_query'] = array(); // now create the tax queries foreach ($terms as $k => $v) { $args['tax_query'][] = array('taxonomy' => $k, 'field' => 'slug', 'terms' => $v); } } // filters $args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id']); // get posts grouped by post type $groups = acf_get_grouped_posts($args); // bail early if no posts if (empty($groups)) { return false; } // loop foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); // data $data = array('text' => $group_title, 'children' => array()); // convert post objects to post titles foreach (array_keys($posts) as $post_id) { $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search); } // order posts by search if ($is_search && empty($args['orderby'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]); } // append to $results $results[] = $data; } // optgroup or single if (count($args['post_type']) == 1) { $results = $results[0]['children']; } // vars $response = array('results' => $results, 'limit' => $args['posts_per_page']); // return return $response; }
function acf_get_valid_field($field = false) { // $field must be an array if (!is_array($field)) { $field = array(); } // bail ealry if already valid if (!empty($field['_valid'])) { return $field; } // defaults $field = acf_parse_args($field, array('ID' => 0, 'key' => '', 'label' => '', 'name' => '', 'prefix' => '', 'type' => 'text', 'value' => null, 'menu_order' => 0, 'instructions' => '', 'required' => 0, 'id' => '', 'class' => '', 'conditional_logic' => 0, 'parent' => 0, 'wrapper' => array('width' => '', 'class' => '', 'id' => ''), '_name' => '', '_input' => '', '_valid' => 0)); // _name $field['_name'] = $field['name']; // translate acf_translate_keys($field, acf_get_setting('l10n_field')); // field specific defaults $field = apply_filters("acf/get_valid_field", $field); $field = apply_filters("acf/get_valid_field/type={$field['type']}", $field); // field is now valid $field['_valid'] = 1; // return return $field; }
function acf_get_grouped_posts($args) { // vars $r = array(); // defaults $args = acf_parse_args($args, array('posts_per_page' => -1, 'paged' => 0, 'post_type' => 'post', 'orderby' => 'menu_order title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => false, 'update_post_meta_cache' => false)); // find array of post_type $post_types = acf_force_type_array($args['post_type']); $post_types_labels = acf_get_pretty_post_types($post_types); // attachment doesn't work if it is the only item in an array if (count($post_types) == 1) { $args['post_type'] = current($post_types); } // add filter to orderby post type add_filter('posts_orderby', '_acf_orderby_post_type', 10, 2); // get posts $posts = get_posts($args); // loop foreach ($post_types as $post_type) { // vars $this_posts = array(); $this_group = array(); // populate $this_posts foreach (array_keys($posts) as $key) { if ($posts[$key]->post_type == $post_type) { $this_posts[] = acf_extract_var($posts, $key); } } // bail early if no posts for this post type if (empty($this_posts)) { continue; } // sort into hierachial order! // this will fail if a search has taken place because parents wont exist if (is_post_type_hierarchical($post_type) && empty($args['s'])) { // vars $match_id = $this_posts[0]->ID; $offset = 0; $length = count($this_posts); $parent = acf_maybe_get($args, 'post_parent', 0); // reset $this_posts $this_posts = array(); // get all posts $all_args = array_merge($args, array('posts_per_page' => -1, 'paged' => 0, 'post_type' => $post_type)); $all_posts = get_posts($all_args); // loop over posts and find $i foreach ($all_posts as $offset => $p) { if ($p->ID == $match_id) { break; } } // order posts $all_posts = get_page_children($parent, $all_posts); for ($i = $offset; $i < $offset + $length; $i++) { $this_posts[] = acf_extract_var($all_posts, $i); } } // populate $this_posts foreach (array_keys($this_posts) as $key) { // extract post $post = acf_extract_var($this_posts, $key); // add to group $this_group[$post->ID] = $post; } // group by post type $post_type_name = $post_types_labels[$post_type]; $r[$post_type_name] = $this_group; } // return return $r; }
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 render_field($field) { /* * Review the data of $field. * This will show what data is available */ // validate value if (empty($field['value'])) { $field['value'] = array(); } // value $field['value'] = acf_parse_args($field['value'], array('x' => '', 'y' => '')); // vars $atts = array('id' => $field['id'], 'class' => "acf-openlayers_map {$field['class']}"); ?> <div <?php acf_esc_attr_e($atts); ?> > <div class="acf-hidden"> <?php foreach ($field['value'] as $k => $v) { ?> <input type="hidden" id="input-<?php echo $k; ?> " name="<?php echo esc_attr($field['name']); ?> [<?php echo $k; ?> ]" value="<?php echo esc_attr($v); ?> " /> <?php } ?> </div> <div id="map" class="map"></div> </div> <?php }
function acf_get_field_group_visibility($field_group, $args = array()) { // vars $args = acf_parse_args($args, array('post_id' => 0, 'post_type' => 0, 'page_template' => 0, 'page_parent' => 0, 'page_type' => 0, 'post_status' => 0, 'post_format' => 0, 'post_taxonomy' => null, 'taxonomy' => 0, 'user_id' => 0, 'user_role' => 0, 'user_form' => 0, 'attachment' => 0, 'comment' => 0, 'widget' => 0, 'lang' => acf_get_setting('current_language'), 'ajax' => false)); // filter for 3rd party customization $args = apply_filters('acf/location/screen', $args, $field_group); // bail early if not active if (!$field_group['active']) { return false; } // vars $visibility = false; // loop through location rules foreach ($field_group['location'] as $group_id => $group) { // start of as true, this way, any rule that doesn't match will cause this varaible to false $match_group = true; // loop over group rules if (!empty($group)) { foreach ($group as $rule_id => $rule) { $match = apply_filters('acf/location/rule_match/' . $rule['param'], false, $rule, $args); if (!$match) { $match_group = false; break; } } } // all rules must havematched! if ($match_group) { $visibility = true; } } // return return $visibility; }
function acf_form_data($args = array()) { // make sure scripts and styles have been included // case: front end bbPress edit user acf_enqueue_scripts(); // defaults $args = acf_parse_args($args, array('post_id' => 0, 'nonce' => 'post', 'validation' => 1, 'ajax' => 0)); // save form_data for later actions acf_update_setting('form_data', $args); // enqueue uploader if page allows AJAX fields to appear if ($args['ajax']) { add_action('admin_footer', 'acf_enqueue_uploader', 1); //acf_enqueue_uploader(); } ?> <div id="acf-form-data" class="acf-hidden"> <input type="hidden" name="_acfnonce" value="<?php echo wp_create_nonce($args['nonce']); ?> " /> <input type="hidden" name="_acfchanged" value="0" /> <?php do_action('acf/input/form_data', $args); ?> </div> <?php }
function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'lang' => false, 'field_key' => '', 'paged' => 1)); // vars $r = array(); $args = array(); // paged $args['posts_per_page'] = 20; $args['paged'] = $options['paged']; // load field $field = acf_get_field($options['field_key']); if (!$field) { return false; } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_get_array($field['post_type']); } else { $args['post_type'] = acf_get_post_types(); } // create tax queries if (!empty($field['taxonomy'])) { // append to $args $args['tax_query'] = array(); // decode terms $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']); // now create the tax queries foreach ($taxonomies as $taxonomy => $terms) { $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } // search if ($options['s']) { $args['s'] = $options['s']; } // filters $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']); $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']); // add archives to $r if ($args['paged'] == 1) { $archives = array(); $archives[] = array('id' => home_url(), 'text' => home_url()); foreach ($args['post_type'] as $post_type) { $archive_link = get_post_type_archive_link($post_type); if ($archive_link) { $archives[] = array('id' => $archive_link, 'text' => $archive_link); } } // search if (!empty($args['s'])) { foreach (array_keys($archives) as $i) { if (strpos($archives[$i]['text'], $args['s']) === false) { unset($archives[$i]); } } $archives = array_values($archives); } if (!empty($archives)) { $r[] = array('text' => __('Archives', 'acf'), 'children' => $archives); } } // get posts grouped by post type $groups = acf_get_grouped_posts($args); if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $posts = acf_extract_var($groups, $group_title); $titles = array(); // data $data = array('text' => $group_title, 'children' => array()); foreach (array_keys($posts) as $post_id) { // override data $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']); } // order by search if (!empty($args['s'])) { $posts = acf_order_by_search($posts, $args['s']); } // append to $data foreach (array_keys($posts) as $post_id) { $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]); } // append to $r $r[] = $data; } } // return return $r; }