[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Oct 13 01:00:02 2024 Cross-referenced by PHPXref 0.7.1