ms-blogs.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. <?php
  2. /**
  3. * Site/blog functions that work with the blogs table and related data.
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since MU (3.0.0)
  8. */
  9. /**
  10. * Update the last_updated field for the current site.
  11. *
  12. * @since MU (3.0.0)
  13. */
  14. function wpmu_update_blogs_date() {
  15. $site_id = get_current_blog_id();
  16. update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) );
  17. /**
  18. * Fires after the blog details are updated.
  19. *
  20. * @since MU (3.0.0)
  21. *
  22. * @param int $blog_id Site ID.
  23. */
  24. do_action( 'wpmu_blog_updated', $site_id );
  25. }
  26. /**
  27. * Get a full blog URL, given a blog id.
  28. *
  29. * @since MU (3.0.0)
  30. *
  31. * @param int $blog_id Blog ID
  32. * @return string Full URL of the blog if found. Empty string if not.
  33. */
  34. function get_blogaddress_by_id( $blog_id ) {
  35. $bloginfo = get_site( (int) $blog_id );
  36. if ( empty( $bloginfo ) ) {
  37. return '';
  38. }
  39. $scheme = parse_url( $bloginfo->home, PHP_URL_SCHEME );
  40. $scheme = empty( $scheme ) ? 'http' : $scheme;
  41. return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
  42. }
  43. /**
  44. * Get a full blog URL, given a blog name.
  45. *
  46. * @since MU (3.0.0)
  47. *
  48. * @param string $blogname The (subdomain or directory) name
  49. * @return string
  50. */
  51. function get_blogaddress_by_name( $blogname ) {
  52. if ( is_subdomain_install() ) {
  53. if ( $blogname == 'main' )
  54. $blogname = 'www';
  55. $url = rtrim( network_home_url(), '/' );
  56. if ( !empty( $blogname ) )
  57. $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
  58. } else {
  59. $url = network_home_url( $blogname );
  60. }
  61. return esc_url( $url . '/' );
  62. }
  63. /**
  64. * Retrieves a sites ID given its (subdomain or directory) slug.
  65. *
  66. * @since MU (3.0.0)
  67. * @since 4.7.0 Converted to use get_sites().
  68. *
  69. * @param string $slug A site's slug.
  70. * @return int|null The site ID, or null if no site is found for the given slug.
  71. */
  72. function get_id_from_blogname( $slug ) {
  73. $current_network = get_network();
  74. $slug = trim( $slug, '/' );
  75. if ( is_subdomain_install() ) {
  76. $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
  77. $path = $current_network->path;
  78. } else {
  79. $domain = $current_network->domain;
  80. $path = $current_network->path . $slug . '/';
  81. }
  82. $site_ids = get_sites( array(
  83. 'number' => 1,
  84. 'fields' => 'ids',
  85. 'domain' => $domain,
  86. 'path' => $path,
  87. ) );
  88. if ( empty( $site_ids ) ) {
  89. return null;
  90. }
  91. return array_shift( $site_ids );
  92. }
  93. /**
  94. * Retrieve the details for a blog from the blogs table and blog options.
  95. *
  96. * @since MU (3.0.0)
  97. *
  98. * @global wpdb $wpdb WordPress database abstraction object.
  99. *
  100. * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
  101. * If not specified the current blog ID is used.
  102. * @param bool $get_all Whether to retrieve all details or only the details in the blogs table.
  103. * Default is true.
  104. * @return WP_Site|false Blog details on success. False on failure.
  105. */
  106. function get_blog_details( $fields = null, $get_all = true ) {
  107. global $wpdb;
  108. if ( is_array($fields ) ) {
  109. if ( isset($fields['blog_id']) ) {
  110. $blog_id = $fields['blog_id'];
  111. } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
  112. $key = md5( $fields['domain'] . $fields['path'] );
  113. $blog = wp_cache_get($key, 'blog-lookup');
  114. if ( false !== $blog )
  115. return $blog;
  116. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  117. $nowww = substr( $fields['domain'], 4 );
  118. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
  119. } else {
  120. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
  121. }
  122. if ( $blog ) {
  123. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  124. $blog_id = $blog->blog_id;
  125. } else {
  126. return false;
  127. }
  128. } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
  129. $key = md5( $fields['domain'] );
  130. $blog = wp_cache_get($key, 'blog-lookup');
  131. if ( false !== $blog )
  132. return $blog;
  133. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  134. $nowww = substr( $fields['domain'], 4 );
  135. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
  136. } else {
  137. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
  138. }
  139. if ( $blog ) {
  140. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  141. $blog_id = $blog->blog_id;
  142. } else {
  143. return false;
  144. }
  145. } else {
  146. return false;
  147. }
  148. } else {
  149. if ( ! $fields )
  150. $blog_id = get_current_blog_id();
  151. elseif ( ! is_numeric( $fields ) )
  152. $blog_id = get_id_from_blogname( $fields );
  153. else
  154. $blog_id = $fields;
  155. }
  156. $blog_id = (int) $blog_id;
  157. $all = $get_all == true ? '' : 'short';
  158. $details = wp_cache_get( $blog_id . $all, 'blog-details' );
  159. if ( $details ) {
  160. if ( ! is_object( $details ) ) {
  161. if ( $details == -1 ) {
  162. return false;
  163. } else {
  164. // Clear old pre-serialized objects. Cache clients do better with that.
  165. wp_cache_delete( $blog_id . $all, 'blog-details' );
  166. unset($details);
  167. }
  168. } else {
  169. return $details;
  170. }
  171. }
  172. // Try the other cache.
  173. if ( $get_all ) {
  174. $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
  175. } else {
  176. $details = wp_cache_get( $blog_id, 'blog-details' );
  177. // If short was requested and full cache is set, we can return.
  178. if ( $details ) {
  179. if ( ! is_object( $details ) ) {
  180. if ( $details == -1 ) {
  181. return false;
  182. } else {
  183. // Clear old pre-serialized objects. Cache clients do better with that.
  184. wp_cache_delete( $blog_id, 'blog-details' );
  185. unset($details);
  186. }
  187. } else {
  188. return $details;
  189. }
  190. }
  191. }
  192. if ( empty($details) ) {
  193. $details = WP_Site::get_instance( $blog_id );
  194. if ( ! $details ) {
  195. // Set the full cache.
  196. wp_cache_set( $blog_id, -1, 'blog-details' );
  197. return false;
  198. }
  199. }
  200. if ( ! $details instanceof WP_Site ) {
  201. $details = new WP_Site( $details );
  202. }
  203. if ( ! $get_all ) {
  204. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  205. return $details;
  206. }
  207. switch_to_blog( $blog_id );
  208. $details->blogname = get_option( 'blogname' );
  209. $details->siteurl = get_option( 'siteurl' );
  210. $details->post_count = get_option( 'post_count' );
  211. $details->home = get_option( 'home' );
  212. restore_current_blog();
  213. /**
  214. * Filters a blog's details.
  215. *
  216. * @since MU (3.0.0)
  217. * @deprecated 4.7.0 Use site_details
  218. *
  219. * @param object $details The blog details.
  220. */
  221. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
  222. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  223. $key = md5( $details->domain . $details->path );
  224. wp_cache_set( $key, $details, 'blog-lookup' );
  225. return $details;
  226. }
  227. /**
  228. * Clear the blog details cache.
  229. *
  230. * @since MU (3.0.0)
  231. *
  232. * @param int $blog_id Optional. Blog ID. Defaults to current blog.
  233. */
  234. function refresh_blog_details( $blog_id = 0 ) {
  235. $blog_id = (int) $blog_id;
  236. if ( ! $blog_id ) {
  237. $blog_id = get_current_blog_id();
  238. }
  239. clean_blog_cache( $blog_id );
  240. }
  241. /**
  242. * Update the details for a blog. Updates the blogs table for a given blog id.
  243. *
  244. * @since MU (3.0.0)
  245. *
  246. * @global wpdb $wpdb WordPress database abstraction object.
  247. *
  248. * @param int $blog_id Blog ID
  249. * @param array $details Array of details keyed by blogs table field names.
  250. * @return bool True if update succeeds, false otherwise.
  251. */
  252. function update_blog_details( $blog_id, $details = array() ) {
  253. global $wpdb;
  254. if ( empty($details) )
  255. return false;
  256. if ( is_object($details) )
  257. $details = get_object_vars($details);
  258. $current_details = get_site( $blog_id );
  259. if ( empty($current_details) )
  260. return false;
  261. $current_details = get_object_vars($current_details);
  262. $details = array_merge($current_details, $details);
  263. $details['last_updated'] = current_time('mysql', true);
  264. $update_details = array();
  265. $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
  266. foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
  267. if ( 'path' === $field ) {
  268. $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
  269. }
  270. $update_details[ $field ] = $details[ $field ];
  271. }
  272. $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
  273. if ( false === $result )
  274. return false;
  275. // If spam status changed, issue actions.
  276. if ( $details['spam'] != $current_details['spam'] ) {
  277. if ( $details['spam'] == 1 ) {
  278. /**
  279. * Fires when the 'spam' status is added to a blog.
  280. *
  281. * @since MU (3.0.0)
  282. *
  283. * @param int $blog_id Blog ID.
  284. */
  285. do_action( 'make_spam_blog', $blog_id );
  286. } else {
  287. /**
  288. * Fires when the 'spam' status is removed from a blog.
  289. *
  290. * @since MU (3.0.0)
  291. *
  292. * @param int $blog_id Blog ID.
  293. */
  294. do_action( 'make_ham_blog', $blog_id );
  295. }
  296. }
  297. // If mature status changed, issue actions.
  298. if ( $details['mature'] != $current_details['mature'] ) {
  299. if ( $details['mature'] == 1 ) {
  300. /**
  301. * Fires when the 'mature' status is added to a blog.
  302. *
  303. * @since 3.1.0
  304. *
  305. * @param int $blog_id Blog ID.
  306. */
  307. do_action( 'mature_blog', $blog_id );
  308. } else {
  309. /**
  310. * Fires when the 'mature' status is removed from a blog.
  311. *
  312. * @since 3.1.0
  313. *
  314. * @param int $blog_id Blog ID.
  315. */
  316. do_action( 'unmature_blog', $blog_id );
  317. }
  318. }
  319. // If archived status changed, issue actions.
  320. if ( $details['archived'] != $current_details['archived'] ) {
  321. if ( $details['archived'] == 1 ) {
  322. /**
  323. * Fires when the 'archived' status is added to a blog.
  324. *
  325. * @since MU (3.0.0)
  326. *
  327. * @param int $blog_id Blog ID.
  328. */
  329. do_action( 'archive_blog', $blog_id );
  330. } else {
  331. /**
  332. * Fires when the 'archived' status is removed from a blog.
  333. *
  334. * @since MU (3.0.0)
  335. *
  336. * @param int $blog_id Blog ID.
  337. */
  338. do_action( 'unarchive_blog', $blog_id );
  339. }
  340. }
  341. // If deleted status changed, issue actions.
  342. if ( $details['deleted'] != $current_details['deleted'] ) {
  343. if ( $details['deleted'] == 1 ) {
  344. /**
  345. * Fires when the 'deleted' status is added to a blog.
  346. *
  347. * @since 3.5.0
  348. *
  349. * @param int $blog_id Blog ID.
  350. */
  351. do_action( 'make_delete_blog', $blog_id );
  352. } else {
  353. /**
  354. * Fires when the 'deleted' status is removed from a blog.
  355. *
  356. * @since 3.5.0
  357. *
  358. * @param int $blog_id Blog ID.
  359. */
  360. do_action( 'make_undelete_blog', $blog_id );
  361. }
  362. }
  363. if ( isset( $details['public'] ) ) {
  364. switch_to_blog( $blog_id );
  365. update_option( 'blog_public', $details['public'] );
  366. restore_current_blog();
  367. }
  368. clean_blog_cache( $blog_id );
  369. return true;
  370. }
  371. /**
  372. * Clean the blog cache
  373. *
  374. * @since 3.5.0
  375. *
  376. * @global bool $_wp_suspend_cache_invalidation
  377. *
  378. * @param WP_Site|int $blog The site object or ID to be cleared from cache.
  379. */
  380. function clean_blog_cache( $blog ) {
  381. global $_wp_suspend_cache_invalidation;
  382. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  383. return;
  384. }
  385. if ( empty( $blog ) ) {
  386. return;
  387. }
  388. $blog_id = $blog;
  389. $blog = get_site( $blog_id );
  390. if ( ! $blog ) {
  391. if ( ! is_numeric( $blog_id ) ) {
  392. return;
  393. }
  394. // Make sure a WP_Site object exists even when the site has been deleted.
  395. $blog = new WP_Site( (object) array(
  396. 'blog_id' => $blog_id,
  397. 'domain' => null,
  398. 'path' => null,
  399. ) );
  400. }
  401. $blog_id = $blog->blog_id;
  402. $domain_path_key = md5( $blog->domain . $blog->path );
  403. wp_cache_delete( $blog_id, 'sites' );
  404. wp_cache_delete( $blog_id, 'site-details' );
  405. wp_cache_delete( $blog_id, 'blog-details' );
  406. wp_cache_delete( $blog_id . 'short' , 'blog-details' );
  407. wp_cache_delete( $domain_path_key, 'blog-lookup' );
  408. wp_cache_delete( $domain_path_key, 'blog-id-cache' );
  409. wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
  410. wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
  411. /**
  412. * Fires immediately after a site has been removed from the object cache.
  413. *
  414. * @since 4.6.0
  415. *
  416. * @param int $id Blog ID.
  417. * @param WP_Site $blog Site object.
  418. * @param string $domain_path_key md5 hash of domain and path.
  419. */
  420. do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
  421. wp_cache_set( 'last_changed', microtime(), 'sites' );
  422. /**
  423. * Fires after the blog details cache is cleared.
  424. *
  425. * @since 3.4.0
  426. * @deprecated 4.9.0 Use clean_site_cache
  427. *
  428. * @param int $blog_id Blog ID.
  429. */
  430. do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
  431. }
  432. /**
  433. * Cleans the site details cache for a site.
  434. *
  435. * @since 4.7.4
  436. *
  437. * @param int $site_id Optional. Site ID. Default is the current site ID.
  438. */
  439. function clean_site_details_cache( $site_id = 0 ) {
  440. $site_id = (int) $site_id;
  441. if ( ! $site_id ) {
  442. $site_id = get_current_blog_id();
  443. }
  444. wp_cache_delete( $site_id, 'site-details' );
  445. wp_cache_delete( $site_id, 'blog-details' );
  446. }
  447. /**
  448. * Retrieves site data given a site ID or site object.
  449. *
  450. * Site data will be cached and returned after being passed through a filter.
  451. * If the provided site is empty, the current site global will be used.
  452. *
  453. * @since 4.6.0
  454. *
  455. * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
  456. * @return WP_Site|null The site object or null if not found.
  457. */
  458. function get_site( $site = null ) {
  459. if ( empty( $site ) ) {
  460. $site = get_current_blog_id();
  461. }
  462. if ( $site instanceof WP_Site ) {
  463. $_site = $site;
  464. } elseif ( is_object( $site ) ) {
  465. $_site = new WP_Site( $site );
  466. } else {
  467. $_site = WP_Site::get_instance( $site );
  468. }
  469. if ( ! $_site ) {
  470. return null;
  471. }
  472. /**
  473. * Fires after a site is retrieved.
  474. *
  475. * @since 4.6.0
  476. *
  477. * @param WP_Site $_site Site data.
  478. */
  479. $_site = apply_filters( 'get_site', $_site );
  480. return $_site;
  481. }
  482. /**
  483. * Adds any sites from the given ids to the cache that do not already exist in cache.
  484. *
  485. * @since 4.6.0
  486. * @access private
  487. *
  488. * @see update_site_cache()
  489. * @global wpdb $wpdb WordPress database abstraction object.
  490. *
  491. * @param array $ids ID list.
  492. */
  493. function _prime_site_caches( $ids ) {
  494. global $wpdb;
  495. $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
  496. if ( ! empty( $non_cached_ids ) ) {
  497. $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
  498. update_site_cache( $fresh_sites );
  499. }
  500. }
  501. /**
  502. * Updates sites in cache.
  503. *
  504. * @since 4.6.0
  505. *
  506. * @param array $sites Array of site objects.
  507. */
  508. function update_site_cache( $sites ) {
  509. if ( ! $sites ) {
  510. return;
  511. }
  512. foreach ( $sites as $site ) {
  513. wp_cache_add( $site->blog_id, $site, 'sites' );
  514. wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
  515. }
  516. }
  517. /**
  518. * Retrieves a list of sites matching requested arguments.
  519. *
  520. * @since 4.6.0
  521. * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
  522. *
  523. * @see WP_Site_Query::parse_query()
  524. *
  525. * @param string|array $args {
  526. * Optional. Array or query string of site query parameters. Default empty.
  527. *
  528. * @type array $site__in Array of site IDs to include. Default empty.
  529. * @type array $site__not_in Array of site IDs to exclude. Default empty.
  530. * @type bool $count Whether to return a site count (true) or array of site objects.
  531. * Default false.
  532. * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query.
  533. * Default null.
  534. * @type string $fields Site fields to return. Accepts 'ids' (returns an array of site IDs)
  535. * or empty (returns an array of complete site objects). Default empty.
  536. * @type int $ID A site ID to only return that site. Default empty.
  537. * @type int $number Maximum number of sites to retrieve. Default 100.
  538. * @type int $offset Number of sites to offset the query. Used to build LIMIT clause.
  539. * Default 0.
  540. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
  541. * @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'path',
  542. * 'network_id', 'last_updated', 'registered', 'domain_length',
  543. * 'path_length', 'site__in' and 'network__in'. Also accepts false,
  544. * an empty array, or 'none' to disable `ORDER BY` clause.
  545. * Default 'id'.
  546. * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
  547. * @type int $network_id Limit results to those affiliated with a given network ID. If 0,
  548. * include all networks. Default 0.
  549. * @type array $network__in Array of network IDs to include affiliated sites for. Default empty.
  550. * @type array $network__not_in Array of network IDs to exclude affiliated sites for. Default empty.
  551. * @type string $domain Limit results to those affiliated with a given domain. Default empty.
  552. * @type array $domain__in Array of domains to include affiliated sites for. Default empty.
  553. * @type array $domain__not_in Array of domains to exclude affiliated sites for. Default empty.
  554. * @type string $path Limit results to those affiliated with a given path. Default empty.
  555. * @type array $path__in Array of paths to include affiliated sites for. Default empty.
  556. * @type array $path__not_in Array of paths to exclude affiliated sites for. Default empty.
  557. * @type int $public Limit results to public sites. Accepts '1' or '0'. Default empty.
  558. * @type int $archived Limit results to archived sites. Accepts '1' or '0'. Default empty.
  559. * @type int $mature Limit results to mature sites. Accepts '1' or '0'. Default empty.
  560. * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty.
  561. * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty.
  562. * @type int $lang_id Limit results to a language ID. Default empty.
  563. * @type array $lang__in Array of language IDs to include affiliated sites for. Default empty.
  564. * @type array $lang__not_in Array of language IDs to exclude affiliated sites for. Default empty.
  565. * @type string $search Search term(s) to retrieve matching sites for. Default empty.
  566. * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'.
  567. * Default empty array.
  568. * @type bool $update_site_cache Whether to prime the cache for found sites. Default true.
  569. * }
  570. * @return array|int List of WP_Site objects, a list of site ids when 'fields' is set to 'ids',
  571. * or the number of sites when 'count' is passed as a query var.
  572. */
  573. function get_sites( $args = array() ) {
  574. $query = new WP_Site_Query();
  575. return $query->query( $args );
  576. }
  577. /**
  578. * Retrieve option value for a given blog id based on name of option.
  579. *
  580. * If the option does not exist or does not have a value, then the return value
  581. * will be false. This is useful to check whether you need to install an option
  582. * and is commonly used during installation of plugin options and to test
  583. * whether upgrading is required.
  584. *
  585. * If the option was serialized then it will be unserialized when it is returned.
  586. *
  587. * @since MU (3.0.0)
  588. *
  589. * @param int $id A blog ID. Can be null to refer to the current blog.
  590. * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  591. * @param mixed $default Optional. Default value to return if the option does not exist.
  592. * @return mixed Value set for the option.
  593. */
  594. function get_blog_option( $id, $option, $default = false ) {
  595. $id = (int) $id;
  596. if ( empty( $id ) )
  597. $id = get_current_blog_id();
  598. if ( get_current_blog_id() == $id )
  599. return get_option( $option, $default );
  600. switch_to_blog( $id );
  601. $value = get_option( $option, $default );
  602. restore_current_blog();
  603. /**
  604. * Filters a blog option value.
  605. *
  606. * The dynamic portion of the hook name, `$option`, refers to the blog option name.
  607. *
  608. * @since 3.5.0
  609. *
  610. * @param string $value The option value.
  611. * @param int $id Blog ID.
  612. */
  613. return apply_filters( "blog_option_{$option}", $value, $id );
  614. }
  615. /**
  616. * Add a new option for a given blog id.
  617. *
  618. * You do not need to serialize values. If the value needs to be serialized, then
  619. * it will be serialized before it is inserted into the database. Remember,
  620. * resources can not be serialized or added as an option.
  621. *
  622. * You can create options without values and then update the values later.
  623. * Existing options will not be updated and checks are performed to ensure that you
  624. * aren't adding a protected WordPress option. Care should be taken to not name
  625. * options the same as the ones which are protected.
  626. *
  627. * @since MU (3.0.0)
  628. *
  629. * @param int $id A blog ID. Can be null to refer to the current blog.
  630. * @param string $option Name of option to add. Expected to not be SQL-escaped.
  631. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  632. * @return bool False if option was not added and true if option was added.
  633. */
  634. function add_blog_option( $id, $option, $value ) {
  635. $id = (int) $id;
  636. if ( empty( $id ) )
  637. $id = get_current_blog_id();
  638. if ( get_current_blog_id() == $id )
  639. return add_option( $option, $value );
  640. switch_to_blog( $id );
  641. $return = add_option( $option, $value );
  642. restore_current_blog();
  643. return $return;
  644. }
  645. /**
  646. * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
  647. *
  648. * @since MU (3.0.0)
  649. *
  650. * @param int $id A blog ID. Can be null to refer to the current blog.
  651. * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  652. * @return bool True, if option is successfully deleted. False on failure.
  653. */
  654. function delete_blog_option( $id, $option ) {
  655. $id = (int) $id;
  656. if ( empty( $id ) )
  657. $id = get_current_blog_id();
  658. if ( get_current_blog_id() == $id )
  659. return delete_option( $option );
  660. switch_to_blog( $id );
  661. $return = delete_option( $option );
  662. restore_current_blog();
  663. return $return;
  664. }
  665. /**
  666. * Update an option for a particular blog.
  667. *
  668. * @since MU (3.0.0)
  669. *
  670. * @param int $id The blog id.
  671. * @param string $option The option key.
  672. * @param mixed $value The option value.
  673. * @param mixed $deprecated Not used.
  674. * @return bool True on success, false on failure.
  675. */
  676. function update_blog_option( $id, $option, $value, $deprecated = null ) {
  677. $id = (int) $id;
  678. if ( null !== $deprecated )
  679. _deprecated_argument( __FUNCTION__, '3.1.0' );
  680. if ( get_current_blog_id() == $id )
  681. return update_option( $option, $value );
  682. switch_to_blog( $id );
  683. $return = update_option( $option, $value );
  684. restore_current_blog();
  685. return $return;
  686. }
  687. /**
  688. * Switch the current blog.
  689. *
  690. * This function is useful if you need to pull posts, or other information,
  691. * from other blogs. You can switch back afterwards using restore_current_blog().
  692. *
  693. * Things that aren't switched:
  694. * - plugins. See #14941
  695. *
  696. * @see restore_current_blog()
  697. * @since MU (3.0.0)
  698. *
  699. * @global wpdb $wpdb
  700. * @global int $blog_id
  701. * @global array $_wp_switched_stack
  702. * @global bool $switched
  703. * @global string $table_prefix
  704. * @global WP_Object_Cache $wp_object_cache
  705. *
  706. * @param int $new_blog The id of the blog you want to switch to. Default: current blog
  707. * @param bool $deprecated Deprecated argument
  708. * @return true Always returns True.
  709. */
  710. function switch_to_blog( $new_blog, $deprecated = null ) {
  711. global $wpdb;
  712. $blog_id = get_current_blog_id();
  713. if ( empty( $new_blog ) ) {
  714. $new_blog = $blog_id;
  715. }
  716. $GLOBALS['_wp_switched_stack'][] = $blog_id;
  717. /*
  718. * If we're switching to the same blog id that we're on,
  719. * set the right vars, do the associated actions, but skip
  720. * the extra unnecessary work
  721. */
  722. if ( $new_blog == $blog_id ) {
  723. /**
  724. * Fires when the blog is switched.
  725. *
  726. * @since MU (3.0.0)
  727. *
  728. * @param int $new_blog New blog ID.
  729. * @param int $new_blog Blog ID.
  730. */
  731. do_action( 'switch_blog', $new_blog, $new_blog );
  732. $GLOBALS['switched'] = true;
  733. return true;
  734. }
  735. $wpdb->set_blog_id( $new_blog );
  736. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  737. $prev_blog_id = $blog_id;
  738. $GLOBALS['blog_id'] = $new_blog;
  739. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  740. wp_cache_switch_to_blog( $new_blog );
  741. } else {
  742. global $wp_object_cache;
  743. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  744. $global_groups = $wp_object_cache->global_groups;
  745. } else {
  746. $global_groups = false;
  747. }
  748. wp_cache_init();
  749. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  750. if ( is_array( $global_groups ) ) {
  751. wp_cache_add_global_groups( $global_groups );
  752. } else {
  753. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
  754. }
  755. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  756. }
  757. }
  758. /** This filter is documented in wp-includes/ms-blogs.php */
  759. do_action( 'switch_blog', $new_blog, $prev_blog_id );
  760. $GLOBALS['switched'] = true;
  761. return true;
  762. }
  763. /**
  764. * Restore the current blog, after calling switch_to_blog()
  765. *
  766. * @see switch_to_blog()
  767. * @since MU (3.0.0)
  768. *
  769. * @global wpdb $wpdb
  770. * @global array $_wp_switched_stack
  771. * @global int $blog_id
  772. * @global bool $switched
  773. * @global string $table_prefix
  774. * @global WP_Object_Cache $wp_object_cache
  775. *
  776. * @return bool True on success, false if we're already on the current blog
  777. */
  778. function restore_current_blog() {
  779. global $wpdb;
  780. if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
  781. return false;
  782. }
  783. $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
  784. $blog_id = get_current_blog_id();
  785. if ( $blog_id == $blog ) {
  786. /** This filter is documented in wp-includes/ms-blogs.php */
  787. do_action( 'switch_blog', $blog, $blog );
  788. // If we still have items in the switched stack, consider ourselves still 'switched'
  789. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  790. return true;
  791. }
  792. $wpdb->set_blog_id( $blog );
  793. $prev_blog_id = $blog_id;
  794. $GLOBALS['blog_id'] = $blog;
  795. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
  796. if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
  797. wp_cache_switch_to_blog( $blog );
  798. } else {
  799. global $wp_object_cache;
  800. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
  801. $global_groups = $wp_object_cache->global_groups;
  802. } else {
  803. $global_groups = false;
  804. }
  805. wp_cache_init();
  806. if ( function_exists( 'wp_cache_add_global_groups' ) ) {
  807. if ( is_array( $global_groups ) ) {
  808. wp_cache_add_global_groups( $global_groups );
  809. } else {
  810. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
  811. }
  812. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
  813. }
  814. }
  815. /** This filter is documented in wp-includes/ms-blogs.php */
  816. do_action( 'switch_blog', $blog, $prev_blog_id );
  817. // If we still have items in the switched stack, consider ourselves still 'switched'
  818. $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
  819. return true;
  820. }
  821. /**
  822. * Switches the initialized roles and current user capabilities to another site.
  823. *
  824. * @since 4.9.0
  825. *
  826. * @param int $new_site_id New site ID.
  827. * @param int $old_site_id Old site ID.
  828. */
  829. function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
  830. if ( $new_site_id == $old_site_id ) {
  831. return;
  832. }
  833. if ( ! did_action( 'init' ) ) {
  834. return;
  835. }
  836. wp_roles()->for_site( $new_site_id );
  837. wp_get_current_user()->for_site( $new_site_id );
  838. }
  839. /**
  840. * Determines if switch_to_blog() is in effect
  841. *
  842. * @since 3.5.0
  843. *
  844. * @global array $_wp_switched_stack
  845. *
  846. * @return bool True if switched, false otherwise.
  847. */
  848. function ms_is_switched() {
  849. return ! empty( $GLOBALS['_wp_switched_stack'] );
  850. }
  851. /**
  852. * Check if a particular blog is archived.
  853. *
  854. * @since MU (3.0.0)
  855. *
  856. * @param int $id The blog id
  857. * @return string Whether the blog is archived or not
  858. */
  859. function is_archived( $id ) {
  860. return get_blog_status($id, 'archived');
  861. }
  862. /**
  863. * Update the 'archived' status of a particular blog.
  864. *
  865. * @since MU (3.0.0)
  866. *
  867. * @param int $id The blog id
  868. * @param string $archived The new status
  869. * @return string $archived
  870. */
  871. function update_archived( $id, $archived ) {
  872. update_blog_status($id, 'archived', $archived);
  873. return $archived;
  874. }
  875. /**
  876. * Update a blog details field.
  877. *
  878. * @since MU (3.0.0)
  879. *
  880. * @global wpdb $wpdb WordPress database abstraction object.
  881. *
  882. * @param int $blog_id BLog ID
  883. * @param string $pref A field name
  884. * @param string $value Value for $pref
  885. * @param null $deprecated
  886. * @return string|false $value
  887. */
  888. function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
  889. global $wpdb;
  890. if ( null !== $deprecated )
  891. _deprecated_argument( __FUNCTION__, '3.1.0' );
  892. if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  893. return $value;
  894. $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
  895. if ( false === $result )
  896. return false;
  897. clean_blog_cache( $blog_id );
  898. if ( 'spam' == $pref ) {
  899. if ( $value == 1 ) {
  900. /** This filter is documented in wp-includes/ms-blogs.php */
  901. do_action( 'make_spam_blog', $blog_id );
  902. } else {
  903. /** This filter is documented in wp-includes/ms-blogs.php */
  904. do_action( 'make_ham_blog', $blog_id );
  905. }
  906. } elseif ( 'mature' == $pref ) {
  907. if ( $value == 1 ) {
  908. /** This filter is documented in wp-includes/ms-blogs.php */
  909. do_action( 'mature_blog', $blog_id );
  910. } else {
  911. /** This filter is documented in wp-includes/ms-blogs.php */
  912. do_action( 'unmature_blog', $blog_id );
  913. }
  914. } elseif ( 'archived' == $pref ) {
  915. if ( $value == 1 ) {
  916. /** This filter is documented in wp-includes/ms-blogs.php */
  917. do_action( 'archive_blog', $blog_id );
  918. } else {
  919. /** This filter is documented in wp-includes/ms-blogs.php */
  920. do_action( 'unarchive_blog', $blog_id );
  921. }
  922. } elseif ( 'deleted' == $pref ) {
  923. if ( $value == 1 ) {
  924. /** This filter is documented in wp-includes/ms-blogs.php */
  925. do_action( 'make_delete_blog', $blog_id );
  926. } else {
  927. /** This filter is documented in wp-includes/ms-blogs.php */
  928. do_action( 'make_undelete_blog', $blog_id );
  929. }
  930. } elseif ( 'public' == $pref ) {
  931. /**
  932. * Fires after the current blog's 'public' setting is updated.
  933. *
  934. * @since MU (3.0.0)
  935. *
  936. * @param int $blog_id Blog ID.
  937. * @param string $value The value of blog status.
  938. */
  939. do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
  940. }
  941. return $value;
  942. }
  943. /**
  944. * Get a blog details field.
  945. *
  946. * @since MU (3.0.0)
  947. *
  948. * @global wpdb $wpdb WordPress database abstraction object.
  949. *
  950. * @param int $id The blog id
  951. * @param string $pref A field name
  952. * @return bool|string|null $value
  953. */
  954. function get_blog_status( $id, $pref ) {
  955. global $wpdb;
  956. $details = get_site( $id );
  957. if ( $details )
  958. return $details->$pref;
  959. return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
  960. }
  961. /**
  962. * Get a list of most recently updated blogs.
  963. *
  964. * @since MU (3.0.0)
  965. *
  966. * @global wpdb $wpdb WordPress database abstraction object.
  967. *
  968. * @param mixed $deprecated Not used
  969. * @param int $start The offset
  970. * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
  971. * @return array The list of blogs
  972. */
  973. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  974. global $wpdb;
  975. if ( ! empty( $deprecated ) )
  976. _deprecated_argument( __FUNCTION__, 'MU' ); // never used
  977. return $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", get_current_network_id(), $start, $quantity ), ARRAY_A );
  978. }
  979. /**
  980. * Retrieves a list of networks.
  981. *
  982. * @since 4.6.0
  983. *
  984. * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
  985. * for information on accepted arguments. Default empty array.
  986. * @return array|int List of WP_Network objects, a list of network ids when 'fields' is set to 'ids',
  987. * or the number of networks when 'count' is passed as a query var.
  988. */
  989. function get_networks( $args = array() ) {
  990. $query = new WP_Network_Query();
  991. return $query->query( $args );
  992. }
  993. /**
  994. * Retrieves network data given a network ID or network object.
  995. *
  996. * Network data will be cached and returned after being passed through a filter.
  997. * If the provided network is empty, the current network global will be used.
  998. *
  999. * @since 4.6.0
  1000. *
  1001. * @global WP_Network $current_site
  1002. *
  1003. * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
  1004. * @return WP_Network|null The network object or null if not found.
  1005. */
  1006. function get_network( $network = null ) {
  1007. global $current_site;
  1008. if ( empty( $network ) && isset( $current_site ) ) {
  1009. $network = $current_site;
  1010. }
  1011. if ( $network instanceof WP_Network ) {
  1012. $_network = $network;
  1013. } elseif ( is_object( $network ) ) {
  1014. $_network = new WP_Network( $network );
  1015. } else {
  1016. $_network = WP_Network::get_instance( $network );
  1017. }
  1018. if ( ! $_network ) {
  1019. return null;
  1020. }
  1021. /**
  1022. * Fires after a network is retrieved.
  1023. *
  1024. * @since 4.6.0
  1025. *
  1026. * @param WP_Network $_network Network data.
  1027. */
  1028. $_network = apply_filters( 'get_network', $_network );
  1029. return $_network;
  1030. }
  1031. /**
  1032. * Removes a network from the object cache.
  1033. *
  1034. * @since 4.6.0
  1035. *
  1036. * @global bool $_wp_suspend_cache_invalidation
  1037. *
  1038. * @param int|array $ids Network ID or an array of network IDs to remove from cache.
  1039. */
  1040. function clean_network_cache( $ids ) {
  1041. global $_wp_suspend_cache_invalidation;
  1042. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  1043. return;
  1044. }
  1045. foreach ( (array) $ids as $id ) {
  1046. wp_cache_delete( $id, 'networks' );
  1047. /**
  1048. * Fires immediately after a network has been removed from the object cache.
  1049. *
  1050. * @since 4.6.0
  1051. *
  1052. * @param int $id Network ID.
  1053. */
  1054. do_action( 'clean_network_cache', $id );
  1055. }
  1056. wp_cache_set( 'last_changed', microtime(), 'networks' );
  1057. }
  1058. /**
  1059. * Updates the network cache of given networks.
  1060. *
  1061. * Will add the networks in $networks to the cache. If network ID already exists
  1062. * in the network cache then it will not be updated. The network is added to the
  1063. * cache using the network group with the key using the ID of the networks.
  1064. *
  1065. * @since 4.6.0
  1066. *
  1067. * @param array $networks Array of network row objects.
  1068. */
  1069. function update_network_cache( $networks ) {
  1070. foreach ( (array) $networks as $network ) {
  1071. wp_cache_add( $network->id, $network, 'networks' );
  1072. }
  1073. }
  1074. /**
  1075. * Adds any networks from the given IDs to the cache that do not already exist in cache.
  1076. *
  1077. * @since 4.6.0
  1078. * @access private
  1079. *
  1080. * @see update_network_cache()
  1081. * @global wpdb $wpdb WordPress database abstraction object.
  1082. *
  1083. * @param array $network_ids Array of network IDs.
  1084. */
  1085. function _prime_network_caches( $network_ids ) {
  1086. global $wpdb;
  1087. $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
  1088. if ( !empty( $non_cached_ids ) ) {
  1089. $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
  1090. update_network_cache( $fresh_networks );
  1091. }
  1092. }
  1093. /**
  1094. * Handler for updating the site's last updated date when a post is published or
  1095. * an already published post is changed.
  1096. *
  1097. * @since 3.3.0
  1098. *
  1099. * @param string $new_status The new post status
  1100. * @param string $old_status The old post status
  1101. * @param object $post Post object
  1102. */
  1103. function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
  1104. $post_type_obj = get_post_type_object( $post->post_type );
  1105. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  1106. return;
  1107. }
  1108. if ( 'publish' != $new_status && 'publish' != $old_status ) {
  1109. return;
  1110. }
  1111. // Post was freshly published, published post was saved, or published post was unpublished.
  1112. wpmu_update_blogs_date();
  1113. }
  1114. /**
  1115. * Handler for updating the current site's last updated date when a published
  1116. * post is deleted.
  1117. *
  1118. * @since 3.4.0
  1119. *
  1120. * @param int $post_id Post ID
  1121. */
  1122. function _update_blog_date_on_post_delete( $post_id ) {
  1123. $post = get_post( $post_id );
  1124. $post_type_obj = get_post_type_object( $post->post_type );
  1125. if ( ! $post_type_obj || ! $post_type_obj->public ) {
  1126. return;
  1127. }
  1128. if ( 'publish' != $post->post_status ) {
  1129. return;
  1130. }
  1131. wpmu_update_blogs_date();
  1132. }
  1133. /**
  1134. * Handler for updating the current site's posts count when a post is deleted.
  1135. *
  1136. * @since 4.0.0
  1137. *
  1138. * @param int $post_id Post ID.
  1139. */
  1140. function _update_posts_count_on_delete( $post_id ) {
  1141. $post = get_post( $post_id );
  1142. if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
  1143. return;
  1144. }
  1145. update_posts_count();
  1146. }
  1147. /**
  1148. * Handler for updating the current site's posts count when a post status changes.
  1149. *
  1150. * @since 4.0.0
  1151. * @since 4.9.0 Added the `$post` parameter.
  1152. *
  1153. * @param string $new_status The status the post is changing to.
  1154. * @param string $old_status The status the post is changing from.
  1155. * @param WP_Post $post Post object
  1156. */
  1157. function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
  1158. if ( $new_status === $old_status ) {
  1159. return;
  1160. }
  1161. if ( 'post' !== get_post_type( $post ) ) {
  1162. return;
  1163. }
  1164. if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
  1165. return;
  1166. }
  1167. update_posts_count();
  1168. }