No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

560 líneas
17 KiB

  1. <?php
  2. /**
  3. * Network API: WP_Network_Query class
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used for querying networks.
  11. *
  12. * @since 4.6.0
  13. *
  14. * @see WP_Network_Query::__construct() for accepted arguments.
  15. */
  16. class WP_Network_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. * Query vars set by the user.
  42. *
  43. * @since 4.6.0
  44. * @access public
  45. * @var array
  46. */
  47. public $query_vars;
  48. /**
  49. * Default values for query vars.
  50. *
  51. * @since 4.6.0
  52. * @access public
  53. * @var array
  54. */
  55. public $query_var_defaults;
  56. /**
  57. * List of networks located by the query.
  58. *
  59. * @since 4.6.0
  60. * @access public
  61. * @var array
  62. */
  63. public $networks;
  64. /**
  65. * The amount of found networks for the current query.
  66. *
  67. * @since 4.6.0
  68. * @access public
  69. * @var int
  70. */
  71. public $found_networks = 0;
  72. /**
  73. * The number of pages.
  74. *
  75. * @since 4.6.0
  76. * @access public
  77. * @var int
  78. */
  79. public $max_num_pages = 0;
  80. /**
  81. * Constructor.
  82. *
  83. * Sets up the network query, based on the query vars passed.
  84. *
  85. * @since 4.6.0
  86. * @access public
  87. *
  88. * @param string|array $query {
  89. * Optional. Array or query string of network query parameters. Default empty.
  90. *
  91. * @type array $network__in Array of network IDs to include. Default empty.
  92. * @type array $network__not_in Array of network IDs to exclude. Default empty.
  93. * @type bool $count Whether to return a network count (true) or array of network objects.
  94. * Default false.
  95. * @type string $fields Network fields to return. Accepts 'ids' (returns an array of network IDs)
  96. * or empty (returns an array of complete network objects). Default empty.
  97. * @type int $number Maximum number of networks to retrieve. Default empty (no limit).
  98. * @type int $offset Number of networks to offset the query. Used to build LIMIT clause.
  99. * Default 0.
  100. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
  101. * @type string|array $orderby Network status or array of statuses. Accepts 'id', 'domain', 'path',
  102. * 'domain_length', 'path_length' and 'network__in'. Also accepts false,
  103. * an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
  104. * @type string $order How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
  105. * @type string $domain Limit results to those affiliated with a given domain. Default empty.
  106. * @type array $domain__in Array of domains to include affiliated networks for. Default empty.
  107. * @type array $domain__not_in Array of domains to exclude affiliated networks for. Default empty.
  108. * @type string $path Limit results to those affiliated with a given path. Default empty.
  109. * @type array $path__in Array of paths to include affiliated networks for. Default empty.
  110. * @type array $path__not_in Array of paths to exclude affiliated networks for. Default empty.
  111. * @type string $search Search term(s) to retrieve matching networks for. Default empty.
  112. * @type bool $update_network_cache Whether to prime the cache for found networks. Default true.
  113. * }
  114. */
  115. public function __construct( $query = '' ) {
  116. $this->query_var_defaults = array(
  117. 'network__in' => '',
  118. 'network__not_in' => '',
  119. 'count' => false,
  120. 'fields' => '',
  121. 'number' => '',
  122. 'offset' => '',
  123. 'no_found_rows' => true,
  124. 'orderby' => 'id',
  125. 'order' => 'ASC',
  126. 'domain' => '',
  127. 'domain__in' => '',
  128. 'domain__not_in' => '',
  129. 'path' => '',
  130. 'path__in' => '',
  131. 'path__not_in' => '',
  132. 'search' => '',
  133. 'update_network_cache' => true,
  134. );
  135. if ( ! empty( $query ) ) {
  136. $this->query( $query );
  137. }
  138. }
  139. /**
  140. * Parses arguments passed to the network query with default query parameters.
  141. *
  142. * @since 4.6.0
  143. *
  144. * @access public
  145. *
  146. * @param string|array $query WP_Network_Query arguments. See WP_Network_Query::__construct()
  147. */
  148. public function parse_query( $query = '' ) {
  149. if ( empty( $query ) ) {
  150. $query = $this->query_vars;
  151. }
  152. $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
  153. /**
  154. * Fires after the network query vars have been parsed.
  155. *
  156. * @since 4.6.0
  157. *
  158. * @param WP_Network_Query &$this The WP_Network_Query instance (passed by reference).
  159. */
  160. do_action_ref_array( 'parse_network_query', array( &$this ) );
  161. }
  162. /**
  163. * Sets up the WordPress query for retrieving networks.
  164. *
  165. * @since 4.6.0
  166. * @access public
  167. *
  168. * @param string|array $query Array or URL query string of parameters.
  169. * @return array|int List of networks, or number of networks when 'count' is passed as a query var.
  170. */
  171. public function query( $query ) {
  172. $this->query_vars = wp_parse_args( $query );
  173. return $this->get_networks();
  174. }
  175. /**
  176. * Gets a list of networks matching the query vars.
  177. *
  178. * @since 4.6.0
  179. * @access public
  180. *
  181. * @return int|array The list of networks.
  182. */
  183. public function get_networks() {
  184. $this->parse_query();
  185. /**
  186. * Fires before networks are retrieved.
  187. *
  188. * @since 4.6.0
  189. *
  190. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference.
  191. */
  192. do_action_ref_array( 'pre_get_networks', array( &$this ) );
  193. // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
  194. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
  195. $last_changed = wp_cache_get_last_changed( 'networks' );
  196. $cache_key = "get_network_ids:$key:$last_changed";
  197. $cache_value = wp_cache_get( $cache_key, 'networks' );
  198. if ( false === $cache_value ) {
  199. $network_ids = $this->get_network_ids();
  200. if ( $network_ids ) {
  201. $this->set_found_networks();
  202. }
  203. $cache_value = array(
  204. 'network_ids' => $network_ids,
  205. 'found_networks' => $this->found_networks,
  206. );
  207. wp_cache_add( $cache_key, $cache_value, 'networks' );
  208. } else {
  209. $network_ids = $cache_value['network_ids'];
  210. $this->found_networks = $cache_value['found_networks'];
  211. }
  212. if ( $this->found_networks && $this->query_vars['number'] ) {
  213. $this->max_num_pages = ceil( $this->found_networks / $this->query_vars['number'] );
  214. }
  215. // If querying for a count only, there's nothing more to do.
  216. if ( $this->query_vars['count'] ) {
  217. // $network_ids is actually a count in this case.
  218. return intval( $network_ids );
  219. }
  220. $network_ids = array_map( 'intval', $network_ids );
  221. if ( 'ids' == $this->query_vars['fields'] ) {
  222. $this->networks = $network_ids;
  223. return $this->networks;
  224. }
  225. if ( $this->query_vars['update_network_cache'] ) {
  226. _prime_network_caches( $network_ids );
  227. }
  228. // Fetch full network objects from the primed cache.
  229. $_networks = array();
  230. foreach ( $network_ids as $network_id ) {
  231. if ( $_network = get_network( $network_id ) ) {
  232. $_networks[] = $_network;
  233. }
  234. }
  235. /**
  236. * Filters the network query results.
  237. *
  238. * @since 4.6.0
  239. *
  240. * @param array $results An array of networks.
  241. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference.
  242. */
  243. $_networks = apply_filters_ref_array( 'the_networks', array( $_networks, &$this ) );
  244. // Convert to WP_Network instances
  245. $this->networks = array_map( 'get_network', $_networks );
  246. return $this->networks;
  247. }
  248. /**
  249. * Used internally to get a list of network IDs matching the query vars.
  250. *
  251. * @since 4.6.0
  252. * @access protected
  253. *
  254. * @return int|array A single count of network IDs if a count query. An array of network IDs if a full query.
  255. */
  256. protected function get_network_ids() {
  257. global $wpdb;
  258. $order = $this->parse_order( $this->query_vars['order'] );
  259. // Disable ORDER BY with 'none', an empty array, or boolean false.
  260. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
  261. $orderby = '';
  262. } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
  263. $ordersby = is_array( $this->query_vars['orderby'] ) ?
  264. $this->query_vars['orderby'] :
  265. preg_split( '/[,\s]/', $this->query_vars['orderby'] );
  266. $orderby_array = array();
  267. foreach ( $ordersby as $_key => $_value ) {
  268. if ( ! $_value ) {
  269. continue;
  270. }
  271. if ( is_int( $_key ) ) {
  272. $_orderby = $_value;
  273. $_order = $order;
  274. } else {
  275. $_orderby = $_key;
  276. $_order = $_value;
  277. }
  278. $parsed = $this->parse_orderby( $_orderby );
  279. if ( ! $parsed ) {
  280. continue;
  281. }
  282. if ( 'network__in' === $_orderby ) {
  283. $orderby_array[] = $parsed;
  284. continue;
  285. }
  286. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
  287. }
  288. $orderby = implode( ', ', $orderby_array );
  289. } else {
  290. $orderby = "$wpdb->site.id $order";
  291. }
  292. $number = absint( $this->query_vars['number'] );
  293. $offset = absint( $this->query_vars['offset'] );
  294. if ( ! empty( $number ) ) {
  295. if ( $offset ) {
  296. $limits = 'LIMIT ' . $offset . ',' . $number;
  297. } else {
  298. $limits = 'LIMIT ' . $number;
  299. }
  300. }
  301. if ( $this->query_vars['count'] ) {
  302. $fields = 'COUNT(*)';
  303. } else {
  304. $fields = "$wpdb->site.id";
  305. }
  306. // Parse network IDs for an IN clause.
  307. if ( ! empty( $this->query_vars['network__in'] ) ) {
  308. $this->sql_clauses['where']['network__in'] = "$wpdb->site.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
  309. }
  310. // Parse network IDs for a NOT IN clause.
  311. if ( ! empty( $this->query_vars['network__not_in'] ) ) {
  312. $this->sql_clauses['where']['network__not_in'] = "$wpdb->site.id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
  313. }
  314. if ( ! empty( $this->query_vars['domain'] ) ) {
  315. $this->sql_clauses['where']['domain'] = $wpdb->prepare( "$wpdb->site.domain = %s", $this->query_vars['domain'] );
  316. }
  317. // Parse network domain for an IN clause.
  318. if ( is_array( $this->query_vars['domain__in'] ) ) {
  319. $this->sql_clauses['where']['domain__in'] = "$wpdb->site.domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
  320. }
  321. // Parse network domain for a NOT IN clause.
  322. if ( is_array( $this->query_vars['domain__not_in'] ) ) {
  323. $this->sql_clauses['where']['domain__not_in'] = "$wpdb->site.domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
  324. }
  325. if ( ! empty( $this->query_vars['path'] ) ) {
  326. $this->sql_clauses['where']['path'] = $wpdb->prepare( "$wpdb->site.path = %s", $this->query_vars['path'] );
  327. }
  328. // Parse network path for an IN clause.
  329. if ( is_array( $this->query_vars['path__in'] ) ) {
  330. $this->sql_clauses['where']['path__in'] = "$wpdb->site.path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
  331. }
  332. // Parse network path for a NOT IN clause.
  333. if ( is_array( $this->query_vars['path__not_in'] ) ) {
  334. $this->sql_clauses['where']['path__not_in'] = "$wpdb->site.path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
  335. }
  336. // Falsey search strings are ignored.
  337. if ( strlen( $this->query_vars['search'] ) ) {
  338. $this->sql_clauses['where']['search'] = $this->get_search_sql(
  339. $this->query_vars['search'],
  340. array( "$wpdb->site.domain", "$wpdb->site.path" )
  341. );
  342. }
  343. $join = '';
  344. $where = implode( ' AND ', $this->sql_clauses['where'] );
  345. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
  346. /**
  347. * Filters the network query clauses.
  348. *
  349. * @since 4.6.0
  350. *
  351. * @param array $pieces A compacted array of network query clauses.
  352. * @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference.
  353. */
  354. $clauses = apply_filters_ref_array( 'networks_clauses', array( compact( $pieces ), &$this ) );
  355. $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
  356. $join = isset( $clauses['join'] ) ? $clauses['join'] : '';
  357. $where = isset( $clauses['where'] ) ? $clauses['where'] : '';
  358. $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
  359. $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
  360. $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
  361. if ( $where ) {
  362. $where = 'WHERE ' . $where;
  363. }
  364. if ( $groupby ) {
  365. $groupby = 'GROUP BY ' . $groupby;
  366. }
  367. if ( $orderby ) {
  368. $orderby = "ORDER BY $orderby";
  369. }
  370. $found_rows = '';
  371. if ( ! $this->query_vars['no_found_rows'] ) {
  372. $found_rows = 'SQL_CALC_FOUND_ROWS';
  373. }
  374. $this->sql_clauses['select'] = "SELECT $found_rows $fields";
  375. $this->sql_clauses['from'] = "FROM $wpdb->site $join";
  376. $this->sql_clauses['groupby'] = $groupby;
  377. $this->sql_clauses['orderby'] = $orderby;
  378. $this->sql_clauses['limits'] = $limits;
  379. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
  380. if ( $this->query_vars['count'] ) {
  381. return intval( $wpdb->get_var( $this->request ) );
  382. }
  383. $network_ids = $wpdb->get_col( $this->request );
  384. return array_map( 'intval', $network_ids );
  385. }
  386. /**
  387. * Populates found_networks and max_num_pages properties for the current query
  388. * if the limit clause was used.
  389. *
  390. * @since 4.6.0
  391. * @access private
  392. *
  393. * @global wpdb $wpdb WordPress database abstraction object.
  394. */
  395. private function set_found_networks() {
  396. global $wpdb;
  397. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
  398. /**
  399. * Filters the query used to retrieve found network count.
  400. *
  401. * @since 4.6.0
  402. *
  403. * @param string $found_networks_query SQL query. Default 'SELECT FOUND_ROWS()'.
  404. * @param WP_Network_Query $network_query The `WP_Network_Query` instance.
  405. */
  406. $found_networks_query = apply_filters( 'found_networks_query', 'SELECT FOUND_ROWS()', $this );
  407. $this->found_networks = (int) $wpdb->get_var( $found_networks_query );
  408. }
  409. }
  410. /**
  411. * Used internally to generate an SQL string for searching across multiple columns.
  412. *
  413. * @since 4.6.0
  414. * @access protected
  415. *
  416. * @global wpdb $wpdb WordPress database abstraction object.
  417. *
  418. * @param string $string Search string.
  419. * @param array $columns Columns to search.
  420. *
  421. * @return string Search SQL.
  422. */
  423. protected function get_search_sql( $string, $columns ) {
  424. global $wpdb;
  425. $like = '%' . $wpdb->esc_like( $string ) . '%';
  426. $searches = array();
  427. foreach ( $columns as $column ) {
  428. $searches[] = $wpdb->prepare( "$column LIKE %s", $like );
  429. }
  430. return '(' . implode( ' OR ', $searches ) . ')';
  431. }
  432. /**
  433. * Parses and sanitizes 'orderby' keys passed to the network query.
  434. *
  435. * @since 4.6.0
  436. * @access protected
  437. *
  438. * @global wpdb $wpdb WordPress database abstraction object.
  439. *
  440. * @param string $orderby Alias for the field to order by.
  441. * @return string|false Value to used in the ORDER clause. False otherwise.
  442. */
  443. protected function parse_orderby( $orderby ) {
  444. global $wpdb;
  445. $allowed_keys = array(
  446. 'id',
  447. 'domain',
  448. 'path',
  449. );
  450. $parsed = false;
  451. if ( $orderby == 'network__in' ) {
  452. $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
  453. $parsed = "FIELD( {$wpdb->site}.id, $network__in )";
  454. } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
  455. $field = substr( $orderby, 0, -7 );
  456. $parsed = "CHAR_LENGTH($wpdb->site.$field)";
  457. } elseif ( in_array( $orderby, $allowed_keys ) ) {
  458. $parsed = "$wpdb->site.$orderby";
  459. }
  460. return $parsed;
  461. }
  462. /**
  463. * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary.
  464. *
  465. * @since 4.6.0
  466. * @access protected
  467. *
  468. * @param string $order The 'order' query variable.
  469. * @return string The sanitized 'order' query variable.
  470. */
  471. protected function parse_order( $order ) {
  472. if ( ! is_string( $order ) || empty( $order ) ) {
  473. return 'ASC';
  474. }
  475. if ( 'ASC' === strtoupper( $order ) ) {
  476. return 'ASC';
  477. } else {
  478. return 'DESC';
  479. }
  480. }
  481. }