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