[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> class-wp-site.php (source)

   1  <?php
   2  /**
   3   * Site API: WP_Site class
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 4.5.0
   8   */
   9  
  10  /**
  11   * Core class used for interacting with a multisite site.
  12   *
  13   * This class is used during load to populate the `$current_blog` global and
  14   * setup the current site.
  15   *
  16   * @since 4.5.0
  17   *
  18   * @property int    $id
  19   * @property int    $network_id
  20   * @property string $blogname
  21   * @property string $siteurl
  22   * @property int    $post_count
  23   * @property string $home
  24   */
  25  final class WP_Site {
  26  
  27      /**
  28       * Site ID.
  29       *
  30       * A numeric string, for compatibility reasons.
  31       *
  32       * @since 4.5.0
  33       * @var string
  34       */
  35      public $blog_id;
  36  
  37      /**
  38       * Domain of the site.
  39       *
  40       * @since 4.5.0
  41       * @var string
  42       */
  43      public $domain = '';
  44  
  45      /**
  46       * Path of the site.
  47       *
  48       * @since 4.5.0
  49       * @var string
  50       */
  51      public $path = '';
  52  
  53      /**
  54       * The ID of the site's parent network.
  55       *
  56       * Named "site" vs. "network" for legacy reasons. An individual site's "site" is
  57       * its network.
  58       *
  59       * A numeric string, for compatibility reasons.
  60       *
  61       * @since 4.5.0
  62       * @var string
  63       */
  64      public $site_id = '0';
  65  
  66      /**
  67       * The date on which the site was created or registered.
  68       *
  69       * @since 4.5.0
  70       * @var string Date in MySQL's datetime format.
  71       */
  72      public $registered = '0000-00-00 00:00:00';
  73  
  74      /**
  75       * The date and time on which site settings were last updated.
  76       *
  77       * @since 4.5.0
  78       * @var string Date in MySQL's datetime format.
  79       */
  80      public $last_updated = '0000-00-00 00:00:00';
  81  
  82      /**
  83       * Whether the site should be treated as public.
  84       *
  85       * A numeric string, for compatibility reasons.
  86       *
  87       * @since 4.5.0
  88       * @var string
  89       */
  90      public $public = '1';
  91  
  92      /**
  93       * Whether the site should be treated as archived.
  94       *
  95       * A numeric string, for compatibility reasons.
  96       *
  97       * @since 4.5.0
  98       * @var string
  99       */
 100      public $archived = '0';
 101  
 102      /**
 103       * Whether the site should be treated as mature.
 104       *
 105       * Handling for this does not exist throughout WordPress core, but custom
 106       * implementations exist that require the property to be present.
 107       *
 108       * A numeric string, for compatibility reasons.
 109       *
 110       * @since 4.5.0
 111       * @var string
 112       */
 113      public $mature = '0';
 114  
 115      /**
 116       * Whether the site should be treated as spam.
 117       *
 118       * A numeric string, for compatibility reasons.
 119       *
 120       * @since 4.5.0
 121       * @var string
 122       */
 123      public $spam = '0';
 124  
 125      /**
 126       * Whether the site should be treated as deleted.
 127       *
 128       * A numeric string, for compatibility reasons.
 129       *
 130       * @since 4.5.0
 131       * @var string
 132       */
 133      public $deleted = '0';
 134  
 135      /**
 136       * The language pack associated with this site.
 137       *
 138       * A numeric string, for compatibility reasons.
 139       *
 140       * @since 4.5.0
 141       * @var string
 142       */
 143      public $lang_id = '0';
 144  
 145      /**
 146       * Retrieves a site from the database by its ID.
 147       *
 148       * @since 4.5.0
 149       *
 150       * @global wpdb $wpdb WordPress database abstraction object.
 151       *
 152       * @param int $site_id The ID of the site to retrieve.
 153       * @return WP_Site|false The site's object if found. False if not.
 154       */
 155  	public static function get_instance( $site_id ) {
 156          global $wpdb;
 157  
 158          $site_id = (int) $site_id;
 159          if ( ! $site_id ) {
 160              return false;
 161          }
 162  
 163          $_site = wp_cache_get( $site_id, 'sites' );
 164  
 165          if ( false === $_site ) {
 166              $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );
 167  
 168              if ( empty( $_site ) || is_wp_error( $_site ) ) {
 169                  $_site = -1;
 170              }
 171  
 172              wp_cache_add( $site_id, $_site, 'sites' );
 173          }
 174  
 175          if ( is_numeric( $_site ) ) {
 176              return false;
 177          }
 178  
 179          return new WP_Site( $_site );
 180      }
 181  
 182      /**
 183       * Creates a new WP_Site object.
 184       *
 185       * Will populate object properties from the object provided and assign other
 186       * default properties based on that information.
 187       *
 188       * @since 4.5.0
 189       *
 190       * @param WP_Site|object $site A site object.
 191       */
 192  	public function __construct( $site ) {
 193          foreach ( get_object_vars( $site ) as $key => $value ) {
 194              $this->$key = $value;
 195          }
 196      }
 197  
 198      /**
 199       * Converts an object to array.
 200       *
 201       * @since 4.6.0
 202       *
 203       * @return array Object as array.
 204       */
 205  	public function to_array() {
 206          return get_object_vars( $this );
 207      }
 208  
 209      /**
 210       * Getter.
 211       *
 212       * Allows current multisite naming conventions when getting properties.
 213       * Allows access to extended site properties.
 214       *
 215       * @since 4.6.0
 216       *
 217       * @param string $key Property to get.
 218       * @return mixed Value of the property. Null if not available.
 219       */
 220  	public function __get( $key ) {
 221          switch ( $key ) {
 222              case 'id':
 223                  return (int) $this->blog_id;
 224              case 'network_id':
 225                  return (int) $this->site_id;
 226              case 'blogname':
 227              case 'siteurl':
 228              case 'post_count':
 229              case 'home':
 230              default: // Custom properties added by 'site_details' filter.
 231                  if ( ! did_action( 'ms_loaded' ) ) {
 232                      return null;
 233                  }
 234  
 235                  $details = $this->get_details();
 236                  if ( isset( $details->$key ) ) {
 237                      return $details->$key;
 238                  }
 239          }
 240  
 241          return null;
 242      }
 243  
 244      /**
 245       * Isset-er.
 246       *
 247       * Allows current multisite naming conventions when checking for properties.
 248       * Checks for extended site properties.
 249       *
 250       * @since 4.6.0
 251       *
 252       * @param string $key Property to check if set.
 253       * @return bool Whether the property is set.
 254       */
 255  	public function __isset( $key ) {
 256          switch ( $key ) {
 257              case 'id':
 258              case 'network_id':
 259                  return true;
 260              case 'blogname':
 261              case 'siteurl':
 262              case 'post_count':
 263              case 'home':
 264                  if ( ! did_action( 'ms_loaded' ) ) {
 265                      return false;
 266                  }
 267                  return true;
 268              default: // Custom properties added by 'site_details' filter.
 269                  if ( ! did_action( 'ms_loaded' ) ) {
 270                      return false;
 271                  }
 272  
 273                  $details = $this->get_details();
 274                  if ( isset( $details->$key ) ) {
 275                      return true;
 276                  }
 277          }
 278  
 279          return false;
 280      }
 281  
 282      /**
 283       * Setter.
 284       *
 285       * Allows current multisite naming conventions while setting properties.
 286       *
 287       * @since 4.6.0
 288       *
 289       * @param string $key   Property to set.
 290       * @param mixed  $value Value to assign to the property.
 291       */
 292  	public function __set( $key, $value ) {
 293          switch ( $key ) {
 294              case 'id':
 295                  $this->blog_id = (string) $value;
 296                  break;
 297              case 'network_id':
 298                  $this->site_id = (string) $value;
 299                  break;
 300              default:
 301                  $this->$key = $value;
 302          }
 303      }
 304  
 305      /**
 306       * Retrieves the details for this site.
 307       *
 308       * This method is used internally to lazy-load the extended properties of a site.
 309       *
 310       * @since 4.6.0
 311       *
 312       * @see WP_Site::__get()
 313       *
 314       * @return stdClass A raw site object with all details included.
 315       */
 316  	private function get_details() {
 317          $details = wp_cache_get( $this->blog_id, 'site-details' );
 318  
 319          if ( false === $details ) {
 320  
 321              switch_to_blog( $this->blog_id );
 322              // Create a raw copy of the object for backward compatibility with the filter below.
 323              $details = new stdClass();
 324              foreach ( get_object_vars( $this ) as $key => $value ) {
 325                  $details->$key = $value;
 326              }
 327              $details->blogname   = get_option( 'blogname' );
 328              $details->siteurl    = get_option( 'siteurl' );
 329              $details->post_count = get_option( 'post_count' );
 330              $details->home       = get_option( 'home' );
 331              restore_current_blog();
 332  
 333              wp_cache_set( $this->blog_id, $details, 'site-details' );
 334          }
 335  
 336          /** This filter is documented in wp-includes/ms-blogs.php */
 337          $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
 338  
 339          /**
 340           * Filters a site's extended properties.
 341           *
 342           * @since 4.6.0
 343           *
 344           * @param stdClass $details The site details.
 345           */
 346          $details = apply_filters( 'site_details', $details );
 347  
 348          return $details;
 349      }
 350  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7