WordPress.org

Make WordPress Core

Ticket #44169: users-pre-query-1.patch

File users-pre-query-1.patch, 2.7 KB (added by tlovett1, 2 years ago)
  • wp-includes/class-wp-user-query.php

     
    592592
    593593                $qv =& $this->query_vars;
    594594
    595                 $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
    596 
    597                 if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
    598                         $this->results = $wpdb->get_results( $this->request );
    599                 } else {
    600                         $this->results = $wpdb->get_col( $this->request );
    601                 }
    602 
    603595                /**
    604                  * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
     596                 * Filters the users array before the query takes place.
    605597                 *
    606                  * @since 3.2.0
     598                 * Return a non-null value to bypass WordPress's default user queries.
    607599                 *
    608                  * @global wpdb $wpdb WordPress database abstraction object.
     600                 * Filtering functions that require pagination information are encouraged to set
     601                 * the `total_users` properties of the WP_User_Query object, passed to the filter
     602                 * by reference. If WP_User_Query does not perform a database query, it will not
     603                 * have enough information to generate these values itself.
    609604                 *
    610                  * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
     605                 * @since 5.0.0
     606                 *
     607                 * @param array|null $results Return an array of user data to short-circuit WP's user query,
     608                 *                          or null to allow WP to run its normal queries.
     609                 * @param WP_User_Query   $this  The WP_User_Query instance (passed by reference).
    611610                 */
    612                 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
    613                         $this->total_users = (int) $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
     611                $this->results = apply_filters_ref_array( 'users_pre_query', array( null, &$this ) );
     612
     613                if ( null === $this->results ) {
     614                        $this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
     615
     616                        if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
     617                                $this->results = $wpdb->get_results( $this->request );
     618                        } else {
     619                                $this->results = $wpdb->get_col( $this->request );
     620                        }
     621
     622                        /**
     623                         * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance.
     624                         *
     625                         * @since 3.2.0
     626                         *
     627                         * @global wpdb $wpdb WordPress database abstraction object.
     628                         *
     629                         * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
     630                         */
     631                        if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
     632                                $this->total_users = (int) $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
     633                        }
    614634                }
    615635
    616636                if ( ! $this->results ) {