WordPress.org

Make WordPress Core

Ticket #12966: 12966.2.diff

File 12966.2.diff, 3.5 KB (added by scribu, 8 years ago)

introduce wp_filter_object_list()

  • wp-includes/taxonomy.php

     
    7575function get_taxonomies( $args = array(), $output = 'names' ) {
    7676        global $wp_taxonomies;
    7777
    78         $taxonomies = array();
    79         foreach ( (array) $wp_taxonomies as $taxname => $taxobj )
    80                 if ( empty($args) || array_intersect_assoc((array) $taxobj, $args) )
    81                         $taxonomies[$taxname] = $taxobj;
     78        $field = ('names' == $output) ? 'name' : false;
    8279
    83         if ( 'names' == $output )
    84                 return array_keys($taxonomies);
    85 
    86         return $taxonomies;
     80        return wp_filter_object_list($wp_taxonomies, $args, $field);
    8781}
    8882
     83
    8984/**
    9085 * Return all of the taxonomy names that are of $object_type.
    9186 *
  • wp-includes/post.php

     
    692692 * @param mixed $the_post Optional. Post object or post ID.
    693693 * @return bool|string post type or false on failure.
    694694 */
    695 function get_post_type($the_post = false) {
     695function get_post_type( $the_post = false ) {
    696696        global $post;
    697697
    698698        if ( false === $the_post )
     
    736736 * @since 2.9.0
    737737 * @uses $wp_post_types
    738738 * @see register_post_type
    739  * @see get_post_types
    740739 *
    741740 * @param array|string $args An array of key => value arguments to match against the post types.
    742741 *  Only post types having attributes that match all arguments are returned.
     
    746745function get_post_types( $args = array(), $output = 'names' ) {
    747746        global $wp_post_types;
    748747
    749         $do_names = false;
    750         if ( 'names' == $output )
    751                 $do_names = true;
     748        $field = ('names' == $output) ? 'name' : false;
    752749
    753         $post_types = array();
    754         foreach ( (array) $wp_post_types as $post_type ) {
    755                 if ( empty($args) ) {
    756                         if ( $do_names )
    757                                 $post_types[] = $post_type->name;
    758                         else
    759                                 $post_types[] = $post_type;
    760                 } elseif ( array_intersect_assoc((array) $post_type, $args) ) {
    761                         if ( $do_names )
    762                                 $post_types[] = $post_type->name;
    763                         else
    764                                 $post_types[] = $post_type;
    765                 }
    766         }
    767 
    768         return $post_types;
     750        return wp_filter_object_list($wp_post_types, $args, $field);
    769751}
    770752
    771753/**
  • wp-includes/functions.php

     
    29012901 * @param array|string $list
    29022902 * @return array Sanitized array of IDs
    29032903 */
    2904 function wp_parse_id_list($list) {
     2904function wp_parse_id_list( $list ) {
    29052905        if ( !is_array($list) )
    29062906                $list = preg_split('/[\s,]+/', $list);
    29072907
     
    29092909}
    29102910
    29112911/**
     2912 * Filters a list of objects, returning only those that match all arguments
     2913 *
     2914 * @since 3.0.0
     2915 *
     2916 * @param array $list An array of objects to filter
     2917 * @param array $args An array of key => value arguments to match against each object
     2918 * @param bool|string $field A field from the object to place instead of the entire object
     2919 * @return array A list of objects or object fields
     2920 */
     2921function wp_filter_object_list( $list, $args = array(), $field = false ) {
     2922        if ( !is_array($list) )
     2923                return array();
     2924
     2925        $count = count($args);
     2926
     2927        $filtered = array();
     2928        foreach ( $list as $key => $obj )
     2929                if ( count(array_intersect_assoc(get_object_vars($obj), $args)) == $count )
     2930                        $filtered[$key] = $field ? $obj->$field : $obj;
     2931
     2932        return $filtered;
     2933}
     2934
     2935/**
    29122936 * Determines if default embed handlers should be loaded.
    29132937 *
    29142938 * Checks to make sure that the embeds library hasn't already been loaded. If