/wp-includes/class-wp-network-query.php

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