[ 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   * Update 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   * Get a full blog URL, given a blog ID.
  35   *
  36   * @since MU (3.0.0)
  37   *
  38   * @param int $blog_id Blog ID.
  39   * @return string Full URL of the blog 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   * Get a full blog URL, given a blog name.
  56   *
  57   * @since MU (3.0.0)
  58   *
  59   * @param string $blogname The (subdomain or directory) name
  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   * Retrieve 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   *                                  If not specified the current blog ID is used.
 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 ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
 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 ( 'www.' === substr( $fields['domain'], 0, 4 ) ) {
 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   * Clear 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   * Update the details for a blog. Updates the blogs table for a given blog ID.
 293   *
 294   * @since MU (3.0.0)
 295   *
 296   * @global wpdb $wpdb WordPress database abstraction object.
 297   *
 298   * @param int   $blog_id Blog ID.
 299   * @param array $details Array of details keyed by blogs table field names.
 300   * @return bool True if update succeeds, false otherwise.
 301   */
 302  function update_blog_details( $blog_id, $details = array() ) {
 303      global $wpdb;
 304  
 305      if ( empty( $details ) ) {
 306          return false;
 307      }
 308  
 309      if ( is_object( $details ) ) {
 310          $details = get_object_vars( $details );
 311      }
 312  
 313      $site = wp_update_site( $blog_id, $details );
 314  
 315      if ( is_wp_error( $site ) ) {
 316          return false;
 317      }
 318  
 319      return true;
 320  }
 321  
 322  /**
 323   * Cleans the site details cache for a site.
 324   *
 325   * @since 4.7.4
 326   *
 327   * @param int $site_id Optional. Site ID. Default is the current site ID.
 328   */
 329  function clean_site_details_cache( $site_id = 0 ) {
 330      $site_id = (int) $site_id;
 331      if ( ! $site_id ) {
 332          $site_id = get_current_blog_id();
 333      }
 334  
 335      wp_cache_delete( $site_id, 'site-details' );
 336      wp_cache_delete( $site_id, 'blog-details' );
 337  }
 338  
 339  /**
 340   * Retrieve option value for a given blog id based on name of option.
 341   *
 342   * If the option does not exist or does not have a value, then the return value
 343   * will be false. This is useful to check whether you need to install an option
 344   * and is commonly used during installation of plugin options and to test
 345   * whether upgrading is required.
 346   *
 347   * If the option was serialized then it will be unserialized when it is returned.
 348   *
 349   * @since MU (3.0.0)
 350   *
 351   * @param int    $id      A blog ID. Can be null to refer to the current blog.
 352   * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
 353   * @param mixed  $default Optional. Default value to return if the option does not exist.
 354   * @return mixed Value set for the option.
 355   */
 356  function get_blog_option( $id, $option, $default = false ) {
 357      $id = (int) $id;
 358  
 359      if ( empty( $id ) ) {
 360          $id = get_current_blog_id();
 361      }
 362  
 363      if ( get_current_blog_id() == $id ) {
 364          return get_option( $option, $default );
 365      }
 366  
 367      switch_to_blog( $id );
 368      $value = get_option( $option, $default );
 369      restore_current_blog();
 370  
 371      /**
 372       * Filters a blog option value.
 373       *
 374       * The dynamic portion of the hook name, `$option`, refers to the blog option name.
 375       *
 376       * @since 3.5.0
 377       *
 378       * @param string  $value The option value.
 379       * @param int     $id    Blog ID.
 380       */
 381      return apply_filters( "blog_option_{$option}", $value, $id );
 382  }
 383  
 384  /**
 385   * Add a new option for a given blog ID.
 386   *
 387   * You do not need to serialize values. If the value needs to be serialized, then
 388   * it will be serialized before it is inserted into the database. Remember,
 389   * resources can not be serialized or added as an option.
 390   *
 391   * You can create options without values and then update the values later.
 392   * Existing options will not be updated and checks are performed to ensure that you
 393   * aren't adding a protected WordPress option. Care should be taken to not name
 394   * options the same as the ones which are protected.
 395   *
 396   * @since MU (3.0.0)
 397   *
 398   * @param int    $id     A blog ID. Can be null to refer to the current blog.
 399   * @param string $option Name of option to add. Expected to not be SQL-escaped.
 400   * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
 401   * @return bool True if the option was added, false otherwise.
 402   */
 403  function add_blog_option( $id, $option, $value ) {
 404      $id = (int) $id;
 405  
 406      if ( empty( $id ) ) {
 407          $id = get_current_blog_id();
 408      }
 409  
 410      if ( get_current_blog_id() == $id ) {
 411          return add_option( $option, $value );
 412      }
 413  
 414      switch_to_blog( $id );
 415      $return = add_option( $option, $value );
 416      restore_current_blog();
 417  
 418      return $return;
 419  }
 420  
 421  /**
 422   * Removes option by name for a given blog ID. Prevents removal of protected WordPress options.
 423   *
 424   * @since MU (3.0.0)
 425   *
 426   * @param int    $id     A blog ID. Can be null to refer to the current blog.
 427   * @param string $option Name of option to remove. Expected to not be SQL-escaped.
 428   * @return bool True if the option was deleted, false otherwise.
 429   */
 430  function delete_blog_option( $id, $option ) {
 431      $id = (int) $id;
 432  
 433      if ( empty( $id ) ) {
 434          $id = get_current_blog_id();
 435      }
 436  
 437      if ( get_current_blog_id() == $id ) {
 438          return delete_option( $option );
 439      }
 440  
 441      switch_to_blog( $id );
 442      $return = delete_option( $option );
 443      restore_current_blog();
 444  
 445      return $return;
 446  }
 447  
 448  /**
 449   * Update an option for a particular blog.
 450   *
 451   * @since MU (3.0.0)
 452   *
 453   * @param int    $id         The blog ID.
 454   * @param string $option     The option key.
 455   * @param mixed  $value      The option value.
 456   * @param mixed  $deprecated Not used.
 457   * @return bool True if the value was updated, false otherwise.
 458   */
 459  function update_blog_option( $id, $option, $value, $deprecated = null ) {
 460      $id = (int) $id;
 461  
 462      if ( null !== $deprecated ) {
 463          _deprecated_argument( __FUNCTION__, '3.1.0' );
 464      }
 465  
 466      if ( get_current_blog_id() == $id ) {
 467          return update_option( $option, $value );
 468      }
 469  
 470      switch_to_blog( $id );
 471      $return = update_option( $option, $value );
 472      restore_current_blog();
 473  
 474      return $return;
 475  }
 476  
 477  /**
 478   * Switch the current blog.
 479   *
 480   * This function is useful if you need to pull posts, or other information,
 481   * from other blogs. You can switch back afterwards using restore_current_blog().
 482   *
 483   * Things that aren't switched:
 484   *  - plugins. 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
 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      if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
 539          wp_cache_switch_to_blog( $new_blog_id );
 540      } else {
 541          global $wp_object_cache;
 542  
 543          if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
 544              $global_groups = $wp_object_cache->global_groups;
 545          } else {
 546              $global_groups = false;
 547          }
 548  
 549          wp_cache_init();
 550  
 551          if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 552              if ( is_array( $global_groups ) ) {
 553                  wp_cache_add_global_groups( $global_groups );
 554              } else {
 555                  wp_cache_add_global_groups(
 556                      array(
 557                          'blog-details',
 558                          'blog-id-cache',
 559                          'blog-lookup',
 560                          'blog_meta',
 561                          'global-posts',
 562                          'networks',
 563                          'sites',
 564                          'site-details',
 565                          'site-options',
 566                          'site-transient',
 567                          'rss',
 568                          'users',
 569                          'useremail',
 570                          'userlogins',
 571                          'usermeta',
 572                          'user_meta',
 573                          'userslugs',
 574                      )
 575                  );
 576              }
 577  
 578              wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 579          }
 580      }
 581  
 582      /** This filter is documented in wp-includes/ms-blogs.php */
 583      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
 584  
 585      $GLOBALS['switched'] = true;
 586  
 587      return true;
 588  }
 589  
 590  /**
 591   * Restore the current blog, after calling switch_to_blog().
 592   *
 593   * @see switch_to_blog()
 594   * @since MU (3.0.0)
 595   *
 596   * @global wpdb            $wpdb               WordPress database abstraction object.
 597   * @global array           $_wp_switched_stack
 598   * @global int             $blog_id
 599   * @global bool            $switched
 600   * @global string          $table_prefix
 601   * @global WP_Object_Cache $wp_object_cache
 602   *
 603   * @return bool True on success, false if we're already on the current blog.
 604   */
 605  function restore_current_blog() {
 606      global $wpdb;
 607  
 608      if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
 609          return false;
 610      }
 611  
 612      $new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
 613      $prev_blog_id = get_current_blog_id();
 614  
 615      if ( $new_blog_id == $prev_blog_id ) {
 616          /** This filter is documented in wp-includes/ms-blogs.php */
 617          do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 618  
 619          // If we still have items in the switched stack, consider ourselves still 'switched'.
 620          $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 621  
 622          return true;
 623      }
 624  
 625      $wpdb->set_blog_id( $new_blog_id );
 626      $GLOBALS['blog_id']      = $new_blog_id;
 627      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 628  
 629      if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
 630          wp_cache_switch_to_blog( $new_blog_id );
 631      } else {
 632          global $wp_object_cache;
 633  
 634          if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
 635              $global_groups = $wp_object_cache->global_groups;
 636          } else {
 637              $global_groups = false;
 638          }
 639  
 640          wp_cache_init();
 641  
 642          if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 643              if ( is_array( $global_groups ) ) {
 644                  wp_cache_add_global_groups( $global_groups );
 645              } else {
 646                  wp_cache_add_global_groups(
 647                      array(
 648                          'blog-details',
 649                          'blog-id-cache',
 650                          'blog-lookup',
 651                          'blog_meta',
 652                          'global-posts',
 653                          'networks',
 654                          'sites',
 655                          'site-details',
 656                          'site-options',
 657                          'site-transient',
 658                          'rss',
 659                          'users',
 660                          'useremail',
 661                          'userlogins',
 662                          'usermeta',
 663                          'user_meta',
 664                          'userslugs',
 665                      )
 666                  );
 667              }
 668  
 669              wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 670          }
 671      }
 672  
 673      /** This filter is documented in wp-includes/ms-blogs.php */
 674      do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );
 675  
 676      // If we still have items in the switched stack, consider ourselves still 'switched'.
 677      $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 678  
 679      return true;
 680  }
 681  
 682  /**
 683   * Switches the initialized roles and current user capabilities to another site.
 684   *
 685   * @since 4.9.0
 686   *
 687   * @param int $new_site_id New site ID.
 688   * @param int $old_site_id Old site ID.
 689   */
 690  function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
 691      if ( $new_site_id == $old_site_id ) {
 692          return;
 693      }
 694  
 695      if ( ! did_action( 'init' ) ) {
 696          return;
 697      }
 698  
 699      wp_roles()->for_site( $new_site_id );
 700      wp_get_current_user()->for_site( $new_site_id );
 701  }
 702  
 703  /**
 704   * Determines if switch_to_blog() is in effect
 705   *
 706   * @since 3.5.0
 707   *
 708   * @global array $_wp_switched_stack
 709   *
 710   * @return bool True if switched, false otherwise.
 711   */
 712  function ms_is_switched() {
 713      return ! empty( $GLOBALS['_wp_switched_stack'] );
 714  }
 715  
 716  /**
 717   * Check if a particular blog is archived.
 718   *
 719   * @since MU (3.0.0)
 720   *
 721   * @param int $id Blog ID.
 722   * @return string Whether the blog is archived or not.
 723   */
 724  function is_archived( $id ) {
 725      return get_blog_status( $id, 'archived' );
 726  }
 727  
 728  /**
 729   * Update the 'archived' status of a particular blog.
 730   *
 731   * @since MU (3.0.0)
 732   *
 733   * @param int    $id       Blog ID.
 734   * @param string $archived The new status.
 735   * @return string $archived
 736   */
 737  function update_archived( $id, $archived ) {
 738      update_blog_status( $id, 'archived', $archived );
 739      return $archived;
 740  }
 741  
 742  /**
 743   * Update a blog details field.
 744   *
 745   * @since MU (3.0.0)
 746   * @since 5.1.0 Use wp_update_site() internally.
 747   *
 748   * @global wpdb $wpdb WordPress database abstraction object.
 749   *
 750   * @param int    $blog_id    Blog ID.
 751   * @param string $pref       Field name.
 752   * @param string $value      Field value.
 753   * @param null   $deprecated Not used.
 754   * @return string|false $value
 755   */
 756  function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
 757      global $wpdb;
 758  
 759      if ( null !== $deprecated ) {
 760          _deprecated_argument( __FUNCTION__, '3.1.0' );
 761      }
 762  
 763      $allowed_field_names = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
 764  
 765      if ( ! in_array( $pref, $allowed_field_names, true ) ) {
 766          return $value;
 767      }
 768  
 769      $result = wp_update_site(
 770          $blog_id,
 771          array(
 772              $pref => $value,
 773          )
 774      );
 775  
 776      if ( is_wp_error( $result ) ) {
 777          return false;
 778      }
 779  
 780      return $value;
 781  }
 782  
 783  /**
 784   * Get a blog details field.
 785   *
 786   * @since MU (3.0.0)
 787   *
 788   * @global wpdb $wpdb WordPress database abstraction object.
 789   *
 790   * @param int    $id   Blog ID.
 791   * @param string $pref Field name.
 792   * @return bool|string|null $value
 793   */
 794  function get_blog_status( $id, $pref ) {
 795      global $wpdb;
 796  
 797      $details = get_site( $id );
 798      if ( $details ) {
 799          return $details->$pref;
 800      }
 801  
 802      return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
 803  }
 804  
 805  /**
 806   * Get a list of most recently updated blogs.
 807   *
 808   * @since MU (3.0.0)
 809   *
 810   * @global wpdb $wpdb WordPress database abstraction object.
 811   *
 812   * @param mixed $deprecated Not used.
 813   * @param int   $start      Optional. Number of blogs to offset the query. Used to build LIMIT clause.
 814   *                          Can be used for pagination. Default 0.
 815   * @param int   $quantity   Optional. The maximum number of blogs to retrieve. Default 40.
 816   * @return array The list of blogs.
 817   */
 818  function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
 819      global $wpdb;
 820  
 821      if ( ! empty( $deprecated ) ) {
 822          _deprecated_argument( __FUNCTION__, 'MU' ); // Never used.
 823      }
 824  
 825      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 );
 826  }
 827  
 828  /**
 829   * Handler for updating the site's last updated date when a post is published or
 830   * an already published post is changed.
 831   *
 832   * @since 3.3.0
 833   *
 834   * @param string  $new_status The new post status.
 835   * @param string  $old_status The old post status.
 836   * @param WP_Post $post       Post object.
 837   */
 838  function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
 839      $post_type_obj = get_post_type_object( $post->post_type );
 840      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 841          return;
 842      }
 843  
 844      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 845          return;
 846      }
 847  
 848      // Post was freshly published, published post was saved, or published post was unpublished.
 849  
 850      wpmu_update_blogs_date();
 851  }
 852  
 853  /**
 854   * Handler for updating the current site's last updated date when a published
 855   * post is deleted.
 856   *
 857   * @since 3.4.0
 858   *
 859   * @param int $post_id Post ID
 860   */
 861  function _update_blog_date_on_post_delete( $post_id ) {
 862      $post = get_post( $post_id );
 863  
 864      $post_type_obj = get_post_type_object( $post->post_type );
 865      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 866          return;
 867      }
 868  
 869      if ( 'publish' !== $post->post_status ) {
 870          return;
 871      }
 872  
 873      wpmu_update_blogs_date();
 874  }
 875  
 876  /**
 877   * Handler for updating the current site's posts count when a post is deleted.
 878   *
 879   * @since 4.0.0
 880   *
 881   * @param int $post_id Post ID.
 882   */
 883  function _update_posts_count_on_delete( $post_id ) {
 884      $post = get_post( $post_id );
 885  
 886      if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
 887          return;
 888      }
 889  
 890      update_posts_count();
 891  }
 892  
 893  /**
 894   * Handler for updating the current site's posts count when a post status changes.
 895   *
 896   * @since 4.0.0
 897   * @since 4.9.0 Added the `$post` parameter.
 898   *
 899   * @param string  $new_status The status the post is changing to.
 900   * @param string  $old_status The status the post is changing from.
 901   * @param WP_Post $post       Post object
 902   */
 903  function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
 904      if ( $new_status === $old_status ) {
 905          return;
 906      }
 907  
 908      if ( 'post' !== get_post_type( $post ) ) {
 909          return;
 910      }
 911  
 912      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 913          return;
 914      }
 915  
 916      update_posts_count();
 917  }
 918  
 919  /**
 920   * Count number of sites grouped by site status.
 921   *
 922   * @since 5.3.0
 923   *
 924   * @param int $network_id Optional. The network to get counts for. Default is the current network ID.
 925   * @return int[] {
 926   *     Numbers of sites grouped by site status.
 927   *
 928   *     @type int $all      The total number of sites.
 929   *     @type int $public   The number of public sites.
 930   *     @type int $archived The number of archived sites.
 931   *     @type int $mature   The number of mature sites.
 932   *     @type int $spam     The number of spam sites.
 933   *     @type int $deleted  The number of deleted sites.
 934   * }
 935   */
 936  function wp_count_sites( $network_id = null ) {
 937      if ( empty( $network_id ) ) {
 938          $network_id = get_current_network_id();
 939      }
 940  
 941      $counts = array();
 942      $args   = array(
 943          'network_id'    => $network_id,
 944          'number'        => 1,
 945          'fields'        => 'ids',
 946          'no_found_rows' => false,
 947      );
 948  
 949      $q             = new WP_Site_Query( $args );
 950      $counts['all'] = $q->found_sites;
 951  
 952      $_args    = $args;
 953      $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
 954  
 955      foreach ( $statuses as $status ) {
 956          $_args            = $args;
 957          $_args[ $status ] = 1;
 958  
 959          $q                 = new WP_Site_Query( $_args );
 960          $counts[ $status ] = $q->found_sites;
 961      }
 962  
 963      return $counts;
 964  }


Generated : Fri Aug 19 08:20:02 2022 Cross-referenced by PHPXref