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_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; }
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 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 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; } // 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_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', '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']); // bail early if no field 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/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']); // is search $is_search = !empty($args['s']); // 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); $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 ($is_search) { $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 if (count($args['post_type']) == 1) { $r = $r[0]['children']; } // return return $r; }
function ajax_query() { // options $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'lang' => false, '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; } // WPML if ($options['lang']) { global $sitepress; $sitepress->switch_lang($options['lang']); } // update $args if (!empty($field['post_type'])) { $args['post_type'] = acf_force_type_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 $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); } } $r[] = array('text' => __('Archives', 'acf'), 'children' => $archives); // get posts grouped by post type $groups = acf_get_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 JSON echo json_encode($r); die; }
function get_choices($options = array()) { // defaults $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1)); // vars $r = array(); $args = array(); // paged $args['users_per_page'] = 20; $args['paged'] = $options['paged']; // load field $field = acf_get_field($options['field_key']); // bail early if no field if (!$field) { return false; } // update $args if (!empty($field['role'])) { $args['role'] = acf_get_array($field['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 $groups = acf_get_grouped_users($args); // bail early if no groups if (empty($groups)) { return false; } // loop 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 (!empty($args['s'])) { $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 $r[] = $data; } // optgroup or single if (!empty($args['role']) && count($args['role']) == 1) { $r = $r[0]['children']; } // return return $r; }