ms-site.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. <?php
  2. /**
  3. * Site API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 5.1.0
  8. */
  9. /**
  10. * Inserts a new site into the database.
  11. *
  12. * @since 5.1.0
  13. *
  14. * @global wpdb $wpdb WordPress database abstraction object.
  15. *
  16. * @param array $data {
  17. * Data for the new site that should be inserted.
  18. *
  19. * @type string $domain Site domain. Default empty string.
  20. * @type string $path Site path. Default '/'.
  21. * @type int $network_id The site's network ID. Default is the current network ID.
  22. * @type string $registered When the site was registered, in SQL datetime format. Default is
  23. * the current time.
  24. * @type string $last_updated When the site was last updated, in SQL datetime format. Default is
  25. * the value of $registered.
  26. * @type int $public Whether the site is public. Default 1.
  27. * @type int $archived Whether the site is archived. Default 0.
  28. * @type int $mature Whether the site is mature. Default 0.
  29. * @type int $spam Whether the site is spam. Default 0.
  30. * @type int $deleted Whether the site is deleted. Default 0.
  31. * @type int $lang_id The site's language ID. Currently unused. Default 0.
  32. * @type int $user_id User ID for the site administrator. Passed to the
  33. * `wp_initialize_site` hook.
  34. * @type string $title Site title. Default is 'Site %d' where %d is the site ID. Passed
  35. * to the `wp_initialize_site` hook.
  36. * @type array $options Custom option $key => $value pairs to use. Default empty array. Passed
  37. * to the `wp_initialize_site` hook.
  38. * @type array $meta Custom site metadata $key => $value pairs to use. Default empty array.
  39. * Passed to the `wp_initialize_site` hook.
  40. * }
  41. * @return int|WP_Error The new site's ID on success, or error object on failure.
  42. */
  43. function wp_insert_site( array $data ) {
  44. global $wpdb;
  45. $now = current_time( 'mysql', true );
  46. $defaults = array(
  47. 'domain' => '',
  48. 'path' => '/',
  49. 'network_id' => get_current_network_id(),
  50. 'registered' => $now,
  51. 'last_updated' => $now,
  52. 'public' => 1,
  53. 'archived' => 0,
  54. 'mature' => 0,
  55. 'spam' => 0,
  56. 'deleted' => 0,
  57. 'lang_id' => 0,
  58. );
  59. $prepared_data = wp_prepare_site_data( $data, $defaults );
  60. if ( is_wp_error( $prepared_data ) ) {
  61. return $prepared_data;
  62. }
  63. if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
  64. return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
  65. }
  66. $site_id = (int) $wpdb->insert_id;
  67. clean_blog_cache( $site_id );
  68. $new_site = get_site( $site_id );
  69. if ( ! $new_site ) {
  70. return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
  71. }
  72. /**
  73. * Fires once a site has been inserted into the database.
  74. *
  75. * @since 5.1.0
  76. *
  77. * @param WP_Site $new_site New site object.
  78. */
  79. do_action( 'wp_insert_site', $new_site );
  80. // Extract the passed arguments that may be relevant for site initialization.
  81. $args = array_diff_key( $data, $defaults );
  82. if ( isset( $args['site_id'] ) ) {
  83. unset( $args['site_id'] );
  84. }
  85. /**
  86. * Fires when a site's initialization routine should be executed.
  87. *
  88. * @since 5.1.0
  89. *
  90. * @param WP_Site $new_site New site object.
  91. * @param array $args Arguments for the initialization.
  92. */
  93. do_action( 'wp_initialize_site', $new_site, $args );
  94. // Only compute extra hook parameters if the deprecated hook is actually in use.
  95. if ( has_action( 'wpmu_new_blog' ) ) {
  96. $user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0;
  97. $meta = ! empty( $args['options'] ) ? $args['options'] : array();
  98. // WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
  99. if ( ! array_key_exists( 'WPLANG', $meta ) ) {
  100. $meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
  101. }
  102. // Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys.
  103. // The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
  104. $allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  105. $meta = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );
  106. /**
  107. * Fires immediately after a new site is created.
  108. *
  109. * @since MU (3.0.0)
  110. * @deprecated 5.1.0 Use {@see 'wp_initialize_site'} instead.
  111. *
  112. * @param int $site_id Site ID.
  113. * @param int $user_id User ID.
  114. * @param string $domain Site domain.
  115. * @param string $path Site path.
  116. * @param int $network_id Network ID. Only relevant on multi-network installations.
  117. * @param array $meta Meta data. Used to set initial site options.
  118. */
  119. do_action_deprecated(
  120. 'wpmu_new_blog',
  121. array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ),
  122. '5.1.0',
  123. 'wp_initialize_site'
  124. );
  125. }
  126. return (int) $new_site->id;
  127. }
  128. /**
  129. * Updates a site in the database.
  130. *
  131. * @since 5.1.0
  132. *
  133. * @global wpdb $wpdb WordPress database abstraction object.
  134. *
  135. * @param int $site_id ID of the site that should be updated.
  136. * @param array $data Site data to update. See {@see wp_insert_site()} for the list of supported keys.
  137. * @return int|WP_Error The updated site's ID on success, or error object on failure.
  138. */
  139. function wp_update_site( $site_id, array $data ) {
  140. global $wpdb;
  141. if ( empty( $site_id ) ) {
  142. return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
  143. }
  144. $old_site = get_site( $site_id );
  145. if ( ! $old_site ) {
  146. return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
  147. }
  148. $defaults = $old_site->to_array();
  149. $defaults['network_id'] = (int) $defaults['site_id'];
  150. $defaults['last_updated'] = current_time( 'mysql', true );
  151. unset( $defaults['blog_id'], $defaults['site_id'] );
  152. $data = wp_prepare_site_data( $data, $defaults, $old_site );
  153. if ( is_wp_error( $data ) ) {
  154. return $data;
  155. }
  156. if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
  157. return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
  158. }
  159. clean_blog_cache( $old_site );
  160. $new_site = get_site( $old_site->id );
  161. /**
  162. * Fires once a site has been updated in the database.
  163. *
  164. * @since 5.1.0
  165. *
  166. * @param WP_Site $new_site New site object.
  167. * @param WP_Site $old_site Old site object.
  168. */
  169. do_action( 'wp_update_site', $new_site, $old_site );
  170. return (int) $new_site->id;
  171. }
  172. /**
  173. * Deletes a site from the database.
  174. *
  175. * @since 5.1.0
  176. *
  177. * @global wpdb $wpdb WordPress database abstraction object.
  178. *
  179. * @param int $site_id ID of the site that should be deleted.
  180. * @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
  181. */
  182. function wp_delete_site( $site_id ) {
  183. global $wpdb;
  184. if ( empty( $site_id ) ) {
  185. return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
  186. }
  187. $old_site = get_site( $site_id );
  188. if ( ! $old_site ) {
  189. return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
  190. }
  191. $errors = new WP_Error();
  192. /**
  193. * Fires before a site should be deleted from the database.
  194. *
  195. * Plugins should amend the `$errors` object via its `WP_Error::add()` method. If any errors
  196. * are present, the site will not be deleted.
  197. *
  198. * @since 5.1.0
  199. *
  200. * @param WP_Error $errors Error object to add validation errors to.
  201. * @param WP_Site $old_site The site object to be deleted.
  202. */
  203. do_action( 'wp_validate_site_deletion', $errors, $old_site );
  204. if ( ! empty( $errors->errors ) ) {
  205. return $errors;
  206. }
  207. /**
  208. * Fires before a site is deleted.
  209. *
  210. * @since MU (3.0.0)
  211. * @deprecated 5.1.0
  212. *
  213. * @param int $site_id The site ID.
  214. * @param bool $drop True if site's table should be dropped. Default false.
  215. */
  216. do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.1.0' );
  217. /**
  218. * Fires when a site's uninitialization routine should be executed.
  219. *
  220. * @since 5.1.0
  221. *
  222. * @param WP_Site $old_site Deleted site object.
  223. */
  224. do_action( 'wp_uninitialize_site', $old_site );
  225. if ( is_site_meta_supported() ) {
  226. $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $old_site->id ) );
  227. foreach ( $blog_meta_ids as $mid ) {
  228. delete_metadata_by_mid( 'blog', $mid );
  229. }
  230. }
  231. if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
  232. return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
  233. }
  234. clean_blog_cache( $old_site );
  235. /**
  236. * Fires once a site has been deleted from the database.
  237. *
  238. * @since 5.1.0
  239. *
  240. * @param WP_Site $old_site Deleted site object.
  241. */
  242. do_action( 'wp_delete_site', $old_site );
  243. /**
  244. * Fires after the site is deleted from the network.
  245. *
  246. * @since 4.8.0
  247. * @deprecated 5.1.0
  248. *
  249. * @param int $site_id The site ID.
  250. * @param bool $drop True if site's tables should be dropped. Default false.
  251. */
  252. do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.1.0' );
  253. return $old_site;
  254. }
  255. /**
  256. * Retrieves site data given a site ID or site object.
  257. *
  258. * Site data will be cached and returned after being passed through a filter.
  259. * If the provided site is empty, the current site global will be used.
  260. *
  261. * @since 4.6.0
  262. *
  263. * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
  264. * @return WP_Site|null The site object or null if not found.
  265. */
  266. function get_site( $site = null ) {
  267. if ( empty( $site ) ) {
  268. $site = get_current_blog_id();
  269. }
  270. if ( $site instanceof WP_Site ) {
  271. $_site = $site;
  272. } elseif ( is_object( $site ) ) {
  273. $_site = new WP_Site( $site );
  274. } else {
  275. $_site = WP_Site::get_instance( $site );
  276. }
  277. if ( ! $_site ) {
  278. return null;
  279. }
  280. /**
  281. * Fires after a site is retrieved.
  282. *
  283. * @since 4.6.0
  284. *
  285. * @param WP_Site $_site Site data.
  286. */
  287. $_site = apply_filters( 'get_site', $_site );
  288. return $_site;
  289. }
  290. /**
  291. * Adds any sites from the given IDs to the cache that do not already exist in cache.
  292. *
  293. * @since 4.6.0
  294. * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
  295. * @since 6.1.0 This function is no longer marked as "private".
  296. *
  297. * @see update_site_cache()
  298. * @global wpdb $wpdb WordPress database abstraction object.
  299. *
  300. * @param array $ids ID list.
  301. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true.
  302. */
  303. function _prime_site_caches( $ids, $update_meta_cache = true ) {
  304. global $wpdb;
  305. $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
  306. if ( ! empty( $non_cached_ids ) ) {
  307. $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  308. update_site_cache( $fresh_sites, $update_meta_cache );
  309. }
  310. }
  311. /**
  312. * Updates sites in cache.
  313. *
  314. * @since 4.6.0
  315. * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
  316. *
  317. * @param array $sites Array of site objects.
  318. * @param bool $update_meta_cache Whether to update site meta cache. Default true.
  319. */
  320. function update_site_cache( $sites, $update_meta_cache = true ) {
  321. if ( ! $sites ) {
  322. return;
  323. }
  324. $site_ids = array();
  325. $site_data = array();
  326. $blog_details_data = array();
  327. foreach ( $sites as $site ) {
  328. $site_ids[] = $site->blog_id;
  329. $site_data[ $site->blog_id ] = $site;
  330. $blog_details_data[ $site->blog_id . 'short' ] = $site;
  331. }
  332. wp_cache_add_multiple( $site_data, 'sites' );
  333. wp_cache_add_multiple( $blog_details_data, 'blog-details' );
  334. if ( $update_meta_cache ) {
  335. update_sitemeta_cache( $site_ids );
  336. }
  337. }
  338. /**
  339. * Updates metadata cache for list of site IDs.
  340. *
  341. * Performs SQL query to retrieve all metadata for the sites matching `$site_ids` and stores them in the cache.
  342. * Subsequent calls to `get_site_meta()` will not need to query the database.
  343. *
  344. * @since 5.1.0
  345. *
  346. * @param array $site_ids List of site IDs.
  347. * @return array|false An array of metadata on success, false if there is nothing to update.
  348. */
  349. function update_sitemeta_cache( $site_ids ) {
  350. // Ensure this filter is hooked in even if the function is called early.
  351. if ( ! has_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' ) ) {
  352. add_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' );
  353. }
  354. return update_meta_cache( 'blog', $site_ids );
  355. }
  356. /**
  357. * Retrieves a list of sites matching requested arguments.
  358. *
  359. * @since 4.6.0
  360. * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
  361. *
  362. * @see WP_Site_Query::parse_query()
  363. *
  364. * @param string|array $args Optional. Array or string of arguments. See WP_Site_Query::__construct()
  365. * for information on accepted arguments. Default empty array.
  366. * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
  367. * or the number of sites when 'count' is passed as a query var.
  368. */
  369. function get_sites( $args = array() ) {
  370. $query = new WP_Site_Query();
  371. return $query->query( $args );
  372. }
  373. /**
  374. * Prepares site data for insertion or update in the database.
  375. *
  376. * @since 5.1.0
  377. *
  378. * @param array $data Associative array of site data passed to the respective function.
  379. * See {@see wp_insert_site()} for the possibly included data.
  380. * @param array $defaults Site data defaults to parse $data against.
  381. * @param WP_Site|null $old_site Optional. Old site object if an update, or null if an insertion.
  382. * Default null.
  383. * @return array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation
  384. * error occurred.
  385. */
  386. function wp_prepare_site_data( $data, $defaults, $old_site = null ) {
  387. // Maintain backward-compatibility with `$site_id` as network ID.
  388. if ( isset( $data['site_id'] ) ) {
  389. if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) {
  390. $data['network_id'] = $data['site_id'];
  391. }
  392. unset( $data['site_id'] );
  393. }
  394. /**
  395. * Filters passed site data in order to normalize it.
  396. *
  397. * @since 5.1.0
  398. *
  399. * @param array $data Associative array of site data passed to the respective function.
  400. * See {@see wp_insert_site()} for the possibly included data.
  401. */
  402. $data = apply_filters( 'wp_normalize_site_data', $data );
  403. $allowed_data_fields = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
  404. $data = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $allowed_data_fields ) );
  405. $errors = new WP_Error();
  406. /**
  407. * Fires when data should be validated for a site prior to inserting or updating in the database.
  408. *
  409. * Plugins should amend the `$errors` object via its `WP_Error::add()` method.
  410. *
  411. * @since 5.1.0
  412. *
  413. * @param WP_Error $errors Error object to add validation errors to.
  414. * @param array $data Associative array of complete site data. See {@see wp_insert_site()}
  415. * for the included data.
  416. * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
  417. * or null if it is a new site being inserted.
  418. */
  419. do_action( 'wp_validate_site_data', $errors, $data, $old_site );
  420. if ( ! empty( $errors->errors ) ) {
  421. return $errors;
  422. }
  423. // Prepare for database.
  424. $data['site_id'] = $data['network_id'];
  425. unset( $data['network_id'] );
  426. return $data;
  427. }
  428. /**
  429. * Normalizes data for a site prior to inserting or updating in the database.
  430. *
  431. * @since 5.1.0
  432. *
  433. * @param array $data Associative array of site data passed to the respective function.
  434. * See {@see wp_insert_site()} for the possibly included data.
  435. * @return array Normalized site data.
  436. */
  437. function wp_normalize_site_data( $data ) {
  438. // Sanitize domain if passed.
  439. if ( array_key_exists( 'domain', $data ) ) {
  440. $data['domain'] = trim( $data['domain'] );
  441. $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) );
  442. if ( is_subdomain_install() ) {
  443. $data['domain'] = str_replace( '@', '', $data['domain'] );
  444. }
  445. }
  446. // Sanitize path if passed.
  447. if ( array_key_exists( 'path', $data ) ) {
  448. $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
  449. }
  450. // Sanitize network ID if passed.
  451. if ( array_key_exists( 'network_id', $data ) ) {
  452. $data['network_id'] = (int) $data['network_id'];
  453. }
  454. // Sanitize status fields if passed.
  455. $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
  456. foreach ( $status_fields as $status_field ) {
  457. if ( array_key_exists( $status_field, $data ) ) {
  458. $data[ $status_field ] = (int) $data[ $status_field ];
  459. }
  460. }
  461. // Strip date fields if empty.
  462. $date_fields = array( 'registered', 'last_updated' );
  463. foreach ( $date_fields as $date_field ) {
  464. if ( ! array_key_exists( $date_field, $data ) ) {
  465. continue;
  466. }
  467. if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
  468. unset( $data[ $date_field ] );
  469. }
  470. }
  471. return $data;
  472. }
  473. /**
  474. * Validates data for a site prior to inserting or updating in the database.
  475. *
  476. * @since 5.1.0
  477. *
  478. * @param WP_Error $errors Error object, passed by reference. Will contain validation errors if
  479. * any occurred.
  480. * @param array $data Associative array of complete site data. See {@see wp_insert_site()}
  481. * for the included data.
  482. * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
  483. * or null if it is a new site being inserted.
  484. */
  485. function wp_validate_site_data( $errors, $data, $old_site = null ) {
  486. // A domain must always be present.
  487. if ( empty( $data['domain'] ) ) {
  488. $errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
  489. }
  490. // A path must always be present.
  491. if ( empty( $data['path'] ) ) {
  492. $errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
  493. }
  494. // A network ID must always be present.
  495. if ( empty( $data['network_id'] ) ) {
  496. $errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
  497. }
  498. // Both registration and last updated dates must always be present and valid.
  499. $date_fields = array( 'registered', 'last_updated' );
  500. foreach ( $date_fields as $date_field ) {
  501. if ( empty( $data[ $date_field ] ) ) {
  502. $errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
  503. break;
  504. }
  505. // Allow '0000-00-00 00:00:00', although it be stripped out at this point.
  506. if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) {
  507. $month = substr( $data[ $date_field ], 5, 2 );
  508. $day = substr( $data[ $date_field ], 8, 2 );
  509. $year = substr( $data[ $date_field ], 0, 4 );
  510. $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
  511. if ( ! $valid_date ) {
  512. $errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
  513. break;
  514. }
  515. }
  516. }
  517. if ( ! empty( $errors->errors ) ) {
  518. return;
  519. }
  520. // If a new site, or domain/path/network ID have changed, ensure uniqueness.
  521. if ( ! $old_site
  522. || $data['domain'] !== $old_site->domain
  523. || $data['path'] !== $old_site->path
  524. || $data['network_id'] !== $old_site->network_id
  525. ) {
  526. if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
  527. $errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
  528. }
  529. }
  530. }
  531. /**
  532. * Runs the initialization routine for a given site.
  533. *
  534. * This process includes creating the site's database tables and
  535. * populating them with defaults.
  536. *
  537. * @since 5.1.0
  538. *
  539. * @global wpdb $wpdb WordPress database abstraction object.
  540. * @global WP_Roles $wp_roles WordPress role management object.
  541. *
  542. * @param int|WP_Site $site_id Site ID or object.
  543. * @param array $args {
  544. * Optional. Arguments to modify the initialization behavior.
  545. *
  546. * @type int $user_id Required. User ID for the site administrator.
  547. * @type string $title Site title. Default is 'Site %d' where %d is the
  548. * site ID.
  549. * @type array $options Custom option $key => $value pairs to use. Default
  550. * empty array.
  551. * @type array $meta Custom site metadata $key => $value pairs to use.
  552. * Default empty array.
  553. * }
  554. * @return true|WP_Error True on success, or error object on failure.
  555. */
  556. function wp_initialize_site( $site_id, array $args = array() ) {
  557. global $wpdb, $wp_roles;
  558. if ( empty( $site_id ) ) {
  559. return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
  560. }
  561. $site = get_site( $site_id );
  562. if ( ! $site ) {
  563. return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
  564. }
  565. if ( wp_is_site_initialized( $site ) ) {
  566. return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
  567. }
  568. $network = get_network( $site->network_id );
  569. if ( ! $network ) {
  570. $network = get_network();
  571. }
  572. $args = wp_parse_args(
  573. $args,
  574. array(
  575. 'user_id' => 0,
  576. /* translators: %d: Site ID. */
  577. 'title' => sprintf( __( 'Site %d' ), $site->id ),
  578. 'options' => array(),
  579. 'meta' => array(),
  580. )
  581. );
  582. /**
  583. * Filters the arguments for initializing a site.
  584. *
  585. * @since 5.1.0
  586. *
  587. * @param array $args Arguments to modify the initialization behavior.
  588. * @param WP_Site $site Site that is being initialized.
  589. * @param WP_Network $network Network that the site belongs to.
  590. */
  591. $args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );
  592. $orig_installing = wp_installing();
  593. if ( ! $orig_installing ) {
  594. wp_installing( true );
  595. }
  596. $switch = false;
  597. if ( get_current_blog_id() !== $site->id ) {
  598. $switch = true;
  599. switch_to_blog( $site->id );
  600. }
  601. require_once ABSPATH . 'wp-admin/includes/upgrade.php';
  602. // Set up the database tables.
  603. make_db_current_silent( 'blog' );
  604. $home_scheme = 'http';
  605. $siteurl_scheme = 'http';
  606. if ( ! is_subdomain_install() ) {
  607. if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
  608. $home_scheme = 'https';
  609. }
  610. if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
  611. $siteurl_scheme = 'https';
  612. }
  613. }
  614. // Populate the site's options.
  615. populate_options(
  616. array_merge(
  617. array(
  618. 'home' => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
  619. 'siteurl' => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
  620. 'blogname' => wp_unslash( $args['title'] ),
  621. 'admin_email' => '',
  622. 'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
  623. 'blog_public' => (int) $site->public,
  624. 'WPLANG' => get_network_option( $network->id, 'WPLANG' ),
  625. ),
  626. $args['options']
  627. )
  628. );
  629. // Clean blog cache after populating options.
  630. clean_blog_cache( $site );
  631. // Populate the site's roles.
  632. populate_roles();
  633. $wp_roles = new WP_Roles();
  634. // Populate metadata for the site.
  635. populate_site_meta( $site->id, $args['meta'] );
  636. // Remove all permissions that may exist for the site.
  637. $table_prefix = $wpdb->get_blog_prefix();
  638. delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // Delete all.
  639. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.
  640. // Install default site content.
  641. wp_install_defaults( $args['user_id'] );
  642. // Set the site administrator.
  643. add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
  644. if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
  645. update_user_meta( $args['user_id'], 'primary_blog', $site->id );
  646. }
  647. if ( $switch ) {
  648. restore_current_blog();
  649. }
  650. wp_installing( $orig_installing );
  651. return true;
  652. }
  653. /**
  654. * Runs the uninitialization routine for a given site.
  655. *
  656. * This process includes dropping the site's database tables and deleting its uploads directory.
  657. *
  658. * @since 5.1.0
  659. *
  660. * @global wpdb $wpdb WordPress database abstraction object.
  661. *
  662. * @param int|WP_Site $site_id Site ID or object.
  663. * @return true|WP_Error True on success, or error object on failure.
  664. */
  665. function wp_uninitialize_site( $site_id ) {
  666. global $wpdb;
  667. if ( empty( $site_id ) ) {
  668. return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
  669. }
  670. $site = get_site( $site_id );
  671. if ( ! $site ) {
  672. return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
  673. }
  674. if ( ! wp_is_site_initialized( $site ) ) {
  675. return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) );
  676. }
  677. $users = get_users(
  678. array(
  679. 'blog_id' => $site->id,
  680. 'fields' => 'ids',
  681. )
  682. );
  683. // Remove users from the site.
  684. if ( ! empty( $users ) ) {
  685. foreach ( $users as $user_id ) {
  686. remove_user_from_blog( $user_id, $site->id );
  687. }
  688. }
  689. $switch = false;
  690. if ( get_current_blog_id() !== $site->id ) {
  691. $switch = true;
  692. switch_to_blog( $site->id );
  693. }
  694. $uploads = wp_get_upload_dir();
  695. $tables = $wpdb->tables( 'blog' );
  696. /**
  697. * Filters the tables to drop when the site is deleted.
  698. *
  699. * @since MU (3.0.0)
  700. *
  701. * @param string[] $tables Array of names of the site tables to be dropped.
  702. * @param int $site_id The ID of the site to drop tables for.
  703. */
  704. $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id );
  705. foreach ( (array) $drop_tables as $table ) {
  706. $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  707. }
  708. /**
  709. * Filters the upload base directory to delete when the site is deleted.
  710. *
  711. * @since MU (3.0.0)
  712. *
  713. * @param string $basedir Uploads path without subdirectory. @see wp_upload_dir()
  714. * @param int $site_id The site ID.
  715. */
  716. $dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
  717. $dir = rtrim( $dir, DIRECTORY_SEPARATOR );
  718. $top_dir = $dir;
  719. $stack = array( $dir );
  720. $index = 0;
  721. while ( $index < count( $stack ) ) {
  722. // Get indexed directory from stack.
  723. $dir = $stack[ $index ];
  724. // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
  725. $dh = @opendir( $dir );
  726. if ( $dh ) {
  727. $file = @readdir( $dh );
  728. while ( false !== $file ) {
  729. if ( '.' === $file || '..' === $file ) {
  730. $file = @readdir( $dh );
  731. continue;
  732. }
  733. if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
  734. $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
  735. } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
  736. @unlink( $dir . DIRECTORY_SEPARATOR . $file );
  737. }
  738. $file = @readdir( $dh );
  739. }
  740. @closedir( $dh );
  741. }
  742. $index++;
  743. }
  744. $stack = array_reverse( $stack ); // Last added directories are deepest.
  745. foreach ( (array) $stack as $dir ) {
  746. if ( $dir != $top_dir ) {
  747. @rmdir( $dir );
  748. }
  749. }
  750. // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
  751. if ( $switch ) {
  752. restore_current_blog();
  753. }
  754. return true;
  755. }
  756. /**
  757. * Checks whether a site is initialized.
  758. *
  759. * A site is considered initialized when its database tables are present.
  760. *
  761. * @since 5.1.0
  762. *
  763. * @global wpdb $wpdb WordPress database abstraction object.
  764. *
  765. * @param int|WP_Site $site_id Site ID or object.
  766. * @return bool True if the site is initialized, false otherwise.
  767. */
  768. function wp_is_site_initialized( $site_id ) {
  769. global $wpdb;
  770. if ( is_object( $site_id ) ) {
  771. $site_id = $site_id->blog_id;
  772. }
  773. $site_id = (int) $site_id;
  774. /**
  775. * Filters the check for whether a site is initialized before the database is accessed.
  776. *
  777. * Returning a non-null value will effectively short-circuit the function, returning
  778. * that value instead.
  779. *
  780. * @since 5.1.0
  781. *
  782. * @param bool|null $pre The value to return instead. Default null
  783. * to continue with the check.
  784. * @param int $site_id The site ID that is being checked.
  785. */
  786. $pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
  787. if ( null !== $pre ) {
  788. return (bool) $pre;
  789. }
  790. $switch = false;
  791. if ( get_current_blog_id() !== $site_id ) {
  792. $switch = true;
  793. remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
  794. switch_to_blog( $site_id );
  795. }
  796. $suppress = $wpdb->suppress_errors();
  797. $result = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
  798. $wpdb->suppress_errors( $suppress );
  799. if ( $switch ) {
  800. restore_current_blog();
  801. add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
  802. }
  803. return $result;
  804. }
  805. /**
  806. * Clean the blog cache
  807. *
  808. * @since 3.5.0
  809. *
  810. * @global bool $_wp_suspend_cache_invalidation
  811. *
  812. * @param WP_Site|int $blog The site object or ID to be cleared from cache.
  813. */
  814. function clean_blog_cache( $blog ) {
  815. global $_wp_suspend_cache_invalidation;
  816. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  817. return;
  818. }
  819. if ( empty( $blog ) ) {
  820. return;
  821. }
  822. $blog_id = $blog;
  823. $blog = get_site( $blog_id );
  824. if ( ! $blog ) {
  825. if ( ! is_numeric( $blog_id ) ) {
  826. return;
  827. }
  828. // Make sure a WP_Site object exists even when the site has been deleted.
  829. $blog = new WP_Site(
  830. (object) array(
  831. 'blog_id' => $blog_id,
  832. 'domain' => null,
  833. 'path' => null,
  834. )
  835. );
  836. }
  837. $blog_id = $blog->blog_id;
  838. $domain_path_key = md5( $blog->domain . $blog->path );
  839. wp_cache_delete( $blog_id, 'sites' );
  840. wp_cache_delete( $blog_id, 'site-details' );
  841. wp_cache_delete( $blog_id, 'blog-details' );
  842. wp_cache_delete( $blog_id . 'short', 'blog-details' );
  843. wp_cache_delete( $domain_path_key, 'blog-lookup' );
  844. wp_cache_delete( $domain_path_key, 'blog-id-cache' );
  845. wp_cache_delete( $blog_id, 'blog_meta' );
  846. /**
  847. * Fires immediately after a site has been removed from the object cache.
  848. *
  849. * @since 4.6.0
  850. *
  851. * @param string $id Site ID as a numeric string.
  852. * @param WP_Site $blog Site object.
  853. * @param string $domain_path_key md5 hash of domain and path.
  854. */
  855. do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
  856. wp_cache_set( 'last_changed', microtime(), 'sites' );
  857. /**
  858. * Fires after the blog details cache is cleared.
  859. *
  860. * @since 3.4.0
  861. * @deprecated 4.9.0 Use {@see 'clean_site_cache'} instead.
  862. *
  863. * @param int $blog_id Blog ID.
  864. */
  865. do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
  866. }
  867. /**
  868. * Adds metadata to a site.
  869. *
  870. * @since 5.1.0
  871. *
  872. * @param int $site_id Site ID.
  873. * @param string $meta_key Metadata name.
  874. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
  875. * @param bool $unique Optional. Whether the same key should not be added.
  876. * Default false.
  877. * @return int|false Meta ID on success, false on failure.
  878. */
  879. function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) {
  880. return add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique );
  881. }
  882. /**
  883. * Removes metadata matching criteria from a site.
  884. *
  885. * You can match based on the key, or key and value. Removing based on key and
  886. * value, will keep from removing duplicate metadata with the same key. It also
  887. * allows removing all metadata matching key, if needed.
  888. *
  889. * @since 5.1.0
  890. *
  891. * @param int $site_id Site ID.
  892. * @param string $meta_key Metadata name.
  893. * @param mixed $meta_value Optional. Metadata value. If provided,
  894. * rows will only be removed that match the value.
  895. * Must be serializable if non-scalar. Default empty.
  896. * @return bool True on success, false on failure.
  897. */
  898. function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
  899. return delete_metadata( 'blog', $site_id, $meta_key, $meta_value );
  900. }
  901. /**
  902. * Retrieves metadata for a site.
  903. *
  904. * @since 5.1.0
  905. *
  906. * @param int $site_id Site ID.
  907. * @param string $key Optional. The meta key to retrieve. By default,
  908. * returns data for all keys. Default empty.
  909. * @param bool $single Optional. Whether to return a single value.
  910. * This parameter has no effect if `$key` is not specified.
  911. * Default false.
  912. * @return mixed An array of values if `$single` is false.
  913. * The value of meta data field if `$single` is true.
  914. * False for an invalid `$site_id` (non-numeric, zero, or negative value).
  915. * An empty string if a valid but non-existing site ID is passed.
  916. */
  917. function get_site_meta( $site_id, $key = '', $single = false ) {
  918. return get_metadata( 'blog', $site_id, $key, $single );
  919. }
  920. /**
  921. * Updates metadata for a site.
  922. *
  923. * Use the $prev_value parameter to differentiate between meta fields with the
  924. * same key and site ID.
  925. *
  926. * If the meta field for the site does not exist, it will be added.
  927. *
  928. * @since 5.1.0
  929. *
  930. * @param int $site_id Site ID.
  931. * @param string $meta_key Metadata key.
  932. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
  933. * @param mixed $prev_value Optional. Previous value to check before updating.
  934. * If specified, only update existing metadata entries with
  935. * this value. Otherwise, update all entries. Default empty.
  936. * @return int|bool Meta ID if the key didn't exist, true on successful update,
  937. * false on failure or if the value passed to the function
  938. * is the same as the one that is already in the database.
  939. */
  940. function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) {
  941. return update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value );
  942. }
  943. /**
  944. * Deletes everything from site meta matching meta key.
  945. *
  946. * @since 5.1.0
  947. *
  948. * @param string $meta_key Metadata key to search for when deleting.
  949. * @return bool Whether the site meta key was deleted from the database.
  950. */
  951. function delete_site_meta_by_key( $meta_key ) {
  952. return delete_metadata( 'blog', null, $meta_key, '', true );
  953. }
  954. /**
  955. * Updates the count of sites for a network based on a changed site.
  956. *
  957. * @since 5.1.0
  958. *
  959. * @param WP_Site $new_site The site object that has been inserted, updated or deleted.
  960. * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
  961. * state of that site. Default null.
  962. */
  963. function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
  964. if ( null === $old_site ) {
  965. wp_maybe_update_network_site_counts( $new_site->network_id );
  966. return;
  967. }
  968. if ( $new_site->network_id != $old_site->network_id ) {
  969. wp_maybe_update_network_site_counts( $new_site->network_id );
  970. wp_maybe_update_network_site_counts( $old_site->network_id );
  971. }
  972. }
  973. /**
  974. * Triggers actions on site status updates.
  975. *
  976. * @since 5.1.0
  977. *
  978. * @param WP_Site $new_site The site object after the update.
  979. * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
  980. * state of that site. Default null.
  981. */
  982. function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
  983. $site_id = $new_site->id;
  984. // Use the default values for a site if no previous state is given.
  985. if ( ! $old_site ) {
  986. $old_site = new WP_Site( new stdClass() );
  987. }
  988. if ( $new_site->spam != $old_site->spam ) {
  989. if ( 1 == $new_site->spam ) {
  990. /**
  991. * Fires when the 'spam' status is added to a site.
  992. *
  993. * @since MU (3.0.0)
  994. *
  995. * @param int $site_id Site ID.
  996. */
  997. do_action( 'make_spam_blog', $site_id );
  998. } else {
  999. /**
  1000. * Fires when the 'spam' status is removed from a site.
  1001. *
  1002. * @since MU (3.0.0)
  1003. *
  1004. * @param int $site_id Site ID.
  1005. */
  1006. do_action( 'make_ham_blog', $site_id );
  1007. }
  1008. }
  1009. if ( $new_site->mature != $old_site->mature ) {
  1010. if ( 1 == $new_site->mature ) {
  1011. /**
  1012. * Fires when the 'mature' status is added to a site.
  1013. *
  1014. * @since 3.1.0
  1015. *
  1016. * @param int $site_id Site ID.
  1017. */
  1018. do_action( 'mature_blog', $site_id );
  1019. } else {
  1020. /**
  1021. * Fires when the 'mature' status is removed from a site.
  1022. *
  1023. * @since 3.1.0
  1024. *
  1025. * @param int $site_id Site ID.
  1026. */
  1027. do_action( 'unmature_blog', $site_id );
  1028. }
  1029. }
  1030. if ( $new_site->archived != $old_site->archived ) {
  1031. if ( 1 == $new_site->archived ) {
  1032. /**
  1033. * Fires when the 'archived' status is added to a site.
  1034. *
  1035. * @since MU (3.0.0)
  1036. *
  1037. * @param int $site_id Site ID.
  1038. */
  1039. do_action( 'archive_blog', $site_id );
  1040. } else {
  1041. /**
  1042. * Fires when the 'archived' status is removed from a site.
  1043. *
  1044. * @since MU (3.0.0)
  1045. *
  1046. * @param int $site_id Site ID.
  1047. */
  1048. do_action( 'unarchive_blog', $site_id );
  1049. }
  1050. }
  1051. if ( $new_site->deleted != $old_site->deleted ) {
  1052. if ( 1 == $new_site->deleted ) {
  1053. /**
  1054. * Fires when the 'deleted' status is added to a site.
  1055. *
  1056. * @since 3.5.0
  1057. *
  1058. * @param int $site_id Site ID.
  1059. */
  1060. do_action( 'make_delete_blog', $site_id );
  1061. } else {
  1062. /**
  1063. * Fires when the 'deleted' status is removed from a site.
  1064. *
  1065. * @since 3.5.0
  1066. *
  1067. * @param int $site_id Site ID.
  1068. */
  1069. do_action( 'make_undelete_blog', $site_id );
  1070. }
  1071. }
  1072. if ( $new_site->public != $old_site->public ) {
  1073. /**
  1074. * Fires after the current blog's 'public' setting is updated.
  1075. *
  1076. * @since MU (3.0.0)
  1077. *
  1078. * @param int $site_id Site ID.
  1079. * @param string $value The value of the site status.
  1080. */
  1081. do_action( 'update_blog_public', $site_id, $new_site->public );
  1082. }
  1083. }
  1084. /**
  1085. * Cleans the necessary caches after specific site data has been updated.
  1086. *
  1087. * @since 5.1.0
  1088. *
  1089. * @param WP_Site $new_site The site object after the update.
  1090. * @param WP_Site $old_site The site object prior to the update.
  1091. */
  1092. function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
  1093. if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
  1094. clean_blog_cache( $new_site );
  1095. }
  1096. }
  1097. /**
  1098. * Updates the `blog_public` option for a given site ID.
  1099. *
  1100. * @since 5.1.0
  1101. *
  1102. * @param int $site_id Site ID.
  1103. * @param string $public The value of the site status.
  1104. */
  1105. function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
  1106. // Bail if the site's database tables do not exist (yet).
  1107. if ( ! wp_is_site_initialized( $site_id ) ) {
  1108. return;
  1109. }
  1110. update_blog_option( $site_id, 'blog_public', $public );
  1111. }
  1112. /**
  1113. * Sets the last changed time for the 'sites' cache group.
  1114. *
  1115. * @since 5.1.0
  1116. */
  1117. function wp_cache_set_sites_last_changed() {
  1118. wp_cache_set( 'last_changed', microtime(), 'sites' );
  1119. }
  1120. /**
  1121. * Aborts calls to site meta if it is not supported.
  1122. *
  1123. * @since 5.1.0
  1124. *
  1125. * @global wpdb $wpdb WordPress database abstraction object.
  1126. *
  1127. * @param mixed $check Skip-value for whether to proceed site meta function execution.
  1128. * @return mixed Original value of $check, or false if site meta is not supported.
  1129. */
  1130. function wp_check_site_meta_support_prefilter( $check ) {
  1131. if ( ! is_site_meta_supported() ) {
  1132. /* translators: %s: Database table name. */
  1133. _doing_it_wrong( __FUNCTION__, sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), '5.1.0' );
  1134. return false;
  1135. }
  1136. return $check;
  1137. }