[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-site-query.php (source)

   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       * @var string
  24       */
  25      public $request;
  26  
  27      /**
  28       * SQL query clauses.
  29       *
  30       * @since 4.6.0
  31       * @var array
  32       */
  33      protected $sql_clauses = array(
  34          'select'  => '',
  35          'from'    => '',
  36          'where'   => array(),
  37          'groupby' => '',
  38          'orderby' => '',
  39          'limits'  => '',
  40      );
  41  
  42      /**
  43       * Metadata query container.
  44       *
  45       * @since 5.1.0
  46       * @var WP_Meta_Query
  47       */
  48      public $meta_query = false;
  49  
  50      /**
  51       * Metadata query clauses.
  52       *
  53       * @since 5.1.0
  54       * @var array
  55       */
  56      protected $meta_query_clauses;
  57  
  58      /**
  59       * Date query container.
  60       *
  61       * @since 4.6.0
  62       * @var WP_Date_Query A date query instance.
  63       */
  64      public $date_query = false;
  65  
  66      /**
  67       * Query vars set by the user.
  68       *
  69       * @since 4.6.0
  70       * @var array
  71       */
  72      public $query_vars;
  73  
  74      /**
  75       * Default values for query vars.
  76       *
  77       * @since 4.6.0
  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       * @var array
  87       */
  88      public $sites;
  89  
  90      /**
  91       * The amount of found sites for the current query.
  92       *
  93       * @since 4.6.0
  94       * @var int
  95       */
  96      public $found_sites = 0;
  97  
  98      /**
  99       * The number of pages.
 100       *
 101       * @since 4.6.0
 102       * @var int
 103       */
 104      public $max_num_pages = 0;
 105  
 106      /**
 107       * Sets up the site query, based on the query vars passed.
 108       *
 109       * @since 4.6.0
 110       * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
 111       * @since 5.1.0 Introduced the 'update_site_meta_cache', 'meta_query', 'meta_key',
 112       *              'meta_value', 'meta_type' and 'meta_compare' parameters.
 113       *
 114       * @param string|array $query {
 115       *     Optional. Array or query string of site query parameters. Default empty.
 116       *
 117       *     @type array        $site__in               Array of site IDs to include. Default empty.
 118       *     @type array        $site__not_in           Array of site IDs to exclude. Default empty.
 119       *     @type bool         $count                  Whether to return a site count (true) or array of site objects.
 120       *                                                Default false.
 121       *     @type array        $date_query             Date query clauses to limit sites by. See WP_Date_Query.
 122       *                                                Default null.
 123       *     @type string       $fields                 Site fields to return. Accepts 'ids' (returns an array of site IDs)
 124       *                                                or empty (returns an array of complete site objects). Default empty.
 125       *     @type int          $ID                     A site ID to only return that site. Default empty.
 126       *     @type int          $number                 Maximum number of sites to retrieve. Default 100.
 127       *     @type int          $offset                 Number of sites to offset the query. Used to build LIMIT clause.
 128       *                                                Default 0.
 129       *     @type bool         $no_found_rows          Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
 130       *     @type string|array $orderby                Site status or array of statuses. Accepts 'id', 'domain', 'path',
 131       *                                                'network_id', 'last_updated', 'registered', 'domain_length',
 132       *                                                'path_length', 'site__in' and 'network__in'. Also accepts false,
 133       *                                                an empty array, or 'none' to disable `ORDER BY` clause.
 134       *                                                Default 'id'.
 135       *     @type string       $order                  How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
 136       *     @type int          $network_id             Limit results to those affiliated with a given network ID. If 0,
 137       *                                                include all networks. Default 0.
 138       *     @type array        $network__in            Array of network IDs to include affiliated sites for. Default empty.
 139       *     @type array        $network__not_in        Array of network IDs to exclude affiliated sites for. Default empty.
 140       *     @type string       $domain                 Limit results to those affiliated with a given domain. Default empty.
 141       *     @type array        $domain__in             Array of domains to include affiliated sites for. Default empty.
 142       *     @type array        $domain__not_in         Array of domains to exclude affiliated sites for. Default empty.
 143       *     @type string       $path                   Limit results to those affiliated with a given path. Default empty.
 144       *     @type array        $path__in               Array of paths to include affiliated sites for. Default empty.
 145       *     @type array        $path__not_in           Array of paths to exclude affiliated sites for. Default empty.
 146       *     @type int          $public                 Limit results to public sites. Accepts '1' or '0'. Default empty.
 147       *     @type int          $archived               Limit results to archived sites. Accepts '1' or '0'. Default empty.
 148       *     @type int          $mature                 Limit results to mature sites. Accepts '1' or '0'. Default empty.
 149       *     @type int          $spam                   Limit results to spam sites. Accepts '1' or '0'. Default empty.
 150       *     @type int          $deleted                Limit results to deleted sites. Accepts '1' or '0'. Default empty.
 151       *     @type int          $lang_id                Limit results to a language ID. Default empty.
 152       *     @type array        $lang__in               Array of language IDs to include affiliated sites for. Default empty.
 153       *     @type array        $lang__not_in           Array of language IDs to exclude affiliated sites for. Default empty.
 154       *     @type string       $search                 Search term(s) to retrieve matching sites for. Default empty.
 155       *     @type array        $search_columns         Array of column names to be searched. Accepts 'domain' and 'path'.
 156       *                                                Default empty array.
 157       *     @type bool         $update_site_cache      Whether to prime the cache for found sites. Default true.
 158       *     @type bool         $update_site_meta_cache Whether to prime the metadata cache for found sites. Default true.
 159       *     @type array        $meta_query             Meta query clauses to limit retrieved sites by. See `WP_Meta_Query`.
 160       *                                                Default empty.
 161       *     @type string       $meta_key               Limit sites to those matching a specific metadata key.
 162       *                                                Can be used in conjunction with `$meta_value`. Default empty.
 163       *     @type string       $meta_value             Limit sites to those matching a specific metadata value.
 164       *                                                Usually used in conjunction with `$meta_key`. Default empty.
 165       *     @type string       $meta_type              Data type that the `$meta_value` column will be CAST to for
 166       *                                                comparisons. Default empty.
 167       *     @type string       $meta_compare           Comparison operator to test the `$meta_value`. Default empty.
 168       * }
 169       */
 170  	public function __construct( $query = '' ) {
 171          $this->query_var_defaults = array(
 172              'fields'                 => '',
 173              'ID'                     => '',
 174              'site__in'               => '',
 175              'site__not_in'           => '',
 176              'number'                 => 100,
 177              'offset'                 => '',
 178              'no_found_rows'          => true,
 179              'orderby'                => 'id',
 180              'order'                  => 'ASC',
 181              'network_id'             => 0,
 182              'network__in'            => '',
 183              'network__not_in'        => '',
 184              'domain'                 => '',
 185              'domain__in'             => '',
 186              'domain__not_in'         => '',
 187              'path'                   => '',
 188              'path__in'               => '',
 189              'path__not_in'           => '',
 190              'public'                 => null,
 191              'archived'               => null,
 192              'mature'                 => null,
 193              'spam'                   => null,
 194              'deleted'                => null,
 195              'lang_id'                => null,
 196              'lang__in'               => '',
 197              'lang__not_in'           => '',
 198              'search'                 => '',
 199              'search_columns'         => array(),
 200              'count'                  => false,
 201              'date_query'             => null, // See WP_Date_Query.
 202              'update_site_cache'      => true,
 203              'update_site_meta_cache' => true,
 204              'meta_query'             => '',
 205              'meta_key'               => '',
 206              'meta_value'             => '',
 207              'meta_type'              => '',
 208              'meta_compare'           => '',
 209          );
 210  
 211          if ( ! empty( $query ) ) {
 212              $this->query( $query );
 213          }
 214      }
 215  
 216      /**
 217       * Parses arguments passed to the site query with default query parameters.
 218       *
 219       * @since 4.6.0
 220       *
 221       * @see WP_Site_Query::__construct()
 222       *
 223       * @param string|array $query Array or string of WP_Site_Query arguments. See WP_Site_Query::__construct().
 224       */
 225  	public function parse_query( $query = '' ) {
 226          if ( empty( $query ) ) {
 227              $query = $this->query_vars;
 228          }
 229  
 230          $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
 231  
 232          /**
 233           * Fires after the site query vars have been parsed.
 234           *
 235           * @since 4.6.0
 236           *
 237           * @param WP_Site_Query $this The WP_Site_Query instance (passed by reference).
 238           */
 239          do_action_ref_array( 'parse_site_query', array( &$this ) );
 240      }
 241  
 242      /**
 243       * Sets up the WordPress query for retrieving sites.
 244       *
 245       * @since 4.6.0
 246       *
 247       * @param string|array $query Array or URL query string of parameters.
 248       * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
 249       *                   or the number of sites when 'count' is passed as a query var.
 250       */
 251  	public function query( $query ) {
 252          $this->query_vars = wp_parse_args( $query );
 253  
 254          return $this->get_sites();
 255      }
 256  
 257      /**
 258       * Retrieves a list of sites matching the query vars.
 259       *
 260       * @since 4.6.0
 261       *
 262       * @global wpdb $wpdb WordPress database abstraction object.
 263       *
 264       * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
 265       *                   or the number of sites when 'count' is passed as a query var.
 266       */
 267  	public function get_sites() {
 268          global $wpdb;
 269  
 270          $this->parse_query();
 271  
 272          // Parse meta query.
 273          $this->meta_query = new WP_Meta_Query();
 274          $this->meta_query->parse_query_vars( $this->query_vars );
 275  
 276          /**
 277           * Fires before sites are retrieved.
 278           *
 279           * @since 4.6.0
 280           *
 281           * @param WP_Site_Query $this Current instance of WP_Site_Query (passed by reference).
 282           */
 283          do_action_ref_array( 'pre_get_sites', array( &$this ) );
 284  
 285          // Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
 286          $this->meta_query->parse_query_vars( $this->query_vars );
 287          if ( ! empty( $this->meta_query->queries ) ) {
 288              $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
 289          }
 290  
 291          $site_data = null;
 292  
 293          /**
 294           * Filters the site data before the get_sites query takes place.
 295           *
 296           * Return a non-null value to bypass WordPress' default site queries.
 297           *
 298           * The expected return type from this filter depends on the value passed
 299           * in the request query vars:
 300           * - When `$this->query_vars['count']` is set, the filter should return
 301           *   the site count as an integer.
 302           * - When `'ids' === $this->query_vars['fields']`, the filter should return
 303           *   an array of site IDs.
 304           * - Otherwise the filter should return an array of WP_Site objects.
 305           *
 306           * @since 5.2.0
 307           *
 308           * @param array|int|null $site_data Return an array of site data to short-circuit WP's site query,
 309           *                                  the site count as an integer if `$this->query_vars['count']` is set,
 310           *                                  or null to run the normal queries.
 311           * @param WP_Site_Query  $this      The WP_Site_Query instance, passed by reference.
 312           */
 313          $site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
 314  
 315          if ( null !== $site_data ) {
 316              return $site_data;
 317          }
 318  
 319          // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
 320          $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
 321  
 322          // Ignore the $fields argument as the queried result will be the same regardless.
 323          unset( $_args['fields'] );
 324  
 325          $key          = md5( serialize( $_args ) );
 326          $last_changed = wp_cache_get_last_changed( 'sites' );
 327  
 328          $cache_key   = "get_sites:$key:$last_changed";
 329          $cache_value = wp_cache_get( $cache_key, 'sites' );
 330  
 331          if ( false === $cache_value ) {
 332              $site_ids = $this->get_site_ids();
 333              if ( $site_ids ) {
 334                  $this->set_found_sites();
 335              }
 336  
 337              $cache_value = array(
 338                  'site_ids'    => $site_ids,
 339                  'found_sites' => $this->found_sites,
 340              );
 341              wp_cache_add( $cache_key, $cache_value, 'sites' );
 342          } else {
 343              $site_ids          = $cache_value['site_ids'];
 344              $this->found_sites = $cache_value['found_sites'];
 345          }
 346  
 347          if ( $this->found_sites && $this->query_vars['number'] ) {
 348              $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
 349          }
 350  
 351          // If querying for a count only, there's nothing more to do.
 352          if ( $this->query_vars['count'] ) {
 353              // $site_ids is actually a count in this case.
 354              return (int) $site_ids;
 355          }
 356  
 357          $site_ids = array_map( 'intval', $site_ids );
 358  
 359          if ( 'ids' === $this->query_vars['fields'] ) {
 360              $this->sites = $site_ids;
 361  
 362              return $this->sites;
 363          }
 364  
 365          // Prime site network caches.
 366          if ( $this->query_vars['update_site_cache'] ) {
 367              _prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
 368          }
 369  
 370          // Fetch full site objects from the primed cache.
 371          $_sites = array();
 372          foreach ( $site_ids as $site_id ) {
 373              $_site = get_site( $site_id );
 374              if ( $_site ) {
 375                  $_sites[] = $_site;
 376              }
 377          }
 378  
 379          /**
 380           * Filters the site query results.
 381           *
 382           * @since 4.6.0
 383           *
 384           * @param WP_Site[]     $_sites An array of WP_Site objects.
 385           * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
 386           */
 387          $_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
 388  
 389          // Convert to WP_Site instances.
 390          $this->sites = array_map( 'get_site', $_sites );
 391  
 392          return $this->sites;
 393      }
 394  
 395      /**
 396       * Used internally to get a list of site IDs matching the query vars.
 397       *
 398       * @since 4.6.0
 399       *
 400       * @global wpdb $wpdb WordPress database abstraction object.
 401       *
 402       * @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
 403       */
 404  	protected function get_site_ids() {
 405          global $wpdb;
 406  
 407          $order = $this->parse_order( $this->query_vars['order'] );
 408  
 409          // Disable ORDER BY with 'none', an empty array, or boolean false.
 410          if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
 411              $orderby = '';
 412          } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
 413              $ordersby = is_array( $this->query_vars['orderby'] ) ?
 414                  $this->query_vars['orderby'] :
 415                  preg_split( '/[,\s]/', $this->query_vars['orderby'] );
 416  
 417              $orderby_array = array();
 418              foreach ( $ordersby as $_key => $_value ) {
 419                  if ( ! $_value ) {
 420                      continue;
 421                  }
 422  
 423                  if ( is_int( $_key ) ) {
 424                      $_orderby = $_value;
 425                      $_order   = $order;
 426                  } else {
 427                      $_orderby = $_key;
 428                      $_order   = $_value;
 429                  }
 430  
 431                  $parsed = $this->parse_orderby( $_orderby );
 432  
 433                  if ( ! $parsed ) {
 434                      continue;
 435                  }
 436  
 437                  if ( 'site__in' === $_orderby || 'network__in' === $_orderby ) {
 438                      $orderby_array[] = $parsed;
 439                      continue;
 440                  }
 441  
 442                  $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
 443              }
 444  
 445              $orderby = implode( ', ', $orderby_array );
 446          } else {
 447              $orderby = "{$wpdb->blogs}.blog_id $order";
 448          }
 449  
 450          $number = absint( $this->query_vars['number'] );
 451          $offset = absint( $this->query_vars['offset'] );
 452          $limits = '';
 453  
 454          if ( ! empty( $number ) ) {
 455              if ( $offset ) {
 456                  $limits = 'LIMIT ' . $offset . ',' . $number;
 457              } else {
 458                  $limits = 'LIMIT ' . $number;
 459              }
 460          }
 461  
 462          if ( $this->query_vars['count'] ) {
 463              $fields = 'COUNT(*)';
 464          } else {
 465              $fields = "{$wpdb->blogs}.blog_id";
 466          }
 467  
 468          // Parse site IDs for an IN clause.
 469          $site_id = absint( $this->query_vars['ID'] );
 470          if ( ! empty( $site_id ) ) {
 471              $this->sql_clauses['where']['ID'] = $wpdb->prepare( "{$wpdb->blogs}.blog_id = %d", $site_id );
 472          }
 473  
 474          // Parse site IDs for an IN clause.
 475          if ( ! empty( $this->query_vars['site__in'] ) ) {
 476              $this->sql_clauses['where']['site__in'] = "{$wpdb->blogs}.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
 477          }
 478  
 479          // Parse site IDs for a NOT IN clause.
 480          if ( ! empty( $this->query_vars['site__not_in'] ) ) {
 481              $this->sql_clauses['where']['site__not_in'] = "{$wpdb->blogs}.blog_id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__not_in'] ) ) . ' )';
 482          }
 483  
 484          $network_id = absint( $this->query_vars['network_id'] );
 485  
 486          if ( ! empty( $network_id ) ) {
 487              $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
 488          }
 489  
 490          // Parse site network IDs for an IN clause.
 491          if ( ! empty( $this->query_vars['network__in'] ) ) {
 492              $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
 493          }
 494  
 495          // Parse site network IDs for a NOT IN clause.
 496          if ( ! empty( $this->query_vars['network__not_in'] ) ) {
 497              $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
 498          }
 499  
 500          if ( ! empty( $this->query_vars['domain'] ) ) {
 501              $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
 502          }
 503  
 504          // Parse site domain for an IN clause.
 505          if ( is_array( $this->query_vars['domain__in'] ) ) {
 506              $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
 507          }
 508  
 509          // Parse site domain for a NOT IN clause.
 510          if ( is_array( $this->query_vars['domain__not_in'] ) ) {
 511              $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
 512          }
 513  
 514          if ( ! empty( $this->query_vars['path'] ) ) {
 515              $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
 516          }
 517  
 518          // Parse site path for an IN clause.
 519          if ( is_array( $this->query_vars['path__in'] ) ) {
 520              $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
 521          }
 522  
 523          // Parse site path for a NOT IN clause.
 524          if ( is_array( $this->query_vars['path__not_in'] ) ) {
 525              $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
 526          }
 527  
 528          if ( is_numeric( $this->query_vars['archived'] ) ) {
 529              $archived                               = absint( $this->query_vars['archived'] );
 530              $this->sql_clauses['where']['archived'] = $wpdb->prepare( 'archived = %s ', absint( $archived ) );
 531          }
 532  
 533          if ( is_numeric( $this->query_vars['mature'] ) ) {
 534              $mature                               = absint( $this->query_vars['mature'] );
 535              $this->sql_clauses['where']['mature'] = $wpdb->prepare( 'mature = %d ', $mature );
 536          }
 537  
 538          if ( is_numeric( $this->query_vars['spam'] ) ) {
 539              $spam                               = absint( $this->query_vars['spam'] );
 540              $this->sql_clauses['where']['spam'] = $wpdb->prepare( 'spam = %d ', $spam );
 541          }
 542  
 543          if ( is_numeric( $this->query_vars['deleted'] ) ) {
 544              $deleted                               = absint( $this->query_vars['deleted'] );
 545              $this->sql_clauses['where']['deleted'] = $wpdb->prepare( 'deleted = %d ', $deleted );
 546          }
 547  
 548          if ( is_numeric( $this->query_vars['public'] ) ) {
 549              $public                               = absint( $this->query_vars['public'] );
 550              $this->sql_clauses['where']['public'] = $wpdb->prepare( 'public = %d ', $public );
 551          }
 552  
 553          if ( is_numeric( $this->query_vars['lang_id'] ) ) {
 554              $lang_id                               = absint( $this->query_vars['lang_id'] );
 555              $this->sql_clauses['where']['lang_id'] = $wpdb->prepare( 'lang_id = %d ', $lang_id );
 556          }
 557  
 558          // Parse site language IDs for an IN clause.
 559          if ( ! empty( $this->query_vars['lang__in'] ) ) {
 560              $this->sql_clauses['where']['lang__in'] = 'lang_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__in'] ) ) . ' )';
 561          }
 562  
 563          // Parse site language IDs for a NOT IN clause.
 564          if ( ! empty( $this->query_vars['lang__not_in'] ) ) {
 565              $this->sql_clauses['where']['lang__not_in'] = 'lang_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['lang__not_in'] ) ) . ' )';
 566          }
 567  
 568          // Falsey search strings are ignored.
 569          if ( strlen( $this->query_vars['search'] ) ) {
 570              $search_columns = array();
 571  
 572              if ( $this->query_vars['search_columns'] ) {
 573                  $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) );
 574              }
 575  
 576              if ( ! $search_columns ) {
 577                  $search_columns = array( 'domain', 'path' );
 578              }
 579  
 580              /**
 581               * Filters the columns to search in a WP_Site_Query search.
 582               *
 583               * The default columns include 'domain' and 'path.
 584               *
 585               * @since 4.6.0
 586               *
 587               * @param string[]      $search_columns Array of column names to be searched.
 588               * @param string        $search         Text being searched.
 589               * @param WP_Site_Query $this           The current WP_Site_Query instance.
 590               */
 591              $search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
 592  
 593              $this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
 594          }
 595  
 596          $date_query = $this->query_vars['date_query'];
 597          if ( ! empty( $date_query ) && is_array( $date_query ) ) {
 598              $this->date_query                         = new WP_Date_Query( $date_query, 'registered' );
 599              $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
 600          }
 601  
 602          $join    = '';
 603          $groupby = '';
 604  
 605          if ( ! empty( $this->meta_query_clauses ) ) {
 606              $join .= $this->meta_query_clauses['join'];
 607  
 608              // Strip leading 'AND'.
 609              $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
 610  
 611              if ( ! $this->query_vars['count'] ) {
 612                  $groupby = "{$wpdb->blogs}.blog_id";
 613              }
 614          }
 615  
 616          $where = implode( ' AND ', $this->sql_clauses['where'] );
 617  
 618          $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
 619  
 620          /**
 621           * Filters the site query clauses.
 622           *
 623           * @since 4.6.0
 624           *
 625           * @param string[]      $pieces An associative array of site query clauses.
 626           * @param WP_Site_Query $this   Current instance of WP_Site_Query (passed by reference).
 627           */
 628          $clauses = apply_filters_ref_array( 'sites_clauses', array( compact( $pieces ), &$this ) );
 629  
 630          $fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
 631          $join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
 632          $where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
 633          $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
 634          $limits  = isset( $clauses['limits'] ) ? $clauses['limits'] : '';
 635          $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : '';
 636  
 637          if ( $where ) {
 638              $where = 'WHERE ' . $where;
 639          }
 640  
 641          if ( $groupby ) {
 642              $groupby = 'GROUP BY ' . $groupby;
 643          }
 644  
 645          if ( $orderby ) {
 646              $orderby = "ORDER BY $orderby";
 647          }
 648  
 649          $found_rows = '';
 650          if ( ! $this->query_vars['no_found_rows'] ) {
 651              $found_rows = 'SQL_CALC_FOUND_ROWS';
 652          }
 653  
 654          $this->sql_clauses['select']  = "SELECT $found_rows $fields";
 655          $this->sql_clauses['from']    = "FROM $wpdb->blogs $join";
 656          $this->sql_clauses['groupby'] = $groupby;
 657          $this->sql_clauses['orderby'] = $orderby;
 658          $this->sql_clauses['limits']  = $limits;
 659  
 660          $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
 661  
 662          if ( $this->query_vars['count'] ) {
 663              return (int) $wpdb->get_var( $this->request );
 664          }
 665  
 666          $site_ids = $wpdb->get_col( $this->request );
 667  
 668          return array_map( 'intval', $site_ids );
 669      }
 670  
 671      /**
 672       * Populates found_sites and max_num_pages properties for the current query
 673       * if the limit clause was used.
 674       *
 675       * @since 4.6.0
 676       *
 677       * @global wpdb $wpdb WordPress database abstraction object.
 678       */
 679  	private function set_found_sites() {
 680          global $wpdb;
 681  
 682          if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
 683              /**
 684               * Filters the query used to retrieve found site count.
 685               *
 686               * @since 4.6.0
 687               *
 688               * @param string        $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
 689               * @param WP_Site_Query $site_query        The `WP_Site_Query` instance.
 690               */
 691              $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
 692  
 693              $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
 694          }
 695      }
 696  
 697      /**
 698       * Used internally to generate an SQL string for searching across multiple columns.
 699       *
 700       * @since 4.6.0
 701       *
 702       * @global wpdb $wpdb WordPress database abstraction object.
 703       *
 704       * @param string   $string  Search string.
 705       * @param string[] $columns Array of columns to search.
 706       * @return string Search SQL.
 707       */
 708  	protected function get_search_sql( $string, $columns ) {
 709          global $wpdb;
 710  
 711          if ( false !== strpos( $string, '*' ) ) {
 712              $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
 713          } else {
 714              $like = '%' . $wpdb->esc_like( $string ) . '%';
 715          }
 716  
 717          $searches = array();
 718          foreach ( $columns as $column ) {
 719              $searches[] = $wpdb->prepare( "$column LIKE %s", $like );
 720          }
 721  
 722          return '(' . implode( ' OR ', $searches ) . ')';
 723      }
 724  
 725      /**
 726       * Parses and sanitizes 'orderby' keys passed to the site query.
 727       *
 728       * @since 4.6.0
 729       *
 730       * @global wpdb $wpdb WordPress database abstraction object.
 731       *
 732       * @param string $orderby Alias for the field to order by.
 733       * @return string|false Value to used in the ORDER clause. False otherwise.
 734       */
 735  	protected function parse_orderby( $orderby ) {
 736          global $wpdb;
 737  
 738          $parsed = false;
 739  
 740          switch ( $orderby ) {
 741              case 'site__in':
 742                  $site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
 743                  $parsed   = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
 744                  break;
 745              case 'network__in':
 746                  $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
 747                  $parsed      = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
 748                  break;
 749              case 'domain':
 750              case 'last_updated':
 751              case 'path':
 752              case 'registered':
 753                  $parsed = $orderby;
 754                  break;
 755              case 'network_id':
 756                  $parsed = 'site_id';
 757                  break;
 758              case 'domain_length':
 759                  $parsed = 'CHAR_LENGTH(domain)';
 760                  break;
 761              case 'path_length':
 762                  $parsed = 'CHAR_LENGTH(path)';
 763                  break;
 764              case 'id':
 765                  $parsed = "{$wpdb->blogs}.blog_id";
 766                  break;
 767          }
 768  
 769          if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) {
 770              return $parsed;
 771          }
 772  
 773          $meta_clauses = $this->meta_query->get_clauses();
 774          if ( empty( $meta_clauses ) ) {
 775              return $parsed;
 776          }
 777  
 778          $primary_meta_query = reset( $meta_clauses );
 779          if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) {
 780              $orderby = 'meta_value';
 781          }
 782  
 783          switch ( $orderby ) {
 784              case 'meta_value':
 785                  if ( ! empty( $primary_meta_query['type'] ) ) {
 786                      $parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
 787                  } else {
 788                      $parsed = "{$primary_meta_query['alias']}.meta_value";
 789                  }
 790                  break;
 791              case 'meta_value_num':
 792                  $parsed = "{$primary_meta_query['alias']}.meta_value+0";
 793                  break;
 794              default:
 795                  if ( isset( $meta_clauses[ $orderby ] ) ) {
 796                      $meta_clause = $meta_clauses[ $orderby ];
 797                      $parsed      = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
 798                  }
 799          }
 800  
 801          return $parsed;
 802      }
 803  
 804      /**
 805       * Parses an 'order' query variable and cast it to 'ASC' or 'DESC' as necessary.
 806       *
 807       * @since 4.6.0
 808       *
 809       * @param string $order The 'order' query variable.
 810       * @return string The sanitized 'order' query variable.
 811       */
 812  	protected function parse_order( $order ) {
 813          if ( ! is_string( $order ) || empty( $order ) ) {
 814              return 'ASC';
 815          }
 816  
 817          if ( 'ASC' === strtoupper( $order ) ) {
 818              return 'ASC';
 819          } else {
 820              return 'DESC';
 821          }
 822      }
 823  }


Generated : Fri Oct 30 08:20:01 2020 Cross-referenced by PHPXref