Make WordPress Core


Ignore:
Timestamp:
04/16/2010 02:08:58 PM (15 years ago)
Author:
nacin
Message:

Introduce the wp_filter_object_list() helper, with an $operator arg. Fixes an intersection bug in get_post_types() and get_taxonomies(). Also switches $operator default from 'or' to 'and' for get_post_stati(). props scribu, fixes #12966.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r14087 r14108  
    632632 * @see get_post_status_object
    633633 *
    634  * @param array|string $args An array of key => value arguments to match against the post statuses.
    635  *  Only post statuses having attributes that match all arguments are returned.
     634 * @param array|string $args An array of key => value arguments to match against the post status objects.
    636635 * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
    637  * @param string $operator Whether the elements in $args should be logicallly 'or'ed or 'and'ed together. 'or' means only one element from the array needs to match. 'and' means all elements must match. The default is 'or'.
     636 * @param string $operator The logical operation to perform. 'or' means only one element
     637 *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
    638638 * @return array A list of post type names or objects
    639639 */
    640 function get_post_stati( $args = array(), $output = 'names', $operator = 'or' ) {
     640function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
    641641    global $wp_post_statuses;
    642642
    643     $do_names = false;
    644     if ( 'names' == $output )
    645         $do_names = true;
    646 
    647     if ( 'and' == $operator )
    648         $arg_count = count($args);
    649     else
    650         $arg_count = 0;
    651 
    652     $post_statuses = array();
    653     foreach ( (array) $wp_post_statuses as $post_status ) {
    654         if ( empty($args) ) {
    655             if ( $do_names )
    656                 $post_statuses[] = $post_status->name;
    657             else
    658                 $post_statuses[] = $post_status;
    659         } elseif ( $intersect = array_intersect_assoc((array) $post_status, $args) ) {
    660             if ( $arg_count && ( $arg_count != count($intersect) ) )
    661                 continue;
    662             if ( $do_names )
    663                 $post_statuses[] = $post_status->name;
    664             else
    665                 $post_statuses[] = $post_status;
    666         }
    667     }
    668 
    669     return $post_statuses;
     643    $field = ('names' == $output) ? 'name' : false;
     644
     645    return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
    670646}
    671647
     
    702678 * @return bool|string post type or false on failure.
    703679 */
    704 function get_post_type($the_post = false) {
     680function get_post_type( $the_post = false ) {
    705681    global $post;
    706682
     
    746722 * @uses $wp_post_types
    747723 * @see register_post_type
    748  * @see get_post_types
    749  *
    750  * @param array|string $args An array of key => value arguments to match against the post types.
    751  *  Only post types having attributes that match all arguments are returned.
     724 *
     725 * @param array|string $args An array of key => value arguments to match against the post type objects.
    752726 * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
     727 * @param string $operator The logical operation to perform. 'or' means only one element
     728 *  from the array needs to match; 'and' means all elements must match. The default is 'and'.
    753729 * @return array A list of post type names or objects
    754730 */
    755 function get_post_types( $args = array(), $output = 'names' ) {
     731function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
    756732    global $wp_post_types;
    757733
    758     $do_names = false;
    759     if ( 'names' == $output )
    760         $do_names = true;
    761 
    762     $post_types = array();
    763     foreach ( (array) $wp_post_types as $post_type ) {
    764         if ( empty($args) ) {
    765             if ( $do_names )
    766                 $post_types[] = $post_type->name;
    767             else
    768                 $post_types[] = $post_type;
    769         } elseif ( array_intersect_assoc((array) $post_type, $args) ) {
    770             if ( $do_names )
    771                 $post_types[] = $post_type->name;
    772             else
    773                 $post_types[] = $post_type;
    774         }
    775     }
    776 
    777     return $post_types;
     734    $field = ('names' == $output) ? 'name' : false;
     735
     736    return wp_filter_object_list($wp_post_types, $args, $operator, $field);
    778737}
    779738
Note: See TracChangeset for help on using the changeset viewer.