class-wp-site-query.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. #[AllowDynamicProperties]
  17. class WP_Site_Query {
  18. /**
  19. * SQL for database query.
  20. *
  21. * @since 4.6.0
  22. * @var string
  23. */
  24. public $request;
  25. /**
  26. * SQL query clauses.
  27. *
  28. * @since 4.6.0
  29. * @var array
  30. */
  31. protected $sql_clauses = array(
  32. 'select' => '',
  33. 'from' => '',
  34. 'where' => array(),
  35. 'groupby' => '',
  36. 'orderby' => '',
  37. 'limits' => '',
  38. );
  39. /**
  40. * Metadata query container.
  41. *
  42. * @since 5.1.0
  43. * @var WP_Meta_Query
  44. */
  45. public $meta_query = false;
  46. /**
  47. * Metadata query clauses.
  48. *
  49. * @since 5.1.0
  50. * @var array
  51. */
  52. protected $meta_query_clauses;
  53. /**
  54. * Date query container.
  55. *
  56. * @since 4.6.0
  57. * @var WP_Date_Query A date query instance.
  58. */
  59. public $date_query = false;
  60. /**
  61. * Query vars set by the user.
  62. *
  63. * @since 4.6.0
  64. * @var array
  65. */
  66. public $query_vars;
  67. /**
  68. * Default values for query vars.
  69. *
  70. * @since 4.6.0
  71. * @var array
  72. */
  73. public $query_var_defaults;
  74. /**
  75. * List of sites located by the query.
  76. *
  77. * @since 4.6.0
  78. * @var array
  79. */
  80. public $sites;
  81. /**
  82. * The amount of found sites for the current query.
  83. *
  84. * @since 4.6.0
  85. * @var int
  86. */
  87. public $found_sites = 0;
  88. /**
  89. * The number of pages.
  90. *
  91. * @since 4.6.0
  92. * @var int
  93. */
  94. public $max_num_pages = 0;
  95. /**
  96. * Sets up the site query, based on the query vars passed.
  97. *
  98. * @since 4.6.0
  99. * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
  100. * @since 5.1.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
  101. * 'meta_compare_key', 'meta_value', 'meta_type', and 'meta_compare' parameters.
  102. * @since 5.3.0 Introduced the 'meta_type_key' parameter.
  103. *
  104. * @param string|array $query {
  105. * Optional. Array or query string of site query parameters. Default empty.
  106. *
  107. * @type int[] $site__in Array of site IDs to include. Default empty.
  108. * @type int[] $site__not_in Array of site IDs to exclude. Default empty.
  109. * @type bool $count Whether to return a site count (true) or array of site objects.
  110. * Default false.
  111. * @type array $date_query Date query clauses to limit sites by. See WP_Date_Query.
  112. * Default null.
  113. * @type string $fields Site fields to return. Accepts 'ids' (returns an array of site IDs)
  114. * or empty (returns an array of complete site objects). Default empty.
  115. * @type int $ID A site ID to only return that site. Default empty.
  116. * @type int $number Maximum number of sites to retrieve. Default 100.
  117. * @type int $offset Number of sites to offset the query. Used to build LIMIT clause.
  118. * Default 0.
  119. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
  120. * @type string|array $orderby Site status or array of statuses. Accepts:
  121. * - 'id'
  122. * - 'domain'
  123. * - 'path'
  124. * - 'network_id'
  125. * - 'last_updated'
  126. * - 'registered'
  127. * - 'domain_length'
  128. * - 'path_length'
  129. * - 'site__in'
  130. * - 'network__in'
  131. * - 'deleted'
  132. * - 'mature'
  133. * - 'spam'
  134. * - 'archived'
  135. * - 'public'
  136. * - false, an empty array, or 'none' to disable `ORDER BY` clause.
  137. * Default 'id'.
  138. * @type string $order How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
  139. * @type int $network_id Limit results to those affiliated with a given network ID. If 0,
  140. * include all networks. Default 0.
  141. * @type int[] $network__in Array of network IDs to include affiliated sites for. Default empty.
  142. * @type int[] $network__not_in Array of network IDs to exclude affiliated sites for. Default empty.
  143. * @type string $domain Limit results to those affiliated with a given domain. Default empty.
  144. * @type string[] $domain__in Array of domains to include affiliated sites for. Default empty.
  145. * @type string[] $domain__not_in Array of domains to exclude affiliated sites for. Default empty.
  146. * @type string $path Limit results to those affiliated with a given path. Default empty.
  147. * @type string[] $path__in Array of paths to include affiliated sites for. Default empty.
  148. * @type string[] $path__not_in Array of paths to exclude affiliated sites for. Default empty.
  149. * @type int $public Limit results to public sites. Accepts '1' or '0'. Default empty.
  150. * @type int $archived Limit results to archived sites. Accepts '1' or '0'. Default empty.
  151. * @type int $mature Limit results to mature sites. Accepts '1' or '0'. Default empty.
  152. * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty.
  153. * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty.
  154. * @type int $lang_id Limit results to a language ID. Default empty.
  155. * @type string[] $lang__in Array of language IDs to include affiliated sites for. Default empty.
  156. * @type string[] $lang__not_in Array of language IDs to exclude affiliated sites for. Default empty.
  157. * @type string $search Search term(s) to retrieve matching sites for. Default empty.
  158. * @type string[] $search_columns Array of column names to be searched. Accepts 'domain' and 'path'.
  159. * Default empty array.
  160. * @type bool $update_site_cache Whether to prime the cache for found sites. Default true.
  161. * @type bool $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
  162. * @type string|string[] $meta_key Meta key or keys to filter by.
  163. * @type string|string[] $meta_value Meta value or values to filter by.
  164. * @type string $meta_compare MySQL operator used for comparing the meta value.
  165. * See WP_Meta_Query::__construct() for accepted values and default value.
  166. * @type string $meta_compare_key MySQL operator used for comparing the meta key.
  167. * See WP_Meta_Query::__construct() for accepted values and default value.
  168. * @type string $meta_type MySQL data type that the meta_value column will be CAST to for comparisons.
  169. * See WP_Meta_Query::__construct() for accepted values and default value.
  170. * @type string $meta_type_key MySQL data type that the meta_key column will be CAST to for comparisons.
  171. * See WP_Meta_Query::__construct() for accepted values and default value.
  172. * @type array $meta_query An associative array of WP_Meta_Query arguments.
  173. * See WP_Meta_Query::__construct() for accepted values.
  174. * }
  175. */
  176. public function __construct( $query = '' ) {
  177. $this->query_var_defaults = array(
  178. 'fields' => '',
  179. 'ID' => '',
  180. 'site__in' => '',
  181. 'site__not_in' => '',
  182. 'number' => 100,
  183. 'offset' => '',
  184. 'no_found_rows' => true,
  185. 'orderby' => 'id',
  186. 'order' => 'ASC',
  187. 'network_id' => 0,
  188. 'network__in' => '',
  189. 'network__not_in' => '',
  190. 'domain' => '',
  191. 'domain__in' => '',
  192. 'domain__not_in' => '',
  193. 'path' => '',
  194. 'path__in' => '',
  195. 'path__not_in' => '',
  196. 'public' => null,
  197. 'archived' => null,
  198. 'mature' => null,
  199. 'spam' => null,
  200. 'deleted' => null,
  201. 'lang_id' => null,
  202. 'lang__in' => '',
  203. 'lang__not_in' => '',
  204. 'search' => '',
  205. 'search_columns' => array(),
  206. 'count' => false,
  207. 'date_query' => null, // See WP_Date_Query.
  208. 'update_site_cache' => true,
  209. 'update_site_meta_cache' => true,
  210. 'meta_query' => '',
  211. 'meta_key' => '',
  212. 'meta_value' => '',
  213. 'meta_type' => '',
  214. 'meta_compare' => '',
  215. );
  216. if ( ! empty( $query ) ) {
  217. $this->query( $query );
  218. }
  219. }
  220. /**
  221. * Parses arguments passed to the site query with default query parameters.
  222. *
  223. * @since 4.6.0
  224. *
  225. * @see WP_Site_Query::__construct()
  226. *
  227. * @param string|array $query Array or string of WP_Site_Query arguments. See WP_Site_Query::__construct().
  228. */
  229. public function parse_query( $query = '' ) {
  230. if ( empty( $query ) ) {
  231. $query = $this->query_vars;
  232. }
  233. $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
  234. /**
  235. * Fires after the site query vars have been parsed.
  236. *
  237. * @since 4.6.0
  238. *
  239. * @param WP_Site_Query $query The WP_Site_Query instance (passed by reference).
  240. */
  241. do_action_ref_array( 'parse_site_query', array( &$this ) );
  242. }
  243. /**
  244. * Sets up the WordPress query for retrieving sites.
  245. *
  246. * @since 4.6.0
  247. *
  248. * @param string|array $query Array or URL query string of parameters.
  249. * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
  250. * or the number of sites when 'count' is passed as a query var.
  251. */
  252. public function query( $query ) {
  253. $this->query_vars = wp_parse_args( $query );
  254. return $this->get_sites();
  255. }
  256. /**
  257. * Retrieves a list of sites matching the query vars.
  258. *
  259. * @since 4.6.0
  260. *
  261. * @global wpdb $wpdb WordPress database abstraction object.
  262. *
  263. * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
  264. * or the number of sites when 'count' is passed as a query var.
  265. */
  266. public function get_sites() {
  267. global $wpdb;
  268. $this->parse_query();
  269. // Parse meta query.
  270. $this->meta_query = new WP_Meta_Query();
  271. $this->meta_query->parse_query_vars( $this->query_vars );
  272. /**
  273. * Fires before sites are retrieved.
  274. *
  275. * @since 4.6.0
  276. *
  277. * @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
  278. */
  279. do_action_ref_array( 'pre_get_sites', array( &$this ) );
  280. // Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
  281. $this->meta_query->parse_query_vars( $this->query_vars );
  282. if ( ! empty( $this->meta_query->queries ) ) {
  283. $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
  284. }
  285. $site_data = null;
  286. /**
  287. * Filters the site data before the get_sites query takes place.
  288. *
  289. * Return a non-null value to bypass WordPress' default site queries.
  290. *
  291. * The expected return type from this filter depends on the value passed
  292. * in the request query vars:
  293. * - When `$this->query_vars['count']` is set, the filter should return
  294. * the site count as an integer.
  295. * - When `'ids' === $this->query_vars['fields']`, the filter should return
  296. * an array of site IDs.
  297. * - Otherwise the filter should return an array of WP_Site objects.
  298. *
  299. * Note that if the filter returns an array of site data, it will be assigned
  300. * to the `sites` property of the current WP_Site_Query instance.
  301. *
  302. * Filtering functions that require pagination information are encouraged to set
  303. * the `found_sites` and `max_num_pages` properties of the WP_Site_Query object,
  304. * passed to the filter by reference. If WP_Site_Query does not perform a database
  305. * query, it will not have enough information to generate these values itself.
  306. *
  307. * @since 5.2.0
  308. * @since 5.6.0 The returned array of site data is assigned to the `sites` property
  309. * of the current WP_Site_Query instance.
  310. *
  311. * @param array|int|null $site_data Return an array of site data to short-circuit WP's site query,
  312. * the site count as an integer if `$this->query_vars['count']` is set,
  313. * or null to run the normal queries.
  314. * @param WP_Site_Query $query The WP_Site_Query instance, passed by reference.
  315. */
  316. $site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
  317. if ( null !== $site_data ) {
  318. if ( is_array( $site_data ) && ! $this->query_vars['count'] ) {
  319. $this->sites = $site_data;
  320. }
  321. return $site_data;
  322. }
  323. // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
  324. $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
  325. // Ignore the $fields, $update_site_cache, $update_site_meta_cache argument as the queried result will be the same regardless.
  326. unset( $_args['fields'], $_args['update_site_cache'], $_args['update_site_meta_cache'] );
  327. $key = md5( serialize( $_args ) );
  328. $last_changed = wp_cache_get_last_changed( 'sites' );
  329. $cache_key = "get_sites:$key:$last_changed";
  330. $cache_value = wp_cache_get( $cache_key, 'sites' );
  331. if ( false === $cache_value ) {
  332. $site_ids = $this->get_site_ids();
  333. if ( $site_ids ) {
  334. $this->set_found_sites();
  335. }
  336. $cache_value = array(
  337. 'site_ids' => $site_ids,
  338. 'found_sites' => $this->found_sites,
  339. );
  340. wp_cache_add( $cache_key, $cache_value, 'sites' );
  341. } else {
  342. $site_ids = $cache_value['site_ids'];
  343. $this->found_sites = $cache_value['found_sites'];
  344. }
  345. if ( $this->found_sites && $this->query_vars['number'] ) {
  346. $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
  347. }
  348. // If querying for a count only, there's nothing more to do.
  349. if ( $this->query_vars['count'] ) {
  350. // $site_ids is actually a count in this case.
  351. return (int) $site_ids;
  352. }
  353. $site_ids = array_map( 'intval', $site_ids );
  354. if ( 'ids' === $this->query_vars['fields'] ) {
  355. $this->sites = $site_ids;
  356. return $this->sites;
  357. }
  358. // Prime site network caches.
  359. if ( $this->query_vars['update_site_cache'] ) {
  360. _prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
  361. }
  362. // Fetch full site objects from the primed cache.
  363. $_sites = array();
  364. foreach ( $site_ids as $site_id ) {
  365. $_site = get_site( $site_id );
  366. if ( $_site ) {
  367. $_sites[] = $_site;
  368. }
  369. }
  370. /**
  371. * Filters the site query results.
  372. *
  373. * @since 4.6.0
  374. *
  375. * @param WP_Site[] $_sites An array of WP_Site objects.
  376. * @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
  377. */
  378. $_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
  379. // Convert to WP_Site instances.
  380. $this->sites = array_map( 'get_site', $_sites );
  381. return $this->sites;
  382. }
  383. /**
  384. * Used internally to get a list of site IDs matching the query vars.
  385. *
  386. * @since 4.6.0
  387. *
  388. * @global wpdb $wpdb WordPress database abstraction object.
  389. *
  390. * @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
  391. */
  392. protected function get_site_ids() {
  393. global $wpdb;
  394. $order = $this->parse_order( $this->query_vars['order'] );
  395. // Disable ORDER BY with 'none', an empty array, or boolean false.
  396. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
  397. $orderby = '';
  398. } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
  399. $ordersby = is_array( $this->query_vars['orderby'] ) ?
  400. $this->query_vars['orderby'] :
  401. preg_split( '/[,\s]/', $this->query_vars['orderby'] );
  402. $orderby_array = array();
  403. foreach ( $ordersby as $_key => $_value ) {
  404. if ( ! $_value ) {
  405. continue;
  406. }
  407. if ( is_int( $_key ) ) {
  408. $_orderby = $_value;
  409. $_order = $order;
  410. } else {
  411. $_orderby = $_key;
  412. $_order = $_value;
  413. }
  414. $parsed = $this->parse_orderby( $_orderby );
  415. if ( ! $parsed ) {
  416. continue;
  417. }
  418. if ( 'site__in' === $_orderby || 'network__in' === $_orderby ) {
  419. $orderby_array[] = $parsed;
  420. continue;
  421. }
  422. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
  423. }
  424. $orderby = implode( ', ', $orderby_array );
  425. } else {
  426. $orderby = "{$wpdb->blogs}.blog_id $order";
  427. }
  428. $number = absint( $this->query_vars['number'] );
  429. $offset = absint( $this->query_vars['offset'] );
  430. $limits = '';
  431. if ( ! empty( $number ) ) {
  432. if ( $offset ) {
  433. $limits = 'LIMIT ' . $offset . ',' . $number;
  434. } else {
  435. $limits = 'LIMIT ' . $number;
  436. }
  437. }
  438. if ( $this->query_vars['count'] ) {
  439. $fields = 'COUNT(*)';
  440. } else {
  441. $fields = "{$wpdb->blogs}.blog_id";
  442. }
  443. // Parse site IDs for an IN clause.
  444. $site_id = absint( $this->query_vars['ID'] );
  445. if ( ! empty( $site_id ) ) {
  446. $this->sql_clauses['where']['ID'] = $wpdb->prepare( "{$wpdb->blogs}.blog_id = %d", $site_id );
  447. }
  448. // Parse site IDs for an IN clause.
  449. if ( ! empty( $this->query_vars['site__in'] ) ) {
  450. $this->sql_clauses['where']['site__in'] = "{$wpdb->blogs}.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
  451. }
  452. // Parse site IDs for a NOT IN clause.
  453. if ( ! empty( $this->query_vars['site__not_in'] ) ) {
  454. $this->sql_clauses['where']['site__not_in'] = "{$wpdb->blogs}.blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
  455. }
  456. $network_id = absint( $this->query_vars['network_id'] );
  457. if ( ! empty( $network_id ) ) {
  458. $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
  459. }
  460. // Parse site network IDs for an IN clause.
  461. if ( ! empty( $this->query_vars['network__in'] ) ) {
  462. $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
  463. }
  464. // Parse site network IDs for a NOT IN clause.
  465. if ( ! empty( $this->query_vars['network__not_in'] ) ) {
  466. $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
  467. }
  468. if ( ! empty( $this->query_vars['domain'] ) ) {
  469. $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
  470. }
  471. // Parse site domain for an IN clause.
  472. if ( is_array( $this->query_vars['domain__in'] ) ) {
  473. $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
  474. }
  475. // Parse site domain for a NOT IN clause.
  476. if ( is_array( $this->query_vars['domain__not_in'] ) ) {
  477. $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
  478. }
  479. if ( ! empty( $this->query_vars['path'] ) ) {
  480. $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
  481. }
  482. // Parse site path for an IN clause.
  483. if ( is_array( $this->query_vars['path__in'] ) ) {
  484. $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
  485. }
  486. // Parse site path for a NOT IN clause.
  487. if ( is_array( $this->query_vars['path__not_in'] ) ) {
  488. $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
  489. }
  490. if ( is_numeric( $this->query_vars['archived'] ) ) {
  491. $archived = absint( $this->query_vars['archived'] );
  492. $this->sql_clauses['where']['archived'] = $wpdb->prepare( 'archived = %s ', absint( $archived ) );
  493. }
  494. if ( is_numeric( $this->query_vars['mature'] ) ) {
  495. $mature = absint( $this->query_vars['mature'] );
  496. $this->sql_clauses['where']['mature'] = $wpdb->prepare( 'mature = %d ', $mature );
  497. }
  498. if ( is_numeric( $this->query_vars['spam'] ) ) {
  499. $spam = absint( $this->query_vars['spam'] );
  500. $this->sql_clauses['where']['spam'] = $wpdb->prepare( 'spam = %d ', $spam );
  501. }
  502. if ( is_numeric( $this->query_vars['deleted'] ) ) {
  503. $deleted = absint( $this->query_vars['deleted'] );
  504. $this->sql_clauses['where']['deleted'] = $wpdb->prepare( 'deleted = %d ', $deleted );
  505. }
  506. if ( is_numeric( $this->query_vars['public'] ) ) {
  507. $public = absint( $this->query_vars['public'] );
  508. $this->sql_clauses['where']['public'] = $wpdb->prepare( 'public = %d ', $public );
  509. }
  510. if ( is_numeric( $this->query_vars['lang_id'] ) ) {
  511. $lang_id = absint( $this->query_vars['lang_id'] );
  512. $this->sql_clauses['where']['lang_id'] = $wpdb->prepare( 'lang_id = %d ', $lang_id );
  513. }
  514. // Parse site language IDs for an IN clause.
  515. if ( ! empty( $this->query_vars['lang__in'] ) ) {
  516. $this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';
  517. }
  518. // Parse site language IDs for a NOT IN clause.
  519. if ( ! empty( $this->query_vars['lang__not_in'] ) ) {
  520. $this->sql_clauses['where']['lang__not_in'] = 'lang_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__not_in'] ) ) . ' )';
  521. }
  522. // Falsey search strings are ignored.
  523. if ( strlen( $this->query_vars['search'] ) ) {
  524. $search_columns = array();
  525. if ( $this->query_vars['search_columns'] ) {
  526. $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) );
  527. }
  528. if ( ! $search_columns ) {
  529. $search_columns = array( 'domain', 'path' );
  530. }
  531. /**
  532. * Filters the columns to search in a WP_Site_Query search.
  533. *
  534. * The default columns include 'domain' and 'path.
  535. *
  536. * @since 4.6.0
  537. *
  538. * @param string[] $search_columns Array of column names to be searched.
  539. * @param string $search Text being searched.
  540. * @param WP_Site_Query $query The current WP_Site_Query instance.
  541. */
  542. $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
  543. $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
  544. }
  545. $date_query = $this->query_vars['date_query'];
  546. if ( ! empty( $date_query ) && is_array( $date_query ) ) {
  547. $this->date_query = new WP_Date_Query( $date_query, 'registered' );
  548. // Strip leading 'AND'.
  549. $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
  550. }
  551. $join = '';
  552. $groupby = '';
  553. if ( ! empty( $this->meta_query_clauses ) ) {
  554. $join .= $this->meta_query_clauses['join'];
  555. // Strip leading 'AND'.
  556. $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
  557. if ( ! $this->query_vars['count'] ) {
  558. $groupby = "{$wpdb->blogs}.blog_id";
  559. }
  560. }
  561. $where = implode( ' AND ', $this->sql_clauses['where'] );
  562. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
  563. /**
  564. * Filters the site query clauses.
  565. *
  566. * @since 4.6.0
  567. *
  568. * @param string[] $clauses An associative array of site query clauses.
  569. * @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
  570. */
  571. $clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
  572. $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
  573. $join = isset( $clauses['join'] ) ? $clauses['join'] : '';
  574. $where = isset( $clauses['where'] ) ? $clauses['where'] : '';
  575. $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
  576. $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
  577. $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
  578. if ( $where ) {
  579. $where = 'WHERE ' . $where;
  580. }
  581. if ( $groupby ) {
  582. $groupby = 'GROUP BY ' . $groupby;
  583. }
  584. if ( $orderby ) {
  585. $orderby = "ORDER BY $orderby";
  586. }
  587. $found_rows = '';
  588. if ( ! $this->query_vars['no_found_rows'] ) {
  589. $found_rows = 'SQL_CALC_FOUND_ROWS';
  590. }
  591. $this->sql_clauses['select'] = "SELECT $found_rows $fields";
  592. $this->sql_clauses['from'] = "FROM $wpdb->blogs $join";
  593. $this->sql_clauses['groupby'] = $groupby;
  594. $this->sql_clauses['orderby'] = $orderby;
  595. $this->sql_clauses['limits'] = $limits;
  596. $this->request = "
  597. {$this->sql_clauses['select']}
  598. {$this->sql_clauses['from']}
  599. {$where}
  600. {$this->sql_clauses['groupby']}
  601. {$this->sql_clauses['orderby']}
  602. {$this->sql_clauses['limits']}
  603. ";
  604. if ( $this->query_vars['count'] ) {
  605. return (int) $wpdb->get_var( $this->request );
  606. }
  607. $site_ids = $wpdb->get_col( $this->request );
  608. return array_map( 'intval', $site_ids );
  609. }
  610. /**
  611. * Populates found_sites and max_num_pages properties for the current query
  612. * if the limit clause was used.
  613. *
  614. * @since 4.6.0
  615. *
  616. * @global wpdb $wpdb WordPress database abstraction object.
  617. */
  618. private function set_found_sites() {
  619. global $wpdb;
  620. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
  621. /**
  622. * Filters the query used to retrieve found site count.
  623. *
  624. * @since 4.6.0
  625. *
  626. * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
  627. * @param WP_Site_Query $site_query The `WP_Site_Query` instance.
  628. */
  629. $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
  630. $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
  631. }
  632. }
  633. /**
  634. * Used internally to generate an SQL string for searching across multiple columns.
  635. *
  636. * @since 4.6.0
  637. *
  638. * @global wpdb $wpdb WordPress database abstraction object.
  639. *
  640. * @param string $search Search string.
  641. * @param string[] $columns Array of columns to search.
  642. * @return string Search SQL.
  643. */
  644. protected function get_search_sql( $search, $columns ) {
  645. global $wpdb;
  646. if ( false !== strpos( $search, '*' ) ) {
  647. $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $search ) ) ) . '%';
  648. } else {
  649. $like = '%' . $wpdb->esc_like( $search ) . '%';
  650. }
  651. $searches = array();
  652. foreach ( $columns as $column ) {
  653. $searches[] = $wpdb->prepare( "$column LIKE %s", $like );
  654. }
  655. return '(' . implode( ' OR ', $searches ) . ')';
  656. }
  657. /**
  658. * Parses and sanitizes 'orderby' keys passed to the site query.
  659. *
  660. * @since 4.6.0
  661. *
  662. * @global wpdb $wpdb WordPress database abstraction object.
  663. *
  664. * @param string $orderby Alias for the field to order by.
  665. * @return string|false Value to used in the ORDER clause. False otherwise.
  666. */
  667. protected function parse_orderby( $orderby ) {
  668. global $wpdb;
  669. $parsed = false;
  670. switch ( $orderby ) {
  671. case 'site__in':
  672. $site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
  673. $parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
  674. break;
  675. case 'network__in':
  676. $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
  677. $parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
  678. break;
  679. case 'domain':
  680. case 'last_updated':
  681. case 'path':
  682. case 'registered':
  683. case 'deleted':
  684. case 'spam':
  685. case 'mature':
  686. case 'archived':
  687. case 'public':
  688. $parsed = $orderby;
  689. break;
  690. case 'network_id':
  691. $parsed = 'site_id';
  692. break;
  693. case 'domain_length':
  694. $parsed = 'CHAR_LENGTH(domain)';
  695. break;
  696. case 'path_length':
  697. $parsed = 'CHAR_LENGTH(path)';
  698. break;
  699. case 'id':
  700. $parsed = "{$wpdb->blogs}.blog_id";
  701. break;
  702. }
  703. if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) {
  704. return $parsed;
  705. }
  706. $meta_clauses = $this->meta_query->get_clauses();
  707. if ( empty( $meta_clauses ) ) {
  708. return $parsed;
  709. }
  710. $primary_meta_query = reset( $meta_clauses );
  711. if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) {
  712. $orderby = 'meta_value';
  713. }
  714. switch ( $orderby ) {
  715. case 'meta_value':
  716. if ( ! empty( $primary_meta_query['type'] ) ) {
  717. $parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
  718. } else {
  719. $parsed = "{$primary_meta_query['alias']}.meta_value";
  720. }
  721. break;
  722. case 'meta_value_num':
  723. $parsed = "{$primary_meta_query['alias']}.meta_value+0";
  724. break;
  725. default:
  726. if ( isset( $meta_clauses[ $orderby ] ) ) {
  727. $meta_clause = $meta_clauses[ $orderby ];
  728. $parsed = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
  729. }
  730. }
  731. return $parsed;
  732. }
  733. /**
  734. * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary.
  735. *
  736. * @since 4.6.0
  737. *
  738. * @param string $order The 'order' query variable.
  739. * @return string The sanitized 'order' query variable.
  740. */
  741. protected function parse_order( $order ) {
  742. if ( ! is_string( $order ) || empty( $order ) ) {
  743. return 'ASC';
  744. }
  745. if ( 'ASC' === strtoupper( $order ) ) {
  746. return 'ASC';
  747. } else {
  748. return 'DESC';
  749. }
  750. }
  751. }