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. git-svn-id: https://develop.svn.wordpress.org/trunk@37657 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
1eaf38088f
commit
c64d63e51a
|
@ -17,6 +17,8 @@
|
||||||
* ability to interact with any network of sites is required.
|
* ability to interact with any network of sites is required.
|
||||||
*
|
*
|
||||||
* @since 4.4.0
|
* @since 4.4.0
|
||||||
|
*
|
||||||
|
* @property int $site_id
|
||||||
*/
|
*/
|
||||||
class WP_Network {
|
class WP_Network {
|
||||||
|
|
||||||
|
@ -58,10 +60,10 @@ class WP_Network {
|
||||||
* A numeric string, for compatibility reasons.
|
* A numeric string, for compatibility reasons.
|
||||||
*
|
*
|
||||||
* @since 4.4.0
|
* @since 4.4.0
|
||||||
* @access public
|
* @access private
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $blog_id = 0;
|
private $blog_id = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Domain used to set cookies for this network.
|
* Domain used to set cookies for this network.
|
||||||
|
@ -137,6 +139,71 @@ class WP_Network {
|
||||||
$this->_set_cookie_domain();
|
$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.
|
* Set the site name assigned to the network if one has not been populated.
|
||||||
*
|
*
|
||||||
|
|
|
@ -14,6 +14,9 @@
|
||||||
* setup the current site.
|
* setup the current site.
|
||||||
*
|
*
|
||||||
* @since 4.5.0
|
* @since 4.5.0
|
||||||
|
*
|
||||||
|
* @property int $id
|
||||||
|
* @property int $network_id
|
||||||
*/
|
*/
|
||||||
final class WP_Site {
|
final class WP_Site {
|
||||||
|
|
||||||
|
@ -23,10 +26,10 @@ final class WP_Site {
|
||||||
* A numeric string, for compatibility reasons.
|
* A numeric string, for compatibility reasons.
|
||||||
*
|
*
|
||||||
* @since 4.5.0
|
* @since 4.5.0
|
||||||
* @access public
|
* @access private
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $blog_id;
|
private $blog_id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Domain of the site.
|
* Domain of the site.
|
||||||
|
@ -55,10 +58,10 @@ final class WP_Site {
|
||||||
* A numeric string, for compatibility reasons.
|
* A numeric string, for compatibility reasons.
|
||||||
*
|
*
|
||||||
* @since 4.5.0
|
* @since 4.5.0
|
||||||
* @access public
|
* @access private
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $site_id = '0';
|
private $site_id = '0';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The date on which the site was created or registered.
|
* The date on which the site was created or registered.
|
||||||
|
@ -210,4 +213,79 @@ final class WP_Site {
|
||||||
public function to_array() {
|
public function to_array() {
|
||||||
return get_object_vars( $this );
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue