You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

690 lines
22 KiB

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