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