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


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1