WP_Site_Query

Core class used for querying sites.

Defined (1)

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

/wp-includes/class-wp-site-query.php  
  1. class WP_Site_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. * Date query container. 
  28. * @since 4.6.0 
  29. * @access public 
  30. * @var object WP_Date_Query 
  31. */ 
  32. public $date_query = false; 
  33.  
  34. /** 
  35. * Query vars set by the user. 
  36. * @since 4.6.0 
  37. * @access public 
  38. * @var array 
  39. */ 
  40. public $query_vars; 
  41.  
  42. /** 
  43. * Default values for query vars. 
  44. * @since 4.6.0 
  45. * @access public 
  46. * @var array 
  47. */ 
  48. public $query_var_defaults; 
  49.  
  50. /** 
  51. * List of sites located by the query. 
  52. * @since 4.6.0 
  53. * @access public 
  54. * @var array 
  55. */ 
  56. public $sites; 
  57.  
  58. /** 
  59. * The amount of found sites for the current query. 
  60. * @since 4.6.0 
  61. * @access public 
  62. * @var int 
  63. */ 
  64. public $found_sites = 0; 
  65.  
  66. /** 
  67. * The number of pages. 
  68. * @since 4.6.0 
  69. * @access public 
  70. * @var int 
  71. */ 
  72. public $max_num_pages = 0; 
  73.  
  74. /** 
  75. * Sets up the site query, based on the query vars passed. 
  76. * @since 4.6.0 
  77. * @access public 
  78. * @param string|array $query { 
  79. * Optional. Array or query string of site query parameters. Default empty. 
  80. * @type array $site__in Array of site IDs to include. Default empty. 
  81. * @type array $site__not_in Array of site IDs to exclude. Default empty. 
  82. * @type bool $count Whether to return a site count (true) or array of site objects. 
  83. * Default false. 
  84. * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query. 
  85. * Default null. 
  86. * @type string $fields Site fields to return. Accepts 'ids' (returns an array of site IDs) 
  87. * or empty (returns an array of complete site objects). Default empty. 
  88. * @type int $ID A site ID to only return that site. Default empty. 
  89. * @type int $number Maximum number of sites to retrieve. Default null (no limit). 
  90. * @type int $offset Number of sites to offset the query. Used to build LIMIT clause. 
  91. * Default 0. 
  92. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true. 
  93. * @type string|array $orderby Site status or array of statuses. Accepts 'id', 'domain', 'path',  
  94. * 'network_id', 'last_updated', 'registered', 'domain_length',  
  95. * 'path_length', 'site__in' and 'network__in'. Also accepts false,  
  96. * an empty array, or 'none' to disable `ORDER BY` clause. 
  97. * Default 'id'. 
  98. * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'. 
  99. * @type int $network_id Limit results to those affiliated with a given network ID. If 0,  
  100. * include all networks. Default 0. 
  101. * @type array $network__in Array of network IDs to include affiliated sites for. Default empty. 
  102. * @type array $network__not_in Array of network IDs to exclude affiliated sites for. Default empty. 
  103. * @type string $domain Limit results to those affiliated with a given domain. 
  104. * Default empty. 
  105. * @type array $domain__in Array of domains to include affiliated sites for. Default empty. 
  106. * @type array $domain__not_in Array of domains to exclude affiliated sites for. Default empty. 
  107. * @type string $path Limit results to those affiliated with a given path. 
  108. * Default empty. 
  109. * @type array $path__in Array of paths to include affiliated sites for. Default empty. 
  110. * @type array $path__not_in Array of paths to exclude affiliated sites for. Default empty. 
  111. * @type int $public Limit results to public sites. Accepts '1' or '0'. Default empty. 
  112. * @type int $archived Limit results to archived sites. Accepts '1' or '0'. Default empty. 
  113. * @type int $mature Limit results to mature sites. Accepts '1' or '0'. Default empty. 
  114. * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty. 
  115. * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty. 
  116. * @type string $search Search term(s) to retrieve matching sites for. Default empty. 
  117. * @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'. 
  118. * Default empty array. 
  119. * @type bool $update_site_cache Whether to prime the cache for found sites. Default false. 
  120. * } 
  121. */ 
  122. public function __construct( $query = '' ) { 
  123. $this->query_var_defaults = array( 
  124. 'fields' => '',  
  125. 'ID' => '',  
  126. 'site__in' => '',  
  127. 'site__not_in' => '',  
  128. 'number' => 100,  
  129. 'offset' => '',  
  130. 'no_found_rows' => true,  
  131. 'orderby' => 'id',  
  132. 'order' => 'ASC',  
  133. 'network_id' => 0,  
  134. 'network__in' => '',  
  135. 'network__not_in' => '',  
  136. 'domain' => '',  
  137. 'domain__in' => '',  
  138. 'domain__not_in' => '',  
  139. 'path' => '',  
  140. 'path__in' => '',  
  141. 'path__not_in' => '',  
  142. 'public' => null,  
  143. 'archived' => null,  
  144. 'mature' => null,  
  145. 'spam' => null,  
  146. 'deleted' => null,  
  147. 'search' => '',  
  148. 'search_columns' => array(),  
  149. 'count' => false,  
  150. 'date_query' => null, // See WP_Date_Query 
  151. 'update_site_cache' => true,  
  152. ); 
  153.  
  154. if ( ! empty( $query ) ) { 
  155. $this->query( $query ); 
  156.  
  157. /** 
  158. * Parses arguments passed to the site query with default query parameters. 
  159. * @since 4.6.0 
  160. * @access public 
  161. * @see WP_Site_Query::__construct() 
  162. * @param string|array $query Array or string of WP_Site_Query arguments. See WP_Site_Query::__construct(). 
  163. */ 
  164. public function parse_query( $query = '' ) { 
  165. if ( empty( $query ) ) { 
  166. $query = $this->query_vars; 
  167.  
  168. $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); 
  169.  
  170. /** 
  171. * Fires after the site query vars have been parsed. 
  172. * @since 4.6.0 
  173. * @param WP_Site_Query &$this The WP_Site_Query instance (passed by reference). 
  174. */ 
  175. do_action_ref_array( 'parse_site_query', array( &$this ) ); 
  176.  
  177. /** 
  178. * Sets up the WordPress query for retrieving sites. 
  179. * @since 4.6.0 
  180. * @access public 
  181. * @param string|array $query Array or URL query string of parameters. 
  182. * @return array|int List of sites, or number of sites when 'count' is passed as a query var. 
  183. */ 
  184. public function query( $query ) { 
  185. $this->query_vars = wp_parse_args( $query ); 
  186.  
  187. return $this->get_sites(); 
  188.  
  189. /** 
  190. * Retrieves a list of sites matching the query vars. 
  191. * @since 4.6.0 
  192. * @access public 
  193. * @return array|int List of sites, or number of sites when 'count' is passed as a query var. 
  194. */ 
  195. public function get_sites() { 
  196. $this->parse_query(); 
  197.  
  198. /** 
  199. * Fires before sites are retrieved. 
  200. * @since 4.6.0 
  201. * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. 
  202. */ 
  203. do_action_ref_array( 'pre_get_sites', 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', 'sites' ); 
  208. if ( ! $last_changed ) { 
  209. $last_changed = microtime(); 
  210. wp_cache_set( 'last_changed', $last_changed, 'sites' ); 
  211.  
  212. $cache_key = "get_sites:$key:$last_changed"; 
  213. $cache_value = wp_cache_get( $cache_key, 'sites' ); 
  214.  
  215. if ( false === $cache_value ) { 
  216. $site_ids = $this->get_site_ids(); 
  217. if ( $site_ids ) { 
  218. $this->set_found_sites(); 
  219.  
  220. $cache_value = array( 
  221. 'site_ids' => $site_ids,  
  222. 'found_sites' => $this->found_sites,  
  223. ); 
  224. wp_cache_add( $cache_key, $cache_value, 'sites' ); 
  225. } else { 
  226. $site_ids = $cache_value['site_ids']; 
  227. $this->found_sites = $cache_value['found_sites']; 
  228.  
  229. if ( $this->found_sites && $this->query_vars['number'] ) { 
  230. $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] ); 
  231.  
  232. // If querying for a count only, there's nothing more to do. 
  233. if ( $this->query_vars['count'] ) { 
  234. // $site_ids is actually a count in this case. 
  235. return intval( $site_ids ); 
  236.  
  237. $site_ids = array_map( 'intval', $site_ids ); 
  238.  
  239. if ( 'ids' == $this->query_vars['fields'] ) { 
  240. $this->sites = $site_ids; 
  241.  
  242. return $this->sites; 
  243.  
  244. // Prime site network caches. 
  245. if ( $this->query_vars['update_site_cache'] ) { 
  246. _prime_site_caches( $site_ids ); 
  247.  
  248. // Fetch full site objects from the primed cache. 
  249. $_sites = array(); 
  250. foreach ( $site_ids as $site_id ) { 
  251. if ( $_site = get_site( $site_id ) ) { 
  252. $_sites[] = $_site; 
  253.  
  254. /** 
  255. * Filters the site query results. 
  256. * @since 4.6.0 
  257. * @param array $results An array of sites. 
  258. * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. 
  259. */ 
  260. $_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) ); 
  261.  
  262. // Convert to WP_Site instances. 
  263. $this->sites = array_map( 'get_site', $_sites ); 
  264.  
  265. return $this->sites; 
  266.  
  267. /** 
  268. * Used internally to get a list of site IDs matching the query vars. 
  269. * @since 4.6.0 
  270. * @access protected 
  271. * @global wpdb $wpdb WordPress database abstraction object. 
  272. * @return int|array A single count of site IDs if a count query. An array of site IDs if a full query. 
  273. */ 
  274. protected function get_site_ids() { 
  275. global $wpdb; 
  276.  
  277. $order = $this->parse_order( $this->query_vars['order'] ); 
  278.  
  279. // Disable ORDER BY with 'none', an empty array, or boolean false. 
  280. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { 
  281. $orderby = ''; 
  282. } elseif ( ! empty( $this->query_vars['orderby'] ) ) { 
  283. $ordersby = is_array( $this->query_vars['orderby'] ) ? 
  284. $this->query_vars['orderby'] : 
  285. preg_split( '/[, \s]/', $this->query_vars['orderby'] ); 
  286.  
  287. $orderby_array = array(); 
  288. foreach ( $ordersby as $_key => $_value ) { 
  289. if ( ! $_value ) { 
  290. continue; 
  291.  
  292. if ( is_int( $_key ) ) { 
  293. $_orderby = $_value; 
  294. $_order = $order; 
  295. } else { 
  296. $_orderby = $_key; 
  297. $_order = $_value; 
  298.  
  299. $parsed = $this->parse_orderby( $_orderby ); 
  300.  
  301. if ( ! $parsed ) { 
  302. continue; 
  303.  
  304. if ( 'site__in' === $_orderby || 'network__in' === $_orderby ) { 
  305. $orderby_array[] = $parsed; 
  306. continue; 
  307.  
  308. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 
  309.  
  310. $orderby = implode( ', ', $orderby_array ); 
  311. } else { 
  312. $orderby = "blog_id $order"; 
  313.  
  314. $number = absint( $this->query_vars['number'] ); 
  315. $offset = absint( $this->query_vars['offset'] ); 
  316.  
  317. if ( ! empty( $number ) ) { 
  318. if ( $offset ) { 
  319. $limits = 'LIMIT ' . $offset . ', ' . $number; 
  320. } else { 
  321. $limits = 'LIMIT ' . $number; 
  322.  
  323. if ( $this->query_vars['count'] ) { 
  324. $fields = 'COUNT(*)'; 
  325. } else { 
  326. $fields = 'blog_id'; 
  327.  
  328. // Parse site IDs for an IN clause. 
  329. $site_id = absint( $this->query_vars['ID'] ); 
  330. if ( ! empty( $site_id ) ) { 
  331. $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id ); 
  332.  
  333. // Parse site IDs for an IN clause. 
  334. if ( ! empty( $this->query_vars['site__in'] ) ) { 
  335. $this->sql_clauses['where']['site__in'] = "blog_id IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )'; 
  336.  
  337. // Parse site IDs for a NOT IN clause. 
  338. if ( ! empty( $this->query_vars['site__not_in'] ) ) { 
  339. $this->sql_clauses['where']['site__not_in'] = "blog_id NOT IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )'; 
  340.  
  341. $network_id = absint( $this->query_vars['network_id'] ); 
  342.  
  343. if ( ! empty( $network_id ) ) { 
  344. $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id ); 
  345.  
  346. // Parse site network IDs for an IN clause. 
  347. if ( ! empty( $this->query_vars['network__in'] ) ) { 
  348. $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )'; 
  349.  
  350. // Parse site network IDs for a NOT IN clause. 
  351. if ( ! empty( $this->query_vars['network__not_in'] ) ) { 
  352. $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )'; 
  353.  
  354. if ( ! empty( $this->query_vars['domain'] ) ) { 
  355. $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] ); 
  356.  
  357. // Parse site domain for an IN clause. 
  358. if ( is_array( $this->query_vars['domain__in'] ) ) { 
  359. $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )"; 
  360.  
  361. // Parse site domain for a NOT IN clause. 
  362. if ( is_array( $this->query_vars['domain__not_in'] ) ) { 
  363. $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )"; 
  364.  
  365. if ( ! empty( $this->query_vars['path'] ) ) { 
  366. $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] ); 
  367.  
  368. // Parse site path for an IN clause. 
  369. if ( is_array( $this->query_vars['path__in'] ) ) { 
  370. $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )"; 
  371.  
  372. // Parse site path for a NOT IN clause. 
  373. if ( is_array( $this->query_vars['path__not_in'] ) ) { 
  374. $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )"; 
  375.  
  376. if ( is_numeric( $this->query_vars['archived'] ) ) { 
  377. $archived = absint( $this->query_vars['archived'] ); 
  378. $this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived ); 
  379.  
  380. if ( is_numeric( $this->query_vars['mature'] ) ) { 
  381. $mature = absint( $this->query_vars['mature'] ); 
  382. $this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature ); 
  383.  
  384. if ( is_numeric( $this->query_vars['spam'] ) ) { 
  385. $spam = absint( $this->query_vars['spam'] ); 
  386. $this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam ); 
  387.  
  388. if ( is_numeric( $this->query_vars['deleted'] ) ) { 
  389. $deleted = absint( $this->query_vars['deleted'] ); 
  390. $this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted ); 
  391.  
  392. if ( is_numeric( $this->query_vars['public'] ) ) { 
  393. $public = absint( $this->query_vars['public'] ); 
  394. $this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public ); 
  395.  
  396. // Falsey search strings are ignored. 
  397. if ( strlen( $this->query_vars['search'] ) ) { 
  398. $search_columns = array(); 
  399.  
  400. if ( $this->query_vars['search_columns'] ) { 
  401. $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) ); 
  402.  
  403. if ( ! $search_columns ) { 
  404. $search_columns = array( 'domain', 'path' ); 
  405.  
  406. /** 
  407. * Filters the columns to search in a WP_Site_Query search. 
  408. * The default columns include 'domain' and 'path. 
  409. * @since 4.6.0 
  410. * @param array $search_columns Array of column names to be searched. 
  411. * @param string $search Text being searched. 
  412. * @param WP_Site_Query $this The current WP_Site_Query instance. 
  413. */ 
  414. $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this ); 
  415.  
  416. $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns ); 
  417.  
  418. $date_query = $this->query_vars['date_query']; 
  419. if ( ! empty( $date_query ) && is_array( $date_query ) ) { 
  420. $this->date_query = new WP_Date_Query( $date_query, 'registered' ); 
  421. $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); 
  422.  
  423. $where = implode( ' AND ', $this->sql_clauses['where'] ); 
  424.  
  425. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); 
  426.  
  427. /** 
  428. * Filters the site query clauses. 
  429. * @since 4.6.0 
  430. * @param array $pieces A compacted array of site query clauses. 
  431. * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference. 
  432. */ 
  433. $clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) ); 
  434.  
  435. $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; 
  436. $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; 
  437. $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; 
  438. $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; 
  439. $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; 
  440. $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; 
  441.  
  442. if ( $where ) { 
  443. $where = 'WHERE ' . $where; 
  444.  
  445. if ( $groupby ) { 
  446. $groupby = 'GROUP BY ' . $groupby; 
  447.  
  448. if ( $orderby ) { 
  449. $orderby = "ORDER BY $orderby"; 
  450.  
  451. $found_rows = ''; 
  452. if ( ! $this->query_vars['no_found_rows'] ) { 
  453. $found_rows = 'SQL_CALC_FOUND_ROWS'; 
  454.  
  455. $this->sql_clauses['select'] = "SELECT $found_rows $fields"; 
  456. $this->sql_clauses['from'] = "FROM $wpdb->blogs $join"; 
  457. $this->sql_clauses['groupby'] = $groupby; 
  458. $this->sql_clauses['orderby'] = $orderby; 
  459. $this->sql_clauses['limits'] = $limits; 
  460.  
  461. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 
  462.  
  463. if ( $this->query_vars['count'] ) { 
  464. return intval( $wpdb->get_var( $this->request ) ); 
  465.  
  466. $site_ids = $wpdb->get_col( $this->request ); 
  467.  
  468. return array_map( 'intval', $site_ids ); 
  469.  
  470. /** 
  471. * Populates found_sites and max_num_pages properties for the current query 
  472. * if the limit clause was used. 
  473. * @since 4.6.0 
  474. * @access private 
  475. * @global wpdb $wpdb WordPress database abstraction object. 
  476. */ 
  477. private function set_found_sites() { 
  478. global $wpdb; 
  479.  
  480. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { 
  481. /** 
  482. * Filters the query used to retrieve found site count. 
  483. * @since 4.6.0 
  484. * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'. 
  485. * @param WP_Site_Query $site_query The `WP_Site_Query` instance. 
  486. */ 
  487. $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this ); 
  488.  
  489. $this->found_sites = (int) $wpdb->get_var( $found_sites_query ); 
  490.  
  491. /** 
  492. * Used internally to generate an SQL string for searching across multiple columns. 
  493. * @since 4.6.0 
  494. * @access protected 
  495. * @global wpdb $wpdb WordPress database abstraction object. 
  496. * @param string $string Search string. 
  497. * @param array $columns Columns to search. 
  498. * @return string Search SQL. 
  499. */ 
  500. protected function get_search_sql( $string, $columns ) { 
  501. global $wpdb; 
  502.  
  503. if ( false !== strpos( $string, '*' ) ) { 
  504. $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%'; 
  505. } else { 
  506. $like = '%' . $wpdb->esc_like( $string ) . '%'; 
  507.  
  508. $searches = array(); 
  509. foreach ( $columns as $column ) { 
  510. $searches[] = $wpdb->prepare( "$column LIKE %s", $like ); 
  511.  
  512. return '(' . implode( ' OR ', $searches ) . ')'; 
  513.  
  514. /** 
  515. * Parses and sanitizes 'orderby' keys passed to the site query. 
  516. * @since 4.6.0 
  517. * @access protected 
  518. * @global wpdb $wpdb WordPress database abstraction object. 
  519. * @param string $orderby Alias for the field to order by. 
  520. * @return string|false Value to used in the ORDER clause. False otherwise. 
  521. */ 
  522. protected function parse_orderby( $orderby ) { 
  523. global $wpdb; 
  524.  
  525. $parsed = false; 
  526.  
  527. switch ( $orderby ) { 
  528. case 'site__in': 
  529. $site__in = implode( ', ', array_map( 'absint', $this->query_vars['site__in'] ) ); 
  530. $parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )"; 
  531. break; 
  532. case 'network__in': 
  533. $network__in = implode( ', ', array_map( 'absint', $this->query_vars['network__in'] ) ); 
  534. $parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )"; 
  535. break; 
  536. case 'domain': 
  537. case 'last_updated': 
  538. case 'path': 
  539. case 'registered': 
  540. $parsed = $orderby; 
  541. break; 
  542. case 'network_id': 
  543. $parsed = 'site_id'; 
  544. break; 
  545. case 'domain_length': 
  546. $parsed = 'CHAR_LENGTH(domain)'; 
  547. break; 
  548. case 'path_length': 
  549. $parsed = 'CHAR_LENGTH(path)'; 
  550. break; 
  551. case 'id': 
  552. $parsed = 'blog_id'; 
  553. break; 
  554.  
  555. return $parsed; 
  556.  
  557. /** 
  558. * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary. 
  559. * @since 4.6.0 
  560. * @access protected 
  561. * @param string $order The 'order' query variable. 
  562. * @return string The sanitized 'order' query variable. 
  563. */ 
  564. protected function parse_order( $order ) { 
  565. if ( ! is_string( $order ) || empty( $order ) ) { 
  566. return 'ASC'; 
  567.  
  568. if ( 'ASC' === strtoupper( $order ) ) { 
  569. return 'ASC'; 
  570. } else { 
  571. return 'DESC';