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)); }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { // get posts $value = acf_get_posts(array('post__in' => $value, 'post_type' => $field['post_type'])); } // return return $value; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { // return false as $value may be '' (from DB) which doesn't make much sense return false; } // get posts $posts = acf_get_posts(array('post_type' => 'attachment', 'post__in' => $value)); // update value to include $post foreach (array_keys($posts) as $i) { $posts[$i] = acf_get_attachment($posts[$i]); } // return return $posts; }
function get_posts($value, $field) { // force value to array $value = acf_get_array($value); // get selected post ID's $post__in = array(); foreach ($value as $k => $v) { if (is_numeric($v)) { // append to $post__in $post__in[] = (int) $v; } } // bail early if no posts if (empty($post__in)) { return $value; } // get posts $posts = acf_get_posts(array('post__in' => $post__in, 'post_type' => $field['post_type'])); // override value with post $return = array(); // append to $return foreach ($value as $k => $v) { if (is_numeric($v)) { // extract first post $post = array_shift($posts); // append if ($post) { $return[] = $post; } } else { $return[] = $v; } } // return return $return; }
function get_attachments($value) { // bail early if no value if (empty($value)) { return false; } // force value to array $post__in = acf_get_array($value); // get posts $posts = acf_get_posts(array('post_type' => 'attachment', 'post__in' => $post__in)); // return return $posts; }
function get_posts($value, $field) { // numeric $value = acf_get_numeric($value); // bail early if no value if (empty($value)) { return false; } // get posts $posts = acf_get_posts(array('post__in' => $value, 'post_type' => $field['post_type'])); // return return $posts; }
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; } // WPML if ($options['lang']) { global $sitepress; if (!empty($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/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_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_posts($value) { // force value to array $value = acf_force_type_array($value); // get selected post ID's $post__in = array(); foreach (array_keys($value) as $k) { if (is_numeric($value[$k])) { // convert to int $value[$k] = intval($value[$k]); // append to $post__in $post__in[] = $value[$k]; } } // bail early if no posts if (empty($post__in)) { return $value; } // get posts $posts = acf_get_posts(array('post__in' => $post__in)); // override value with post $return = array(); // append to $return foreach ($value as $k => $v) { if (is_numeric($v)) { // find matching $post foreach ($posts as $post) { if ($post->ID == $v) { $return[] = $post; break; } } } else { $return[] = $v; } } // return return $return; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { // get posts $value = acf_get_posts(array('post__in' => $value)); } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
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; }