[ 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 ( $blog->archived == '1' || $blog->spam == '1' ) {
 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.
 207       * @param string             $domain   The requested domain.
 208       * @param string             $path     The requested path, in full.
 209       * @param int|null           $segments The suggested number of paths to consult.
 210       *                                     Default null, meaning the entire path was to be consulted.
 211       * @param string[]           $paths    The paths to search for, based on $path and $segments.
 212       */
 213      $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
 214      if ( null !== $pre ) {
 215          if ( false !== $pre && ! $pre instanceof WP_Site ) {
 216              $pre = new WP_Site( $pre );
 217          }
 218          return $pre;
 219      }
 220  
 221      /*
 222       * @todo
 223       * caching, etc. Consider alternative optimization routes,
 224       * perhaps as an opt-in for plugins, rather than using the pre_* filter.
 225       * For example: The segments filter can expand or ignore paths.
 226       * If persistent caching is enabled, we could query the DB for a path <> '/'
 227       * then cache whether we can just always ignore paths.
 228       */
 229  
 230      // Either www or non-www is supported, not both. If a www domain is requested,
 231      // query for both to provide the proper redirect.
 232      $domains = array( $domain );
 233      if ( 'www.' === substr( $domain, 0, 4 ) ) {
 234          $domains[] = substr( $domain, 4 );
 235      }
 236  
 237      $args = array(
 238          'number'                 => 1,
 239          'update_site_meta_cache' => false,
 240      );
 241  
 242      if ( count( $domains ) > 1 ) {
 243          $args['domain__in']               = $domains;
 244          $args['orderby']['domain_length'] = 'DESC';
 245      } else {
 246          $args['domain'] = array_shift( $domains );
 247      }
 248  
 249      if ( count( $paths ) > 1 ) {
 250          $args['path__in']               = $paths;
 251          $args['orderby']['path_length'] = 'DESC';
 252      } else {
 253          $args['path'] = array_shift( $paths );
 254      }
 255  
 256      $result = get_sites( $args );
 257      $site   = array_shift( $result );
 258  
 259      if ( $site ) {
 260          return $site;
 261      }
 262  
 263      return false;
 264  }
 265  
 266  /**
 267   * Identifies the network and site of a requested domain and path and populates the
 268   * corresponding network and site global objects as part of the multisite bootstrap process.
 269   *
 270   * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
 271   * a function to facilitate unit tests. It should not be used outside of core.
 272   *
 273   * Usually, it's easier to query the site first, which then declares its network.
 274   * In limited situations, we either can or must find the network first.
 275   *
 276   * If a network and site are found, a `true` response will be returned so that the
 277   * request can continue.
 278   *
 279   * If neither a network or site is found, `false` or a URL string will be returned
 280   * so that either an error can be shown or a redirect can occur.
 281   *
 282   * @since 4.6.0
 283   * @access private
 284   *
 285   * @global WP_Network $current_site The current network.
 286   * @global WP_Site    $current_blog The current site.
 287   *
 288   * @param string $domain    The requested domain.
 289   * @param string $path      The requested path.
 290   * @param bool   $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
 291   *                          Default false.
 292   * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
 293   *                     False if bootstrap could not be properly completed.
 294   *                     Redirect URL if parts exist, but the request as a whole can not be fulfilled.
 295   */
 296  function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
 297      global $current_site, $current_blog;
 298  
 299      // If the network is defined in wp-config.php, we can simply use that.
 300      if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
 301          $current_site         = new stdClass;
 302          $current_site->id     = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
 303          $current_site->domain = DOMAIN_CURRENT_SITE;
 304          $current_site->path   = PATH_CURRENT_SITE;
 305          if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
 306              $current_site->blog_id = BLOG_ID_CURRENT_SITE;
 307          } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
 308              $current_site->blog_id = BLOGID_CURRENT_SITE;
 309          }
 310  
 311          if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
 312              $current_blog = get_site_by_path( $domain, $path );
 313          } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
 314              // If the current network has a path and also matches the domain and path of the request,
 315              // we need to look for a site using the first path segment following the network's path.
 316              $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
 317          } else {
 318              // Otherwise, use the first path segment (as usual).
 319              $current_blog = get_site_by_path( $domain, $path, 1 );
 320          }
 321      } elseif ( ! $subdomain ) {
 322          /*
 323           * A "subdomain" installation can be re-interpreted to mean "can support any domain".
 324           * If we're not dealing with one of these installations, then the important part is determining
 325           * the network first, because we need the network's path to identify any sites.
 326           */
 327          $current_site = wp_cache_get( 'current_network', 'site-options' );
 328          if ( ! $current_site ) {
 329              // Are there even two networks installed?
 330              $networks = get_networks( array( 'number' => 2 ) );
 331              if ( count( $networks ) === 1 ) {
 332                  $current_site = array_shift( $networks );
 333                  wp_cache_add( 'current_network', $current_site, 'site-options' );
 334              } elseif ( empty( $networks ) ) {
 335                  // A network not found hook should fire here.
 336                  return false;
 337              }
 338          }
 339  
 340          if ( empty( $current_site ) ) {
 341              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 342          }
 343  
 344          if ( empty( $current_site ) ) {
 345              /**
 346               * Fires when a network cannot be found based on the requested domain and path.
 347               *
 348               * At the time of this action, the only recourse is to redirect somewhere
 349               * and exit. If you want to declare a particular network, do so earlier.
 350               *
 351               * @since 4.4.0
 352               *
 353               * @param string $domain       The domain used to search for a network.
 354               * @param string $path         The path used to search for a path.
 355               */
 356              do_action( 'ms_network_not_found', $domain, $path );
 357  
 358              return false;
 359          } elseif ( $path === $current_site->path ) {
 360              $current_blog = get_site_by_path( $domain, $path );
 361          } else {
 362              // Search the network path + one more path segment (on top of the network path).
 363              $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
 364          }
 365      } else {
 366          // Find the site by the domain and at most the first path segment.
 367          $current_blog = get_site_by_path( $domain, $path, 1 );
 368          if ( $current_blog ) {
 369              $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
 370          } else {
 371              // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
 372              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 373          }
 374      }
 375  
 376      // The network declared by the site trumps any constants.
 377      if ( $current_blog && $current_blog->site_id != $current_site->id ) {
 378          $current_site = WP_Network::get_instance( $current_blog->site_id );
 379      }
 380  
 381      // No network has been found, bail.
 382      if ( empty( $current_site ) ) {
 383          /** This action is documented in wp-includes/ms-settings.php */
 384          do_action( 'ms_network_not_found', $domain, $path );
 385  
 386          return false;
 387      }
 388  
 389      // During activation of a new subdomain, the requested site does not yet exist.
 390      if ( empty( $current_blog ) && wp_installing() ) {
 391          $current_blog          = new stdClass;
 392          $current_blog->blog_id = 1;
 393          $blog_id               = 1;
 394          $current_blog->public  = 1;
 395      }
 396  
 397      // No site has been found, bail.
 398      if ( empty( $current_blog ) ) {
 399          // We're going to redirect to the network URL, with some possible modifications.
 400          $scheme      = is_ssl() ? 'https' : 'http';
 401          $destination = "$scheme://{$current_site->domain}{$current_site->path}";
 402  
 403          /**
 404           * Fires when a network can be determined but a site cannot.
 405           *
 406           * At the time of this action, the only recourse is to redirect somewhere
 407           * and exit. If you want to declare a particular site, do so earlier.
 408           *
 409           * @since 3.9.0
 410           *
 411           * @param object $current_site The network that had been determined.
 412           * @param string $domain       The domain used to search for a site.
 413           * @param string $path         The path used to search for a site.
 414           */
 415          do_action( 'ms_site_not_found', $current_site, $domain, $path );
 416  
 417          if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
 418              // For a "subdomain" installation, redirect to the signup form specifically.
 419              $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
 420          } elseif ( $subdomain ) {
 421              // For a "subdomain" installation, the NOBLOGREDIRECT constant
 422              // can be used to avoid a redirect to the signup form.
 423              // Using the ms_site_not_found action is preferred to the constant.
 424              if ( '%siteurl%' !== NOBLOGREDIRECT ) {
 425                  $destination = NOBLOGREDIRECT;
 426              }
 427          } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
 428              /*
 429               * If the domain we were searching for matches the network's domain,
 430               * it's no use redirecting back to ourselves -- it'll cause a loop.
 431               * As we couldn't find a site, we're simply not installed.
 432               */
 433              return false;
 434          }
 435  
 436          return $destination;
 437      }
 438  
 439      // Figure out the current network's main site.
 440      if ( empty( $current_site->blog_id ) ) {
 441          $current_site->blog_id = get_main_site_id( $current_site->id );
 442      }
 443  
 444      return true;
 445  }
 446  
 447  /**
 448   * Displays a failure message.
 449   *
 450   * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
 451   *
 452   * @access private
 453   * @since 3.0.0
 454   * @since 4.4.0 The `$domain` and `$path` parameters were added.
 455   *
 456   * @global wpdb $wpdb WordPress database abstraction object.
 457   *
 458   * @param string $domain The requested domain for the error to reference.
 459   * @param string $path   The requested path for the error to reference.
 460   */
 461  function ms_not_installed( $domain, $path ) {
 462      global $wpdb;
 463  
 464      if ( ! is_admin() ) {
 465          dead_db();
 466      }
 467  
 468      wp_load_translations_early();
 469  
 470      $title = __( 'Error establishing a database connection' );
 471  
 472      $msg   = '<h1>' . $title . '</h1>';
 473      $msg  .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
 474      $msg  .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
 475      $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
 476      if ( ! $wpdb->get_var( $query ) ) {
 477          $msg .= '<p>' . sprintf(
 478              /* translators: %s: Table name. */
 479              __( '<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.' ),
 480              '<code>' . $wpdb->site . '</code>'
 481          ) . '</p>';
 482      } else {
 483          $msg .= '<p>' . sprintf(
 484              /* translators: 1: Site URL, 2: Table name, 3: Database name. */
 485              __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
 486              '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
 487              '<code>' . $wpdb->blogs . '</code>',
 488              '<code>' . DB_NAME . '</code>'
 489          ) . '</p>';
 490      }
 491      $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
 492      $msg .= sprintf(
 493          /* translators: %s: Documentation URL. */
 494          __( 'Read the <a href="%s" target="_blank">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' ),
 495          __( 'https://wordpress.org/support/article/debugging-a-wordpress-network/' )
 496      );
 497      $msg .= ' ' . __( 'If you&#8217;re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
 498      foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
 499          if ( 'sitecategories' == $t ) {
 500              continue;
 501          }
 502          $msg .= '<li>' . $table . '</li>';
 503      }
 504      $msg .= '</ul>';
 505  
 506      wp_die( $msg, $title, array( 'response' => 500 ) );
 507  }
 508  
 509  /**
 510   * This deprecated function formerly set the site_name property of the $current_site object.
 511   *
 512   * This function simply returns the object, as before.
 513   * The bootstrap takes care of setting site_name.
 514   *
 515   * @access private
 516   * @since 3.0.0
 517   * @deprecated 3.9.0 Use get_current_site() instead.
 518   *
 519   * @param object $current_site
 520   * @return object
 521   */
 522  function get_current_site_name( $current_site ) {
 523      _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
 524      return $current_site;
 525  }
 526  
 527  /**
 528   * This deprecated function managed much of the site and network loading in multisite.
 529   *
 530   * The current bootstrap code is now responsible for parsing the site and network load as
 531   * well as setting the global $current_site object.
 532   *
 533   * @access private
 534   * @since 3.0.0
 535   * @deprecated 3.9.0
 536   *
 537   * @global object $current_site
 538   *
 539   * @return object
 540   */
 541  function wpmu_current_site() {
 542      global $current_site;
 543      _deprecated_function( __FUNCTION__, '3.9.0' );
 544      return $current_site;
 545  }
 546  
 547  /**
 548   * Retrieve an object containing information about the requested network.
 549   *
 550   * @since 3.9.0
 551   * @deprecated 4.7.0 Use `get_network()`
 552   * @see get_network()
 553   *
 554   * @internal In 4.6.0, converted to use get_network()
 555   *
 556   * @param object|int $network The network's database row or ID.
 557   * @return WP_Network|false Object containing network information if found, false if not.
 558   */
 559  function wp_get_network( $network ) {
 560      _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
 561  
 562      $network = get_network( $network );
 563      if ( null === $network ) {
 564          return false;
 565      }
 566  
 567      return $network;
 568  }


Generated: Mon Nov 18 01:00:04 2019 Cross-referenced by PHPXref 0.7.1