Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/07/2024 01:46:38 AM (11 months ago)
Author:
espellcaste
Message:

Move some BP_User_Query::populate_extras() properties to custom action hooks.

The action of populating class properties were moved from the populate_extras method into action hooks, allowing developers to remove any of them, if needed.

Props r-a-y and imath.

Fixes #9205

File:
1 edited

Legend:

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

    r13896 r13994  
    677677     *
    678678     * @since 1.7.0
    679      *
    680      * @global wpdb $wpdb WordPress database object.
     679     * @since 15.0.0 Some properties were moved into action hooks.
    681680     */
    682681    public function populate_extras() {
    683         global $wpdb;
    684682
    685683        // Bail if no users.
     
    708706         * avoid running multiple queries per user in the loop.
    709707         *
    710          * Two BuddyPress components currently do this:
     708         * Three BuddyPress components currently do this:
    711709         * - XProfile: To override display names.
    712          * - Friends:  To set whether or not a user is the current users friend.
     710         * - Friends:  To set whether a user is the current users friend.
     711         * - Members:  To set last activity, total friend count, last update,
     712         *             and meta_key/meta_value if passed to the query.
    713713         *
    714714         * @see bp_xprofile_filter_user_query_populate_extras()
    715715         * @see bp_friends_filter_user_query_populate_extras()
     716         * @see bp_members_filter_user_query_populate_extras_last_activity()
     717         * @see bp_members_filter_user_query_populate_extras_friend_count_latest_update()
     718         * @see bp_members_filter_user_query_populate_extras_meta()
    716719         *
    717720         * @since 1.7.0
     
    721724         */
    722725        do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) );
    723 
    724         // Fetch last_active data from the activity table.
    725         $last_activities = BP_Core_User::get_last_activity( $this->user_ids );
    726 
    727         // Set a last_activity value for each user, even if it's empty.
    728         foreach ( $this->results as $user_id => $user ) {
    729             $user_last_activity                       = isset( $last_activities[ $user_id ]['date_recorded'] ) ? $last_activities[ $user_id ]['date_recorded'] : '';
    730             $this->results[ $user_id ]->last_activity = $user_last_activity;
    731         }
    732 
    733         // Fetch usermeta data
    734         // We want the three following pieces of info from usermeta:
    735         // - friend count
    736         // - latest update.
    737         $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' );
    738         $bp_latest_update_key   = bp_get_user_meta_key( 'bp_latest_update' );
    739 
    740         // Total_friend_count must be set for each user, even if its
    741         // value is 0.
    742         foreach ( $this->results as $uindex => $user ) {
    743             $this->results[ $uindex ]->total_friend_count = 0;
    744         }
    745 
    746         // Create, prepare, and run the separate usermeta query.
    747         $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key ) );
    748 
    749         // The $members_template global expects the index key to be different
    750         // from the meta_key in some cases, so we rejig things here.
    751         foreach ( $user_metas as $user_meta ) {
    752             switch ( $user_meta->meta_key ) {
    753                 case $total_friend_count_key:
    754                     $key = 'total_friend_count';
    755                     break;
    756 
    757                 case $bp_latest_update_key:
    758                     $key = 'latest_update';
    759                     break;
    760             }
    761 
    762             if ( isset( $this->results[ $user_meta->user_id ] ) ) {
    763                 $this->results[ $user_meta->user_id ]->{$key} = $user_meta->meta_value;
    764             }
    765         }
    766 
    767         // When meta_key or meta_value have been passed to the query,
    768         // fetch the resulting values for use in the template functions.
    769         if ( ! empty( $this->query_vars['meta_key'] ) ) {
    770             $meta_sql = array(
    771                 'select' => 'SELECT user_id, meta_key, meta_value',
    772                 'from'   => "FROM $wpdb->usermeta",
    773                 'where'  => $wpdb->prepare( 'WHERE meta_key = %s', $this->query_vars['meta_key'] ),
    774             );
    775 
    776             if ( false !== $this->query_vars['meta_value'] ) {
    777                 $meta_sql['where'] .= $wpdb->prepare( ' AND meta_value = %s', $this->query_vars['meta_value'] );
    778             }
    779 
    780             $metas = $wpdb->get_results( "{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}" );
    781 
    782             if ( ! empty( $metas ) ) {
    783                 foreach ( $metas as $meta ) {
    784                     if ( isset( $this->results[ $meta->user_id ] ) ) {
    785                         $this->results[ $meta->user_id ]->meta_key = $meta->meta_key;
    786 
    787                         if ( ! empty( $meta->meta_value ) ) {
    788                             $this->results[ $meta->user_id ]->meta_value = $meta->meta_value;
    789                         }
    790                     }
    791                 }
    792             }
    793         }
    794726    }
    795727
Note: See TracChangeset for help on using the changeset viewer.