class-wp-site-query.php 23 KB

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