ms-network.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. foreach ( (array) $ids as $id ) {
  76. wp_cache_delete( $id, 'networks' );
  77. /**
  78. * Fires immediately after a network has been removed from the object cache.
  79. *
  80. * @since 4.6.0
  81. *
  82. * @param int $id Network ID.
  83. */
  84. do_action( 'clean_network_cache', $id );
  85. }
  86. wp_cache_set( 'last_changed', microtime(), 'networks' );
  87. }
  88. /**
  89. * Updates the network cache of given networks.
  90. *
  91. * Will add the networks in $networks to the cache. If network ID already exists
  92. * in the network cache then it will not be updated. The network is added to the
  93. * cache using the network group with the key using the ID of the networks.
  94. *
  95. * @since 4.6.0
  96. *
  97. * @param array $networks Array of network row objects.
  98. */
  99. function update_network_cache( $networks ) {
  100. foreach ( (array) $networks as $network ) {
  101. wp_cache_add( $network->id, $network, 'networks' );
  102. }
  103. }
  104. /**
  105. * Adds any networks from the given IDs to the cache that do not already exist in cache.
  106. *
  107. * @since 4.6.0
  108. * @access private
  109. *
  110. * @see update_network_cache()
  111. * @global wpdb $wpdb WordPress database abstraction object.
  112. *
  113. * @param array $network_ids Array of network IDs.
  114. */
  115. function _prime_network_caches( $network_ids ) {
  116. global $wpdb;
  117. $non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
  118. if ( ! empty( $non_cached_ids ) ) {
  119. $fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  120. update_network_cache( $fresh_networks );
  121. }
  122. }