[ 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      exit;
  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   * @phpstan-param null $deprecated
 461   */
 462  function update_blog_option( $id, $option, $value, $deprecated = null ) {
 463      $id = (int) $id;
 464  
 465      if ( null !== $deprecated ) {
 466          _deprecated_argument( __FUNCTION__, '3.1.0' );
 467      }
 468  
 469      if ( get_current_blog_id() === $id ) {
 470          return update_option( $option, $value );
 471      }
 472  
 473      switch_to_blog( $id );
 474      $return = update_option( $option, $value );
 475      restore_current_blog();
 476  
 477      return $return;
 478  }
 479  
 480  /**
 481   * Switches the current blog.
 482   *
 483   * This function is useful if you need to pull posts, or other information,
 484   * from other blogs. You can switch back afterwards using restore_current_blog().
 485   *
 486   * PHP code loaded with the originally requested site, such as code from a plugin or theme, does not switch. See #14941.
 487   *
 488   * @see restore_current_blog()
 489   * @since MU (3.0.0)
 490   *
 491   * @global wpdb            $wpdb               WordPress database abstraction object.
 492   * @global int             $blog_id
 493   * @global array           $_wp_switched_stack
 494   * @global bool            $switched
 495   * @global string          $table_prefix       The database table prefix.
 496   * @global WP_Object_Cache $wp_object_cache
 497   *
 498   * @param int  $new_blog_id The ID of the blog to switch to. Default: current blog.
 499   * @param bool $deprecated  Not used.
 500   * @return true Always returns true.
 501   *
 502   * @phpstan-param null $deprecated
 503   */
 504  function switch_to_blog( $new_blog_id, $deprecated = null ) {
 505      global $wpdb;
 506  
 507      $prev_blog_id = get_current_blog_id();
 508      if ( empty( $new_blog_id ) ) {
 509          $new_blog_id = $prev_blog_id;
 510      }
 511  
 512      $GLOBALS['_wp_switched_stack'][] = $prev_blog_id;
 513  
 514      /*
 515       * If we're switching to the same blog id that we're on,
 516       * set the right vars, do the associated actions, but skip
 517       * the extra unnecessary work
 518       */
 519      if ( $new_blog_id === $prev_blog_id ) {
 520          /**
 521           * Fires when the blog is switched.
 522           *
 523           * @since MU (3.0.0)
 524           * @since 5.4.0 The `$context` parameter was added.
 525           *
 526           * @param int    $new_blog_id  New blog ID.
 527           * @param int    $prev_blog_id Previous blog ID.
 528           * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
 529           *                             or 'restore' when called from restore_current_blog().
 530           */
 531          do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
 532  
 533          $GLOBALS['switched'] = true;
 534  
 535          return true;
 536      }
 537  
 538      $wpdb->set_blog_id( $new_blog_id );
 539      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 540      $GLOBALS['blog_id']      = $new_blog_id;
 541  
 542      wp_cache_switch_to_blog( $new_blog_id );
 543  
 544      /** This filter is documented in wp-includes/ms-blogs.php */
 545      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
 546  
 547      $GLOBALS['switched'] = true;
 548  
 549      return true;
 550  }
 551  
 552  /**
 553   * Restores the current blog, after calling switch_to_blog().
 554   *
 555   * @see switch_to_blog()
 556   * @since MU (3.0.0)
 557   *
 558   * @global wpdb            $wpdb               WordPress database abstraction object.
 559   * @global array           $_wp_switched_stack
 560   * @global int             $blog_id
 561   * @global bool            $switched
 562   * @global string          $table_prefix       The database table prefix.
 563   * @global WP_Object_Cache $wp_object_cache
 564   *
 565   * @return bool True on success, false if we're already on the current blog.
 566   */
 567  function restore_current_blog() {
 568      global $wpdb;
 569  
 570      if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
 571          return false;
 572      }
 573  
 574      $new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
 575      $prev_blog_id = get_current_blog_id();
 576  
 577      if ( $new_blog_id === $prev_blog_id ) {
 578          /** This filter is documented in wp-includes/ms-blogs.php */
 579          do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 580  
 581          // If we still have items in the switched stack, consider ourselves still 'switched'.
 582          $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 583  
 584          return true;
 585      }
 586  
 587      $wpdb->set_blog_id( $new_blog_id );
 588      $GLOBALS['blog_id']      = $new_blog_id;
 589      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 590  
 591      wp_cache_switch_to_blog( $new_blog_id );
 592  
 593      /** This filter is documented in wp-includes/ms-blogs.php */
 594      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 595  
 596      // If we still have items in the switched stack, consider ourselves still 'switched'.
 597      $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 598  
 599      return true;
 600  }
 601  
 602  /**
 603   * Fallback logic for switching cache context when an object cache drop-in lacks
 604   * a switch_to_blog() method.
 605   *
 606   * Reinitializes the cache and restores global/non-persistent groups.
 607   *
 608   * Used by the wp_cache_switch_to_blog() compatibility function, abstracted only
 609   * to allow for unit testing outside of the drop-in plugin inclusion circus.
 610   *
 611   * @since 7.0.0
 612   *
 613   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 614   */
 615  function wp_cache_switch_to_blog_fallback() {
 616      global $wp_object_cache;
 617  
 618      $global_groups         = false;
 619      $non_persistent_groups = false;
 620  
 621      if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) {
 622  
 623          // Get the global groups as they are.
 624          $group_names = $wp_object_cache->global_groups;
 625  
 626          // Get global group keys if non-numeric array.
 627          if ( ! wp_is_numeric_array( $group_names ) ) {
 628              $group_names = array_keys( $group_names );
 629          }
 630  
 631          $global_groups = $group_names;
 632  
 633          /*
 634           * Non-persistent groups: Check for no_mc_groups first (memcached drop-in).
 635           * Fall back to cache structure (default cache).
 636           */
 637          if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) {
 638              $non_persistent_groups = $wp_object_cache->no_mc_groups;
 639          } elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) {
 640              $all_groups            = array_keys( $wp_object_cache->cache );
 641              $non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) );
 642          }
 643      }
 644  
 645      wp_cache_init();
 646  
 647      if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 648          if ( ! is_array( $global_groups ) || empty( $global_groups ) ) {
 649              $global_groups = array(
 650                  'blog-details',
 651                  'blog-id-cache',
 652                  'blog-lookup',
 653                  'blog_meta',
 654                  'global-posts',
 655                  'image_editor',
 656                  'networks',
 657                  'network-queries',
 658                  'sites',
 659                  'site-details',
 660                  'site-options',
 661                  'site-queries',
 662                  'site-transient',
 663                  'theme_files',
 664                  'translation_files',
 665                  'rss',
 666                  'users',
 667                  'user-queries',
 668                  'user_meta',
 669                  'useremail',
 670                  'userlogins',
 671                  'userslugs',
 672              );
 673          }
 674  
 675          wp_cache_add_global_groups( $global_groups );
 676      }
 677  
 678      if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {
 679          if ( ! is_array( $non_persistent_groups ) || empty( $non_persistent_groups ) ) {
 680              $non_persistent_groups = array(
 681                  'counts',
 682                  'plugins',
 683                  'theme_json',
 684              );
 685          }
 686  
 687          wp_cache_add_non_persistent_groups( $non_persistent_groups );
 688      }
 689  }
 690  
 691  /**
 692   * Switches the initialized roles and current user capabilities to another site.
 693   *
 694   * @since 4.9.0
 695   *
 696   * @param int $new_site_id New site ID.
 697   * @param int $old_site_id Old site ID.
 698   */
 699  function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
 700      if ( $new_site_id === $old_site_id ) {
 701          return;
 702      }
 703  
 704      if ( ! did_action( 'init' ) ) {
 705          return;
 706      }
 707  
 708      wp_roles()->for_site( $new_site_id );
 709      wp_get_current_user()->for_site( $new_site_id );
 710  }
 711  
 712  /**
 713   * Determines if switch_to_blog() is in effect.
 714   *
 715   * @since 3.5.0
 716   *
 717   * @global array $_wp_switched_stack
 718   *
 719   * @return bool True if switched, false otherwise.
 720   */
 721  function ms_is_switched() {
 722      return ! empty( $GLOBALS['_wp_switched_stack'] );
 723  }
 724  
 725  /**
 726   * Checks if a particular blog is archived.
 727   *
 728   * @since MU (3.0.0)
 729   *
 730   * @param int $id Blog ID.
 731   * @return string Whether the blog is archived or not.
 732   */
 733  function is_archived( $id ) {
 734      return get_blog_status( $id, 'archived' );
 735  }
 736  
 737  /**
 738   * Updates the 'archived' status of a particular blog.
 739   *
 740   * @since MU (3.0.0)
 741   *
 742   * @param int    $id       Blog ID.
 743   * @param string $archived The new status.
 744   * @return string The new archived status.
 745   */
 746  function update_archived( $id, $archived ) {
 747      update_blog_status( $id, 'archived', $archived );
 748      return $archived;
 749  }
 750  
 751  /**
 752   * Updates a blog details field.
 753   *
 754   * @since MU (3.0.0)
 755   * @since 5.1.0 Use wp_update_site() internally.
 756   *
 757   * @global wpdb $wpdb WordPress database abstraction object.
 758   *
 759   * @param int    $blog_id    Blog ID.
 760   * @param string $pref       Field name.
 761   * @param string $value      Field value.
 762   * @param mixed  $deprecated Not used.
 763   * @return string|false The passed field value, or false if the update failed.
 764   */
 765  function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
 766      global $wpdb;
 767  
 768      if ( null !== $deprecated ) {
 769          _deprecated_argument( __FUNCTION__, '3.1.0' );
 770      }
 771  
 772      $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
 773  
 774      if ( ! in_array( $pref, $allowed_field_names, true ) ) {
 775          return $value;
 776      }
 777  
 778      $result = wp_update_site(
 779          $blog_id,
 780          array(
 781              $pref => $value,
 782          )
 783      );
 784  
 785      if ( is_wp_error( $result ) ) {
 786          return false;
 787      }
 788  
 789      return $value;
 790  }
 791  
 792  /**
 793   * Gets a blog details field.
 794   *
 795   * @since MU (3.0.0)
 796   *
 797   * @global wpdb $wpdb WordPress database abstraction object.
 798   *
 799   * @param int    $id   Blog ID.
 800   * @param string $pref Field name.
 801   * @return bool|string|null The field value, or null if not found.
 802   */
 803  function get_blog_status( $id, $pref ) {
 804      global $wpdb;
 805  
 806      $details = get_site( $id );
 807      if ( $details ) {
 808          return $details->$pref;
 809      }
 810  
 811      return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
 812  }
 813  
 814  /**
 815   * Gets a list of most recently updated blogs.
 816   *
 817   * @since MU (3.0.0)
 818   *
 819   * @global wpdb $wpdb WordPress database abstraction object.
 820   *
 821   * @param mixed $deprecated Not used.
 822   * @param int   $start      Optional. Number of blogs to offset the query. Used to build LIMIT clause.
 823   *                          Can be used for pagination. Default 0.
 824   * @param int   $quantity   Optional. The maximum number of blogs to retrieve. Default 40.
 825   * @return array The list of blogs.
 826   *
 827   * @phpstan-param '' $deprecated
 828   */
 829  function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
 830      global $wpdb;
 831  
 832      if ( ! empty( $deprecated ) ) {
 833          _deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
 834      }
 835  
 836      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 );
 837  }
 838  
 839  /**
 840   * Handler for updating the site's last updated date when a post is published or
 841   * an already published post is changed.
 842   *
 843   * @since 3.3.0
 844   *
 845   * @param string  $new_status The new post status.
 846   * @param string  $old_status The old post status.
 847   * @param WP_Post $post       Post object.
 848   */
 849  function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
 850      $post_type_obj = get_post_type_object( $post->post_type );
 851      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 852          return;
 853      }
 854  
 855      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 856          return;
 857      }
 858  
 859      // Post was freshly published, published post was saved, or published post was unpublished.
 860  
 861      wpmu_update_blogs_date();
 862  }
 863  
 864  /**
 865   * Handler for updating the current site's last updated date when a published
 866   * post is deleted.
 867   *
 868   * @since 3.4.0
 869   *
 870   * @param int $post_id Post ID
 871   */
 872  function _update_blog_date_on_post_delete( $post_id ) {
 873      $post = get_post( $post_id );
 874  
 875      $post_type_obj = get_post_type_object( $post->post_type );
 876      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 877          return;
 878      }
 879  
 880      if ( 'publish' !== $post->post_status ) {
 881          return;
 882      }
 883  
 884      wpmu_update_blogs_date();
 885  }
 886  
 887  /**
 888   * Handler for updating the current site's posts count when a post is deleted.
 889   *
 890   * @since 4.0.0
 891   * @since 6.2.0 Added the `$post` parameter.
 892   *
 893   * @param int     $post_id Post ID.
 894   * @param WP_Post $post    Post object.
 895   */
 896  function _update_posts_count_on_delete( $post_id, $post ) {
 897      if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
 898          return;
 899      }
 900  
 901      update_posts_count();
 902  }
 903  
 904  /**
 905   * Handler for updating the current site's posts count when a post status changes.
 906   *
 907   * @since 4.0.0
 908   * @since 4.9.0 Added the `$post` parameter.
 909   *
 910   * @param string       $new_status The status the post is changing to.
 911   * @param string       $old_status The status the post is changing from.
 912   * @param WP_Post|null $post       Post object.
 913   */
 914  function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
 915      if ( $new_status === $old_status ) {
 916          return;
 917      }
 918  
 919      if ( 'post' !== get_post_type( $post ) ) {
 920          return;
 921      }
 922  
 923      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 924          return;
 925      }
 926  
 927      update_posts_count();
 928  }
 929  
 930  /**
 931   * Counts number of sites grouped by site status.
 932   *
 933   * @since 5.3.0
 934   *
 935   * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
 936   * @return int[] {
 937   *     Numbers of sites grouped by site status.
 938   *
 939   *     @type int $all      The total number of sites.
 940   *     @type int $public   The number of public sites.
 941   *     @type int $archived The number of archived sites.
 942   *     @type int $mature   The number of mature sites.
 943   *     @type int $spam     The number of spam sites.
 944   *     @type int $deleted  The number of deleted sites.
 945   * }
 946   */
 947  function wp_count_sites( $network_id = null ) {
 948      if ( empty( $network_id ) ) {
 949          $network_id = get_current_network_id();
 950      }
 951  
 952      $counts = array();
 953      $args   = array(
 954          'network_id'    => $network_id,
 955          'number'        => 1,
 956          'fields'        => 'ids',
 957          'no_found_rows' => false,
 958      );
 959  
 960      $q             = new WP_Site_Query( $args );
 961      $counts['all'] = $q->found_sites;
 962  
 963      $_args    = $args;
 964      $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
 965  
 966      foreach ( $statuses as $status ) {
 967          $_args            = $args;
 968          $_args[ $status ] = 1;
 969  
 970          $q                 = new WP_Site_Query( $_args );
 971          $counts[ $status ] = $q->found_sites;
 972      }
 973  
 974      return $counts;
 975  }


Generated : Thu Oct 15 08:20:32 2026 Cross-referenced by PHPXref