Make WordPress Core

Changeset 34804


Ignore:
Timestamp:
10/03/2015 06:44:40 PM (11 years ago)
Author:
boonebgorges
Message:

Ensure that WP_User_Query vars are filled after 'pre_get_users'.

This prevents notices from being thrown when a 'pre_get_users' callback
removes required values from the list of query_vars.

For backward compatibility with previous uses of 'pre_get_users', default
values are parsed both before and after the action is fired.

Fixes #33449.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r34531 r34804  
    7474                        $this->query();
    7575                }
     76        }
     77
     78        /**
     79         * Fills in missing query variables with default values.
     80         *
     81         * @since 4.4.0
     82         * @access public
     83         *
     84         * @param array $args Query vars, as passed to `WP_User_Query`.
     85         * @return array Complete query variables with undefined ones filled in with defaults.
     86         */
     87        public static function fill_query_vars( $args ) {
     88                $defaults = array(
     89                        'blog_id' => $GLOBALS['blog_id'],
     90                        'role' => '',
     91                        'meta_key' => '',
     92                        'meta_value' => '',
     93                        'meta_compare' => '',
     94                        'include' => array(),
     95                        'exclude' => array(),
     96                        'search' => '',
     97                        'search_columns' => array(),
     98                        'orderby' => 'login',
     99                        'order' => 'ASC',
     100                        'offset' => '',
     101                        'number' => '',
     102                        'paged' => 1,
     103                        'count_total' => true,
     104                        'fields' => 'all',
     105                        'who' => '',
     106                        'has_published_posts' => null,
     107                );
     108
     109                return wp_parse_args( $args, $defaults );
    76110        }
    77111
     
    147181                if ( empty( $this->query_vars ) || ! empty( $query ) ) {
    148182                        $this->query_limit = null;
    149                         $this->query_vars = wp_parse_args( $query, array(
    150                                 'blog_id' => $GLOBALS['blog_id'],
    151                                 'role' => '',
    152                                 'meta_key' => '',
    153                                 'meta_value' => '',
    154                                 'meta_compare' => '',
    155                                 'include' => array(),
    156                                 'exclude' => array(),
    157                                 'search' => '',
    158                                 'search_columns' => array(),
    159                                 'orderby' => 'login',
    160                                 'order' => 'ASC',
    161                                 'offset' => '',
    162                                 'number' => '',
    163                                 'paged' => 1,
    164                                 'count_total' => true,
    165                                 'fields' => 'all',
    166                                 'who' => '',
    167                                 'has_published_posts' => null,
    168                         ) );
     183                        $this->query_vars = $this->fill_query_vars( $query );
    169184                }
    170185
     
    182197                do_action( 'pre_get_users', $this );
    183198
     199                // Ensure that query vars are filled after 'pre_get_users'.
    184200                $qv =& $this->query_vars;
     201                $qv =  $this->fill_query_vars( $qv );
    185202
    186203                if ( is_array( $qv['fields'] ) ) {
  • trunk/tests/phpunit/tests/user/query.php

    r34531 r34804  
    875875                $this->assertEquals( array( $users[2], $users[1] ), $q->results );
    876876        }
     877
     878        /**
     879         * @ticket 33449
     880         */
     881        public function test_query_vars_should_be_filled_in_after_pre_get_users() {
     882                $query_vars = array( 'blog_id', 'role', 'meta_key', 'meta_value', 'meta_compare', 'include', 'exclude', 'search', 'search_columns', 'orderby', 'order', 'offset', 'number', 'paged', 'count_total', 'fields', 'who', 'has_published_posts' );
     883
     884                add_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
     885                $q = new WP_User_Query( array_fill_keys( $query_vars, '1' ) );
     886                remove_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
     887
     888                foreach ( $query_vars as $query_var ) {
     889                        $this->assertTrue( array_key_exists( $query_var, $q->query_vars ), "$query_var does not exist." );
     890                }
     891
     892        }
     893
     894        public function filter_pre_get_users_args( $q ) {
     895                foreach ( $q->query_vars as $k => $v ) {
     896                        unset( $q->query_vars[ $k ] );
     897                }
     898        }
    877899}
Note: See TracChangeset for help on using the changeset viewer.