function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // get choices $response = $this->get_ajax_query($_POST); // return acf_send_ajax_results($response); }
function ajax_search() { // validate if (!acf_verify_ajax()) { die; } // options $args = acf_parse_args($_POST, array('s' => '', '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']; } // get oembed echo $this->wp_oembed_get($args['s'], $args['width'], $args['height']); // die die; }
function ajax_render_location_value() { // validate if (!acf_verify_ajax()) { die; } // call function $this->render_location_value($_POST); // 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)); }
function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // get choices $choices = $this->get_choices($_POST); // validate if (!$choices) { die; } // return JSON echo json_encode($choices); die; }
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 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['users_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; } // role if (!empty($field['role'])) { $args['role'] = acf_get_array($field['role']); } // search if ($is_search) { // 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 $groups = acf_get_grouped_users($args); // loop if (!empty($groups)) { foreach (array_keys($groups) as $group_title) { // vars $users = acf_extract_var($groups, $group_title); $data = array('text' => $group_title, 'children' => array()); // append users foreach (array_keys($users) as $user_id) { $users[$user_id] = $this->get_result($users[$user_id], $field, $options['post_id']); } // order by search if ($is_search && empty($args['orderby'])) { $users = acf_order_by_search($users, $args['s']); } // append to $data foreach ($users as $id => $title) { $data['children'][] = array('id' => $id, 'text' => $title); } // append to $r $results[] = $data; } } // optgroup or single if (!empty($args['role']) && count($args['role']) == 1) { $results = $results[0]['children']; } // return acf_send_ajax_results(array('results' => $results, 'limit' => $args['users_per_page'])); }
function get_field_groups() { // options $options = acf_parse_args($_POST, array('nonce' => '', 'post_id' => 0, 'ajax' => 1, 'exists' => array())); // vars $json = array(); $exists = acf_extract_var($options, 'exists'); // verify nonce if (!acf_verify_ajax()) { die; } // get field groups $field_groups = acf_get_field_groups($options); // bail early if no field groups if (empty($field_groups)) { wp_send_json_success($json); } // loop through field groups foreach ($field_groups as $i => $field_group) { // vars $item = array('key' => $field_group['key'], 'title' => $field_group['title'], 'html' => '', 'style' => ''); // style if ($i == 0) { $item['style'] = acf_get_field_group_style($field_group); } // html if (!in_array($field_group['key'], $exists)) { // load fields $fields = acf_get_fields($field_group); // get field HTML ob_start(); // render acf_render_fields($options['post_id'], $fields, 'div', $field_group['instruction_placement']); $item['html'] = ob_get_clean(); } // append $json[] = $item; } // return wp_send_json_success($json); }
function ajax_validate_save_post() { // validate if (!acf_verify_ajax()) { die; } // vars $json = array('valid' => 1, 'errors' => 0); // success if (acf_validate_save_post()) { wp_send_json_success($json); } // update vars $json['valid'] = 0; $json['errors'] = acf_get_validation_errors(); // return wp_send_json_success($json); }