[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Network API: WP_Network class
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used for interacting with a multisite network.
  12   *
  13   * This class is used during load to populate the `$current_site` global and
  14   * setup the current network.
  15   *
  16   * This class is most useful in WordPress multi-network installations where the
  17   * ability to interact with any network of sites is required.
  18   *
  19   * @since 4.4.0
  20   *
  21   * @property int $id
  22   * @property int $site_id
  23   */
  24  #[AllowDynamicProperties]
  25  class WP_Network {
  26  
  27      /**
  28       * Network ID.
  29       *
  30       * @since 4.4.0
  31       * @since 4.6.0 Converted from public to private to explicitly enable more intuitive
  32       *              access via magic methods. As part of the access change, the type was
  33       *              also changed from `string` to `int`.
  34       * @var int
  35       */
  36      private $id;
  37  
  38      /**
  39       * Domain of the network.
  40       *
  41       * @since 4.4.0
  42       * @var string
  43       */
  44      public $domain = '';
  45  
  46      /**
  47       * Path of the network.
  48       *
  49       * @since 4.4.0
  50       * @var string
  51       */
  52      public $path = '';
  53  
  54      /**
  55       * The ID of the network's main site.
  56       *
  57       * Named "blog" vs. "site" for legacy reasons. A main site is mapped to
  58       * the network when the network is created.
  59       *
  60       * A numeric string, for compatibility reasons.
  61       *
  62       * @since 4.4.0
  63       * @var string
  64       */
  65      private $blog_id = '0';
  66  
  67      /**
  68       * Domain used to set cookies for this network.
  69       *
  70       * @since 4.4.0
  71       * @var string
  72       */
  73      public $cookie_domain = '';
  74  
  75      /**
  76       * Name of this network.
  77       *
  78       * Named "site" vs. "network" for legacy reasons.
  79       *
  80       * @since 4.4.0
  81       * @var string
  82       */
  83      public $site_name = '';
  84  
  85      /**
  86       * Retrieves a network from the database by its ID.
  87       *
  88       * @since 4.4.0
  89       *
  90       * @global wpdb $wpdb WordPress database abstraction object.
  91       *
  92       * @param int $network_id The ID of the network to retrieve.
  93       * @return WP_Network|false The network's object if found. False if not.
  94       */
  95  	public static function get_instance( $network_id ) {
  96          global $wpdb;
  97  
  98          $network_id = (int) $network_id;
  99          if ( ! $network_id ) {
 100              return false;
 101          }
 102  
 103          $_network = wp_cache_get( $network_id, 'networks' );
 104  
 105          if ( false === $_network ) {
 106              $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
 107  
 108              if ( empty( $_network ) || is_wp_error( $_network ) ) {
 109                  $_network = -1;
 110              }
 111  
 112              wp_cache_add( $network_id, $_network, 'networks' );
 113          }
 114  
 115          if ( is_numeric( $_network ) ) {
 116              return false;
 117          }
 118  
 119          return new WP_Network( $_network );
 120      }
 121  
 122      /**
 123       * Creates a new WP_Network object.
 124       *
 125       * Will populate object properties from the object provided and assign other
 126       * default properties based on that information.
 127       *
 128       * @since 4.4.0
 129       *
 130       * @param WP_Network|object $network A network object.
 131       */
 132  	public function __construct( $network ) {
 133          foreach ( get_object_vars( $network ) as $key => $value ) {
 134              $this->$key = $value;
 135          }
 136  
 137          $this->_set_site_name();
 138          $this->_set_cookie_domain();
 139      }
 140  
 141      /**
 142       * Getter.
 143       *
 144       * Allows current multisite naming conventions when getting properties.
 145       *
 146       * @since 4.6.0
 147       *
 148       * @param string $key Property to get.
 149       * @return mixed Value of the property. Null if not available.
 150       */
 151  	public function __get( $key ) {
 152          switch ( $key ) {
 153              case 'id':
 154                  return (int) $this->id;
 155              case 'blog_id':
 156                  return (string) $this->get_main_site_id();
 157              case 'site_id':
 158                  return $this->get_main_site_id();
 159          }
 160  
 161          return null;
 162      }
 163  
 164      /**
 165       * Isset-er.
 166       *
 167       * Allows current multisite naming conventions when checking for properties.
 168       *
 169       * @since 4.6.0
 170       *
 171       * @param string $key Property to check if set.
 172       * @return bool Whether the property is set.
 173       */
 174  	public function __isset( $key ) {
 175          switch ( $key ) {
 176              case 'id':
 177              case 'blog_id':
 178              case 'site_id':
 179                  return true;
 180          }
 181  
 182          return false;
 183      }
 184  
 185      /**
 186       * Setter.
 187       *
 188       * Allows current multisite naming conventions while setting properties.
 189       *
 190       * @since 4.6.0
 191       *
 192       * @param string $key   Property to set.
 193       * @param mixed  $value Value to assign to the property.
 194       */
 195  	public function __set( $key, $value ) {
 196          switch ( $key ) {
 197              case 'id':
 198                  $this->id = (int) $value;
 199                  break;
 200              case 'blog_id':
 201              case 'site_id':
 202                  $this->blog_id = (string) $value;
 203                  break;
 204              default:
 205                  $this->$key = $value;
 206          }
 207      }
 208  
 209      /**
 210       * Returns the main site ID for the network.
 211       *
 212       * Internal method used by the magic getter for the 'blog_id' and 'site_id'
 213       * properties.
 214       *
 215       * @since 4.9.0
 216       *
 217       * @return int The ID of the main site.
 218       */
 219  	private function get_main_site_id() {
 220          /**
 221           * Filters the main site ID.
 222           *
 223           * Returning a positive integer will effectively short-circuit the function.
 224           *
 225           * @since 4.9.0
 226           *
 227           * @param int|null   $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
 228           * @param WP_Network $network      The network object for which the main site was detected.
 229           */
 230          $main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
 231  
 232          if ( 0 < $main_site_id ) {
 233              return $main_site_id;
 234          }
 235  
 236          if ( 0 < (int) $this->blog_id ) {
 237              return (int) $this->blog_id;
 238          }
 239  
 240          if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' )
 241              && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
 242              || ( defined( 'SITE_ID_CURRENT_SITE' ) && (int) SITE_ID_CURRENT_SITE === $this->id )
 243          ) {
 244              if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
 245                  $this->blog_id = (string) BLOG_ID_CURRENT_SITE;
 246  
 247                  return (int) $this->blog_id;
 248              }
 249  
 250              if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
 251                  $this->blog_id = (string) BLOGID_CURRENT_SITE;
 252  
 253                  return (int) $this->blog_id;
 254              }
 255          }
 256  
 257          $site = get_site();
 258          if ( $site->domain === $this->domain && $site->path === $this->path ) {
 259              $main_site_id = (int) $site->id;
 260          } else {
 261  
 262              $main_site_id = get_network_option( $this->id, 'main_site' );
 263              if ( false === $main_site_id ) {
 264                  $_sites       = get_sites(
 265                      array(
 266                          'fields'     => 'ids',
 267                          'number'     => 1,
 268                          'domain'     => $this->domain,
 269                          'path'       => $this->path,
 270                          'network_id' => $this->id,
 271                      )
 272                  );
 273                  $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
 274  
 275                  update_network_option( $this->id, 'main_site', $main_site_id );
 276              }
 277          }
 278  
 279          $this->blog_id = (string) $main_site_id;
 280  
 281          return (int) $this->blog_id;
 282      }
 283  
 284      /**
 285       * Sets the site name assigned to the network if one has not been populated.
 286       *
 287       * @since 4.4.0
 288       */
 289  	private function _set_site_name() {
 290          if ( ! empty( $this->site_name ) ) {
 291              return;
 292          }
 293  
 294          $default         = ucfirst( $this->domain );
 295          $this->site_name = get_network_option( $this->id, 'site_name', $default );
 296      }
 297  
 298      /**
 299       * Sets the cookie domain based on the network domain if one has
 300       * not been populated.
 301       *
 302       * @todo What if the domain of the network doesn't match the current site?
 303       *
 304       * @since 4.4.0
 305       */
 306  	private function _set_cookie_domain() {
 307          if ( ! empty( $this->cookie_domain ) ) {
 308              return;
 309          }
 310  
 311          $this->cookie_domain = $this->domain;
 312          if ( str_starts_with( $this->cookie_domain, 'www.' ) ) {
 313              $this->cookie_domain = substr( $this->cookie_domain, 4 );
 314          }
 315      }
 316  
 317      /**
 318       * Retrieves the closest matching network for a domain and path.
 319       *
 320       * This will not necessarily return an exact match for a domain and path. Instead, it
 321       * breaks the domain and path into pieces that are then used to match the closest
 322       * possibility from a query.
 323       *
 324       * The intent of this method is to match a network during bootstrap for a
 325       * requested site address.
 326       *
 327       * @since 4.4.0
 328       *
 329       * @param string   $domain   Domain to check.
 330       * @param string   $path     Path to check.
 331       * @param int|null $segments Path segments to use. Defaults to null, or the full path.
 332       * @return WP_Network|false Network object if successful. False when no network is found.
 333       */
 334  	public static function get_by_path( $domain = '', $path = '', $segments = null ) {
 335          $domains = array( $domain );
 336          $pieces  = explode( '.', $domain );
 337  
 338          /*
 339           * It's possible one domain to search is 'com', but it might as well
 340           * be 'localhost' or some other locally mapped domain.
 341           */
 342          while ( array_shift( $pieces ) ) {
 343              if ( ! empty( $pieces ) ) {
 344                  $domains[] = implode( '.', $pieces );
 345              }
 346          }
 347  
 348          /*
 349           * If we've gotten to this function during normal execution, there is
 350           * more than one network installed. At this point, who knows how many
 351           * we have. Attempt to optimize for the situation where networks are
 352           * only domains, thus meaning paths never need to be considered.
 353           *
 354           * This is a very basic optimization; anything further could have
 355           * drawbacks depending on the setup, so this is best done per-installation.
 356           */
 357          $using_paths = true;
 358          if ( wp_using_ext_object_cache() ) {
 359              $using_paths = get_networks(
 360                  array(
 361                      'number'       => 1,
 362                      'count'        => true,
 363                      'path__not_in' => '/',
 364                  )
 365              );
 366          }
 367  
 368          $paths = array();
 369          if ( $using_paths ) {
 370              $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
 371  
 372              /**
 373               * Filters the number of path segments to consider when searching for a site.
 374               *
 375               * @since 3.9.0
 376               *
 377               * @param int|null $segments The number of path segments to consider. WordPress by default looks at
 378               *                           one path segment. The function default of null only makes sense when you
 379               *                           know the requested path should match a network.
 380               * @param string   $domain   The requested domain.
 381               * @param string   $path     The requested path, in full.
 382               */
 383              $segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
 384  
 385              if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
 386                  $path_segments = array_slice( $path_segments, 0, $segments );
 387              }
 388  
 389              while ( count( $path_segments ) ) {
 390                  $paths[] = '/' . implode( '/', $path_segments ) . '/';
 391                  array_pop( $path_segments );
 392              }
 393  
 394              $paths[] = '/';
 395          }
 396  
 397          /**
 398           * Determines a network by its domain and path.
 399           *
 400           * This allows one to short-circuit the default logic, perhaps by
 401           * replacing it with a routine that is more optimal for your setup.
 402           *
 403           * Return null to avoid the short-circuit. Return false if no network
 404           * can be found at the requested domain and path. Otherwise, return
 405           * an object from wp_get_network().
 406           *
 407           * @since 3.9.0
 408           *
 409           * @param null|false|WP_Network $network  Network value to return by path. Default null
 410           *                                        to continue retrieving the network.
 411           * @param string                $domain   The requested domain.
 412           * @param string                $path     The requested path, in full.
 413           * @param int|null              $segments The suggested number of paths to consult.
 414           *                                        Default null, meaning the entire path was to be consulted.
 415           * @param string[]              $paths    Array of paths to search for, based on `$path` and `$segments`.
 416           */
 417          $pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
 418          if ( null !== $pre ) {
 419              return $pre;
 420          }
 421  
 422          if ( ! $using_paths ) {
 423              $networks = get_networks(
 424                  array(
 425                      'number'     => 1,
 426                      'orderby'    => array(
 427                          'domain_length' => 'DESC',
 428                      ),
 429                      'domain__in' => $domains,
 430                  )
 431              );
 432  
 433              if ( ! empty( $networks ) ) {
 434                  return array_shift( $networks );
 435              }
 436  
 437              return false;
 438          }
 439  
 440          $networks = get_networks(
 441              array(
 442                  'orderby'    => array(
 443                      'domain_length' => 'DESC',
 444                      'path_length'   => 'DESC',
 445                  ),
 446                  'domain__in' => $domains,
 447                  'path__in'   => $paths,
 448              )
 449          );
 450  
 451          /*
 452           * Domains are sorted by length of domain, then by length of path.
 453           * The domain must match for the path to be considered. Otherwise,
 454           * a network with the path of / will suffice.
 455           */
 456          $found = false;
 457          foreach ( $networks as $network ) {
 458              if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
 459                  if ( in_array( $network->path, $paths, true ) ) {
 460                      $found = true;
 461                      break;
 462                  }
 463              }
 464              if ( '/' === $network->path ) {
 465                  $found = true;
 466                  break;
 467              }
 468          }
 469  
 470          if ( true === $found ) {
 471              return $network;
 472          }
 473  
 474          return false;
 475      }
 476  }


Generated : Thu May 9 08:20:02 2024 Cross-referenced by PHPXref