Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/01/2024 10:19:51 PM (2 years ago)
Author:
espellcaste
Message:

WPCS: Part III: miscellaneous fixes for some of the files of the core component.

Follow-up to [13883] and [13886]

See #9164 and #7228

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-user-query.php

    r13395 r13887  
    1818 *
    1919 * @since 1.7.0
    20  * @since 10.0.0 Added $date_query parameter.
     20 * @since 10.0.0 Added `$date_query` parameter.
    2121 *
    2222 * @param array $query {
     
    4444 *     @type array|string      $member_type__in     Array or comma-separated list of member types to limit results to.
    4545 *     @type array|string      $member_type__not_in Array or comma-separated list of member types that will be
    46  *                                                                   excluded from results.
     46 *                                                       excluded from results.
    4747 *     @type string|bool       $meta_key            Limit results to users that have usermeta associated with this meta_key.
    4848 *                                                  Usually used with $meta_value. Default: false.
     
    7272         *
    7373         * @since 1.8.0
     74         *
    7475         * @var array
    7576         */
     
    8081         *
    8182         * @since 1.7.0
     83         *
    8284         * @var array
    8385         */
     
    8890         *
    8991         * @since 1.7.0
     92         *
    9093         * @var array
    9194         */
     
    9699         *
    97100         * @since 1.7.0
     101         *
    98102         * @var int
    99103         */
     
    104108         *
    105109         * @since 1.7.0
     110         *
    106111         * @var array
    107112         */
     
    112117         *
    113118         * @since 1.7.0
     119         *
    114120         * @var array
    115121         */
     
    120126         *
    121127         * @since 2.2.0
     128         *
    122129         * @var string
    123130         */
     
    128135         *
    129136         * @since 1.7.0
     137         *
    130138         * @var string
    131139         */
     
    136144         *
    137145         * @since 1.7.0
    138          * @var string
    139          */
    140         protected $no_results = array( 'join' => '', 'where' => '0 = 1' );
    141 
     146         *
     147         * @var array
     148         */
     149        protected $no_results = array(
     150                'join'  => '',
     151                'where' => '0 = 1',
     152        );
    142153
    143154        /** Methods ***************************************************************/
     
    233244         * Prepare the query for user_ids.
    234245         *
     246         * @global wpdb $wpdb WordPress database object.
     247         *
    235248         * @since 1.7.0
    236249         */
     
    256269                $sql = array(
    257270                        'select'  => '',
    258                         'where'   => array('1=1'),
     271                        'where'   => array( '1=1' ),
    259272                        'orderby' => '',
    260273                        'order'   => '',
    261                         'limit'   => ''
     274                        'limit'   => '',
    262275                );
    263276
     
    271284                        // Filter 'bp_user_query_online_interval' to modify the
    272285                        // number of minutes used as an interval.
    273                         case 'online' :
    274                                 $this->uid_name = 'user_id';
     286                        case 'online':
     287                                $this->uid_name  = 'user_id';
    275288                                $this->uid_table = $bp->members->table_name_last_activity;
    276                                 $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    277                                 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
     289                                $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     290                                $sql['where'][]  = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
    278291
    279292                                /**
     
    284297                                 * @param int $value Amount of minutes for threshold. Default 15.
    285298                                 */
    286                                 $sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) );
    287                                 $sql['orderby'] = "ORDER BY u.date_recorded";
    288                                 $sql['order']   = "DESC";
     299                                $sql['where'][] = $wpdb->prepare( 'u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )', apply_filters( 'bp_user_query_online_interval', 15 ) );
     300                                $sql['orderby'] = 'ORDER BY u.date_recorded';
     301                                $sql['order']   = 'DESC';
    289302
    290303                                // Date query.
     
    298311                        // 'active', 'newest', and 'random' queries
    299312                        // all happen against the last_activity usermeta key.
    300                         case 'active' :
    301                         case 'newest' :
    302                         case 'random' :
    303                                 $this->uid_name = 'user_id';
     313                        case 'active':
     314                        case 'newest':
     315                        case 'random':
     316                                $this->uid_name  = 'user_id';
    304317                                $this->uid_table = $bp->members->table_name_last_activity;
    305                                 $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    306                                 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
    307 
    308                                 if ( 'newest' == $type ) {
    309                                         $sql['orderby'] = "ORDER BY u.user_id";
    310                                         $sql['order'] = "DESC";
    311                                 } elseif ( 'random' == $type ) {
    312                                         $sql['orderby'] = "ORDER BY rand()";
     318                                $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     319                                $sql['where'][]  = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );
     320
     321                                if ( 'newest' === $type ) {
     322                                        $sql['orderby'] = 'ORDER BY u.user_id';
     323                                        $sql['order']   = 'DESC';
     324                                } elseif ( 'random' === $type ) {
     325                                        $sql['orderby'] = 'ORDER BY rand()';
    313326                                } else {
    314                                         $sql['orderby'] = "ORDER BY u.date_recorded";
    315                                         $sql['order'] = "DESC";
     327                                        $sql['orderby'] = 'ORDER BY u.date_recorded';
     328                                        $sql['order']   = 'DESC';
    316329                                }
    317330
     
    325338
    326339                        // 'popular' sorts by the 'total_friend_count' usermeta.
    327                         case 'popular' :
    328                                 $this->uid_name = 'user_id';
     340                        case 'popular':
     341                                $this->uid_name  = 'user_id';
    329342                                $this->uid_table = $wpdb->usermeta;
    330                                 $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    331                                 $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) );
    332                                 $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)";
    333                                 $sql['order']   = "DESC";
     343                                $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     344                                $sql['where'][]  = $wpdb->prepare( 'u.meta_key = %s', bp_get_user_meta_key( 'total_friend_count' ) );
     345                                $sql['orderby']  = 'ORDER BY CONVERT(u.meta_value, SIGNED)';
     346                                $sql['order']    = 'DESC';
    334347
    335348                                break;
    336349
    337350                        // 'alphabetical' sorts depend on the xprofile setup.
    338                         case 'alphabetical' :
    339 
     351                        case 'alphabetical':
    340352                                // We prefer to do alphabetical sorts against the display_name field
    341353                                // of wp_users, because the table is smaller and better indexed. We
     
    344356                                // @todo remove need for bp_is_active() check.
    345357                                if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) {
    346                                         $this->uid_name = 'ID';
     358                                        $this->uid_name  = 'ID';
    347359                                        $this->uid_table = $wpdb->users;
    348                                         $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    349                                         $sql['orderby'] = "ORDER BY u.display_name";
    350                                         $sql['order']   = "ASC";
    351 
    352                                 // When profile sync is disabled, alphabetical sorts must happen against
    353                                 // the xprofile table.
     360                                        $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     361                                        $sql['orderby']  = 'ORDER BY u.display_name';
     362                                        $sql['order']    = 'ASC';
     363
     364                                        // When profile sync is disabled, alphabetical sorts must happen against
     365                                        // the xprofile table.
    354366                                } else {
    355                                         $this->uid_name = 'user_id';
     367                                        $this->uid_name  = 'user_id';
    356368                                        $this->uid_table = $bp->profile->table_name_data;
    357                                         $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    358                                         $sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() );
    359                                         $sql['orderby'] = "ORDER BY u.value";
    360                                         $sql['order']   = "ASC";
     369                                        $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     370                                        $sql['where'][]  = $wpdb->prepare( 'u.field_id = %d', bp_xprofile_fullname_field_id() );
     371                                        $sql['orderby']  = 'ORDER BY u.value';
     372                                        $sql['order']    = 'ASC';
    361373                                }
    362374
     
    364376                                // to infer spam/deleted/non-activated users. To ensure that these users
    365377                                // are filtered out, we add an appropriate sub-query.
    366                                 $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . " )";
     378                                $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . ' )';
    367379
    368380                                break;
    369381
    370382                        // Any other 'type' falls through.
    371                         default :
    372                                 $this->uid_name = 'ID';
     383                        default:
     384                                $this->uid_name  = 'ID';
    373385                                $this->uid_table = $wpdb->users;
    374                                 $sql['select']  = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
     386                                $sql['select']   = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";
    375387
    376388                                // In this case, we assume that a plugin is
     
    387399
    388400                // An array containing nothing but 0 should always fail.
    389                 if ( 1 === count( $include_ids ) && 0 == reset( $include_ids ) ) {
     401                if ( 1 === count( $include_ids ) && 0 === reset( $include_ids ) ) {
    390402                        $sql['where'][] = $this->no_results['where'];
    391403                } elseif ( ! empty( $include_ids ) ) {
     
    409421                                $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})";
    410422
    411                         // If the user has no friends, the query should always
    412                         // return no users.
     423                                // If the user has no friends, the query should always
     424                                // return no users.
    413425                        } else {
    414426                                $sql['where'][] = $this->no_results['where'];
     
    427439                                $search_terms_space   = '%' . $search_terms . ' %';
    428440                        } elseif ( $search_wildcard === 'right' ) {
    429                                 $search_terms_nospace =        $search_terms . '%';
     441                                $search_terms_nospace = $search_terms . '%';
    430442                                $search_terms_space   = '% ' . $search_terms . '%';
    431443                        } else {
     
    434446                        }
    435447
    436                         $matched_user_ids = $wpdb->get_col( $wpdb->prepare(
    437                                 "SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s )",
    438                                 $search_terms_nospace,
    439                                 $search_terms_space,
    440                                 $search_terms_nospace,
    441                                 $search_terms_space
    442                         ) );
    443 
    444                         $match_in_clause = empty( $matched_user_ids) ? 'NULL' : implode( ',', $matched_user_ids );
     448                        $matched_user_ids = $wpdb->get_col(
     449                                $wpdb->prepare(
     450                                        "SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s )",
     451                                        $search_terms_nospace,
     452                                        $search_terms_space,
     453                                        $search_terms_nospace,
     454                                        $search_terms_space
     455                                )
     456                        );
     457
     458                        $match_in_clause        = empty( $matched_user_ids ) ? 'NULL' : implode( ',', $matched_user_ids );
    445459                        $sql['where']['search'] = "u.{$this->uid_name} IN ({$match_in_clause})";
    446460                }
     
    455469                        $member_type_clause = $this->get_sql_clause_for_member_types( $member_type__not_in, 'NOT IN' );
    456470
    457                 // Member types to include.
     471                        // Member types to include.
    458472                } elseif ( ! empty( $member_type ) ) {
    459473                        $member_type_clause = $this->get_sql_clause_for_member_types( $member_type, 'IN' );
     
    470484
    471485                        if ( false !== $meta_value ) {
    472                                 $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value );
     486                                $meta_sql .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );
    473487                        }
    474488
     
    476490
    477491                        if ( ! empty( $found_user_ids ) ) {
    478                                 $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")";
     492                                $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ')';
    479493                        } else {
    480494                                $sql['where'][] = '1 = 0';
     
    483497
    484498                // 'per_page', 'page' - handles LIMIT.
    485                 if ( !empty( $per_page ) && !empty( $page ) ) {
    486                         $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) );
     499                if ( ! empty( $per_page ) && ! empty( $page ) ) {
     500                        $sql['limit'] = $wpdb->prepare( 'LIMIT %d, %d', intval( ( $page - 1 ) * $per_page ), intval( $per_page ) );
    487501                } else {
    488502                        $sql['limit'] = '';
     
    524538         * Also used to quickly perform user total counts.
    525539         *
     540         * @global wpdb $wpdb WordPress database object.
     541         *
    526542         * @since 1.7.0
    527543         */
     
    530546
    531547                // If counting using SQL_CALC_FOUND_ROWS, set it up here.
    532                 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {
     548                if ( 'sql_calc_found_rows' === $this->query_vars['count_total'] ) {
    533549                        $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] );
    534550                }
     
    538554
    539555                // Get the total user count.
    540                 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {
     556                if ( 'sql_calc_found_rows' === $this->query_vars['count_total'] ) {
    541557
    542558                        /**
     
    552568                         * @param BP_User_Query $user_query Current BP_User_Query instance.
    553569                         */
    554                         $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "SELECT FOUND_ROWS()", $this ) );
    555                 } elseif ( 'count_query' == $this->query_vars['count_total'] ) {
    556                         $count_select      = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] );
     570                        $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', 'SELECT FOUND_ROWS()', $this ) );
     571                } elseif ( 'count_query' === $this->query_vars['count_total'] ) {
     572                        $count_select = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] );
    557573
    558574                        /** This filter is documented in bp-core/classes/class-bp-user-query.php */
     
    582598                 * @param BP_User_Query $user_query Current BP_User_Query instance.
    583599                 */
    584                 $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array(
    585 
    586                         // Relevant.
    587                         'fields'      => $fields,
    588                         'include'     => $this->user_ids,
    589 
    590                         // Overrides
    591                         'blog_id'     => 0,    // BP does not require blog roles.
    592                         'count_total' => false // We already have a count.
    593 
    594                 ), $this ) );
     600                $wp_user_query = new WP_User_Query(
     601                        apply_filters(
     602                                'bp_wp_user_query_args',
     603                                array(
     604                                        // Relevant.
     605                                        'fields'      => $fields,
     606                                        'include'     => $this->user_ids,
     607
     608                                        // Overrides.
     609                                        'blog_id'     => 0,    // BP does not require blog roles.
     610                                        'count_total' => false, // We already have a count.
     611                                ),
     612                                $this
     613                        )
     614                );
    595615
    596616                /*
     
    613633                foreach ( $this->user_ids as $key => $uid ) {
    614634                        if ( isset( $r[ $uid ] ) ) {
    615                                 $r[ $uid ]->ID = (int) $uid;
     635                                $r[ $uid ]->ID          = (int) $uid;
    616636                                $r[ $uid ]->user_status = (int) $r[ $uid ]->user_status;
    617637
     
    622642                                $this->results[ $uid ]->id = (int) $uid;
    623643
    624                         // Remove user ID from original user_ids property.
     644                                // Remove user ID from original user_ids property.
    625645                        } else {
    626646                                unset( $this->user_ids[ $key ] );
     
    641661         * @since 1.8.0
    642662         *
    643          * @param array $include Sanitized array of user IDs, as passed to the 'include'
    644          *                       parameter of the class constructor.
     663         * @param array $include_ids Sanitized array of user IDs, as passed to the 'include'
     664         *                           parameter of the class constructor.
    645665         * @return array The list of users to which the main query should be
    646666         *               limited.
    647667         */
    648         public function get_include_ids( $include = array() ) {
    649                 return $include;
     668        public function get_include_ids( $include_ids = array() ) {
     669                return $include_ids;
    650670        }
    651671
     
    671691                // In the case of the 'popular' sort type, we force
    672692                // populate_extras to true, because we need the friend counts.
    673                 if ( 'popular' == $this->query_vars['type'] ) {
     693                if ( 'popular' === $this->query_vars['type'] ) {
    674694                        $this->query_vars['populate_extras'] = 1;
    675695                }
     
    707727                // Set a last_activity value for each user, even if it's empty.
    708728                foreach ( $this->results as $user_id => $user ) {
    709                         $user_last_activity = isset( $last_activities[ $user_id ]['date_recorded'] ) ? $last_activities[ $user_id ]['date_recorded'] : '';
     729                        $user_last_activity                       = isset( $last_activities[ $user_id ]['date_recorded'] ) ? $last_activities[ $user_id ]['date_recorded'] : '';
    710730                        $this->results[ $user_id ]->last_activity = $user_last_activity;
    711731                }
     
    716736                // - latest update.
    717737                $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' );
    718                 $bp_latest_update_key   = bp_get_user_meta_key( 'bp_latest_update'   );
     738                $bp_latest_update_key   = bp_get_user_meta_key( 'bp_latest_update' );
    719739
    720740                // Total_friend_count must be set for each user, even if its
    721741                // value is 0.
    722742                foreach ( $this->results as $uindex => $user ) {
    723                         $this->results[$uindex]->total_friend_count = 0;
     743                        $this->results[ $uindex ]->total_friend_count = 0;
    724744                }
    725745
     
    731751                foreach ( $user_metas as $user_meta ) {
    732752                        switch ( $user_meta->meta_key ) {
    733                                 case $total_friend_count_key :
     753                                case $total_friend_count_key:
    734754                                        $key = 'total_friend_count';
    735755                                        break;
    736756
    737                                 case $bp_latest_update_key :
     757                                case $bp_latest_update_key:
    738758                                        $key = 'latest_update';
    739759                                        break;
     
    749769                if ( ! empty( $this->query_vars['meta_key'] ) ) {
    750770                        $meta_sql = array(
    751                                 'select' => "SELECT user_id, meta_key, meta_value",
     771                                'select' => 'SELECT user_id, meta_key, meta_value',
    752772                                'from'   => "FROM $wpdb->usermeta",
    753                                 'where'  => $wpdb->prepare( "WHERE meta_key = %s", $this->query_vars['meta_key'] )
     773                                'where'  => $wpdb->prepare( 'WHERE meta_key = %s', $this->query_vars['meta_key'] ),
    754774                        );
    755775
    756776                        if ( false !== $this->query_vars['meta_value'] ) {
    757                                 $meta_sql['where'] .= $wpdb->prepare( " AND meta_value = %s", $this->query_vars['meta_value'] );
     777                                $meta_sql['where'] .= $wpdb->prepare( ' AND meta_value = %s', $this->query_vars['meta_value'] );
    758778                        }
    759779
     
    779799         * @since 2.4.0
    780800         *
     801         * @global wpdb $wpdb WordPress database object.
     802         *
    781803         * @param string|array $member_types Array or comma-separated list of member types.
    782804         * @param string       $operator     'IN' or 'NOT IN'.
     
    803825                }
    804826
    805                 $tax_query = new WP_Tax_Query( array(
     827                $tax_query = new WP_Tax_Query(
    806828                        array(
    807                                 'taxonomy' => bp_get_member_type_tax_name(),
    808                                 'field'    => 'name',
    809                                 'operator' => $operator,
    810                                 'terms'    => $types,
    811                         ),
    812                 ) );
     829                                array(
     830                                        'taxonomy' => bp_get_member_type_tax_name(),
     831                                        'field'    => 'name',
     832                                        'operator' => $operator,
     833                                        'terms'    => $types,
     834                                ),
     835                        )
     836                );
    813837
    814838                // Switch to the root blog, where member type taxonomies live.
     
    828852                        $clause = $this->no_results['where'];
    829853
    830                 // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'.
     854                        // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'.
    831855                } elseif ( 'NOT IN' === $operator ) {
    832856                        $clause = preg_replace( '/^\s*AND\s*/', '', $sql_clauses['where'] );
    833857
    834                 // IN clauses must be converted to a subquery.
     858                        // IN clauses must be converted to a subquery.
    835859                } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
    836860                        $clause = "u.{$this->uid_name} IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
Note: See TracChangeset for help on using the changeset viewer.