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