[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Fri Jul 24 08:20:19 2026 Cross-referenced by PHPXref