Skip to:
Content

BuddyPress.org

Changeset 12754


Ignore:
Timestamp:
10/15/2020 06:33:17 PM (4 years ago)
Author:
imath
Message:

Members: stop mirroring user last_activity data to user metadata

The primary storage location for user last_activity is the activity table. For backward compatibility reasons, we used to mirror that data into user metadata.

Backward compatibility user last_activity metadata mirroring is now only done if the legacy user query is enabled. This can be achieved by forcing the bp_use_legacy_user_query filter to return true.

Props boonebgorges, r-a-y

Fixes #7882

Location:
trunk
Files:
4 edited

Legend:

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

    r12588 r12754  
    761761        // Fetch the user's last_activity.
    762762        if ( 'active' != $type ) {
    763             $user_activity = $wpdb->get_results( $wpdb->prepare( "SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key( 'last_activity' ) ) );
     763            $user_activity = self::get_last_activity( $user_ids );
    764764            for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) {
    765765                foreach ( (array) $user_activity as $activity ) {
    766                     if ( $activity->id == $paged_users[$i]->id )
    767                         $paged_users[$i]->last_activity = $activity->last_activity;
     766                    if ( ! empty( $activity['user_id'] ) && (int) $activity['user_id'] === (int) $paged_users[$i]->id ) {
     767                        $paged_users[$i]->last_activity = $activity['date_recorded'];
     768                    }
    768769                }
    769770            }
  • trunk/src/bp-members/bp-members-functions.php

    r12729 r12754  
    127127    ), 'core_get_users' );
    128128
    129     // For legacy users. Use of BP_Core_User::get_users() is deprecated.
    130     if ( apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, $r ) ) {
     129    /**
     130     * For legacy users. Use of BP_Core_User::get_users() is deprecated.
     131     *
     132     * Forcing this filter to true will use the legacy user query. As of
     133     * BuddyPress 7.0.0, mirroring of the 'last_activity' value to usermeta
     134     * is also disabled if true. See bp_update_user_last_activity().
     135     *
     136     * @since 2.0.0
     137     *
     138     * @param bool   $retval   Defaults to false.
     139     * @param string $function Current function name.
     140     * @param array  $r        User query arguments.
     141     */
     142    $use_legacy_query = apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, $r );
     143
     144    if ( $use_legacy_query ) {
    131145        $retval = BP_Core_User::get_users(
    132146            $r['type'],
     
    10491063 *
    10501064 * @since 1.9.0
     1065 * @since 7.0.0 Backward compatibility usermeta mirroring is only allowed if the
     1066 *              legacy user query is enabled.
    10511067 *
    10521068 * @param int    $user_id ID of the user being updated.
     
    10711087    }
    10721088
    1073     // As of BuddyPress 2.0, last_activity is no longer stored in usermeta.
    1074     // However, we mirror it there for backward compatibility. Do not use!
    1075     // Remove our warning and re-add.
    1076     remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10 );
    1077     remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10 );
    1078     bp_update_user_meta( $user_id, 'last_activity', $time );
    1079     add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
    1080     add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 4 );
     1089    /** This filter is documented in bp_core_get_users() */
     1090    $use_legacy_query = apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, [ 'user_id' => $user_id ] );
     1091
     1092    /*
     1093     * As of BuddyPress 2.0, last_activity is no longer stored in usermeta.
     1094     * However, we mirror it there for backward compatibility. Do not use!
     1095     *
     1096     * As of BuddyPress 7.0, mirroring is only allowed if the legacy user
     1097     * query is enabled.
     1098     */
     1099    if ( $use_legacy_query ) {
     1100        remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10 );
     1101        remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10 );
     1102        bp_update_user_meta( $user_id, 'last_activity', $time );
     1103        add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 );
     1104        add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 4 );
     1105    }
    10811106
    10821107    return BP_Core_User::update_last_activity( $user_id, $time );
  • trunk/tests/phpunit/testcases/core/class-bp-core-user.php

    r11737 r12754  
    1010     */
    1111    public function test_get_users_with_exclude_querystring() {
     12        add_filter( 'bp_use_legacy_user_query', '__return_true' );
     13
    1214        $u1 = self::factory()->user->create();
    1315        $u2 = self::factory()->user->create();
     
    1921        $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
    2022
     23        remove_filter( 'bp_use_legacy_user_query', '__return_true' );
     24
    2125        $this->assertEquals( array( $u2 ), $user_ids );
    2226    }
     
    2630     */
    2731    public function test_get_users_with_exclude_array() {
     32        add_filter( 'bp_use_legacy_user_query', '__return_true' );
     33
    2834        $u1 = self::factory()->user->create();
    2935        $u2 = self::factory()->user->create();
     
    3945        $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
    4046
     47        remove_filter( 'bp_use_legacy_user_query', '__return_true' );
     48
    4149        $this->assertEquals( array( $u2 ), $user_ids );
    4250    }
     
    4654     */
    4755    public function test_get_users_with_include_querystring() {
     56        add_filter( 'bp_use_legacy_user_query', '__return_true' );
     57
    4858        $u1 = self::factory()->user->create( array(
    4959            'last_activity' => gmdate( 'Y-m-d H:i:s' ),
     
    6171        $user_ids = wp_parse_id_list( wp_list_pluck( $users['users'], 'id' ) );
    6272
     73        remove_filter( 'bp_use_legacy_user_query', '__return_true' );
     74
    6375        $this->assertEquals( array( $u1, $u3 ), $user_ids );
    6476    }
     
    6880     */
    6981    public function test_get_users_with_include_array() {
     82        add_filter( 'bp_use_legacy_user_query', '__return_true' );
     83
    7084        $u1 = self::factory()->user->create( array(
    7185            'last_activity' => gmdate( 'Y-m-d H:i:s' ),
     
    91105        $user_ids = array_map( 'intval', $user_ids );
    92106
     107        remove_filter( 'bp_use_legacy_user_query', '__return_true' );
     108
    93109        $this->assertEquals( array( $u1, $u3 ), $user_ids );
    94110    }
  • trunk/tests/phpunit/testcases/members/functions.php

    r12679 r12754  
    387387        // Setup parameters to assert to be the same
    388388        $expected = $time;
    389         $found    = bp_get_user_meta( $user, 'last_activity', true );
     389        $found    = bp_get_user_last_activity( $user );
    390390
    391391        $this->assertSame( $expected, $found );
Note: See TracChangeset for help on using the changeset viewer.