Make WordPress Core


Ignore:
Timestamp:
06/08/2016 04:13:04 AM (10 years ago)
Author:
jeremyfelt
Message:

Multisite: Allow access to network and site properties using current naming conventions

  • Add magic __get(), __set(), and __isset() methods to WP_Site and `WP_Network.
  • Provide (int) $network->site_id for (string) $network->blog_id
  • Provide (int) $site->id for (string) $site->blog_id
  • Provide (int) $site->network_id for (string) $site->site_id

Props flixos90, jeremyfelt.
Fixes #36717.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-site.php

    r37468 r37657  
    1515 *
    1616 * @since 4.5.0
     17 *
     18 * @property int $id
     19 * @property int $network_id
    1720 */
    1821final class WP_Site {
     
    2427         *
    2528         * @since 4.5.0
    26          * @access public
    27          * @var string
    28          */
    29         public $blog_id;
     29         * @access private
     30         * @var string
     31         */
     32        private $blog_id;
    3033
    3134        /**
     
    5659         *
    5760         * @since 4.5.0
    58          * @access public
    59          * @var string
    60          */
    61         public $site_id = '0';
     61         * @access private
     62         * @var string
     63         */
     64        private $site_id = '0';
    6265
    6366        /**
     
    211214                return get_object_vars( $this );
    212215        }
     216
     217        /**
     218         * Getter.
     219         *
     220         * Allows current multisite naming conventions when getting properties.
     221         *
     222         * @since 4.6.0
     223         * @access public
     224         *
     225         * @param string $key Property to get.
     226         * @return mixed Value of the property. Null if not available.
     227         */
     228        public function __get( $key ) {
     229                switch ( $key ) {
     230                        case 'id':
     231                                return (int) $this->blog_id;
     232                        case 'blog_id':
     233                                return $this->blog_id;
     234                        case 'site_id':
     235                                return $this->site_id;
     236                        case 'network_id':
     237                                return (int) $this->site_id;
     238                }
     239
     240                return null;
     241        }
     242
     243        /**
     244         * Isset-er.
     245         *
     246         * Allows current multisite naming conventions when checking for properties.
     247         *
     248         * @since 4.6.0
     249         * @access public
     250         *
     251         * @param string $key Property to check if set.
     252         * @return bool Whether the property is set.
     253         */
     254        public function __isset( $key ) {
     255                switch ( $key ) {
     256                        case 'id':
     257                        case 'blog_id':
     258                        case 'site_id':
     259                        case 'network_id':
     260                                return true;
     261                }
     262
     263                return false;
     264        }
     265
     266        /**
     267         * Setter.
     268         *
     269         * Allows current multisite naming conventions while setting properties.
     270         *
     271         * @since 4.6.0
     272         * @access public
     273         *
     274         * @param string $key   Property to set.
     275         * @param mixed  $value Value to assign to the property.
     276         */
     277        public function __set( $key, $value ) {
     278                switch( $key ) {
     279                        case 'id':
     280                        case 'blog_id':
     281                                $this->blog_id = (string) $value;
     282                                break;
     283                        case 'site_id':
     284                        case 'network_id':
     285                                $this->site_id = (string) $value;
     286                                break;
     287                        default:
     288                                $this->$key = $value;
     289                }
     290        }
    213291}
Note: See TracChangeset for help on using the changeset viewer.