[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> ms-site.php (source)

   1  <?php
   2  /**
   3   * Site API
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 5.1.0
   8   */
   9  
  10  /**
  11   * Inserts a new site into the database.
  12   *
  13   * @since 5.1.0
  14   *
  15   * @global wpdb $wpdb WordPress database abstraction object.
  16   *
  17   * @param array $data {
  18   *     Data for the new site that should be inserted.
  19   *
  20   *     @type string $domain       Site domain. Default empty string.
  21   *     @type string $path         Site path. Default '/'.
  22   *     @type int    $network_id   The site's network ID. Default is the current network ID.
  23   *     @type string $registered   When the site was registered, in SQL datetime format. Default is
  24   *                                the current time.
  25   *     @type string $last_updated When the site was last updated, in SQL datetime format. Default is
  26   *                                the value of $registered.
  27   *     @type int    $public       Whether the site is public. Default 1.
  28   *     @type int    $archived     Whether the site is archived. Default 0.
  29   *     @type int    $mature       Whether the site is mature. Default 0.
  30   *     @type int    $spam         Whether the site is spam. Default 0.
  31   *     @type int    $deleted      Whether the site is deleted. Default 0.
  32   *     @type int    $lang_id      The site's language ID. Currently unused. Default 0.
  33   *     @type int    $user_id      User ID for the site administrator. Passed to the
  34   *                                `wp_initialize_site` hook.
  35   *     @type string $title        Site title. Default is 'Site %d' where %d is the site ID. Passed
  36   *                                to the `wp_initialize_site` hook.
  37   *     @type array  $options      Custom option $key => $value pairs to use. Default empty array. Passed
  38   *                                to the `wp_initialize_site` hook.
  39   *     @type array  $meta         Custom site metadata $key => $value pairs to use. Default empty array.
  40   *                                Passed to the `wp_initialize_site` hook.
  41   * }
  42   * @return int|WP_Error The new site's ID on success, or error object on failure.
  43   */
  44  function wp_insert_site( array $data ) {
  45      global $wpdb;
  46  
  47      $now = current_time( 'mysql', true );
  48  
  49      $defaults = array(
  50          'domain'       => '',
  51          'path'         => '/',
  52          'network_id'   => get_current_network_id(),
  53          'registered'   => $now,
  54          'last_updated' => $now,
  55          'public'       => 1,
  56          'archived'     => 0,
  57          'mature'       => 0,
  58          'spam'         => 0,
  59          'deleted'      => 0,
  60          'lang_id'      => 0,
  61      );
  62  
  63      $prepared_data = wp_prepare_site_data( $data, $defaults );
  64      if ( is_wp_error( $prepared_data ) ) {
  65          return $prepared_data;
  66      }
  67  
  68      if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
  69          return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error );
  70      }
  71  
  72      $site_id = (int) $wpdb->insert_id;
  73  
  74      clean_blog_cache( $site_id );
  75  
  76      $new_site = get_site( $site_id );
  77  
  78      if ( ! $new_site ) {
  79          return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
  80      }
  81  
  82      /**
  83       * Fires once a site has been inserted into the database.
  84       *
  85       * @since 5.1.0
  86       *
  87       * @param WP_Site $new_site New site object.
  88       */
  89      do_action( 'wp_insert_site', $new_site );
  90  
  91      // Extract the passed arguments that may be relevant for site initialization.
  92      $args = array_diff_key( $data, $defaults );
  93      if ( isset( $args['site_id'] ) ) {
  94          unset( $args['site_id'] );
  95      }
  96  
  97      /**
  98       * Fires when a site's initialization routine should be executed.
  99       *
 100       * @since 5.1.0
 101       *
 102       * @param WP_Site $new_site New site object.
 103       * @param array   $args     Arguments for the initialization.
 104       */
 105      do_action( 'wp_initialize_site', $new_site, $args );
 106  
 107      // Only compute extra hook parameters if the deprecated hook is actually in use.
 108      if ( has_action( 'wpmu_new_blog' ) ) {
 109          $user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0;
 110          $meta    = ! empty( $args['options'] ) ? $args['options'] : array();
 111  
 112          // WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0.
 113          if ( ! array_key_exists( 'WPLANG', $meta ) ) {
 114              $meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' );
 115          }
 116  
 117          // Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys.
 118          // The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`.
 119          $allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
 120          $meta                = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );
 121  
 122          /**
 123           * Fires immediately after a new site is created.
 124           *
 125           * @since MU (3.0.0)
 126           * @deprecated 5.1.0 Use {@see 'wp_insert_site'} instead.
 127           *
 128           * @param int    $site_id    Site ID.
 129           * @param int    $user_id    User ID.
 130           * @param string $domain     Site domain.
 131           * @param string $path       Site path.
 132           * @param int    $network_id Network ID. Only relevant on multi-network installations.
 133           * @param array  $meta       Meta data. Used to set initial site options.
 134           */
 135          do_action_deprecated(
 136              'wpmu_new_blog',
 137              array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ),
 138              '5.1.0',
 139              'wp_insert_site'
 140          );
 141      }
 142  
 143      return (int) $new_site->id;
 144  }
 145  
 146  /**
 147   * Updates a site in the database.
 148   *
 149   * @since 5.1.0
 150   *
 151   * @global wpdb $wpdb WordPress database abstraction object.
 152   *
 153   * @param int   $site_id ID of the site that should be updated.
 154   * @param array $data    Site data to update. See {@see wp_insert_site()} for the list of supported keys.
 155   * @return int|WP_Error The updated site's ID on success, or error object on failure.
 156   */
 157  function wp_update_site( $site_id, array $data ) {
 158      global $wpdb;
 159  
 160      if ( empty( $site_id ) ) {
 161          return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
 162      }
 163  
 164      $old_site = get_site( $site_id );
 165      if ( ! $old_site ) {
 166          return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
 167      }
 168  
 169      $defaults                 = $old_site->to_array();
 170      $defaults['network_id']   = (int) $defaults['site_id'];
 171      $defaults['last_updated'] = current_time( 'mysql', true );
 172      unset( $defaults['blog_id'], $defaults['site_id'] );
 173  
 174      $data = wp_prepare_site_data( $data, $defaults, $old_site );
 175      if ( is_wp_error( $data ) ) {
 176          return $data;
 177      }
 178  
 179      if ( false === $wpdb->update( $wpdb->blogs, $data, array( 'blog_id' => $old_site->id ) ) ) {
 180          return new WP_Error( 'db_update_error', __( 'Could not update site in the database.' ), $wpdb->last_error );
 181      }
 182  
 183      clean_blog_cache( $old_site );
 184  
 185      $new_site = get_site( $old_site->id );
 186  
 187      /**
 188       * Fires once a site has been updated in the database.
 189       *
 190       * @since 5.1.0
 191       *
 192       * @param WP_Site $new_site New site object.
 193       * @param WP_Site $old_site Old site object.
 194       */
 195      do_action( 'wp_update_site', $new_site, $old_site );
 196  
 197      return (int) $new_site->id;
 198  }
 199  
 200  /**
 201   * Deletes a site from the database.
 202   *
 203   * @since 5.1.0
 204   *
 205   * @global wpdb $wpdb WordPress database abstraction object.
 206   *
 207   * @param int $site_id ID of the site that should be deleted.
 208   * @return WP_Site|WP_Error The deleted site object on success, or error object on failure.
 209   */
 210  function wp_delete_site( $site_id ) {
 211      global $wpdb;
 212  
 213      if ( empty( $site_id ) ) {
 214          return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
 215      }
 216  
 217      $old_site = get_site( $site_id );
 218      if ( ! $old_site ) {
 219          return new WP_Error( 'site_not_exist', __( 'Site does not exist.' ) );
 220      }
 221  
 222      $errors = new WP_Error();
 223  
 224      /**
 225       * Fires before a site should be deleted from the database.
 226       *
 227       * Plugins should amend the `$errors` object via its `WP_Error::add()` method. If any errors
 228       * are present, the site will not be deleted.
 229       *
 230       * @since 5.1.0
 231       *
 232       * @param WP_Error $errors   Error object to add validation errors to.
 233       * @param WP_Site  $old_site The site object to be deleted.
 234       */
 235      do_action( 'wp_validate_site_deletion', $errors, $old_site );
 236  
 237      if ( ! empty( $errors->errors ) ) {
 238          return $errors;
 239      }
 240  
 241      /**
 242       * Fires before a site is deleted.
 243       *
 244       * @since MU (3.0.0)
 245       * @deprecated 5.1.0
 246       *
 247       * @param int  $site_id The site ID.
 248       * @param bool $drop    True if site's table should be dropped. Default false.
 249       */
 250      do_action_deprecated( 'delete_blog', array( $old_site->id, true ), '5.1.0' );
 251  
 252      /**
 253       * Fires when a site's uninitialization routine should be executed.
 254       *
 255       * @since 5.1.0
 256       *
 257       * @param WP_Site $old_site Deleted site object.
 258       */
 259      do_action( 'wp_uninitialize_site', $old_site );
 260  
 261      if ( is_site_meta_supported() ) {
 262          $blog_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->blogmeta WHERE blog_id = %d ", $old_site->id ) );
 263          foreach ( $blog_meta_ids as $mid ) {
 264              delete_metadata_by_mid( 'blog', $mid );
 265          }
 266      }
 267  
 268      if ( false === $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $old_site->id ) ) ) {
 269          return new WP_Error( 'db_delete_error', __( 'Could not delete site from the database.' ), $wpdb->last_error );
 270      }
 271  
 272      clean_blog_cache( $old_site );
 273  
 274      /**
 275       * Fires once a site has been deleted from the database.
 276       *
 277       * @since 5.1.0
 278       *
 279       * @param WP_Site $old_site Deleted site object.
 280       */
 281      do_action( 'wp_delete_site', $old_site );
 282  
 283      /**
 284       * Fires after the site is deleted from the network.
 285       *
 286       * @since 4.8.0
 287       * @deprecated 5.1.0
 288       *
 289       * @param int  $site_id The site ID.
 290       * @param bool $drop    True if site's tables should be dropped. Default false.
 291       */
 292      do_action_deprecated( 'deleted_blog', array( $old_site->id, true ), '5.1.0' );
 293  
 294      return $old_site;
 295  }
 296  
 297  /**
 298   * Retrieves site data given a site ID or site object.
 299   *
 300   * Site data will be cached and returned after being passed through a filter.
 301   * If the provided site is empty, the current site global will be used.
 302   *
 303   * @since 4.6.0
 304   *
 305   * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site.
 306   * @return WP_Site|null The site object or null if not found.
 307   */
 308  function get_site( $site = null ) {
 309      if ( empty( $site ) ) {
 310          $site = get_current_blog_id();
 311      }
 312  
 313      if ( $site instanceof WP_Site ) {
 314          $_site = $site;
 315      } elseif ( is_object( $site ) ) {
 316          $_site = new WP_Site( $site );
 317      } else {
 318          $_site = WP_Site::get_instance( $site );
 319      }
 320  
 321      if ( ! $_site ) {
 322          return null;
 323      }
 324  
 325      /**
 326       * Fires after a site is retrieved.
 327       *
 328       * @since 4.6.0
 329       *
 330       * @param WP_Site $_site Site data.
 331       */
 332      $_site = apply_filters( 'get_site', $_site );
 333  
 334      return $_site;
 335  }
 336  
 337  /**
 338   * Adds any sites from the given IDs to the cache that do not already exist in cache.
 339   *
 340   * @since 4.6.0
 341   * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
 342   * @access private
 343   *
 344   * @see update_site_cache()
 345   * @global wpdb $wpdb WordPress database abstraction object.
 346   *
 347   * @param array $ids               ID list.
 348   * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
 349   */
 350  function _prime_site_caches( $ids, $update_meta_cache = true ) {
 351      global $wpdb;
 352  
 353      $non_cached_ids = _get_non_cached_ids( $ids, 'sites' );
 354      if ( ! empty( $non_cached_ids ) ) {
 355          $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
 356  
 357          update_site_cache( $fresh_sites, $update_meta_cache );
 358      }
 359  }
 360  
 361  /**
 362   * Updates sites in cache.
 363   *
 364   * @since 4.6.0
 365   * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
 366   *
 367   * @param array $sites             Array of site objects.
 368   * @param bool  $update_meta_cache Whether to update site meta cache. Default true.
 369   */
 370  function update_site_cache( $sites, $update_meta_cache = true ) {
 371      if ( ! $sites ) {
 372          return;
 373      }
 374      $site_ids = array();
 375      foreach ( $sites as $site ) {
 376          $site_ids[] = $site->blog_id;
 377          wp_cache_add( $site->blog_id, $site, 'sites' );
 378          wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' );
 379      }
 380  
 381      if ( $update_meta_cache ) {
 382          update_sitemeta_cache( $site_ids );
 383      }
 384  }
 385  
 386  /**
 387   * Updates metadata cache for list of site IDs.
 388   *
 389   * Performs SQL query to retrieve all metadata for the sites matching `$site_ids` and stores them in the cache.
 390   * Subsequent calls to `get_site_meta()` will not need to query the database.
 391   *
 392   * @since 5.1.0
 393   *
 394   * @param array $site_ids List of site IDs.
 395   * @return array|false An array of metadata on success, false if there is nothing to update.
 396   */
 397  function update_sitemeta_cache( $site_ids ) {
 398      // Ensure this filter is hooked in even if the function is called early.
 399      if ( ! has_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' ) ) {
 400          add_filter( 'update_blog_metadata_cache', 'wp_check_site_meta_support_prefilter' );
 401      }
 402      return update_meta_cache( 'blog', $site_ids );
 403  }
 404  
 405  /**
 406   * Retrieves a list of sites matching requested arguments.
 407   *
 408   * @since 4.6.0
 409   * @since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.
 410   *
 411   * @see WP_Site_Query::parse_query()
 412   *
 413   * @param string|array $args {
 414   *     Optional. Array or query string of site query parameters. Default empty.
 415   *
 416   *     @type array        $site__in          Array of site IDs to include. Default empty.
 417   *     @type array        $site__not_in      Array of site IDs to exclude. Default empty.
 418   *     @type bool         $count             Whether to return a site count (true) or array of site objects.
 419   *                                           Default false.
 420   *     @type array        $date_query        Date query clauses to limit sites by. See WP_Date_Query.
 421   *                                           Default null.
 422   *     @type string       $fields            Site fields to return. Accepts 'ids' (returns an array of site IDs)
 423   *                                           or empty (returns an array of complete site objects). Default empty.
 424   *     @type int          $ID                A site ID to only return that site. Default empty.
 425   *     @type int          $number            Maximum number of sites to retrieve. Default 100.
 426   *     @type int          $offset            Number of sites to offset the query. Used to build LIMIT clause.
 427   *                                           Default 0.
 428   *     @type bool         $no_found_rows     Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
 429   *     @type string|array $orderby           Site status or array of statuses. Accepts 'id', 'domain', 'path',
 430   *                                           'network_id', 'last_updated', 'registered', 'domain_length',
 431   *                                           'path_length', 'site__in' and 'network__in'. Also accepts false,
 432   *                                           an empty array, or 'none' to disable `ORDER BY` clause.
 433   *                                           Default 'id'.
 434   *     @type string       $order             How to order retrieved sites. Accepts 'ASC', 'DESC'. Default 'ASC'.
 435   *     @type int          $network_id        Limit results to those affiliated with a given network ID. If 0,
 436   *                                           include all networks. Default 0.
 437   *     @type array        $network__in       Array of network IDs to include affiliated sites for. Default empty.
 438   *     @type array        $network__not_in   Array of network IDs to exclude affiliated sites for. Default empty.
 439   *     @type string       $domain            Limit results to those affiliated with a given domain. Default empty.
 440   *     @type array        $domain__in        Array of domains to include affiliated sites for. Default empty.
 441   *     @type array        $domain__not_in    Array of domains to exclude affiliated sites for. Default empty.
 442   *     @type string       $path              Limit results to those affiliated with a given path. Default empty.
 443   *     @type array        $path__in          Array of paths to include affiliated sites for. Default empty.
 444   *     @type array        $path__not_in      Array of paths to exclude affiliated sites for. Default empty.
 445   *     @type int          $public            Limit results to public sites. Accepts '1' or '0'. Default empty.
 446   *     @type int          $archived          Limit results to archived sites. Accepts '1' or '0'. Default empty.
 447   *     @type int          $mature            Limit results to mature sites. Accepts '1' or '0'. Default empty.
 448   *     @type int          $spam              Limit results to spam sites. Accepts '1' or '0'. Default empty.
 449   *     @type int          $deleted           Limit results to deleted sites. Accepts '1' or '0'. Default empty.
 450   *     @type int          $lang_id           Limit results to a language ID. Default empty.
 451   *     @type array        $lang__in          Array of language IDs to include affiliated sites for. Default empty.
 452   *     @type array        $lang__not_in      Array of language IDs to exclude affiliated sites for. Default empty.
 453   *     @type string       $search            Search term(s) to retrieve matching sites for. Default empty.
 454   *     @type array        $search_columns    Array of column names to be searched. Accepts 'domain' and 'path'.
 455   *                                           Default empty array.
 456   *     @type bool         $update_site_cache Whether to prime the cache for found sites. Default true.
 457   * }
 458   * @return array|int List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids',
 459   *                   or the number of sites when 'count' is passed as a query var.
 460   */
 461  function get_sites( $args = array() ) {
 462      $query = new WP_Site_Query();
 463  
 464      return $query->query( $args );
 465  }
 466  
 467  /**
 468   * Prepares site data for insertion or update in the database.
 469   *
 470   * @since 5.1.0
 471   *
 472   * @param array        $data     Associative array of site data passed to the respective function.
 473   *                               See {@see wp_insert_site()} for the possibly included data.
 474   * @param array        $defaults Site data defaults to parse $data against.
 475   * @param WP_Site|null $old_site Optional. Old site object if an update, or null if an insertion.
 476   *                               Default null.
 477   * @return array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation
 478   *                        error occurred.
 479   */
 480  function wp_prepare_site_data( $data, $defaults, $old_site = null ) {
 481  
 482      // Maintain backward-compatibility with `$site_id` as network ID.
 483      if ( isset( $data['site_id'] ) ) {
 484          if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) {
 485              $data['network_id'] = $data['site_id'];
 486          }
 487          unset( $data['site_id'] );
 488      }
 489  
 490      /**
 491       * Filters passed site data in order to normalize it.
 492       *
 493       * @since 5.1.0
 494       *
 495       * @param array $data Associative array of site data passed to the respective function.
 496       *                    See {@see wp_insert_site()} for the possibly included data.
 497       */
 498      $data = apply_filters( 'wp_normalize_site_data', $data );
 499  
 500      $allowed_data_fields = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
 501      $data                = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $allowed_data_fields ) );
 502  
 503      $errors = new WP_Error();
 504  
 505      /**
 506       * Fires when data should be validated for a site prior to inserting or updating in the database.
 507       *
 508       * Plugins should amend the `$errors` object via its `WP_Error::add()` method.
 509       *
 510       * @since 5.1.0
 511       *
 512       * @param WP_Error     $errors   Error object to add validation errors to.
 513       * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}
 514       *                               for the included data.
 515       * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
 516       *                               or null if it is a new site being inserted.
 517       */
 518      do_action( 'wp_validate_site_data', $errors, $data, $old_site );
 519  
 520      if ( ! empty( $errors->errors ) ) {
 521          return $errors;
 522      }
 523  
 524      // Prepare for database.
 525      $data['site_id'] = $data['network_id'];
 526      unset( $data['network_id'] );
 527  
 528      return $data;
 529  }
 530  
 531  /**
 532   * Normalizes data for a site prior to inserting or updating in the database.
 533   *
 534   * @since 5.1.0
 535   *
 536   * @param array $data Associative array of site data passed to the respective function.
 537   *                    See {@see wp_insert_site()} for the possibly included data.
 538   * @return array Normalized site data.
 539   */
 540  function wp_normalize_site_data( $data ) {
 541      // Sanitize domain if passed.
 542      if ( array_key_exists( 'domain', $data ) ) {
 543          $data['domain'] = trim( $data['domain'] );
 544          $data['domain'] = preg_replace( '/\s+/', '', sanitize_user( $data['domain'], true ) );
 545          if ( is_subdomain_install() ) {
 546              $data['domain'] = str_replace( '@', '', $data['domain'] );
 547          }
 548      }
 549  
 550      // Sanitize path if passed.
 551      if ( array_key_exists( 'path', $data ) ) {
 552          $data['path'] = trailingslashit( '/' . trim( $data['path'], '/' ) );
 553      }
 554  
 555      // Sanitize network ID if passed.
 556      if ( array_key_exists( 'network_id', $data ) ) {
 557          $data['network_id'] = (int) $data['network_id'];
 558      }
 559  
 560      // Sanitize status fields if passed.
 561      $status_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
 562      foreach ( $status_fields as $status_field ) {
 563          if ( array_key_exists( $status_field, $data ) ) {
 564              $data[ $status_field ] = (int) $data[ $status_field ];
 565          }
 566      }
 567  
 568      // Strip date fields if empty.
 569      $date_fields = array( 'registered', 'last_updated' );
 570      foreach ( $date_fields as $date_field ) {
 571          if ( ! array_key_exists( $date_field, $data ) ) {
 572              continue;
 573          }
 574  
 575          if ( empty( $data[ $date_field ] ) || '0000-00-00 00:00:00' === $data[ $date_field ] ) {
 576              unset( $data[ $date_field ] );
 577          }
 578      }
 579  
 580      return $data;
 581  }
 582  
 583  /**
 584   * Validates data for a site prior to inserting or updating in the database.
 585   *
 586   * @since 5.1.0
 587   *
 588   * @param WP_Error     $errors   Error object, passed by reference. Will contain validation errors if
 589   *                               any occurred.
 590   * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}
 591   *                               for the included data.
 592   * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,
 593   *                               or null if it is a new site being inserted.
 594   */
 595  function wp_validate_site_data( $errors, $data, $old_site = null ) {
 596      // A domain must always be present.
 597      if ( empty( $data['domain'] ) ) {
 598          $errors->add( 'site_empty_domain', __( 'Site domain must not be empty.' ) );
 599      }
 600  
 601      // A path must always be present.
 602      if ( empty( $data['path'] ) ) {
 603          $errors->add( 'site_empty_path', __( 'Site path must not be empty.' ) );
 604      }
 605  
 606      // A network ID must always be present.
 607      if ( empty( $data['network_id'] ) ) {
 608          $errors->add( 'site_empty_network_id', __( 'Site network ID must be provided.' ) );
 609      }
 610  
 611      // Both registration and last updated dates must always be present and valid.
 612      $date_fields = array( 'registered', 'last_updated' );
 613      foreach ( $date_fields as $date_field ) {
 614          if ( empty( $data[ $date_field ] ) ) {
 615              $errors->add( 'site_empty_' . $date_field, __( 'Both registration and last updated dates must be provided.' ) );
 616              break;
 617          }
 618  
 619          // Allow '0000-00-00 00:00:00', although it be stripped out at this point.
 620          if ( '0000-00-00 00:00:00' !== $data[ $date_field ] ) {
 621              $month      = substr( $data[ $date_field ], 5, 2 );
 622              $day        = substr( $data[ $date_field ], 8, 2 );
 623              $year       = substr( $data[ $date_field ], 0, 4 );
 624              $valid_date = wp_checkdate( $month, $day, $year, $data[ $date_field ] );
 625              if ( ! $valid_date ) {
 626                  $errors->add( 'site_invalid_' . $date_field, __( 'Both registration and last updated dates must be valid dates.' ) );
 627                  break;
 628              }
 629          }
 630      }
 631  
 632      if ( ! empty( $errors->errors ) ) {
 633          return;
 634      }
 635  
 636      // If a new site, or domain/path/network ID have changed, ensure uniqueness.
 637      if ( ! $old_site
 638          || $data['domain'] !== $old_site->domain
 639          || $data['path'] !== $old_site->path
 640          || $data['network_id'] !== $old_site->network_id
 641      ) {
 642          if ( domain_exists( $data['domain'], $data['path'], $data['network_id'] ) ) {
 643              $errors->add( 'site_taken', __( 'Sorry, that site already exists!' ) );
 644          }
 645      }
 646  }
 647  
 648  /**
 649   * Runs the initialization routine for a given site.
 650   *
 651   * This process includes creating the site's database tables and
 652   * populating them with defaults.
 653   *
 654   * @since 5.1.0
 655   *
 656   * @global wpdb     $wpdb     WordPress database abstraction object.
 657   * @global WP_Roles $wp_roles WordPress role management object.
 658   *
 659   * @param int|WP_Site $site_id Site ID or object.
 660   * @param array       $args    {
 661   *     Optional. Arguments to modify the initialization behavior.
 662   *
 663   *     @type int    $user_id Required. User ID for the site administrator.
 664   *     @type string $title   Site title. Default is 'Site %d' where %d is the
 665   *                           site ID.
 666   *     @type array  $options Custom option $key => $value pairs to use. Default
 667   *                           empty array.
 668   *     @type array  $meta    Custom site metadata $key => $value pairs to use.
 669   *                           Default empty array.
 670   * }
 671   * @return bool|WP_Error True on success, or error object on failure.
 672   */
 673  function wp_initialize_site( $site_id, array $args = array() ) {
 674      global $wpdb, $wp_roles;
 675  
 676      if ( empty( $site_id ) ) {
 677          return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
 678      }
 679  
 680      $site = get_site( $site_id );
 681      if ( ! $site ) {
 682          return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
 683      }
 684  
 685      if ( wp_is_site_initialized( $site ) ) {
 686          return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
 687      }
 688  
 689      $network = get_network( $site->network_id );
 690      if ( ! $network ) {
 691          $network = get_network();
 692      }
 693  
 694      $args = wp_parse_args(
 695          $args,
 696          array(
 697              'user_id' => 0,
 698              /* translators: %d: Site ID. */
 699              'title'   => sprintf( __( 'Site %d' ), $site->id ),
 700              'options' => array(),
 701              'meta'    => array(),
 702          )
 703      );
 704  
 705      /**
 706       * Filters the arguments for initializing a site.
 707       *
 708       * @since 5.1.0
 709       *
 710       * @param array      $args    Arguments to modify the initialization behavior.
 711       * @param WP_Site    $site    Site that is being initialized.
 712       * @param WP_Network $network Network that the site belongs to.
 713       */
 714      $args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );
 715  
 716      $orig_installing = wp_installing();
 717      if ( ! $orig_installing ) {
 718          wp_installing( true );
 719      }
 720  
 721      $switch = false;
 722      if ( get_current_blog_id() !== $site->id ) {
 723          $switch = true;
 724          switch_to_blog( $site->id );
 725      }
 726  
 727      require_once ABSPATH . 'wp-admin/includes/upgrade.php';
 728  
 729      // Set up the database tables.
 730      make_db_current_silent( 'blog' );
 731  
 732      $home_scheme    = 'http';
 733      $siteurl_scheme = 'http';
 734      if ( ! is_subdomain_install() ) {
 735          if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
 736              $home_scheme = 'https';
 737          }
 738          if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
 739              $siteurl_scheme = 'https';
 740          }
 741      }
 742  
 743      // Populate the site's options.
 744      populate_options(
 745          array_merge(
 746              array(
 747                  'home'        => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
 748                  'siteurl'     => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
 749                  'blogname'    => wp_unslash( $args['title'] ),
 750                  'admin_email' => '',
 751                  'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
 752                  'blog_public' => (int) $site->public,
 753                  'WPLANG'      => get_network_option( $network->id, 'WPLANG' ),
 754              ),
 755              $args['options']
 756          )
 757      );
 758  
 759      // Clean blog cache after populating options.
 760      clean_blog_cache( $site );
 761  
 762      // Populate the site's roles.
 763      populate_roles();
 764      $wp_roles = new WP_Roles();
 765  
 766      // Populate metadata for the site.
 767      populate_site_meta( $site->id, $args['meta'] );
 768  
 769      // Remove all permissions that may exist for the site.
 770      $table_prefix = $wpdb->get_blog_prefix();
 771      delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true );   // Delete all.
 772      delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.
 773  
 774      // Install default site content.
 775      wp_install_defaults( $args['user_id'] );
 776  
 777      // Set the site administrator.
 778      add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
 779      if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
 780          update_user_meta( $args['user_id'], 'primary_blog', $site->id );
 781      }
 782  
 783      if ( $switch ) {
 784          restore_current_blog();
 785      }
 786  
 787      wp_installing( $orig_installing );
 788  
 789      return true;
 790  }
 791  
 792  /**
 793   * Runs the uninitialization routine for a given site.
 794   *
 795   * This process includes dropping the site's database tables and deleting its uploads directory.
 796   *
 797   * @since 5.1.0
 798   *
 799   * @global wpdb $wpdb WordPress database abstraction object.
 800   *
 801   * @param int|WP_Site $site_id Site ID or object.
 802   * @return bool|WP_Error True on success, or error object on failure.
 803   */
 804  function wp_uninitialize_site( $site_id ) {
 805      global $wpdb;
 806  
 807      if ( empty( $site_id ) ) {
 808          return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
 809      }
 810  
 811      $site = get_site( $site_id );
 812      if ( ! $site ) {
 813          return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
 814      }
 815  
 816      if ( ! wp_is_site_initialized( $site ) ) {
 817          return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) );
 818      }
 819  
 820      $users = get_users(
 821          array(
 822              'blog_id' => $site->id,
 823              'fields'  => 'ids',
 824          )
 825      );
 826  
 827      // Remove users from the site.
 828      if ( ! empty( $users ) ) {
 829          foreach ( $users as $user_id ) {
 830              remove_user_from_blog( $user_id, $site->id );
 831          }
 832      }
 833  
 834      $switch = false;
 835      if ( get_current_blog_id() !== $site->id ) {
 836          $switch = true;
 837          switch_to_blog( $site->id );
 838      }
 839  
 840      $uploads = wp_get_upload_dir();
 841  
 842      $tables = $wpdb->tables( 'blog' );
 843  
 844      /**
 845       * Filters the tables to drop when the site is deleted.
 846       *
 847       * @since MU (3.0.0)
 848       *
 849       * @param string[] $tables  Array of names of the site tables to be dropped.
 850       * @param int      $site_id The ID of the site to drop tables for.
 851       */
 852      $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id );
 853  
 854      foreach ( (array) $drop_tables as $table ) {
 855          $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
 856      }
 857  
 858      /**
 859       * Filters the upload base directory to delete when the site is deleted.
 860       *
 861       * @since MU (3.0.0)
 862       *
 863       * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
 864       * @param int    $site_id            The site ID.
 865       */
 866      $dir     = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id );
 867      $dir     = rtrim( $dir, DIRECTORY_SEPARATOR );
 868      $top_dir = $dir;
 869      $stack   = array( $dir );
 870      $index   = 0;
 871  
 872      while ( $index < count( $stack ) ) {
 873          // Get indexed directory from stack.
 874          $dir = $stack[ $index ];
 875  
 876          // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
 877          $dh = @opendir( $dir );
 878          if ( $dh ) {
 879              $file = @readdir( $dh );
 880              while ( false !== $file ) {
 881                  if ( '.' === $file || '..' === $file ) {
 882                      $file = @readdir( $dh );
 883                      continue;
 884                  }
 885  
 886                  if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) {
 887                      $stack[] = $dir . DIRECTORY_SEPARATOR . $file;
 888                  } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) {
 889                      @unlink( $dir . DIRECTORY_SEPARATOR . $file );
 890                  }
 891  
 892                  $file = @readdir( $dh );
 893              }
 894              @closedir( $dh );
 895          }
 896          $index++;
 897      }
 898  
 899      $stack = array_reverse( $stack ); // Last added directories are deepest.
 900      foreach ( (array) $stack as $dir ) {
 901          if ( $dir != $top_dir ) {
 902              @rmdir( $dir );
 903          }
 904      }
 905  
 906      // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
 907      if ( $switch ) {
 908          restore_current_blog();
 909      }
 910  
 911      return true;
 912  }
 913  
 914  /**
 915   * Checks whether a site is initialized.
 916   *
 917   * A site is considered initialized when its database tables are present.
 918   *
 919   * @since 5.1.0
 920   *
 921   * @global wpdb $wpdb WordPress database abstraction object.
 922   *
 923   * @param int|WP_Site $site_id Site ID or object.
 924   * @return bool True if the site is initialized, false otherwise.
 925   */
 926  function wp_is_site_initialized( $site_id ) {
 927      global $wpdb;
 928  
 929      if ( is_object( $site_id ) ) {
 930          $site_id = $site_id->blog_id;
 931      }
 932      $site_id = (int) $site_id;
 933  
 934      /**
 935       * Filters the check for whether a site is initialized before the database is accessed.
 936       *
 937       * Returning a non-null value will effectively short-circuit the function, returning
 938       * that value instead.
 939       *
 940       * @since 5.1.0
 941       *
 942       * @param bool|null $pre     The value to return instead. Default null
 943       *                           to continue with the check.
 944       * @param int       $site_id The site ID that is being checked.
 945       */
 946      $pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
 947      if ( null !== $pre ) {
 948          return (bool) $pre;
 949      }
 950  
 951      $switch = false;
 952      if ( get_current_blog_id() !== $site_id ) {
 953          $switch = true;
 954          remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
 955          switch_to_blog( $site_id );
 956      }
 957  
 958      $suppress = $wpdb->suppress_errors();
 959      $result   = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
 960      $wpdb->suppress_errors( $suppress );
 961  
 962      if ( $switch ) {
 963          restore_current_blog();
 964          add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
 965      }
 966  
 967      return $result;
 968  }
 969  
 970  /**
 971   * Clean the blog cache
 972   *
 973   * @since 3.5.0
 974   *
 975   * @global bool $_wp_suspend_cache_invalidation
 976   *
 977   * @param WP_Site|int $blog The site object or ID to be cleared from cache.
 978   */
 979  function clean_blog_cache( $blog ) {
 980      global $_wp_suspend_cache_invalidation;
 981  
 982      if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
 983          return;
 984      }
 985  
 986      if ( empty( $blog ) ) {
 987          return;
 988      }
 989  
 990      $blog_id = $blog;
 991      $blog    = get_site( $blog_id );
 992      if ( ! $blog ) {
 993          if ( ! is_numeric( $blog_id ) ) {
 994              return;
 995          }
 996  
 997          // Make sure a WP_Site object exists even when the site has been deleted.
 998          $blog = new WP_Site(
 999              (object) array(
1000                  'blog_id' => $blog_id,
1001                  'domain'  => null,
1002                  'path'    => null,
1003              )
1004          );
1005      }
1006  
1007      $blog_id         = $blog->blog_id;
1008      $domain_path_key = md5( $blog->domain . $blog->path );
1009  
1010      wp_cache_delete( $blog_id, 'sites' );
1011      wp_cache_delete( $blog_id, 'site-details' );
1012      wp_cache_delete( $blog_id, 'blog-details' );
1013      wp_cache_delete( $blog_id . 'short', 'blog-details' );
1014      wp_cache_delete( $domain_path_key, 'blog-lookup' );
1015      wp_cache_delete( $domain_path_key, 'blog-id-cache' );
1016      wp_cache_delete( $blog_id, 'blog_meta' );
1017  
1018      /**
1019       * Fires immediately after a site has been removed from the object cache.
1020       *
1021       * @since 4.6.0
1022       *
1023       * @param int     $id              Blog ID.
1024       * @param WP_Site $blog            Site object.
1025       * @param string  $domain_path_key md5 hash of domain and path.
1026       */
1027      do_action( 'clean_site_cache', $blog_id, $blog, $domain_path_key );
1028  
1029      wp_cache_set( 'last_changed', microtime(), 'sites' );
1030  
1031      /**
1032       * Fires after the blog details cache is cleared.
1033       *
1034       * @since 3.4.0
1035       * @deprecated 4.9.0 Use {@see 'clean_site_cache'} instead.
1036       *
1037       * @param int $blog_id Blog ID.
1038       */
1039      do_action_deprecated( 'refresh_blog_details', array( $blog_id ), '4.9.0', 'clean_site_cache' );
1040  }
1041  
1042  /**
1043   * Adds metadata to a site.
1044   *
1045   * @since 5.1.0
1046   *
1047   * @param int    $site_id    Site ID.
1048   * @param string $meta_key   Metadata name.
1049   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1050   * @param bool   $unique     Optional. Whether the same key should not be added.
1051   *                           Default false.
1052   * @return int|false Meta ID on success, false on failure.
1053   */
1054  function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) {
1055      return add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique );
1056  }
1057  
1058  /**
1059   * Removes metadata matching criteria from a site.
1060   *
1061   * You can match based on the key, or key and value. Removing based on key and
1062   * value, will keep from removing duplicate metadata with the same key. It also
1063   * allows removing all metadata matching key, if needed.
1064   *
1065   * @since 5.1.0
1066   *
1067   * @param int    $site_id    Site ID.
1068   * @param string $meta_key   Metadata name.
1069   * @param mixed  $meta_value Optional. Metadata value. If provided,
1070   *                           rows will only be removed that match the value.
1071   *                           Must be serializable if non-scalar. Default empty.
1072   * @return bool True on success, false on failure.
1073   */
1074  function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) {
1075      return delete_metadata( 'blog', $site_id, $meta_key, $meta_value );
1076  }
1077  
1078  /**
1079   * Retrieves metadata for a site.
1080   *
1081   * @since 5.1.0
1082   *
1083   * @param int    $site_id Site ID.
1084   * @param string $key     Optional. The meta key to retrieve. By default,
1085   *                        returns data for all keys. Default empty.
1086   * @param bool   $single  Optional. Whether to return a single value.
1087   *                        This parameter has no effect if $key is not specified.
1088   *                        Default false.
1089   * @return mixed An array if $single is false. The value of meta data field
1090   *               if $single is true.
1091   */
1092  function get_site_meta( $site_id, $key = '', $single = false ) {
1093      return get_metadata( 'blog', $site_id, $key, $single );
1094  }
1095  
1096  /**
1097   * Updates metadata for a site.
1098   *
1099   * Use the $prev_value parameter to differentiate between meta fields with the
1100   * same key and site ID.
1101   *
1102   * If the meta field for the site does not exist, it will be added.
1103   *
1104   * @since 5.1.0
1105   *
1106   * @param int    $site_id    Site ID.
1107   * @param string $meta_key   Metadata key.
1108   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
1109   * @param mixed  $prev_value Optional. Previous value to check before updating.
1110   *                           If specified, only update existing metadata entries with
1111   *                           this value. Otherwise, update all entries. Default empty.
1112   * @return int|bool Meta ID if the key didn't exist, true on successful update,
1113   *                  false on failure or if the value passed to the function
1114   *                  is the same as the one that is already in the database.
1115   */
1116  function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) {
1117      return update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value );
1118  }
1119  
1120  /**
1121   * Deletes everything from site meta matching meta key.
1122   *
1123   * @since 5.1.0
1124   *
1125   * @param string $meta_key Metadata key to search for when deleting.
1126   * @return bool Whether the site meta key was deleted from the database.
1127   */
1128  function delete_site_meta_by_key( $meta_key ) {
1129      return delete_metadata( 'blog', null, $meta_key, '', true );
1130  }
1131  
1132  /**
1133   * Updates the count of sites for a network based on a changed site.
1134   *
1135   * @since 5.1.0
1136   *
1137   * @param WP_Site      $new_site The site object that has been inserted, updated or deleted.
1138   * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
1139   *                               state of that site. Default null.
1140   */
1141  function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
1142      if ( null === $old_site ) {
1143          wp_maybe_update_network_site_counts( $new_site->network_id );
1144          return;
1145      }
1146  
1147      if ( $new_site->network_id != $old_site->network_id ) {
1148          wp_maybe_update_network_site_counts( $new_site->network_id );
1149          wp_maybe_update_network_site_counts( $old_site->network_id );
1150      }
1151  }
1152  
1153  /**
1154   * Triggers actions on site status updates.
1155   *
1156   * @since 5.1.0
1157   *
1158   * @param WP_Site      $new_site The site object after the update.
1159   * @param WP_Site|null $old_site Optional. If $new_site has been updated, this must be the previous
1160   *                               state of that site. Default null.
1161   */
1162  function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
1163      $site_id = $new_site->id;
1164  
1165      // Use the default values for a site if no previous state is given.
1166      if ( ! $old_site ) {
1167          $old_site = new WP_Site( new stdClass() );
1168      }
1169  
1170      if ( $new_site->spam != $old_site->spam ) {
1171          if ( 1 == $new_site->spam ) {
1172  
1173              /**
1174               * Fires when the 'spam' status is added to a site.
1175               *
1176               * @since MU (3.0.0)
1177               *
1178               * @param int $site_id Site ID.
1179               */
1180              do_action( 'make_spam_blog', $site_id );
1181          } else {
1182  
1183              /**
1184               * Fires when the 'spam' status is removed from a site.
1185               *
1186               * @since MU (3.0.0)
1187               *
1188               * @param int $site_id Site ID.
1189               */
1190              do_action( 'make_ham_blog', $site_id );
1191          }
1192      }
1193  
1194      if ( $new_site->mature != $old_site->mature ) {
1195          if ( 1 == $new_site->mature ) {
1196  
1197              /**
1198               * Fires when the 'mature' status is added to a site.
1199               *
1200               * @since 3.1.0
1201               *
1202               * @param int $site_id Site ID.
1203               */
1204              do_action( 'mature_blog', $site_id );
1205          } else {
1206  
1207              /**
1208               * Fires when the 'mature' status is removed from a site.
1209               *
1210               * @since 3.1.0
1211               *
1212               * @param int $site_id Site ID.
1213               */
1214              do_action( 'unmature_blog', $site_id );
1215          }
1216      }
1217  
1218      if ( $new_site->archived != $old_site->archived ) {
1219          if ( 1 == $new_site->archived ) {
1220  
1221              /**
1222               * Fires when the 'archived' status is added to a site.
1223               *
1224               * @since MU (3.0.0)
1225               *
1226               * @param int $site_id Site ID.
1227               */
1228              do_action( 'archive_blog', $site_id );
1229          } else {
1230  
1231              /**
1232               * Fires when the 'archived' status is removed from a site.
1233               *
1234               * @since MU (3.0.0)
1235               *
1236               * @param int $site_id Site ID.
1237               */
1238              do_action( 'unarchive_blog', $site_id );
1239          }
1240      }
1241  
1242      if ( $new_site->deleted != $old_site->deleted ) {
1243          if ( 1 == $new_site->deleted ) {
1244  
1245              /**
1246               * Fires when the 'deleted' status is added to a site.
1247               *
1248               * @since 3.5.0
1249               *
1250               * @param int $site_id Site ID.
1251               */
1252              do_action( 'make_delete_blog', $site_id );
1253          } else {
1254  
1255              /**
1256               * Fires when the 'deleted' status is removed from a site.
1257               *
1258               * @since 3.5.0
1259               *
1260               * @param int $site_id Site ID.
1261               */
1262              do_action( 'make_undelete_blog', $site_id );
1263          }
1264      }
1265  
1266      if ( $new_site->public != $old_site->public ) {
1267  
1268          /**
1269           * Fires after the current blog's 'public' setting is updated.
1270           *
1271           * @since MU (3.0.0)
1272           *
1273           * @param int    $site_id Site ID.
1274           * @param string $value   The value of the site status.
1275           */
1276          do_action( 'update_blog_public', $site_id, $new_site->public );
1277      }
1278  }
1279  
1280  /**
1281   * Cleans the necessary caches after specific site data has been updated.
1282   *
1283   * @since 5.1.0
1284   *
1285   * @param WP_Site $new_site The site object after the update.
1286   * @param WP_Site $old_site The site obejct prior to the update.
1287   */
1288  function wp_maybe_clean_new_site_cache_on_update( $new_site, $old_site ) {
1289      if ( $old_site->domain !== $new_site->domain || $old_site->path !== $new_site->path ) {
1290          clean_blog_cache( $new_site );
1291      }
1292  }
1293  
1294  /**
1295   * Updates the `blog_public` option for a given site ID.
1296   *
1297   * @since 5.1.0
1298   *
1299   * @param int    $site_id Site ID.
1300   * @param string $public  The value of the site status.
1301   */
1302  function wp_update_blog_public_option_on_site_update( $site_id, $public ) {
1303  
1304      // Bail if the site's database tables do not exist (yet).
1305      if ( ! wp_is_site_initialized( $site_id ) ) {
1306          return;
1307      }
1308  
1309      update_blog_option( $site_id, 'blog_public', $public );
1310  }
1311  
1312  /**
1313   * Sets the last changed time for the 'sites' cache group.
1314   *
1315   * @since 5.1.0
1316   */
1317  function wp_cache_set_sites_last_changed() {
1318      wp_cache_set( 'last_changed', microtime(), 'sites' );
1319  }
1320  
1321  /**
1322   * Aborts calls to site meta if it is not supported.
1323   *
1324   * @since 5.1.0
1325   *
1326   * @global wpdb $wpdb WordPress database abstraction object.
1327   *
1328   * @param mixed $check Skip-value for whether to proceed site meta function execution.
1329   * @return mixed Original value of $check, or false if site meta is not supported.
1330   */
1331  function wp_check_site_meta_support_prefilter( $check ) {
1332      if ( ! is_site_meta_supported() ) {
1333          /* translators: %s: Database table name. */
1334          _doing_it_wrong( __FUNCTION__, sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), '5.1.0' );
1335          return false;
1336      }
1337  
1338      return $check;
1339  }


Generated: Sat Jul 4 01:00:03 2020 Cross-referenced by PHPXref 0.7.1