[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> ms-load.php (source)

   1  <?php
   2  /**
   3   * These functions are needed to load Multisite.
   4   *
   5   * @since 3.0.0
   6   *
   7   * @package WordPress
   8   * @subpackage Multisite
   9   */
  10  
  11  /**
  12   * Whether a subdomain configuration is enabled.
  13   *
  14   * @since 3.0.0
  15   *
  16   * @return bool True if subdomain configuration is enabled, false otherwise.
  17   */
  18  function is_subdomain_install() {
  19      if ( defined( 'SUBDOMAIN_INSTALL' ) ) {
  20          return SUBDOMAIN_INSTALL;
  21      }
  22  
  23      return ( defined( 'VHOST' ) && VHOST == 'yes' );
  24  }
  25  
  26  /**
  27   * Returns array of network plugin files to be included in global scope.
  28   *
  29   * The default directory is wp-content/plugins. To change the default directory
  30   * manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
  31   *
  32   * @access private
  33   * @since 3.1.0
  34   *
  35   * @return string[] Array of absolute paths to files to include.
  36   */
  37  function wp_get_active_network_plugins() {
  38      $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
  39      if ( empty( $active_plugins ) ) {
  40          return array();
  41      }
  42  
  43      $plugins        = array();
  44      $active_plugins = array_keys( $active_plugins );
  45      sort( $active_plugins );
  46  
  47      foreach ( $active_plugins as $plugin ) {
  48          if ( ! validate_file( $plugin )                     // $plugin must validate as file.
  49              && '.php' == substr( $plugin, -4 )              // $plugin must end with '.php'.
  50              && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
  51              ) {
  52              $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
  53          }
  54      }
  55  
  56      return $plugins;
  57  }
  58  
  59  /**
  60   * Checks status of current blog.
  61   *
  62   * Checks if the blog is deleted, inactive, archived, or spammed.
  63   *
  64   * Dies with a default message if the blog does not pass the check.
  65   *
  66   * To change the default message when a blog does not pass the check,
  67   * use the wp-content/blog-deleted.php, blog-inactive.php and
  68   * blog-suspended.php drop-ins.
  69   *
  70   * @since 3.0.0
  71   *
  72   * @return true|string Returns true on success, or drop-in file to include.
  73   */
  74  function ms_site_check() {
  75  
  76      /**
  77       * Filters checking the status of the current blog.
  78       *
  79       * @since 3.0.0
  80       *
  81       * @param bool|null $check Whether to skip the blog status check. Default null.
  82       */
  83      $check = apply_filters( 'ms_site_check', null );
  84      if ( null !== $check ) {
  85          return true;
  86      }
  87  
  88      // Allow super admins to see blocked sites.
  89      if ( is_super_admin() ) {
  90          return true;
  91      }
  92  
  93      $blog = get_site();
  94  
  95      if ( '1' == $blog->deleted ) {
  96          if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) ) {
  97              return WP_CONTENT_DIR . '/blog-deleted.php';
  98          } else {
  99              wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
 100          }
 101      }
 102  
 103      if ( '2' == $blog->deleted ) {
 104          if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
 105              return WP_CONTENT_DIR . '/blog-inactive.php';
 106          } else {
 107              $admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_network()->domain ) );
 108              wp_die(
 109                  sprintf(
 110                      /* translators: %s: Admin email link. */
 111                      __( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
 112                      sprintf( '<a href="mailto:%1$s">%1$s</a>', $admin_email )
 113                  )
 114              );
 115          }
 116      }
 117  
 118      if ( '1' == $blog->archived || '1' == $blog->spam ) {
 119          if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) ) {
 120              return WP_CONTENT_DIR . '/blog-suspended.php';
 121          } else {
 122              wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
 123          }
 124      }
 125  
 126      return true;
 127  }
 128  
 129  /**
 130   * Retrieve the closest matching network for a domain and path.
 131   *
 132   * @since 3.9.0
 133   *
 134   * @internal In 4.4.0, converted to a wrapper for WP_Network::get_by_path()
 135   *
 136   * @param string   $domain   Domain to check.
 137   * @param string   $path     Path to check.
 138   * @param int|null $segments Path segments to use. Defaults to null, or the full path.
 139   * @return WP_Network|false Network object if successful. False when no network is found.
 140   */
 141  function get_network_by_path( $domain, $path, $segments = null ) {
 142      return WP_Network::get_by_path( $domain, $path, $segments );
 143  }
 144  
 145  /**
 146   * Retrieves the closest matching site object by its domain and path.
 147   *
 148   * This will not necessarily return an exact match for a domain and path. Instead, it
 149   * breaks the domain and path into pieces that are then used to match the closest
 150   * possibility from a query.
 151   *
 152   * The intent of this method is to match a site object during bootstrap for a
 153   * requested site address
 154   *
 155   * @since 3.9.0
 156   * @since 4.7.0 Updated to always return a `WP_Site` object.
 157   *
 158   * @global wpdb $wpdb WordPress database abstraction object.
 159   *
 160   * @param string   $domain   Domain to check.
 161   * @param string   $path     Path to check.
 162   * @param int|null $segments Path segments to use. Defaults to null, or the full path.
 163   * @return WP_Site|false Site object if successful. False when no site is found.
 164   */
 165  function get_site_by_path( $domain, $path, $segments = null ) {
 166      $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
 167  
 168      /**
 169       * Filters the number of path segments to consider when searching for a site.
 170       *
 171       * @since 3.9.0
 172       *
 173       * @param int|null $segments The number of path segments to consider. WordPress by default looks at
 174       *                           one path segment following the network path. The function default of
 175       *                           null only makes sense when you know the requested path should match a site.
 176       * @param string   $domain   The requested domain.
 177       * @param string   $path     The requested path, in full.
 178       */
 179      $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
 180  
 181      if ( null !== $segments && count( $path_segments ) > $segments ) {
 182          $path_segments = array_slice( $path_segments, 0, $segments );
 183      }
 184  
 185      $paths = array();
 186  
 187      while ( count( $path_segments ) ) {
 188          $paths[] = '/' . implode( '/', $path_segments ) . '/';
 189          array_pop( $path_segments );
 190      }
 191  
 192      $paths[] = '/';
 193  
 194      /**
 195       * Determine a site by its domain and path.
 196       *
 197       * This allows one to short-circuit the default logic, perhaps by
 198       * replacing it with a routine that is more optimal for your setup.
 199       *
 200       * Return null to avoid the short-circuit. Return false if no site
 201       * can be found at the requested domain and path. Otherwise, return
 202       * a site object.
 203       *
 204       * @since 3.9.0
 205       *
 206       * @param null|false|WP_Site $site     Site value to return by path. Default null
 207       *                                     to continue retrieving the site.
 208       * @param string             $domain   The requested domain.
 209       * @param string             $path     The requested path, in full.
 210       * @param int|null           $segments The suggested number of paths to consult.
 211       *                                     Default null, meaning the entire path was to be consulted.
 212       * @param string[]           $paths    The paths to search for, based on $path and $segments.
 213       */
 214      $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
 215      if ( null !== $pre ) {
 216          if ( false !== $pre && ! $pre instanceof WP_Site ) {
 217              $pre = new WP_Site( $pre );
 218          }
 219          return $pre;
 220      }
 221  
 222      /*
 223       * @todo
 224       * Caching, etc. Consider alternative optimization routes,
 225       * perhaps as an opt-in for plugins, rather than using the pre_* filter.
 226       * For example: The segments filter can expand or ignore paths.
 227       * If persistent caching is enabled, we could query the DB for a path <> '/'
 228       * then cache whether we can just always ignore paths.
 229       */
 230  
 231      // Either www or non-www is supported, not both. If a www domain is requested,
 232      // query for both to provide the proper redirect.
 233      $domains = array( $domain );
 234      if ( 'www.' === substr( $domain, 0, 4 ) ) {
 235          $domains[] = substr( $domain, 4 );
 236      }
 237  
 238      $args = array(
 239          'number'                 => 1,
 240          'update_site_meta_cache' => false,
 241      );
 242  
 243      if ( count( $domains ) > 1 ) {
 244          $args['domain__in']               = $domains;
 245          $args['orderby']['domain_length'] = 'DESC';
 246      } else {
 247          $args['domain'] = array_shift( $domains );
 248      }
 249  
 250      if ( count( $paths ) > 1 ) {
 251          $args['path__in']               = $paths;
 252          $args['orderby']['path_length'] = 'DESC';
 253      } else {
 254          $args['path'] = array_shift( $paths );
 255      }
 256  
 257      $result = get_sites( $args );
 258      $site   = array_shift( $result );
 259  
 260      if ( $site ) {
 261          return $site;
 262      }
 263  
 264      return false;
 265  }
 266  
 267  /**
 268   * Identifies the network and site of a requested domain and path and populates the
 269   * corresponding network and site global objects as part of the multisite bootstrap process.
 270   *
 271   * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
 272   * a function to facilitate unit tests. It should not be used outside of core.
 273   *
 274   * Usually, it's easier to query the site first, which then declares its network.
 275   * In limited situations, we either can or must find the network first.
 276   *
 277   * If a network and site are found, a `true` response will be returned so that the
 278   * request can continue.
 279   *
 280   * If neither a network or site is found, `false` or a URL string will be returned
 281   * so that either an error can be shown or a redirect can occur.
 282   *
 283   * @since 4.6.0
 284   * @access private
 285   *
 286   * @global WP_Network $current_site The current network.
 287   * @global WP_Site    $current_blog The current site.
 288   *
 289   * @param string $domain    The requested domain.
 290   * @param string $path      The requested path.
 291   * @param bool   $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
 292   *                          Default false.
 293   * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
 294   *                     False if bootstrap could not be properly completed.
 295   *                     Redirect URL if parts exist, but the request as a whole can not be fulfilled.
 296   */
 297  function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
 298      global $current_site, $current_blog;
 299  
 300      // If the network is defined in wp-config.php, we can simply use that.
 301      if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
 302          $current_site         = new stdClass;
 303          $current_site->id     = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
 304          $current_site->domain = DOMAIN_CURRENT_SITE;
 305          $current_site->path   = PATH_CURRENT_SITE;
 306          if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
 307              $current_site->blog_id = BLOG_ID_CURRENT_SITE;
 308          } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
 309              $current_site->blog_id = BLOGID_CURRENT_SITE;
 310          }
 311  
 312          if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
 313              $current_blog = get_site_by_path( $domain, $path );
 314          } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
 315              // If the current network has a path and also matches the domain and path of the request,
 316              // we need to look for a site using the first path segment following the network's path.
 317              $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
 318          } else {
 319              // Otherwise, use the first path segment (as usual).
 320              $current_blog = get_site_by_path( $domain, $path, 1 );
 321          }
 322      } elseif ( ! $subdomain ) {
 323          /*
 324           * A "subdomain" installation can be re-interpreted to mean "can support any domain".
 325           * If we're not dealing with one of these installations, then the important part is determining
 326           * the network first, because we need the network's path to identify any sites.
 327           */
 328          $current_site = wp_cache_get( 'current_network', 'site-options' );
 329          if ( ! $current_site ) {
 330              // Are there even two networks installed?
 331              $networks = get_networks( array( 'number' => 2 ) );
 332              if ( count( $networks ) === 1 ) {
 333                  $current_site = array_shift( $networks );
 334                  wp_cache_add( 'current_network', $current_site, 'site-options' );
 335              } elseif ( empty( $networks ) ) {
 336                  // A network not found hook should fire here.
 337                  return false;
 338              }
 339          }
 340  
 341          if ( empty( $current_site ) ) {
 342              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 343          }
 344  
 345          if ( empty( $current_site ) ) {
 346              /**
 347               * Fires when a network cannot be found based on the requested domain and path.
 348               *
 349               * At the time of this action, the only recourse is to redirect somewhere
 350               * and exit. If you want to declare a particular network, do so earlier.
 351               *
 352               * @since 4.4.0
 353               *
 354               * @param string $domain       The domain used to search for a network.
 355               * @param string $path         The path used to search for a path.
 356               */
 357              do_action( 'ms_network_not_found', $domain, $path );
 358  
 359              return false;
 360          } elseif ( $path === $current_site->path ) {
 361              $current_blog = get_site_by_path( $domain, $path );
 362          } else {
 363              // Search the network path + one more path segment (on top of the network path).
 364              $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
 365          }
 366      } else {
 367          // Find the site by the domain and at most the first path segment.
 368          $current_blog = get_site_by_path( $domain, $path, 1 );
 369          if ( $current_blog ) {
 370              $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
 371          } else {
 372              // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
 373              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 374          }
 375      }
 376  
 377      // The network declared by the site trumps any constants.
 378      if ( $current_blog && $current_blog->site_id != $current_site->id ) {
 379          $current_site = WP_Network::get_instance( $current_blog->site_id );
 380      }
 381  
 382      // No network has been found, bail.
 383      if ( empty( $current_site ) ) {
 384          /** This action is documented in wp-includes/ms-settings.php */
 385          do_action( 'ms_network_not_found', $domain, $path );
 386  
 387          return false;
 388      }
 389  
 390      // During activation of a new subdomain, the requested site does not yet exist.
 391      if ( empty( $current_blog ) && wp_installing() ) {
 392          $current_blog          = new stdClass;
 393          $current_blog->blog_id = 1;
 394          $blog_id               = 1;
 395          $current_blog->public  = 1;
 396      }
 397  
 398      // No site has been found, bail.
 399      if ( empty( $current_blog ) ) {
 400          // We're going to redirect to the network URL, with some possible modifications.
 401          $scheme      = is_ssl() ? 'https' : 'http';
 402          $destination = "$scheme://{$current_site->domain}{$current_site->path}";
 403  
 404          /**
 405           * Fires when a network can be determined but a site cannot.
 406           *
 407           * At the time of this action, the only recourse is to redirect somewhere
 408           * and exit. If you want to declare a particular site, do so earlier.
 409           *
 410           * @since 3.9.0
 411           *
 412           * @param object $current_site The network that had been determined.
 413           * @param string $domain       The domain used to search for a site.
 414           * @param string $path         The path used to search for a site.
 415           */
 416          do_action( 'ms_site_not_found', $current_site, $domain, $path );
 417  
 418          if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
 419              // For a "subdomain" installation, redirect to the signup form specifically.
 420              $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
 421          } elseif ( $subdomain ) {
 422              /*
 423               * For a "subdomain" installation, the NOBLOGREDIRECT constant
 424               * can be used to avoid a redirect to the signup form.
 425               * Using the ms_site_not_found action is preferred to the constant.
 426               */
 427              if ( '%siteurl%' !== NOBLOGREDIRECT ) {
 428                  $destination = NOBLOGREDIRECT;
 429              }
 430          } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
 431              /*
 432               * If the domain we were searching for matches the network's domain,
 433               * it's no use redirecting back to ourselves -- it'll cause a loop.
 434               * As we couldn't find a site, we're simply not installed.
 435               */
 436              return false;
 437          }
 438  
 439          return $destination;
 440      }
 441  
 442      // Figure out the current network's main site.
 443      if ( empty( $current_site->blog_id ) ) {
 444          $current_site->blog_id = get_main_site_id( $current_site->id );
 445      }
 446  
 447      return true;
 448  }
 449  
 450  /**
 451   * Displays a failure message.
 452   *
 453   * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
 454   *
 455   * @access private
 456   * @since 3.0.0
 457   * @since 4.4.0 The `$domain` and `$path` parameters were added.
 458   *
 459   * @global wpdb $wpdb WordPress database abstraction object.
 460   *
 461   * @param string $domain The requested domain for the error to reference.
 462   * @param string $path   The requested path for the error to reference.
 463   */
 464  function ms_not_installed( $domain, $path ) {
 465      global $wpdb;
 466  
 467      if ( ! is_admin() ) {
 468          dead_db();
 469      }
 470  
 471      wp_load_translations_early();
 472  
 473      $title = __( 'Error establishing a database connection' );
 474  
 475      $msg   = '<h1>' . $title . '</h1>';
 476      $msg  .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
 477      $msg  .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
 478      $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
 479      if ( ! $wpdb->get_var( $query ) ) {
 480          $msg .= '<p>' . sprintf(
 481              /* translators: %s: Table name. */
 482              __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
 483              '<code>' . $wpdb->site . '</code>'
 484          ) . '</p>';
 485      } else {
 486          $msg .= '<p>' . sprintf(
 487              /* translators: 1: Site URL, 2: Table name, 3: Database name. */
 488              __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
 489              '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
 490              '<code>' . $wpdb->blogs . '</code>',
 491              '<code>' . DB_NAME . '</code>'
 492          ) . '</p>';
 493      }
 494      $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
 495      $msg .= sprintf(
 496          /* translators: %s: Documentation URL. */
 497          __( 'Read the <a href="%s" target="_blank">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' ),
 498          __( 'https://wordpress.org/support/article/debugging-a-wordpress-network/' )
 499      );
 500      $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
 501      foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
 502          if ( 'sitecategories' == $t ) {
 503              continue;
 504          }
 505          $msg .= '<li>' . $table . '</li>';
 506      }
 507      $msg .= '</ul>';
 508  
 509      wp_die( $msg, $title, array( 'response' => 500 ) );
 510  }
 511  
 512  /**
 513   * This deprecated function formerly set the site_name property of the $current_site object.
 514   *
 515   * This function simply returns the object, as before.
 516   * The bootstrap takes care of setting site_name.
 517   *
 518   * @access private
 519   * @since 3.0.0
 520   * @deprecated 3.9.0 Use get_current_site() instead.
 521   *
 522   * @param object $current_site
 523   * @return object
 524   */
 525  function get_current_site_name( $current_site ) {
 526      _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
 527      return $current_site;
 528  }
 529  
 530  /**
 531   * This deprecated function managed much of the site and network loading in multisite.
 532   *
 533   * The current bootstrap code is now responsible for parsing the site and network load as
 534   * well as setting the global $current_site object.
 535   *
 536   * @access private
 537   * @since 3.0.0
 538   * @deprecated 3.9.0
 539   *
 540   * @global object $current_site
 541   *
 542   * @return object
 543   */
 544  function wpmu_current_site() {
 545      global $current_site;
 546      _deprecated_function( __FUNCTION__, '3.9.0' );
 547      return $current_site;
 548  }
 549  
 550  /**
 551   * Retrieve an object containing information about the requested network.
 552   *
 553   * @since 3.9.0
 554   * @deprecated 4.7.0 Use `get_network()`
 555   * @see get_network()
 556   *
 557   * @internal In 4.6.0, converted to use get_network()
 558   *
 559   * @param object|int $network The network's database row or ID.
 560   * @return WP_Network|false Object containing network information if found, false if not.
 561   */
 562  function wp_get_network( $network ) {
 563      _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
 564  
 565      $network = get_network( $network );
 566      if ( null === $network ) {
 567          return false;
 568      }
 569  
 570      return $network;
 571  }


Generated: Sun Apr 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1