Make WordPress Core


Ignore:
Timestamp:
05/07/2022 03:30:51 AM (4 years ago)
Author:
peterwilsoncc
Message:

Users: Allow any DB field to be returned by WP_User_Query.

Restore behaviour of fields parameter in WP_User_Query to allow developers to specify any database field to be returned either individually or as part of a subset. Add these fields to the documentation.

When a subset of fields includes the id paramater, include it in the results in both upper and lowercase to maintain backward compatibility.

Follow up to [53327].

Props dd32, pbearne, kraftbj, peterwilsoncc.
Fixes #53177.

File:
1 edited

Legend:

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

    r53327 r53362  
    231231         *                                                - 'user_url'
    232232         *                                                - 'user_registered'
     233         *                                                - 'user_pass'
     234         *                                                - 'user_activation_key'
     235         *                                                - 'user_status'
     236         *                                                - 'spam' (only available on multisite installs)
     237         *                                                - 'deleted' (only available on multisite installs)
    233238         *                                                - 'all' for all fields
    234239         *                                                - 'all_with_meta' to include meta fields.
     
    276281
    277282                $allowed_fields = array(
    278                         'ID',
    279                         'display_name',
     283                        'id',
    280284                        'user_login',
     285                        'user_pass',
    281286                        'user_nicename',
    282287                        'user_email',
    283288                        'user_url',
    284289                        'user_registered',
     290                        'user_activation_key',
     291                        'user_status',
     292                        'display_name',
    285293                );
     294                if ( is_multisite() ) {
     295                        $allowed_fields[] = 'spam';
     296                        $allowed_fields[] = 'deleted';
     297                }
    286298
    287299                if ( is_array( $qv['fields'] ) ) {
     300                        $qv['fields'] = array_map( 'strtolower', $qv['fields'] );
    288301                        $qv['fields'] = array_intersect( array_unique( $qv['fields'] ), $allowed_fields );
    289302
    290303                        if ( empty( $qv['fields'] ) ) {
    291                                 $qv['fields'] = array( 'ID' );
     304                                $qv['fields'] = array( 'id' );
    292305                        }
    293306
    294307                        $this->query_fields = array();
    295308                        foreach ( $qv['fields'] as $field ) {
    296                                 $field                = 'ID' === $field ? 'ID' : sanitize_key( $field );
     309                                $field                = 'id' === $field ? 'ID' : sanitize_key( $field );
    297310                                $this->query_fields[] = "$wpdb->users.$field";
    298311                        }
     
    303316                        $this->query_fields = "$wpdb->users.ID";
    304317                } else {
    305                         $field              = 'ID' === $qv['fields'] ? 'ID' : sanitize_key( $qv['fields'] );
     318                        $field              = 'id' === strtolower( $qv['fields'] ) ? 'ID' : sanitize_key( $qv['fields'] );
    306319                        $this->query_fields = "$wpdb->users.$field";
    307320                }
     
    823836                        return;
    824837                }
    825 
    826                 if ( 'all_with_meta' === $qv['fields'] ) {
     838                if (
     839                        is_array( $qv['fields'] ) &&
     840                        isset( $this->results[0]->ID )
     841                ) {
     842                        foreach ( $this->results as $result ) {
     843                                $result->id = $result->ID;
     844                        }
     845                } elseif ( 'all_with_meta' === $qv['fields'] ) {
    827846                        cache_users( $this->results );
    828847
Note: See TracChangeset for help on using the changeset viewer.