PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/ms-load.php

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