[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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              && str_ends_with( $plugin, '.php' )             // $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      /*
 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       */
 233      $domains = array( $domain );
 234      if ( str_starts_with( $domain, 'www.' ) ) {
 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              /*
 316               * If the current network has a path and also matches the domain and path of the request,
 317               * we need to look for a site using the first path segment following the network's path.
 318               */
 319              $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
 320          } else {
 321              // Otherwise, use the first path segment (as usual).
 322              $current_blog = get_site_by_path( $domain, $path, 1 );
 323          }
 324      } elseif ( ! $subdomain ) {
 325          /*
 326           * A "subdomain" installation can be re-interpreted to mean "can support any domain".
 327           * If we're not dealing with one of these installations, then the important part is determining
 328           * the network first, because we need the network's path to identify any sites.
 329           */
 330          $current_site = wp_cache_get( 'current_network', 'site-options' );
 331          if ( ! $current_site ) {
 332              // Are there even two networks installed?
 333              $networks = get_networks( array( 'number' => 2 ) );
 334              if ( count( $networks ) === 1 ) {
 335                  $current_site = array_shift( $networks );
 336                  wp_cache_add( 'current_network', $current_site, 'site-options' );
 337              } elseif ( empty( $networks ) ) {
 338                  // A network not found hook should fire here.
 339                  return false;
 340              }
 341          }
 342  
 343          if ( empty( $current_site ) ) {
 344              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 345          }
 346  
 347          if ( empty( $current_site ) ) {
 348              /**
 349               * Fires when a network cannot be found based on the requested domain and path.
 350               *
 351               * At the time of this action, the only recourse is to redirect somewhere
 352               * and exit. If you want to declare a particular network, do so earlier.
 353               *
 354               * @since 4.4.0
 355               *
 356               * @param string $domain       The domain used to search for a network.
 357               * @param string $path         The path used to search for a path.
 358               */
 359              do_action( 'ms_network_not_found', $domain, $path );
 360  
 361              return false;
 362          } elseif ( $path === $current_site->path ) {
 363              $current_blog = get_site_by_path( $domain, $path );
 364          } else {
 365              // Search the network path + one more path segment (on top of the network path).
 366              $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
 367          }
 368      } else {
 369          // Find the site by the domain and at most the first path segment.
 370          $current_blog = get_site_by_path( $domain, $path, 1 );
 371          if ( $current_blog ) {
 372              $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
 373          } else {
 374              // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
 375              $current_site = WP_Network::get_by_path( $domain, $path, 1 );
 376          }
 377      }
 378  
 379      // The network declared by the site trumps any constants.
 380      if ( $current_blog && (int) $current_blog->site_id !== $current_site->id ) {
 381          $current_site = WP_Network::get_instance( $current_blog->site_id );
 382      }
 383  
 384      // No network has been found, bail.
 385      if ( empty( $current_site ) ) {
 386          /** This action is documented in wp-includes/ms-settings.php */
 387          do_action( 'ms_network_not_found', $domain, $path );
 388  
 389          return false;
 390      }
 391  
 392      // During activation of a new subdomain, the requested site does not yet exist.
 393      if ( empty( $current_blog ) && wp_installing() ) {
 394          $current_blog          = new stdClass();
 395          $current_blog->blog_id = 1;
 396          $blog_id               = 1;
 397          $current_blog->public  = 1;
 398      }
 399  
 400      // No site has been found, bail.
 401      if ( empty( $current_blog ) ) {
 402          // We're going to redirect to the network URL, with some possible modifications.
 403          $scheme      = is_ssl() ? 'https' : 'http';
 404          $destination = "$scheme://{$current_site->domain}{$current_site->path}";
 405  
 406          /**
 407           * Fires when a network can be determined but a site cannot.
 408           *
 409           * At the time of this action, the only recourse is to redirect somewhere
 410           * and exit. If you want to declare a particular site, do so earlier.
 411           *
 412           * @since 3.9.0
 413           *
 414           * @param WP_Network $current_site The network that had been determined.
 415           * @param string     $domain       The domain used to search for a site.
 416           * @param string     $path         The path used to search for a site.
 417           */
 418          do_action( 'ms_site_not_found', $current_site, $domain, $path );
 419  
 420          if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
 421              // For a "subdomain" installation, redirect to the signup form specifically.
 422              $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
 423          } elseif ( $subdomain ) {
 424              /*
 425               * For a "subdomain" installation, the NOBLOGREDIRECT constant
 426               * can be used to avoid a redirect to the signup form.
 427               * Using the ms_site_not_found action is preferred to the constant.
 428               */
 429              if ( '%siteurl%' !== NOBLOGREDIRECT ) {
 430                  $destination = NOBLOGREDIRECT;
 431              }
 432          } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
 433              /*
 434               * If the domain we were searching for matches the network's domain,
 435               * it's no use redirecting back to ourselves -- it'll cause a loop.
 436               * As we couldn't find a site, we're simply not installed.
 437               */
 438              return false;
 439          }
 440  
 441          return $destination;
 442      }
 443  
 444      // Figure out the current network's main site.
 445      if ( empty( $current_site->blog_id ) ) {
 446          $current_site->blog_id = get_main_site_id( $current_site->id );
 447      }
 448  
 449      return true;
 450  }
 451  
 452  /**
 453   * Displays a failure message.
 454   *
 455   * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
 456   *
 457   * @access private
 458   * @since 3.0.0
 459   * @since 4.4.0 The `$domain` and `$path` parameters were added.
 460   *
 461   * @global wpdb $wpdb WordPress database abstraction object.
 462   *
 463   * @param string $domain The requested domain for the error to reference.
 464   * @param string $path   The requested path for the error to reference.
 465   */
 466  function ms_not_installed( $domain, $path ) {
 467      global $wpdb;
 468  
 469      if ( ! is_admin() ) {
 470          dead_db();
 471      }
 472  
 473      wp_load_translations_early();
 474  
 475      $title = __( 'Error establishing a database connection' );
 476  
 477      $msg   = '<h1>' . $title . '</h1>';
 478      $msg  .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
 479      $msg  .= ' ' . __( 'If you are the owner of this network please check that your host&#8217;s database server is running properly and all tables are error free.' ) . '</p>';
 480      $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
 481      if ( ! $wpdb->get_var( $query ) ) {
 482          $msg .= '<p>' . sprintf(
 483              /* translators: %s: Table name. */
 484              __( '<strong>Database tables are missing.</strong> This means that your host&#8217;s database server is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
 485              '<code>' . $wpdb->site . '</code>'
 486          ) . '</p>';
 487      } else {
 488          $msg .= '<p>' . sprintf(
 489              /* translators: 1: Site URL, 2: Table name, 3: Database name. */
 490              __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
 491              '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
 492              '<code>' . $wpdb->blogs . '</code>',
 493              '<code>' . DB_NAME . '</code>'
 494          ) . '</p>';
 495      }
 496      $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
 497      $msg .= sprintf(
 498          /* translators: %s: Documentation URL. */
 499          __( '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.' ),
 500          __( 'https://developer.wordpress.org/advanced-administration/debug/debug-network/' )
 501      );
 502      $msg .= ' ' . __( 'If you are still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
 503      foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
 504          if ( 'sitecategories' === $t ) {
 505              continue;
 506          }
 507          $msg .= '<li>' . $table . '</li>';
 508      }
 509      $msg .= '</ul>';
 510  
 511      wp_die( $msg, $title, array( 'response' => 500 ) );
 512  }
 513  
 514  /**
 515   * This deprecated function formerly set the site_name property of the $current_site object.
 516   *
 517   * This function simply returns the object, as before.
 518   * The bootstrap takes care of setting site_name.
 519   *
 520   * @access private
 521   * @since 3.0.0
 522   * @deprecated 3.9.0 Use get_current_site() instead.
 523   *
 524   * @param WP_Network $current_site
 525   * @return WP_Network
 526   */
 527  function get_current_site_name( $current_site ) {
 528      _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
 529      return $current_site;
 530  }
 531  
 532  /**
 533   * This deprecated function managed much of the site and network loading in multisite.
 534   *
 535   * The current bootstrap code is now responsible for parsing the site and network load as
 536   * well as setting the global $current_site object.
 537   *
 538   * @access private
 539   * @since 3.0.0
 540   * @deprecated 3.9.0
 541   *
 542   * @global WP_Network $current_site
 543   *
 544   * @return WP_Network
 545   */
 546  function wpmu_current_site() {
 547      global $current_site;
 548      _deprecated_function( __FUNCTION__, '3.9.0' );
 549      return $current_site;
 550  }
 551  
 552  /**
 553   * Retrieves an object containing information about the requested network.
 554   *
 555   * @since 3.9.0
 556   * @deprecated 4.7.0 Use get_network()
 557   * @see get_network()
 558   *
 559   * @internal In 4.6.0, converted to use get_network()
 560   *
 561   * @param object|int $network The network's database row or ID.
 562   * @return WP_Network|false Object containing network information if found, false if not.
 563   */
 564  function wp_get_network( $network ) {
 565      _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
 566  
 567      $network = get_network( $network );
 568      if ( null === $network ) {
 569          return false;
 570      }
 571  
 572      return $network;
 573  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref