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