WP_Network_Query

Core class used for querying networks.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-network-query.php  
  1. class WP_Network_Query { 
  2.  
  3. /** 
  4. * SQL for database query. 
  5. * @since 4.6.0 
  6. * @access public 
  7. * @var string 
  8. */ 
  9. public $request; 
  10.  
  11. /** 
  12. * SQL query clauses. 
  13. * @since 4.6.0 
  14. * @access protected 
  15. * @var array 
  16. */ 
  17. protected $sql_clauses = array( 
  18. 'select' => '',  
  19. 'from' => '',  
  20. 'where' => array(),  
  21. 'groupby' => '',  
  22. 'orderby' => '',  
  23. 'limits' => '',  
  24. ); 
  25.  
  26. /** 
  27. * Query vars set by the user. 
  28. * @since 4.6.0 
  29. * @access public 
  30. * @var array 
  31. */ 
  32. public $query_vars; 
  33.  
  34. /** 
  35. * Default values for query vars. 
  36. * @since 4.6.0 
  37. * @access public 
  38. * @var array 
  39. */ 
  40. public $query_var_defaults; 
  41.  
  42. /** 
  43. * List of networks located by the query. 
  44. * @since 4.6.0 
  45. * @access public 
  46. * @var array 
  47. */ 
  48. public $networks; 
  49.  
  50. /** 
  51. * The amount of found networks for the current query. 
  52. * @since 4.6.0 
  53. * @access public 
  54. * @var int 
  55. */ 
  56. public $found_networks = 0; 
  57.  
  58. /** 
  59. * The number of pages. 
  60. * @since 4.6.0 
  61. * @access public 
  62. * @var int 
  63. */ 
  64. public $max_num_pages = 0; 
  65.  
  66. /** 
  67. * Constructor. 
  68. * Sets up the network query, based on the query vars passed. 
  69. * @since 4.6.0 
  70. * @access public 
  71. * @param string|array $query { 
  72. * Optional. Array or query string of network query parameters. Default empty. 
  73. * @type array $network__in Array of network IDs to include. Default empty. 
  74. * @type array $network__not_in Array of network IDs to exclude. Default empty. 
  75. * @type bool $count Whether to return a network count (true) or array of network objects. 
  76. * Default false. 
  77. * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs) 
  78. * or empty (returns an array of complete network objects). Default empty. 
  79. * @type int $number Maximum number of networks to retrieve. Default null (no limit). 
  80. * @type int $offset Number of networks to offset the query. Used to build LIMIT clause. 
  81. * Default 0. 
  82. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. 
  83. * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path',  
  84. * 'domain_length', 'path_length' and 'network__in'. Also accepts false,  
  85. * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'. 
  86. * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'. 
  87. * @type string $domain Limit results to those affiliated with a given network ID. 
  88. * Default current network ID. 
  89. * @type array $domain__in Array of domains to include affiliated networks for. Default empty. 
  90. * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty. 
  91. * @type string $path Limit results to those affiliated with a given network ID. 
  92. * Default current network ID. 
  93. * @type array $path__in Array of paths to include affiliated networks for. Default empty. 
  94. * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty. 
  95. * @type string $search Search term(s) to retrieve matching networks for. Default empty. 
  96. * @type bool $update_network_cache Whether to prime the cache for found networks. Default true. 
  97. * } 
  98. */ 
  99. public function __construct( $query = '' ) { 
  100. $this->query_var_defaults = array( 
  101. 'network__in' => '',  
  102. 'network__not_in' => '',  
  103. 'count' => false,  
  104. 'fields' => '',  
  105. 'number' => '',  
  106. 'offset' => '',  
  107. 'no_found_rows' => true,  
  108. 'orderby' => 'id',  
  109. 'order' => 'ASC',  
  110. 'domain' => '',  
  111. 'domain__in' => '',  
  112. 'domain__not_in' => '',  
  113. 'path' => '',  
  114. 'path__in' => '',  
  115. 'path__not_in' => '',  
  116. 'search' => '',  
  117. 'update_network_cache' => true,  
  118. ); 
  119.  
  120. if ( ! empty( $query ) ) { 
  121. $this->query( $query ); 
  122.  
  123. /** 
  124. * Parses arguments passed to the network query with default query parameters. 
  125. * @since 4.6.0 
  126. * @access public 
  127. * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct() 
  128. */ 
  129. public function parse_query( $query = '' ) { 
  130. if ( empty( $query ) ) { 
  131. $query = $this->query_vars; 
  132.  
  133. $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); 
  134.  
  135. /** 
  136. * Fires after the network query vars have been parsed. 
  137. * @since 4.6.0 
  138. * @param WP_Network_Query &$this The WP_Network_Query instance (passed by reference). 
  139. */ 
  140. do_action_ref_array( 'parse_network_query', array( &$this ) ); 
  141.  
  142. /** 
  143. * Sets up the WordPress query for retrieving networks. 
  144. * @since 4.6.0 
  145. * @access public 
  146. * @param string|array $query Array or URL query string of parameters. 
  147. * @return array|int List of networks, or number of networks when 'count' is passed as a query var. 
  148. */ 
  149. public function query( $query ) { 
  150. $this->query_vars = wp_parse_args( $query ); 
  151. return $this->get_networks(); 
  152.  
  153. /** 
  154. * Gets a list of networks matching the query vars. 
  155. * @since 4.6.0 
  156. * @access public 
  157. * @return int|array The list of networks. 
  158. */ 
  159. public function get_networks() { 
  160. $this->parse_query(); 
  161.  
  162. /** 
  163. * Fires before networks are retrieved. 
  164. * @since 4.6.0 
  165. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. 
  166. */ 
  167. do_action_ref_array( 'pre_get_networks', array( &$this ) ); 
  168.  
  169. // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 
  170. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 
  171. $last_changed = wp_cache_get( 'last_changed', 'networks' ); 
  172. if ( ! $last_changed ) { 
  173. $last_changed = microtime(); 
  174. wp_cache_set( 'last_changed', $last_changed, 'networks' ); 
  175.  
  176. $cache_key = "get_network_ids:$key:$last_changed"; 
  177. $cache_value = wp_cache_get( $cache_key, 'networks' ); 
  178.  
  179. if ( false === $cache_value ) { 
  180. $network_ids = $this->get_network_ids(); 
  181. if ( $network_ids ) { 
  182. $this->set_found_networks(); 
  183.  
  184. $cache_value = array( 
  185. 'network_ids' => $network_ids,  
  186. 'found_networks' => $this->found_networks,  
  187. ); 
  188. wp_cache_add( $cache_key, $cache_value, 'networks' ); 
  189. } else { 
  190. $network_ids = $cache_value['network_ids']; 
  191. $this->found_networks = $cache_value['found_networks']; 
  192.  
  193. if ( $this->found_networks && $this->query_vars['number'] ) { 
  194. $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] ); 
  195.  
  196. // If querying for a count only, there's nothing more to do. 
  197. if ( $this->query_vars['count'] ) { 
  198. // $network_ids is actually a count in this case. 
  199. return intval( $network_ids ); 
  200.  
  201. $network_ids = array_map( 'intval', $network_ids ); 
  202.  
  203. if ( 'ids' == $this->query_vars['fields'] ) { 
  204. $this->networks = $network_ids; 
  205. return $this->networks; 
  206.  
  207. if ( $this->query_vars['update_network_cache'] ) { 
  208. _prime_network_caches( $network_ids ); 
  209.  
  210. // Fetch full network objects from the primed cache. 
  211. $_networks = array(); 
  212. foreach ( $network_ids as $network_id ) { 
  213. if ( $_network = get_network( $network_id ) ) { 
  214. $_networks[] = $_network; 
  215.  
  216. /** 
  217. * Filters the network query results. 
  218. * @since 4.6.0 
  219. * @param array $results An array of networks. 
  220. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. 
  221. */ 
  222. $_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) ); 
  223.  
  224. // Convert to WP_Network instances 
  225. $this->networks = array_map( 'get_network', $_networks ); 
  226.  
  227. return $this->networks; 
  228.  
  229. /** 
  230. * Used internally to get a list of network IDs matching the query vars. 
  231. * @since 4.6.0 
  232. * @access protected 
  233. * @return int|array A single count of network IDs if a count query. An array of network IDs if a full query. 
  234. */ 
  235. protected function get_network_ids() { 
  236. global $wpdb; 
  237.  
  238. $order = $this->parse_order( $this->query_vars['order'] ); 
  239.  
  240. // Disable ORDER BY with 'none', an empty array, or boolean false. 
  241. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { 
  242. $orderby = ''; 
  243. } elseif ( ! empty( $this->query_vars['orderby'] ) ) { 
  244. $ordersby = is_array( $this->query_vars['orderby'] ) ? 
  245. $this->query_vars['orderby'] : 
  246. preg_split( '/[, \s]/', $this->query_vars['orderby'] ); 
  247.  
  248. $orderby_array = array(); 
  249. foreach ( $ordersby as $_key => $_value ) { 
  250. if ( ! $_value ) { 
  251. continue; 
  252.  
  253. if ( is_int( $_key ) ) { 
  254. $_orderby = $_value; 
  255. $_order = $order; 
  256. } else { 
  257. $_orderby = $_key; 
  258. $_order = $_value; 
  259.  
  260. $parsed = $this->parse_orderby( $_orderby ); 
  261.  
  262. if ( ! $parsed ) { 
  263. continue; 
  264.  
  265. if ( 'network__in' === $_orderby ) { 
  266. $orderby_array[] = $parsed; 
  267. continue; 
  268.  
  269. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 
  270.  
  271. $orderby = implode( ', ', $orderby_array ); 
  272. } else { 
  273. $orderby = "$wpdb->site.id $order"; 
  274.  
  275. $number = absint( $this->query_vars['number'] ); 
  276. $offset = absint( $this->query_vars['offset'] ); 
  277.  
  278. if ( ! empty( $number ) ) { 
  279. if ( $offset ) { 
  280. $limits = 'LIMIT ' . $offset . ', ' . $number; 
  281. } else { 
  282. $limits = 'LIMIT ' . $number; 
  283.  
  284. if ( $this->query_vars['count'] ) { 
  285. $fields = 'COUNT(*)'; 
  286. } else { 
  287. $fields = "$wpdb->site.id"; 
  288.  
  289. // Parse network IDs for an IN clause. 
  290. if ( ! empty( $this->query_vars['network__in'] ) ) { 
  291. $this->sql_clauses['where']['network__in'] = "$wpdb->site.id IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )'; 
  292.  
  293. // Parse network IDs for a NOT IN clause. 
  294. if ( ! empty( $this->query_vars['network__not_in'] ) ) { 
  295. $this->sql_clauses['where']['network__not_in'] = "$wpdb->site.id NOT IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )'; 
  296.  
  297. if ( ! empty( $this->query_vars['domain'] ) ) { 
  298. $this->sql_clauses['where']['domain'] = $wpdb->prepare( "$wpdb->site.domain = %s", $this->query_vars['domain'] ); 
  299.  
  300. // Parse network domain for an IN clause. 
  301. if ( is_array( $this->query_vars['domain__in'] ) ) { 
  302. $this->sql_clauses['where']['domain__in'] = "$wpdb->site.domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )"; 
  303.  
  304. // Parse network domain for a NOT IN clause. 
  305. if ( is_array( $this->query_vars['domain__not_in'] ) ) { 
  306. $this->sql_clauses['where']['domain__not_in'] = "$wpdb->site.domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )"; 
  307.  
  308. if ( ! empty( $this->query_vars['path'] ) ) { 
  309. $this->sql_clauses['where']['path'] = $wpdb->prepare( "$wpdb->site.path = %s", $this->query_vars['path'] ); 
  310.  
  311. // Parse network path for an IN clause. 
  312. if ( is_array( $this->query_vars['path__in'] ) ) { 
  313. $this->sql_clauses['where']['path__in'] = "$wpdb->site.path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )"; 
  314.  
  315. // Parse network path for a NOT IN clause. 
  316. if ( is_array( $this->query_vars['path__not_in'] ) ) { 
  317. $this->sql_clauses['where']['path__not_in'] = "$wpdb->site.path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )"; 
  318.  
  319. // Falsey search strings are ignored. 
  320. if ( strlen( $this->query_vars['search'] ) ) { 
  321. $this->sql_clauses['where']['search'] = $this->get_search_sql( 
  322. $this->query_vars['search'],  
  323. array( "$wpdb->site.domain", "$wpdb->site.path" ) 
  324. ); 
  325.  
  326. $join = ''; 
  327.  
  328. $where = implode( ' AND ', $this->sql_clauses['where'] ); 
  329.  
  330. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); 
  331.  
  332. /** 
  333. * Filters the network query clauses. 
  334. * @since 4.6.0 
  335. * @param array $pieces A compacted array of network query clauses. 
  336. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference. 
  337. */ 
  338. $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) ); 
  339.  
  340. $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; 
  341. $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; 
  342. $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; 
  343. $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; 
  344. $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; 
  345. $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; 
  346.  
  347. if ( $where ) { 
  348. $where = 'WHERE ' . $where; 
  349.  
  350. if ( $groupby ) { 
  351. $groupby = 'GROUP BY ' . $groupby; 
  352.  
  353. if ( $orderby ) { 
  354. $orderby = "ORDER BY $orderby"; 
  355.  
  356. $found_rows = ''; 
  357. if ( ! $this->query_vars['no_found_rows'] ) { 
  358. $found_rows = 'SQL_CALC_FOUND_ROWS'; 
  359.  
  360. $this->sql_clauses['select'] = "SELECT $found_rows $fields"; 
  361. $this->sql_clauses['from'] = "FROM $wpdb->site $join"; 
  362. $this->sql_clauses['groupby'] = $groupby; 
  363. $this->sql_clauses['orderby'] = $orderby; 
  364. $this->sql_clauses['limits'] = $limits; 
  365.  
  366. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 
  367.  
  368. if ( $this->query_vars['count'] ) { 
  369. return intval( $wpdb->get_var( $this->request ) ); 
  370.  
  371. $network_ids = $wpdb->get_col( $this->request ); 
  372.  
  373. return array_map( 'intval', $network_ids ); 
  374.  
  375. /** 
  376. * Populates found_networks and max_num_pages properties for the current query 
  377. * if the limit clause was used. 
  378. * @since 4.6.0 
  379. * @access private 
  380. * @global wpdb $wpdb WordPress database abstraction object. 
  381. */ 
  382. private function set_found_networks() { 
  383. global $wpdb; 
  384.  
  385. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { 
  386. /** 
  387. * Filters the query used to retrieve found network count. 
  388. * @since 4.6.0 
  389. * @param string $found_networks_query SQL query. Default 'SELECT FOUND_ROWS()'. 
  390. * @param WP_Network_Query $network_query The `WP_Network_Query` instance. 
  391. */ 
  392. $found_networks_query = apply_filters( 'found_networks_query', 'SELECT FOUND_ROWS()', $this ); 
  393.  
  394. $this->found_networks = (int) $wpdb->get_var( $found_networks_query ); 
  395.  
  396. /** 
  397. * Used internally to generate an SQL string for searching across multiple columns. 
  398. * @since 4.6.0 
  399. * @access protected 
  400. * @global wpdb $wpdb WordPress database abstraction object. 
  401. * @param string $string Search string. 
  402. * @param array $columns Columns to search. 
  403. * @return string Search SQL. 
  404. */ 
  405. protected function get_search_sql( $string, $columns ) { 
  406. global $wpdb; 
  407.  
  408. $like = '%' . $wpdb->esc_like( $string ) . '%'; 
  409.  
  410. $searches = array(); 
  411. foreach ( $columns as $column ) { 
  412. $searches[] = $wpdb->prepare( "$column LIKE %s", $like ); 
  413.  
  414. return '(' . implode( ' OR ', $searches ) . ')'; 
  415.  
  416. /** 
  417. * Parses and sanitizes 'orderby' keys passed to the network query. 
  418. * @since 4.6.0 
  419. * @access protected 
  420. * @global wpdb $wpdb WordPress database abstraction object. 
  421. * @param string $orderby Alias for the field to order by. 
  422. * @return string|false Value to used in the ORDER clause. False otherwise. 
  423. */ 
  424. protected function parse_orderby( $orderby ) { 
  425. global $wpdb; 
  426.  
  427. $allowed_keys = array( 
  428. 'id',  
  429. 'domain',  
  430. 'path',  
  431. ); 
  432.  
  433. $parsed = false; 
  434. if ( $orderby == 'network__in' ) { 
  435. $network__in = implode( ', ', array_map( 'absint', $this->query_vars['network__in'] ) ); 
  436. $parsed = "FIELD( {$wpdb->site}.id, $network__in )"; 
  437. } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) { 
  438. $field = substr( $orderby, 0, -7 ); 
  439. $parsed = "CHAR_LENGTH($wpdb->site.$field)"; 
  440. } elseif ( in_array( $orderby, $allowed_keys ) ) { 
  441. $parsed = "$wpdb->site.$orderby"; 
  442.  
  443. return $parsed; 
  444.  
  445. /** 
  446. * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary. 
  447. * @since 4.6.0 
  448. * @access protected 
  449. * @param string $order The 'order' query variable. 
  450. * @return string The sanitized 'order' query variable. 
  451. */ 
  452. protected function parse_order( $order ) { 
  453. if ( ! is_string( $order ) || empty( $order ) ) { 
  454. return 'ASC'; 
  455.  
  456. if ( 'ASC' === strtoupper( $order ) ) { 
  457. return 'ASC'; 
  458. } else { 
  459. return 'DESC';