class-wp-site-query.php 23 KB

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