ms-site.php 42 KB

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