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