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.

Built from https://develop.svn.wordpress.org/trunk@37657


git-svn-id: http://core.svn.wordpress.org/trunk@37623 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Jeremy Felt 2016-06-08 04:14:29 +00:00
parent 4f264b6d07
commit b885fe6761
3 changed files with 152 additions and 7 deletions

View File

@ -17,6 +17,8 @@
* ability to interact with any network of sites is required.
*
* @since 4.4.0
*
* @property int $site_id
*/
class WP_Network {
@ -58,10 +60,10 @@ class WP_Network {
* A numeric string, for compatibility reasons.
*
* @since 4.4.0
* @access public
* @access private
* @var string
*/
public $blog_id = 0;
private $blog_id = 0;
/**
* Domain used to set cookies for this network.
@ -137,6 +139,71 @@ class WP_Network {
$this->_set_cookie_domain();
}
/**
* Getter.
*
* Allows current multisite naming conventions when getting properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to get.
* @return mixed Value of the property. Null if not available.
*/
public function __get( $key ) {
switch ( $key ) {
case 'blog_id':
return $this->blog_id;
case 'site_id':
return (int) $this->blog_id;
}
return null;
}
/**
* Isset-er.
*
* Allows current multisite naming conventions when checking for properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to check if set.
* @return bool Whether the property is set.
*/
public function __isset( $key ) {
switch( $key ) {
case 'blog_id':
case 'site_id':
return true;
}
return false;
}
/**
* Setter.
*
* Allows current multisite naming conventions while setting properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to set.
* @param mixed $value Value to assign to the property.
*/
public function __set( $key, $value ) {
switch( $key ) {
case 'blog_id':
case 'site_id':
$this->blog_id = (string) $value;
break;
default:
$this->$key = $value;
}
}
/**
* Set the site name assigned to the network if one has not been populated.
*

View File

@ -14,6 +14,9 @@
* setup the current site.
*
* @since 4.5.0
*
* @property int $id
* @property int $network_id
*/
final class WP_Site {
@ -23,10 +26,10 @@ final class WP_Site {
* A numeric string, for compatibility reasons.
*
* @since 4.5.0
* @access public
* @access private
* @var string
*/
public $blog_id;
private $blog_id;
/**
* Domain of the site.
@ -55,10 +58,10 @@ final class WP_Site {
* A numeric string, for compatibility reasons.
*
* @since 4.5.0
* @access public
* @access private
* @var string
*/
public $site_id = '0';
private $site_id = '0';
/**
* The date on which the site was created or registered.
@ -210,4 +213,79 @@ final class WP_Site {
public function to_array() {
return get_object_vars( $this );
}
/**
* Getter.
*
* Allows current multisite naming conventions when getting properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to get.
* @return mixed Value of the property. Null if not available.
*/
public function __get( $key ) {
switch ( $key ) {
case 'id':
return (int) $this->blog_id;
case 'blog_id':
return $this->blog_id;
case 'site_id':
return $this->site_id;
case 'network_id':
return (int) $this->site_id;
}
return null;
}
/**
* Isset-er.
*
* Allows current multisite naming conventions when checking for properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to check if set.
* @return bool Whether the property is set.
*/
public function __isset( $key ) {
switch ( $key ) {
case 'id':
case 'blog_id':
case 'site_id':
case 'network_id':
return true;
}
return false;
}
/**
* Setter.
*
* Allows current multisite naming conventions while setting properties.
*
* @since 4.6.0
* @access public
*
* @param string $key Property to set.
* @param mixed $value Value to assign to the property.
*/
public function __set( $key, $value ) {
switch( $key ) {
case 'id':
case 'blog_id':
$this->blog_id = (string) $value;
break;
case 'site_id':
case 'network_id':
$this->site_id = (string) $value;
break;
default:
$this->$key = $value;
}
}
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37656';
$wp_version = '4.6-alpha-37657';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.