[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> ms-network.php (source)

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


Generated : Sat Sep 26 08:20:30 2026 Cross-referenced by PHPXref