Code Duplication    Length = 79-81 lines in 2 locations

src/wp-includes/class-wp-network-query.php 1 location

@@ 198-276 (lines=79) @@
195
	 *
196
	 * @return int|array The list of networks.
197
	 */
198
	public function get_networks() {
199
		$this->parse_query();
200
201
		/**
202
		 * Fires before networks are retrieved.
203
		 *
204
		 * @since 4.6.0
205
		 *
206
		 * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference.
207
		 */
208
		do_action_ref_array( 'pre_get_networks', array( &$this ) );
209
210
		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
211
		$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
212
		$last_changed = wp_cache_get_last_changed( 'networks' );
213
214
		$cache_key = "get_network_ids:$key:$last_changed";
215
		$cache_value = wp_cache_get( $cache_key, 'networks' );
216
217
		if ( false === $cache_value ) {
218
			$network_ids = $this->get_network_ids();
219
			if ( $network_ids ) {
220
				$this->set_found_networks();
221
			}
222
223
			$cache_value = array(
224
				'network_ids' => $network_ids,
225
				'found_networks' => $this->found_networks,
226
			);
227
			wp_cache_add( $cache_key, $cache_value, 'networks' );
228
		} else {
229
			$network_ids = $cache_value['network_ids'];
230
			$this->found_networks = $cache_value['found_networks'];
231
		}
232
233
		if ( $this->found_networks && $this->query_vars['number'] ) {
234
			$this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
235
		}
236
237
		// If querying for a count only, there's nothing more to do.
238
		if ( $this->query_vars['count'] ) {
239
			// $network_ids is actually a count in this case.
240
			return intval( $network_ids );
241
		}
242
243
		$network_ids = array_map( 'intval', $network_ids );
244
245
		if ( 'ids' == $this->query_vars['fields'] ) {
246
			$this->networks = $network_ids;
247
			return $this->networks;
248
		}
249
250
		if ( $this->query_vars['update_network_cache'] ) {
251
			_prime_network_caches( $network_ids );
252
		}
253
254
		// Fetch full network objects from the primed cache.
255
		$_networks = array();
256
		foreach ( $network_ids as $network_id ) {
257
			if ( $_network = get_network( $network_id ) ) {
258
				$_networks[] = $_network;
259
			}
260
		}
261
262
		/**
263
		 * Filters the network query results.
264
		 *
265
		 * @since 4.6.0
266
		 *
267
		 * @param array            $results  An array of networks.
268
		 * @param WP_Network_Query &$this    Current instance of WP_Network_Query, passed by reference.
269
		 */
270
		$_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
271
272
		// Convert to WP_Network instances
273
		$this->networks = array_map( 'get_network', $_networks );
274
275
		return $this->networks;
276
	}
277
278
	/**
279
	 * Used internally to get a list of network IDs matching the query vars.

src/wp-includes/class-wp-site-query.php 1 location

@@ 241-321 (lines=81) @@
238
	 *
239
	 * @return array|int List of sites, or number of sites when 'count' is passed as a query var.
240
	 */
241
	public function get_sites() {
242
		$this->parse_query();
243
244
		/**
245
		 * Fires before sites are retrieved.
246
		 *
247
		 * @since 4.6.0
248
		 *
249
		 * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference.
250
		 */
251
		do_action_ref_array( 'pre_get_sites', array( &$this ) );
252
253
		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
254
		$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
255
		$last_changed = wp_cache_get_last_changed( 'sites' );
256
257
		$cache_key = "get_sites:$key:$last_changed";
258
		$cache_value = wp_cache_get( $cache_key, 'sites' );
259
260
		if ( false === $cache_value ) {
261
			$site_ids = $this->get_site_ids();
262
			if ( $site_ids ) {
263
				$this->set_found_sites();
264
			}
265
266
			$cache_value = array(
267
				'site_ids' => $site_ids,
268
				'found_sites' => $this->found_sites,
269
			);
270
			wp_cache_add( $cache_key, $cache_value, 'sites' );
271
		} else {
272
			$site_ids = $cache_value['site_ids'];
273
			$this->found_sites = $cache_value['found_sites'];
274
		}
275
276
		if ( $this->found_sites && $this->query_vars['number'] ) {
277
			$this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
278
		}
279
280
		// If querying for a count only, there's nothing more to do.
281
		if ( $this->query_vars['count'] ) {
282
			// $site_ids is actually a count in this case.
283
			return intval( $site_ids );
284
		}
285
286
		$site_ids = array_map( 'intval', $site_ids );
287
288
		if ( 'ids' == $this->query_vars['fields'] ) {
289
			$this->sites = $site_ids;
290
291
			return $this->sites;
292
		}
293
294
		// Prime site network caches.
295
		if ( $this->query_vars['update_site_cache'] ) {
296
			_prime_site_caches( $site_ids );
297
		}
298
299
		// Fetch full site objects from the primed cache.
300
		$_sites = array();
301
		foreach ( $site_ids as $site_id ) {
302
			if ( $_site = get_site( $site_id ) ) {
303
				$_sites[] = $_site;
304
			}
305
		}
306
307
		/**
308
		 * Filters the site query results.
309
		 *
310
		 * @since 4.6.0
311
		 *
312
		 * @param array         $results An array of sites.
313
		 * @param WP_Site_Query &$this   Current instance of WP_Site_Query, passed by reference.
314
		 */
315
		$_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
316
317
		// Convert to WP_Site instances.
318
		$this->sites = array_map( 'get_site', $_sites );
319
320
		return $this->sites;
321
	}
322
323
	/**
324
	 * Used internally to get a list of site IDs matching the query vars.