示例#1
0
 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));
 }
示例#2
0
        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 
        }
示例#3
0
function acf_get_posts($args = array())
{
    // vars
    $posts = array();
    // defaults
    // leave suppress_filters as true becuase we don't want any plugins to modify the query as we know exactly what
    $args = acf_parse_args($args, array('posts_per_page' => -1, 'post_type' => acf_get_post_types(), 'post_status' => 'any'));
    // validate post__in
    if ($args['post__in']) {
        // force value to array
        $args['post__in'] = acf_force_type_array($args['post__in']);
        // convert to int
        $args['post__in'] = array_map('intval', $args['post__in']);
        // add filter to remove post_type
        // use 'query' filter so that 'suppress_filters' can remain true
        add_filter('query', '_acf_get_posts_query');
        // order by post__in
        $args['orderby'] = 'post__in';
    }
    // load posts in 1 query to save multiple DB calls from following code
    $posts = get_posts($args);
    // validate order
    if ($posts && $args['post__in']) {
        // vars
        $order = array();
        // generate sort order
        foreach ($posts as $i => $post) {
            $order[$i] = array_search($post->ID, $args['post__in']);
        }
        // sort
        array_multisort($order, $posts);
    }
    // return
    return $posts;
}
示例#4
0
 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;
 }
示例#5
0
 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;
 }
示例#6
0
 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']));
 }
示例#7
0
 function get_posts($value)
 {
     // force value to array
     $value = acf_force_type_array($value);
     // convert to int
     $value = array_map('intval', $value);
     // load posts in 1 query to save multiple DB calls from following code
     if (count($value) > 1) {
         get_posts(array('posts_per_page' => -1, 'post_type' => acf_get_post_types(), 'post_status' => 'any', 'post__in' => $value));
     }
     // vars
     $posts = array();
     // update value to include $post
     foreach ($value as $post_id) {
         if ($post = get_post($post_id)) {
             $posts[] = $post;
         }
     }
     // return
     return $posts;
 }
function acf_get_pretty_post_types($post_types = array())
{
    // get post types
    if (empty($post_types)) {
        // get all custom post types
        $post_types = acf_get_post_types();
    }
    // get labels
    $ref = array();
    $r = array();
    foreach ($post_types as $post_type) {
        // vars
        $label = $post_type;
        // check that object exists (case exists when importing field group from another install and post type does not exist)
        if (post_type_exists($post_type)) {
            $obj = get_post_type_object($post_type);
            $label = $obj->labels->singular_name;
        }
        // append to r
        $r[$post_type] = $label;
        // increase counter
        if (!isset($ref[$label])) {
            $ref[$label] = 0;
        }
        $ref[$label]++;
    }
    // get slugs
    foreach (array_keys($r) as $i) {
        // vars
        $post_type = $r[$i];
        if ($ref[$post_type] > 1) {
            $r[$i] .= ' (' . $i . ')';
        }
    }
    // return
    return $r;
}
示例#9
0
 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_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;
 }
示例#10
0
 function get_posts($value)
 {
     // force value to array
     $value = acf_force_type_array($value);
     // get selected post ID's
     $post_ids = array();
     foreach ($value as $v) {
         if (is_numeric($v)) {
             $post_ids[] = intval($v);
         }
     }
     // load posts in 1 query to save multiple DB calls from following code
     if (count($post_ids) > 1) {
         get_posts(array('posts_per_page' => -1, 'post_type' => acf_get_post_types(), 'post_status' => 'any', 'post__in' => $post_ids));
     }
     // vars
     $posts = array();
     // update value to include $post
     foreach ($value as $v) {
         if (is_numeric($v)) {
             if ($post = get_post($v)) {
                 $posts[] = $post;
             }
         } else {
             $posts[] = $v;
         }
     }
     // return
     return $posts;
 }
