Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/02/2015 09:57:22 PM (11 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/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r10163 r10164  
    124124         *
    125125         * @since 1.9.0
    126          *
     126         * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_default_visibility()`.
     127         *
     128         * @access protected
    127129         * @var string Default field data visibility.
    128130         */
    129         public $default_visibility = 'public';
     131        protected $default_visibility;
    130132
    131133        /**
     
    133135         *
    134136         * @since 2.3.0
    135          *
     137+        * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_allow_custom_visibility()`.
     138         *
     139         * @access protected
    136140         * @var string Members are allowed/disallowed to modify data visibility.
    137141         */
    138         public $allow_custom_visibility = 'allowed';
     142        protected $allow_custom_visibility;
    139143
    140144        /**
     
    229233                                $this->data = $this->get_field_data( $user_id );
    230234                        }
    231 
    232                         // Get metadata for field.
    233                         $default_visibility       = bp_xprofile_get_meta( $id, 'field', 'default_visibility'      );
    234                         $allow_custom_visibility  = bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' );
    235 
    236                         // Setup default visibility.
    237                         $this->default_visibility = ! empty( $default_visibility )
    238                                 ? $default_visibility
    239                                 : 'public';
    240 
    241                         // Allow members to customize visibilty.
    242                         $this->allow_custom_visibility = ( 'disabled' === $allow_custom_visibility )
    243                                 ? 'disabled'
    244                                 : 'allowed';
     235                }
     236        }
     237
     238        /**
     239         * Magic getter.
     240         *
     241         * @since 2.4.0
     242         *
     243         * @param string $key Property name.
     244         * @return mixed
     245         */
     246        public function __get( $key ) {
     247                switch ( $key ) {
     248                        case 'default_visibility' :
     249                                return $this->get_default_visibility();
     250                                break;
     251
     252                        case 'allow_custom_visibility' :
     253                                return $this->get_allow_custom_visibility();
     254                                break;
     255                }
     256        }
     257
     258        /**
     259         * Magic issetter.
     260         *
     261         * @since 2.4.0
     262         *
     263         * @param string $key Property name.
     264         * @return bool
     265         */
     266        public function __isset( $key ) {
     267                switch ( $key ) {
     268                        // Backward compatibility for when these were public methods.
     269                        case 'allow_custom_visibility' :
     270                        case 'default_visibility' :
     271                                return true;
     272                                break;
    245273                }
    246274        }
     
    693721        }
    694722
     723        /**
     724         * Get the field's default visibility setting.
     725         *
     726         * Lazy-loaded to reduce overhead.
     727         *
     728         * Defaults to 'public' if no visibility setting is found in the database.
     729         *
     730         * @since 2.4.0
     731         *
     732         * @return string
     733         */
     734        public function get_default_visibility() {
     735                if ( ! isset( $this->default_visibility ) ) {
     736                        $this->default_visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' );
     737
     738                        if ( ! $this->default_visibility ) {
     739                                $this->default_visibility = 'public';
     740                        }
     741                }
     742
     743                return $this->default_visibility;
     744        }
     745
     746        /**
     747         * Get whether the field's default visibility can be overridden by users.
     748         *
     749         * Lazy-loaded to reduce overhead.
     750         *
     751         * Defaults to 'allowed'.
     752         *
     753         * @since 4.4.0
     754         *
     755         * @return string 'disabled' or 'allowed'.
     756         */
     757        public function get_allow_custom_visibility() {
     758                if ( ! isset( $this->allow_custom_visibility ) ) {
     759                        $allow_custom_visibility = bp_xprofile_get_meta( $this->id, 'field', 'allow_custom_visibility' );
     760
     761                        if ( 'disabled' === $allow_custom_visibility ) {
     762                                $this->allow_custom_visibility = 'disabled';
     763                        } else {
     764                                $this->allow_custom_visibility = 'allowed';
     765                        }
     766                }
     767
     768                return $this->allow_custom_visibility;
     769        }
     770
    695771        /** Static Methods ********************************************************/
    696772
     
    12541330                                                <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
    12551331
    1256                                                         <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->default_visibility, $level['id'] ); ?>>
     1332                                                        <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->get_default_visibility(), $level['id'] ); ?>>
    12571333                                                                <?php echo esc_html( $level['label'] ); ?>
    12581334                                                        </option>
     
    12661342                                        <ul>
    12671343                                                <li>
    1268                                                         <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->allow_custom_visibility, 'allowed' ); ?> />
     1344                                                        <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->get_allow_custom_visibility(), 'allowed' ); ?> />
    12691345                                                        <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label>
    12701346                                                </li>
    12711347                                                <li>
    1272                                                         <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->allow_custom_visibility, 'disabled' ); ?> />
     1348                                                        <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->get_allow_custom_visibility(), 'disabled' ); ?> />
    12731349                                                        <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label>
    12741350                                                </li>
Note: See TracChangeset for help on using the changeset viewer.