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


Generated : Tue Nov 12 08:20:01 2024 Cross-referenced by PHPXref