[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Apr 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1