示例#11
0
function acf_get_pretty_post_types($post_types = array())
{
    // get post types
    if (empty($post_types)) {
        // get all custom post types
        $post_types = acf_get_post_types();
    }
    // get labels
    $ref = array();
    $r = array();
    foreach (array_keys($post_types) as $i) {
        // vars
        $post_type = acf_extract_var($post_types, $i);
        $obj = get_post_type_object($post_type);
        $name = $obj->labels->singular_name;
        // append to r
        $r[$post_type] = $name;
        // increase counter
        if (!isset($ref[$name])) {
            $ref[$name] = 0;
        }
        $ref[$name]++;
    }
    // get slugs
    foreach (array_keys($r) as $i) {
        // vars
        $post_type = $r[$i];
        if ($ref[$post_type] > 1) {
            $r[$i] .= ' (' . $i . ')';
        }
    }
    // return
    return $r;
}
示例#12
0
    // 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;
    }
}
// post type filter
$post_types = array();
if ($width['post_type']) {
    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);
}
// taxonomy filter
$taxonomies = array();
$term_groups = array();
if ($width['taxonomy']) {
    // taxonomies
    if (!empty($field['taxonomy'])) {
        // get the field's terms
        $term_groups = acf_get_array($field['taxonomy']);
        $term_groups = acf_decode_taxonomy_terms($term_groups);
        // update taxonomies
        $taxonomies = array_keys($term_groups);
    } elseif (!empty($field['post_type'])) {
示例#13
0
 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']) {
         /*
          *  Post
          */
         case "post_type":
             // get post types
             $post_types = acf_get_post_types(array('show_ui' => 1, 'exclude' => array('attachment')));
             // get choices
             $choices = acf_get_pretty_post_types($post_types);
             // end
             break;
         case "post":
             // get post types
             $post_types = acf_get_post_types(array('exclude' => array('page', 'attachment')));
             // get posts grouped by post type
             $groups = acf_get_grouped_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_template":
             // vars
             $templates = wp_get_theme()->get_post_templates();
             $default = apply_filters('default_page_template_title', __('Default Template', 'acf'));
             // choices
             $choices = array('default' => $default);
             // templates
             if (!empty($templates)) {
                 foreach ($templates as $post_type => $post_type_templates) {
                     $choices = array_merge($choices, $post_type_templates);
                 }
             }
             // break
             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":
             global $wp_post_statuses;
             if (!empty($wp_post_statuses)) {
                 foreach ($wp_post_statuses as $status) {
                     $choices[$status->name] = $status->label;
                 }
             }
             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_grouped_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 (no parent)", 'acf'), 'parent' => __("Parent Page (has children)", 'acf'), 'child' => __("Child Page (has parent)", 'acf'));
             break;
         case "page_parent":
             // refer to "page"
             break;
         case "page_template":
             // vars
             $templates = wp_get_theme()->get_page_templates();
             $default = apply_filters('default_page_template_title', __('Default Template', 'acf'));
             // merge
             $choices = array_merge(array('default' => $default), $templates);
             break;
             /*
              *  User
              */
         /*
          *  User
          */
         case "current_user":
             // viewing
             $choices = array('logged_in' => __('Logged in', 'acf'), 'viewing_front' => __('Viewing front end', 'acf'), 'viewing_back' => __('Viewing back end', 'acf'));
             break;
         case "current_user_role":
             // global
             global $wp_roles;
             // specific roles
             $choices = $wp_roles->get_names();
             // multi-site
             if (is_multisite()) {
                 $choices = array_merge(array('super_admin' => __('Super Admin', 'acf')), $choices);
             }
             break;
         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":
             // vars
             $choices = array('all' => __('All', 'acf'));
             // append post types
             $choices = array_merge($choices, acf_get_pretty_post_types());
             // end
             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));
 }
示例#14
0
 /**
  * Match modified values for "Page Type" location rule
  *
  * @param  bool   $match    The true / false variable which must be returned.
  * @param  array  $rule     The current rule that you are matching against.
  * @param  array  $options  Data about the current edit screen, includes any data posted in an AJAX call.
  * @return bool   $match
  */
 public static function location_page_type_rule_match($match, $rule = [], $options = [])
 {
     if ('archives' === $rule['value']) {
         if (empty($options['post_id'])) {
             return false;
         }
         $post = get_post($options['post_id']);
         // Test for Post Archive
         $archive_page = (int) get_option('page_for_posts');
         if ($post->ID != $archive_page) {
             $boilerplate_options = get_option('boilerplate', []);
             if (empty($boilerplate_options)) {
                 return false;
             }
             $exclude = ['post', 'attachment', 'polylang_mo'];
             $post_types = acf_get_post_types($exclude);
             foreach ($post_types as $post_type) {
                 $archive_page = 0;
                 $obj = get_post_type_object($post_type);
                 if (!isset($obj->has_archive) || !$obj->has_archive) {
                     continue;
                 }
                 if (empty($obj->labels->page_for_items_setting) || empty($boilerplate_options[$obj->labels->page_for_items_setting])) {
                     continue;
                 }
                 $archive_page = (int) $boilerplate_options[$obj->labels->page_for_items_setting];
                 if ($post->ID == $archive_page) {
                     break;
                 }
             }
         }
         if ($post->ID == $archive_page) {
             if ($rule['operator'] === '==') {
                 return true;
             } elseif ($rule['operator'] === '!=') {
                 return false;
             }
         } else {
             return true;
         }
     }
     return $match;
 }