Skip to:
Content

BuddyPress.org

Ticket #7404: 7404.diff

File 7404.diff, 3.7 KB (added by boonebgorges, 7 years ago)
  • src/bp-xprofile/bp-xprofile-cache.php

    diff --git src/bp-xprofile/bp-xprofile-cache.php src/bp-xprofile/bp-xprofile-cache.php
    index 12f9acf..ab5153b 100644
    function bp_xprofile_clear_field_cache( $field ) { 
    306306}
    307307add_action( 'xprofile_field_after_save', 'bp_xprofile_clear_field_cache' );
    308308
     309/**
     310 * Clear the field-name cache.
     311 *
     312 * @since 2.8.0
     313 */
     314function bp_xprofile_reset_fields_by_name_cache_incrementor() {
     315        bp_core_reset_incrementor( 'bp_xprofile_fields_by_name' );
     316}
     317add_action( 'xprofile_field_before_save', 'bp_xprofile_reset_fields_by_name_cache_incrementor' );
     318
    309319// List actions to clear super cached pages on, if super cache is installed.
    310320add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
  • src/bp-xprofile/bp-xprofile-functions.php

    diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php
    index fb1bf33..c5c1427 100644
    function xprofile_check_is_required_field( $field_id ) { 
    594594 * @return int $field_id on success, false on failure.
    595595 */
    596596function xprofile_get_field_id_from_name( $field_name ) {
    597         return BP_XProfile_Field::get_id_from_name( $field_name );
     597        $cached = bp_core_get_incremented_cache( $field_name, 'bp_xprofile_fields_by_name' );
     598        if ( false === $cached ) {
     599                $cached = BP_XProfile_Field::get_id_from_name( $field_name );
     600                bp_core_set_incremented_cache( $field_name, 'bp_xprofile_fields_by_name', $cached );
     601        }
     602        return $cached;
    598603}
    599604
    600605/**
  • tests/phpunit/testcases/xprofile/cache.php

    diff --git tests/phpunit/testcases/xprofile/cache.php tests/phpunit/testcases/xprofile/cache.php
    index bc38937..ba6bea2 100644
    class BP_Tests_XProfile_Cache extends BP_UnitTestCase { 
    164164                $field_2 = xprofile_get_field( $f );
    165165                $this->assertSame( 'Bar', $field_2->name );
    166166        }
     167
     168        /**
     169         * @ticket BP7407
     170         */
     171        public function test_get_field_id_from_name_should_be_cached() {
     172                global $wpdb;
     173
     174                $g = $this->factory->xprofile_group->create();
     175                $f = $this->factory->xprofile_field->create( array(
     176                        'field_group_id' => $g,
     177                        'name' => 'Foo',
     178                ) );
     179
     180                // Prime cache.
     181                xprofile_get_field_id_from_name( 'Foo' );
     182
     183                $num_queries = $wpdb->num_queries;
     184
     185                $this->assertSame( $f, xprofile_get_field_id_from_name( 'Foo' ) );
     186                $this->assertSame( $num_queries, $wpdb->num_queries );
     187        }
     188
     189        /**
     190         * @ticket BP7407
     191         */
     192        public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_update() {
     193                $g = $this->factory->xprofile_group->create();
     194                $f1 = $this->factory->xprofile_field->create( array(
     195                        'field_group_id' => $g,
     196                        'name' => 'Foo',
     197                ) );
     198                $f2 = $this->factory->xprofile_field->create( array(
     199                        'field_group_id' => $g,
     200                        'name' => 'Bar',
     201                ) );
     202
     203                // Prime cache.
     204                xprofile_get_field_id_from_name( 'Foo' );
     205                xprofile_get_field_id_from_name( 'Bar' );
     206
     207                // Free up the name 'Bar'.
     208                $field2 = xprofile_get_field( $f2 );
     209                $field2->name = 'Quz';
     210                $field2->save();
     211
     212                // Take the name 'Bar'.
     213                $field1 = xprofile_get_field( $f1 );
     214                $field1->name = 'Bar';
     215                $field1->save();
     216
     217                $this->assertSame( $f1, xprofile_get_field_id_from_name( 'Bar' ) );
     218        }
     219
     220        /**
     221         * @ticket BP7407
     222         */
     223        public function test_get_field_id_from_name_cache_should_be_invalidated_on_field_deletion() {
     224                $g = $this->factory->xprofile_group->create();
     225                $f = $this->factory->xprofile_field->create( array(
     226                        'field_group_id' => $g,
     227                        'name' => 'Foo',
     228                ) );
     229
     230                // Prime cache.
     231                xprofile_get_field_id_from_name( 'Foo' );
     232
     233                xprofile_delete_field( $f );
     234
     235                $this->assertNull( xprofile_get_field_id_from_name( 'Bar' ) );
     236        }
    167237}