[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Site/blog functions that work with the blogs table and related data.
   5   *
   6   * @package WordPress
   7   * @subpackage Multisite
   8   * @since MU (3.0.0)
   9   */
  10  
  11  // Don't load directly.
  12  if ( ! defined( 'ABSPATH' ) ) {
  13      die( '-1' );
  14  }
  15  
  16  require_once  ABSPATH . WPINC . '/ms-site.php';
  17  require_once  ABSPATH . WPINC . '/ms-network.php';
  18  
  19  /**
  20   * Updates the last_updated field for the current site.
  21   *
  22   * @since MU (3.0.0)
  23   */
  24  function wpmu_update_blogs_date() {
  25      $site_id = get_current_blog_id();
  26  
  27      update_blog_details( $site_id, array( 'last_updated' => current_time( 'mysql', true ) ) );
  28      /**
  29       * Fires after the blog details are updated.
  30       *
  31       * @since MU (3.0.0)
  32       *
  33       * @param int $blog_id Site ID.
  34       */
  35      do_action( 'wpmu_blog_updated', $site_id );
  36  }
  37  
  38  /**
  39   * Gets a full site URL, given a site ID.
  40   *
  41   * @since MU (3.0.0)
  42   *
  43   * @param int $blog_id Site ID.
  44   * @return string Full site URL if found. Empty string if not.
  45   */
  46  function get_blogaddress_by_id( $blog_id ) {
  47      $bloginfo = get_site( (int) $blog_id );
  48  
  49      if ( empty( $bloginfo ) ) {
  50          return '';
  51      }
  52  
  53      $scheme = parse_url( $bloginfo->home, PHP_URL_SCHEME );
  54      $scheme = empty( $scheme ) ? 'http' : $scheme;
  55  
  56      return esc_url( $scheme . '://' . $bloginfo->domain . $bloginfo->path );
  57  }
  58  
  59  /**
  60   * Gets a full site URL, given a site name.
  61   *
  62   * @since MU (3.0.0)
  63   *
  64   * @param string $blogname Name of the subdomain or directory.
  65   * @return string
  66   */
  67  function get_blogaddress_by_name( $blogname ) {
  68      if ( is_subdomain_install() ) {
  69          if ( 'main' === $blogname ) {
  70              $blogname = 'www';
  71          }
  72          $url = rtrim( network_home_url(), '/' );
  73          if ( ! empty( $blogname ) ) {
  74              $url = preg_replace( '|^([^\.]+://)|', '$1}' . $blogname . '.', $url );
  75          }
  76      } else {
  77          $url = network_home_url( $blogname );
  78      }
  79      return esc_url( $url . '/' );
  80  }
  81  
  82  /**
  83   * Retrieves a site's ID given its (subdomain or directory) slug.
  84   *
  85   * @since MU (3.0.0)
  86   * @since 4.7.0 Converted to use `get_sites()`.
  87   *
  88   * @param string $slug A site's slug.
  89   * @return int|null The site ID, or null if no site is found for the given slug.
  90   */
  91  function get_id_from_blogname( $slug ) {
  92      $current_network = get_network();
  93      $slug            = trim( $slug, '/' );
  94  
  95      if ( is_subdomain_install() ) {
  96          $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain );
  97          $path   = $current_network->path;
  98      } else {
  99          $domain = $current_network->domain;
 100          $path   = $current_network->path . $slug . '/';
 101      }
 102  
 103      $site_ids = get_sites(
 104          array(
 105              'number'                 => 1,
 106              'fields'                 => 'ids',
 107              'domain'                 => $domain,
 108              'path'                   => $path,
 109              'update_site_meta_cache' => false,
 110          )
 111      );
 112  
 113      if ( empty( $site_ids ) ) {
 114          return null;
 115      }
 116  
 117      return array_shift( $site_ids );
 118  }
 119  
 120  /**
 121   * Retrieves the details for a blog from the blogs table and blog options.
 122   *
 123   * @since MU (3.0.0)
 124   *
 125   * @global wpdb $wpdb WordPress database abstraction object.
 126   *
 127   * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
 128   *                                  Defaults to the current blog ID.
 129   * @param bool             $get_all Whether to retrieve all details or only the details in the blogs table.
 130   *                                  Default is true.
 131   * @return WP_Site|false Blog details on success. False on failure.
 132   */
 133  function get_blog_details( $fields = null, $get_all = true ) {
 134      global $wpdb;
 135  
 136      if ( is_array( $fields ) ) {
 137          if ( isset( $fields['blog_id'] ) ) {
 138              $blog_id = $fields['blog_id'];
 139          } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
 140              $key  = md5( $fields['domain'] . $fields['path'] );
 141              $blog = wp_cache_get( $key, 'blog-lookup' );
 142              if ( false !== $blog ) {
 143                  return $blog;
 144              }
 145              if ( str_starts_with( $fields['domain'], 'www.' ) ) {
 146                  $nowww = substr( $fields['domain'], 4 );
 147                  $blog  = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
 148              } else {
 149                  $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
 150              }
 151              if ( $blog ) {
 152                  wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
 153                  $blog_id = $blog->blog_id;
 154              } else {
 155                  return false;
 156              }
 157          } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
 158              $key  = md5( $fields['domain'] );
 159              $blog = wp_cache_get( $key, 'blog-lookup' );
 160              if ( false !== $blog ) {
 161                  return $blog;
 162              }
 163              if ( str_starts_with( $fields['domain'], 'www.' ) ) {
 164                  $nowww = substr( $fields['domain'], 4 );
 165                  $blog  = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
 166              } else {
 167                  $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
 168              }
 169              if ( $blog ) {
 170                  wp_cache_set( $blog->blog_id . 'short', $blog, 'blog-details' );
 171                  $blog_id = $blog->blog_id;
 172              } else {
 173                  return false;
 174              }
 175          } else {
 176              return false;
 177          }
 178      } else {
 179          if ( ! $fields ) {
 180              $blog_id = get_current_blog_id();
 181          } elseif ( ! is_numeric( $fields ) ) {
 182              $blog_id = get_id_from_blogname( $fields );
 183          } else {
 184              $blog_id = $fields;
 185          }
 186      }
 187  
 188      $blog_id = (int) $blog_id;
 189  
 190      $all     = $get_all ? '' : 'short';
 191      $details = wp_cache_get( $blog_id . $all, 'blog-details' );
 192  
 193      if ( $details ) {
 194          if ( ! is_object( $details ) ) {
 195              if ( -1 === $details ) {
 196                  return false;
 197              } else {
 198                  // Clear old pre-serialized objects. Cache clients do better with that.
 199                  wp_cache_delete( $blog_id . $all, 'blog-details' );
 200                  unset( $details );
 201              }
 202          } else {
 203              return $details;
 204          }
 205      }
 206  
 207      // Try the other cache.
 208      if ( $get_all ) {
 209          $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
 210      } else {
 211          $details = wp_cache_get( $blog_id, 'blog-details' );
 212          // If short was requested and full cache is set, we can return.
 213          if ( $details ) {
 214              if ( ! is_object( $details ) ) {
 215                  if ( -1 === $details ) {
 216                      return false;
 217                  } else {
 218                      // Clear old pre-serialized objects. Cache clients do better with that.
 219                      wp_cache_delete( $blog_id, 'blog-details' );
 220                      unset( $details );
 221                  }
 222              } else {
 223                  return $details;
 224              }
 225          }
 226      }
 227  
 228      if ( empty( $details ) ) {
 229          $details = WP_Site::get_instance( $blog_id );
 230          if ( ! $details ) {
 231              // Set the full cache.
 232              wp_cache_set( $blog_id, -1, 'blog-details' );
 233              return false;
 234          }
 235      }
 236  
 237      if ( ! $details instanceof WP_Site ) {
 238          $details = new WP_Site( $details );
 239      }
 240  
 241      if ( ! $get_all ) {
 242          wp_cache_set( $blog_id . $all, $details, 'blog-details' );
 243          return $details;
 244      }
 245  
 246      $switched_blog = false;
 247  
 248      if ( get_current_blog_id() !== $blog_id ) {
 249          switch_to_blog( $blog_id );
 250          $switched_blog = true;
 251      }
 252  
 253      $details->blogname   = get_option( 'blogname' );
 254      $details->siteurl    = get_option( 'siteurl' );
 255      $details->post_count = get_option( 'post_count' );
 256      $details->home       = get_option( 'home' );
 257  
 258      if ( $switched_blog ) {
 259          restore_current_blog();
 260      }
 261  
 262      /**
 263       * Filters a blog's details.
 264       *
 265       * @since MU (3.0.0)
 266       * @deprecated 4.7.0 Use {@see 'site_details'} instead.
 267       *
 268       * @param WP_Site $details The blog details.
 269       */
 270      $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
 271  
 272      wp_cache_set( $blog_id . $all, $details, 'blog-details' );
 273  
 274      $key = md5( $details->domain . $details->path );
 275      wp_cache_set( $key, $details, 'blog-lookup' );
 276  
 277      return $details;
 278  }
 279  
 280  /**
 281   * Clears the blog details cache.
 282   *
 283   * @since MU (3.0.0)
 284   *
 285   * @param int $blog_id Optional. Blog ID. Defaults to current blog.
 286   */
 287  function refresh_blog_details( $blog_id = 0 ) {
 288      $blog_id = (int) $blog_id;
 289      if ( ! $blog_id ) {
 290          $blog_id = get_current_blog_id();
 291      }
 292  
 293      clean_blog_cache( $blog_id );
 294  }
 295  
 296  /**
 297   * Updates the details for a blog and the blogs table for a given blog ID.
 298   *
 299   * @since MU (3.0.0)
 300   *
 301   * @param int   $blog_id Blog ID.
 302   * @param array $details Array of details keyed by blogs table field names.
 303   * @return bool True if update succeeds, false otherwise.
 304   */
 305  function update_blog_details( $blog_id, $details = array() ) {
 306      if ( empty( $details ) ) {
 307          return false;
 308      }
 309  
 310      if ( is_object( $details ) ) {
 311          $details = get_object_vars( $details );
 312      }
 313  
 314      $site = wp_update_site( $blog_id, $details );
 315  
 316      if ( is_wp_error( $site ) ) {
 317          return false;
 318      }
 319  
 320      return true;
 321  }
 322  
 323  /**
 324   * Cleans the site details cache for a site.
 325   *
 326   * @since 4.7.4
 327   *
 328   * @param int $site_id Optional. Site ID. Default is the current site ID.
 329   */
 330  function clean_site_details_cache( $site_id = 0 ) {
 331      $site_id = (int) $site_id;
 332      if ( ! $site_id ) {
 333          $site_id = get_current_blog_id();
 334      }
 335  
 336      wp_cache_delete( $site_id, 'site-details' );
 337      wp_cache_delete( $site_id, 'blog-details' );
 338  }
 339  
 340  /**
 341   * Retrieves option value for a given blog id based on name of option.
 342   *
 343   * If the option does not exist or does not have a value, then the return value
 344   * will be false. This is useful to check whether you need to install an option
 345   * and is commonly used during installation of plugin options and to test
 346   * whether upgrading is required.
 347   *
 348   * If the option was serialized then it will be unserialized when it is returned.
 349   *
 350   * @since MU (3.0.0)
 351   *
 352   * @param int    $id            A blog ID. Can be null to refer to the current blog.
 353   * @param string $option        Name of option to retrieve. Expected to not be SQL-escaped.
 354   * @param mixed  $default_value Optional. Default value to return if the option does not exist.
 355   * @return mixed Value set for the option.
 356   */
 357  function get_blog_option( $id, $option, $default_value = false ) {
 358      $id = (int) $id;
 359  
 360      if ( empty( $id ) ) {
 361          $id = get_current_blog_id();
 362      }
 363  
 364      if ( get_current_blog_id() === $id ) {
 365          return get_option( $option, $default_value );
 366      }
 367  
 368      switch_to_blog( $id );
 369      $value = get_option( $option, $default_value );
 370      restore_current_blog();
 371  
 372      /**
 373       * Filters a blog option value.
 374       *
 375       * The dynamic portion of the hook name, `$option`, refers to the blog option name.
 376       *
 377       * @since 3.5.0
 378       *
 379       * @param string  $value The option value.
 380       * @param int     $id    Blog ID.
 381       */
 382      return apply_filters( "blog_option_{$option}", $value, $id );
 383  }
 384  
 385  /**
 386   * Adds a new option for a given blog ID.
 387   *
 388   * You do not need to serialize values. If the value needs to be serialized, then
 389   * it will be serialized before it is inserted into the database. Remember,
 390   * resources can not be serialized or added as an option.
 391   *
 392   * You can create options without values and then update the values later.
 393   * Existing options will not be updated and checks are performed to ensure that you
 394   * aren't adding a protected WordPress option. Care should be taken to not name
 395   * options the same as the ones which are protected.
 396   *
 397   * @since MU (3.0.0)
 398   *
 399   * @param int    $id     A blog ID. Can be null to refer to the current blog.
 400   * @param string $option Name of option to add. Expected to not be SQL-escaped.
 401   * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
 402   * @return bool True if the option was added, false otherwise.
 403   */
 404  function add_blog_option( $id, $option, $value ) {
 405      $id = (int) $id;
 406  
 407      if ( empty( $id ) ) {
 408          $id = get_current_blog_id();
 409      }
 410  
 411      if ( get_current_blog_id() === $id ) {
 412          return add_option( $option, $value );
 413      }
 414  
 415      switch_to_blog( $id );
 416      $return = add_option( $option, $value );
 417      restore_current_blog();
 418  
 419      return $return;
 420  }
 421  
 422  /**
 423   * Removes an option by name for a given blog ID. Prevents removal of protected WordPress options.
 424   *
 425   * @since MU (3.0.0)
 426   *
 427   * @param int    $id     A blog ID. Can be null to refer to the current blog.
 428   * @param string $option Name of option to remove. Expected to not be SQL-escaped.
 429   * @return bool True if the option was deleted, false otherwise.
 430   */
 431  function delete_blog_option( $id, $option ) {
 432      $id = (int) $id;
 433  
 434      if ( empty( $id ) ) {
 435          $id = get_current_blog_id();
 436      }
 437  
 438      if ( get_current_blog_id() === $id ) {
 439          return delete_option( $option );
 440      }
 441  
 442      switch_to_blog( $id );
 443      $return = delete_option( $option );
 444      restore_current_blog();
 445  
 446      return $return;
 447  }
 448  
 449  /**
 450   * Updates an option for a particular blog.
 451   *
 452   * @since MU (3.0.0)
 453   *
 454   * @param int    $id         The blog ID.
 455   * @param string $option     The option key.
 456   * @param mixed  $value      The option value.
 457   * @param mixed  $deprecated Not used.
 458   * @return bool True if the value was updated, false otherwise.
 459   */
 460  function update_blog_option( $id, $option, $value, $deprecated = null ) {
 461      $id = (int) $id;
 462  
 463      if ( null !== $deprecated ) {
 464          _deprecated_argument( __FUNCTION__, '3.1.0' );
 465      }
 466  
 467      if ( get_current_blog_id() === $id ) {
 468          return update_option( $option, $value );
 469      }
 470  
 471      switch_to_blog( $id );
 472      $return = update_option( $option, $value );
 473      restore_current_blog();
 474  
 475      return $return;
 476  }
 477  
 478  /**
 479   * Switches the current blog.
 480   *
 481   * This function is useful if you need to pull posts, or other information,
 482   * from other blogs. You can switch back afterwards using restore_current_blog().
 483   *
 484   * PHP code loaded with the originally requested site, such as code from a plugin or theme, does not switch. See #14941.
 485   *
 486   * @see restore_current_blog()
 487   * @since MU (3.0.0)
 488   *
 489   * @global wpdb            $wpdb               WordPress database abstraction object.
 490   * @global int             $blog_id
 491   * @global array           $_wp_switched_stack
 492   * @global bool            $switched
 493   * @global string          $table_prefix       The database table prefix.
 494   * @global WP_Object_Cache $wp_object_cache
 495   *
 496   * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
 497   * @param bool $deprecated  Not used.
 498   * @return true Always returns true.
 499   */
 500  function switch_to_blog( $new_blog_id, $deprecated = null ) {
 501      global $wpdb;
 502  
 503      $prev_blog_id = get_current_blog_id();
 504      if ( empty( $new_blog_id ) ) {
 505          $new_blog_id = $prev_blog_id;
 506      }
 507  
 508      $GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
 509  
 510      /*
 511       * If we're switching to the same blog id that we're on,
 512       * set the right vars, do the associated actions, but skip
 513       * the extra unnecessary work
 514       */
 515      if ( $new_blog_id === $prev_blog_id ) {
 516          /**
 517           * Fires when the blog is switched.
 518           *
 519           * @since MU (3.0.0)
 520           * @since 5.4.0 The `$context` parameter was added.
 521           *
 522           * @param int    $new_blog_id  New blog ID.
 523           * @param int    $prev_blog_id Previous blog ID.
 524           * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
 525           *                             or 'restore' when called from restore_current_blog().
 526           */
 527          do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
 528  
 529          $GLOBALS['switched'] = true;
 530  
 531          return true;
 532      }
 533  
 534      $wpdb->set_blog_id( $new_blog_id );
 535      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 536      $GLOBALS['blog_id']      = $new_blog_id;
 537  
 538      wp_cache_switch_to_blog( $new_blog_id );
 539  
 540      /** This filter is documented in wp-includes/ms-blogs.php */
 541      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
 542  
 543      $GLOBALS['switched'] = true;
 544  
 545      return true;
 546  }
 547  
 548  /**
 549   * Restores the current blog, after calling switch_to_blog().
 550   *
 551   * @see switch_to_blog()
 552   * @since MU (3.0.0)
 553   *
 554   * @global wpdb            $wpdb               WordPress database abstraction object.
 555   * @global array           $_wp_switched_stack
 556   * @global int             $blog_id
 557   * @global bool            $switched
 558   * @global string          $table_prefix       The database table prefix.
 559   * @global WP_Object_Cache $wp_object_cache
 560   *
 561   * @return bool True on success, false if we're already on the current blog.
 562   */
 563  function restore_current_blog() {
 564      global $wpdb;
 565  
 566      if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
 567          return false;
 568      }
 569  
 570      $new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
 571      $prev_blog_id = get_current_blog_id();
 572  
 573      if ( $new_blog_id === $prev_blog_id ) {
 574          /** This filter is documented in wp-includes/ms-blogs.php */
 575          do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 576  
 577          // If we still have items in the switched stack, consider ourselves still 'switched'.
 578          $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 579  
 580          return true;
 581      }
 582  
 583      $wpdb->set_blog_id( $new_blog_id );
 584      $GLOBALS['blog_id']      = $new_blog_id;
 585      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 586  
 587      wp_cache_switch_to_blog( $new_blog_id );
 588  
 589      /** This filter is documented in wp-includes/ms-blogs.php */
 590      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 591  
 592      // If we still have items in the switched stack, consider ourselves still 'switched'.
 593      $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 594  
 595      return true;
 596  }
 597  
 598  /**
 599   * Fallback logic for switching cache context when an object cache drop-in lacks
 600   * a switch_to_blog() method.
 601   *
 602   * Reinitializes the cache and restores global/non-persistent groups.
 603   *
 604   * Used by the wp_cache_switch_to_blog() compatibility function, abstracted only
 605   * to allow for unit testing outside of the drop-in plugin inclusion circus.
 606   *
 607   * @since 7.0.0
 608   *
 609   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 610   */
 611  function wp_cache_switch_to_blog_fallback() {
 612      global $wp_object_cache;
 613  
 614      $global_groups         = false;
 615      $non_persistent_groups = false;
 616  
 617      if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) {
 618  
 619          // Get the global groups as they are.
 620          $group_names = $wp_object_cache->global_groups;
 621  
 622          // Get global group keys if non-numeric array.
 623          if ( ! wp_is_numeric_array( $group_names ) ) {
 624              $group_names = array_keys( $group_names );
 625          }
 626  
 627          $global_groups = $group_names;
 628  
 629          /*
 630           * Non-persistent groups: Check for no_mc_groups first (memcached drop-in).
 631           * Fall back to cache structure (default cache).
 632           */
 633          if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) {
 634              $non_persistent_groups = $wp_object_cache->no_mc_groups;
 635          } elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) {
 636              $all_groups            = array_keys( $wp_object_cache->cache );
 637              $non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) );
 638          }
 639      }
 640  
 641      wp_cache_init();
 642  
 643      if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 644          if ( ! is_array( $global_groups ) || empty( $global_groups ) ) {
 645              $global_groups = array(
 646                  'blog-details',
 647                  'blog-id-cache',
 648                  'blog-lookup',
 649                  'blog_meta',
 650                  'global-posts',
 651                  'image_editor',
 652                  'networks',
 653                  'network-queries',
 654                  'sites',
 655                  'site-details',
 656                  'site-options',
 657                  'site-queries',
 658                  'site-transient',
 659                  'theme_files',
 660                  'translation_files',
 661                  'rss',
 662                  'users',
 663                  'user-queries',
 664                  'user_meta',
 665                  'useremail',
 666                  'userlogins',
 667                  'userslugs',
 668              );
 669          }
 670  
 671          wp_cache_add_global_groups( $global_groups );
 672      }
 673  
 674      if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {
 675          if ( ! is_array( $non_persistent_groups ) || empty( $non_persistent_groups ) ) {
 676              $non_persistent_groups = array(
 677                  'counts',
 678                  'plugins',
 679                  'theme_json',
 680              );
 681          }
 682  
 683          wp_cache_add_non_persistent_groups( $non_persistent_groups );
 684      }
 685  }
 686  
 687  /**
 688   * Switches the initialized roles and current user capabilities to another site.
 689   *
 690   * @since 4.9.0
 691   *
 692   * @param int $new_site_id New site ID.
 693   * @param int $old_site_id Old site ID.
 694   */
 695  function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
 696      if ( $new_site_id === $old_site_id ) {
 697          return;
 698      }
 699  
 700      if ( ! did_action( 'init' ) ) {
 701          return;
 702      }
 703  
 704      wp_roles()->for_site( $new_site_id );
 705      wp_get_current_user()->for_site( $new_site_id );
 706  }
 707  
 708  /**
 709   * Determines if switch_to_blog() is in effect.
 710   *
 711   * @since 3.5.0
 712   *
 713   * @global array $_wp_switched_stack
 714   *
 715   * @return bool True if switched, false otherwise.
 716   */
 717  function ms_is_switched() {
 718      return ! empty( $GLOBALS['_wp_switched_stack'] );
 719  }
 720  
 721  /**
 722   * Checks if a particular blog is archived.
 723   *
 724   * @since MU (3.0.0)
 725   *
 726   * @param int $id Blog ID.
 727   * @return string Whether the blog is archived or not.
 728   */
 729  function is_archived( $id ) {
 730      return get_blog_status( $id, 'archived' );
 731  }
 732  
 733  /**
 734   * Updates the 'archived' status of a particular blog.
 735   *
 736   * @since MU (3.0.0)
 737   *
 738   * @param int    $id       Blog ID.
 739   * @param string $archived The new status.
 740   * @return string $archived
 741   */
 742  function update_archived( $id, $archived ) {
 743      update_blog_status( $id, 'archived', $archived );
 744      return $archived;
 745  }
 746  
 747  /**
 748   * Updates a blog details field.
 749   *
 750   * @since MU (3.0.0)
 751   * @since 5.1.0 Use wp_update_site() internally.
 752   *
 753   * @global wpdb $wpdb WordPress database abstraction object.
 754   *
 755   * @param int    $blog_id    Blog ID.
 756   * @param string $pref       Field name.
 757   * @param string $value      Field value.
 758   * @param mixed  $deprecated Not used.
 759   * @return string|false $value
 760   */
 761  function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
 762      global $wpdb;
 763  
 764      if ( null !== $deprecated ) {
 765          _deprecated_argument( __FUNCTION__, '3.1.0' );
 766      }
 767  
 768      $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
 769  
 770      if ( ! in_array( $pref, $allowed_field_names, true ) ) {
 771          return $value;
 772      }
 773  
 774      $result = wp_update_site(
 775          $blog_id,
 776          array(
 777              $pref => $value,
 778          )
 779      );
 780  
 781      if ( is_wp_error( $result ) ) {
 782          return false;
 783      }
 784  
 785      return $value;
 786  }
 787  
 788  /**
 789   * Gets a blog details field.
 790   *
 791   * @since MU (3.0.0)
 792   *
 793   * @global wpdb $wpdb WordPress database abstraction object.
 794   *
 795   * @param int    $id   Blog ID.
 796   * @param string $pref Field name.
 797   * @return bool|string|null $value
 798   */
 799  function get_blog_status( $id, $pref ) {
 800      global $wpdb;
 801  
 802      $details = get_site( $id );
 803      if ( $details ) {
 804          return $details->$pref;
 805      }
 806  
 807      return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
 808  }
 809  
 810  /**
 811   * Gets a list of most recently updated blogs.
 812   *
 813   * @since MU (3.0.0)
 814   *
 815   * @global wpdb $wpdb WordPress database abstraction object.
 816   *
 817   * @param mixed $deprecated Not used.
 818   * @param int   $start      Optional. Number of blogs to offset the query. Used to build LIMIT clause.
 819   *                          Can be used for pagination. Default 0.
 820   * @param int   $quantity   Optional. The maximum number of blogs to retrieve. Default 40.
 821   * @return array The list of blogs.
 822   */
 823  function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
 824      global $wpdb;
 825  
 826      if ( ! empty( $deprecated ) ) {
 827          _deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
 828      }
 829  
 830      return $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", get_current_network_id(), $start, $quantity ), ARRAY_A );
 831  }
 832  
 833  /**
 834   * Handler for updating the site's last updated date when a post is published or
 835   * an already published post is changed.
 836   *
 837   * @since 3.3.0
 838   *
 839   * @param string  $new_status The new post status.
 840   * @param string  $old_status The old post status.
 841   * @param WP_Post $post       Post object.
 842   */
 843  function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
 844      $post_type_obj = get_post_type_object( $post->post_type );
 845      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 846          return;
 847      }
 848  
 849      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 850          return;
 851      }
 852  
 853      // Post was freshly published, published post was saved, or published post was unpublished.
 854  
 855      wpmu_update_blogs_date();
 856  }
 857  
 858  /**
 859   * Handler for updating the current site's last updated date when a published
 860   * post is deleted.
 861   *
 862   * @since 3.4.0
 863   *
 864   * @param int $post_id Post ID
 865   */
 866  function _update_blog_date_on_post_delete( $post_id ) {
 867      $post = get_post( $post_id );
 868  
 869      $post_type_obj = get_post_type_object( $post->post_type );
 870      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 871          return;
 872      }
 873  
 874      if ( 'publish' !== $post->post_status ) {
 875          return;
 876      }
 877  
 878      wpmu_update_blogs_date();
 879  }
 880  
 881  /**
 882   * Handler for updating the current site's posts count when a post is deleted.
 883   *
 884   * @since 4.0.0
 885   * @since 6.2.0 Added the `$post` parameter.
 886   *
 887   * @param int     $post_id Post ID.
 888   * @param WP_Post $post    Post object.
 889   */
 890  function _update_posts_count_on_delete( $post_id, $post ) {
 891      if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
 892          return;
 893      }
 894  
 895      update_posts_count();
 896  }
 897  
 898  /**
 899   * Handler for updating the current site's posts count when a post status changes.
 900   *
 901   * @since 4.0.0
 902   * @since 4.9.0 Added the `$post` parameter.
 903   *
 904   * @param string       $new_status The status the post is changing to.
 905   * @param string       $old_status The status the post is changing from.
 906   * @param WP_Post|null $post       Post object.
 907   */
 908  function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
 909      if ( $new_status === $old_status ) {
 910          return;
 911      }
 912  
 913      if ( 'post' !== get_post_type( $post ) ) {
 914          return;
 915      }
 916  
 917      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 918          return;
 919      }
 920  
 921      update_posts_count();
 922  }
 923  
 924  /**
 925   * Counts number of sites grouped by site status.
 926   *
 927   * @since 5.3.0
 928   *
 929   * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
 930   * @return int[] {
 931   *     Numbers of sites grouped by site status.
 932   *
 933   *     @type int $all      The total number of sites.
 934   *     @type int $public   The number of public sites.
 935   *     @type int $archived The number of archived sites.
 936   *     @type int $mature   The number of mature sites.
 937   *     @type int $spam     The number of spam sites.
 938   *     @type int $deleted  The number of deleted sites.
 939   * }
 940   */
 941  function wp_count_sites( $network_id = null ) {
 942      if ( empty( $network_id ) ) {
 943          $network_id = get_current_network_id();
 944      }
 945  
 946      $counts = array();
 947      $args   = array(
 948          'network_id'    => $network_id,
 949          'number'        => 1,
 950          'fields'        => 'ids',
 951          'no_found_rows' => false,
 952      );
 953  
 954      $q             = new WP_Site_Query( $args );
 955      $counts['all'] = $q->found_sites;
 956  
 957      $_args    = $args;
 958      $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
 959  
 960      foreach ( $statuses as $status ) {
 961          $_args            = $args;
 962          $_args[ $status ] = 1;
 963  
 964          $q                 = new WP_Site_Query( $_args );
 965          $counts[ $status ] = $q->found_sites;
 966      }
 967  
 968      return $counts;
 969  }


Generated : Wed Jun 24 08:20:11 2026 Cross-referenced by PHPXref