Skip to:
Content

BuddyPress.org

Ticket #8388: 8388.02-w-test.diff

File 8388.02-w-test.diff, 2.9 KB (added by dcavins, 4 years ago)

This patch goes a step further and avoids using the bulk delete statement, instead using xprofile_delete_field_data() so that the before and after actions are called.

  • src/bp-xprofile/bp-xprofile-functions.php

    diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php
    index 060b2ff61..216e7165b 100644
    add_action( 'xprofile_data_after_save', 'xprofile_sync_wp_profile_on_single_fiel 
    844844 * @param int $user_id The ID of the deleted user.
    845845 */
    846846function xprofile_remove_data( $user_id ) {
    847         BP_XProfile_ProfileData::delete_data_for_user( $user_id );
     847        global $wpdb;
     848        $bp = buddypress();
     849
     850        $field_ids = $wpdb->get_col( $wpdb->prepare( "SELECT field_id FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) );
     851
     852        foreach ( $field_ids as $field_id ) {
     853                xprofile_delete_field_data( $field_id, $user_id );
     854        }
    848855}
    849856add_action( 'wpmu_delete_user',  'xprofile_remove_data' );
    850857add_action( 'bp_make_spam_user', 'xprofile_remove_data' );
  • src/bp-xprofile/classes/class-bp-xprofile-profiledata.php

    diff --git src/bp-xprofile/classes/class-bp-xprofile-profiledata.php src/bp-xprofile/classes/class-bp-xprofile-profiledata.php
    index 9db8bfc3a..43c9f6dec 100644
    class BP_XProfile_ProfileData { 
    681681
    682682                $bp = buddypress();
    683683
    684                 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) );
     684                $field_ids = $wpdb->get_col( $wpdb->prepare( "SELECT field_id FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) );
     685
     686                $removed = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) );
     687
     688                if ( ! $removed ) {
     689                        return false;
     690                }
     691
     692                foreach ( $field_ids as $field_id ) {
     693                        wp_cache_delete( "{$user_id}:{$field_id}", 'bp_xprofile_data' );
     694                }
     695
     696                return $removed;
    685697        }
    686698
    687699        /**
  • tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php

    diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php
    index 5f1de867d..fb0be4e02 100644
    class BP_Tests_BP_XProfile_ProfileData_TestCases extends BP_UnitTestCase { 
    419419                $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) );
    420420        }
    421421
     422        /**
     423         * @ticket BP8388
     424         * @group xprofile_remove_data
     425         */
     426        public function test_cache_invalidated_when_xprofile_remove_data_called() {
     427                $u = self::factory()->user->create();
     428                $g = self::factory()->xprofile_group->create();
     429                $f = self::factory()->xprofile_field->create( array(
     430                        'field_group_id' => $g,
     431                ) );
     432
     433                xprofile_set_field_data( $f, $u, 'Foo' );
     434                // Prime the cache.
     435                xprofile_get_field_data( $f, $u );
     436                xprofile_remove_data( $u );
     437
     438                $d = new BP_XProfile_ProfileData( $f, $u );
     439                $this->assertFalse( $d->exists() );
     440        }
     441
    422442        public function filter_time() {
    423443                return $this->last_updated;
    424444        }