ms-network.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Network API
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 5.1.0
  8. */
  9. /**
  10. * Retrieves network data given a network ID or network object.
  11. *
  12. * Network data will be cached and returned after being passed through a filter.
  13. * If the provided network is empty, the current network global will be used.
  14. *
  15. * @since 4.6.0
  16. *
  17. * @global WP_Network $current_site
  18. *
  19. * @param WP_Network|int|null $network Optional. Network to retrieve. Default is the current network.
  20. * @return WP_Network|null The network object or null if not found.
  21. */
  22. function get_network( $network = null ) {
  23. global $current_site;
  24. if ( empty( $network ) && isset( $current_site ) ) {
  25. $network = $current_site;
  26. }
  27. if ( $network instanceof WP_Network ) {
  28. $_network = $network;
  29. } elseif ( is_object( $network ) ) {
  30. $_network = new WP_Network( $network );
  31. } else {
  32. $_network = WP_Network::get_instance( $network );
  33. }
  34. if ( ! $_network ) {
  35. return null;
  36. }
  37. /**
  38. * Fires after a network is retrieved.
  39. *
  40. * @since 4.6.0
  41. *
  42. * @param WP_Network $_network Network data.
  43. */
  44. $_network = apply_filters( 'get_network', $_network );
  45. return $_network;
  46. }
  47. /**
  48. * Retrieves a list of networks.
  49. *
  50. * @since 4.6.0
  51. *
  52. * @param string|array $args Optional. Array or string of arguments. See WP_Network_Query::parse_query()
  53. * for information on accepted arguments. Default empty array.
  54. * @return array|int List of WP_Network objects, a list of network IDs when 'fields' is set to 'ids',
  55. * or the number of networks when 'count' is passed as a query var.
  56. */
  57. function get_networks( $args = array() ) {
  58. $query = new WP_Network_Query();
  59. return $query->query( $args );
  60. }
  61. /**
  62. * Removes a network from the object cache.
  63. *
  64. * @since 4.6.0
  65. *
  66. * @global bool $_wp_suspend_cache_invalidation
  67. *
  68. * @param int|array $ids Network ID or an array of network IDs to remove from cache.
  69. */
  70. function clean_network_cache( $ids ) {
  71. global $_wp_suspend_cache_invalidation;
  72. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  73. return;
  74. }
  75. $network_ids = (array) $ids;
  76. wp_cache_delete_multiple( $network_ids, 'networks' );
  77. foreach ( $network_ids as $id ) {
  78. /**
  79. * Fires immediately after a network has been removed from the object cache.
  80. *
  81. * @since 4.6.0
  82. *
  83. * @param int $id Network ID.
  84. */
  85. do_action( 'clean_network_cache', $id );
  86. }
  87. wp_cache_set( 'last_changed', microtime(), 'networks' );
  88. }
  89. /**
  90. * Updates the network cache of given networks.
  91. *
  92. * Will add the networks in $networks to the cache. If network ID already exists
  93. * in the network cache then it will not be updated. The network is added to the
  94. * cache using the network group with the key using the ID of the networks.
  95. *
  96. * @since 4.6.0
  97. *
  98. * @param array $networks Array of network row objects.
  99. */
  100. function update_network_cache( $networks ) {
  101. $data = array();
  102. foreach ( (array) $networks as $network ) {
  103. $data[ $network->id ] = $network;
  104. }
  105. wp_cache_add_multiple( $data, 'networks' );
  106. }
  107. /**
  108. * Adds any networks from the given IDs to the cache that do not already exist in cache.
  109. *
  110. * @since 4.6.0
  111. * @since 6.1.0 This function is no longer marked as "private".
  112. *
  113. * @see update_network_cache()
  114. * @global wpdb $wpdb WordPress database abstraction object.
  115. *
  116. * @param array $network_ids Array of network IDs.
  117. */
  118. function _prime_network_caches( $network_ids ) {
  119. global $wpdb;
  120. $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
  121. if ( ! empty( $non_cached_ids ) ) {
  122. $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  123. update_network_cache( $fresh_networks );
  124. }
  125. }