Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/02/2015 09:57:22 PM (9 years ago)
Author:
boonebgorges
Message:

Lazy-load visibility properties in BP_XProfile_Field.

Previously, populating a new BP_XProfile_Field object would result in two
queries: one to fetch the field row from the database, and one to populate the
field meta cache in order to set the 'allow_custom_visibility' and
'default_visibility' properties. It's not often the case that we need the
latter properties, which means that the cache-priming query is wasted.

This changeset addresses the shortcoming by loading the properties only when
they're requested:

  • $allow_custom_visibility and $default_visibility are now protected properties of the object. This directs requests for them to the magic methods.
  • Introduce __isset() and __get() to handle backward compatibility.
  • Introduce public getter functions for each property.

See #6638.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field.php

    r10004 r10164  
    55 */
    66class BP_Tests_BP_XProfile_Field_TestCases extends BP_UnitTestCase {
    7    
     7
    88    /**
    99     * @group xprofile_field_save
     
    121121        $this->assertSame( $new_field_id, $field->id );
    122122    }
     123
     124    /**
     125     * @ticket BP6638
     126     */
     127    public function test_default_visibility_should_be_lazy_loaded() {
     128        global $wpdb;
     129
     130        $group = $this->factory->xprofile_group->create();
     131        $field = $this->factory->xprofile_field->create( array(
     132            'field_group_id' => $group,
     133        ) );
     134
     135        bp_xprofile_update_meta( $field, 'field', 'default_visibility', 'loggedin' );
     136
     137        // Initial setup takes just one query.
     138        $num_queries = $wpdb->num_queries;
     139        $field_obj = new BP_XProfile_Field( $field );
     140        $num_queries++;
     141
     142        $this->assertSame( $num_queries, $wpdb->num_queries );
     143
     144        // Fetching the default_visibility should cause another query.
     145        $this->assertSame( 'loggedin', $field_obj->default_visibility );
     146        $num_queries++;
     147
     148        $this->assertSame( $num_queries, $wpdb->num_queries );
     149    }
     150
     151    /**
     152     * @ticket BP6638
     153     */
     154    public function test_allow_custom_visibility_should_be_lazy_loaded() {
     155        global $wpdb;
     156
     157        $group = $this->factory->xprofile_group->create();
     158        $field = $this->factory->xprofile_field->create( array(
     159            'field_group_id' => $group,
     160        ) );
     161
     162        bp_xprofile_update_meta( $field, 'field', 'allow_custom_visibility', 'disabled' );
     163
     164        // Initial setup takes just one query.
     165        $num_queries = $wpdb->num_queries;
     166        $field_obj = new BP_XProfile_Field( $field );
     167        $num_queries++;
     168
     169        $this->assertSame( $num_queries, $wpdb->num_queries );
     170
     171        // Fetching the allow_custom_visibility should cause another query.
     172        $this->assertSame( 'disabled', $field_obj->allow_custom_visibility );
     173        $num_queries++;
     174
     175        $this->assertSame( $num_queries, $wpdb->num_queries );
     176    }
    123177}
Note: See TracChangeset for help on using the changeset viewer.