[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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 site_details
 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           *
 512           * @param int $new_blog_id  New blog ID.
 513           * @param int $prev_blog_id Previous blog ID.
 514           */
 515          do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
 516          $GLOBALS['switched'] = true;
 517          return true;
 518      }
 519  
 520      $wpdb->set_blog_id( $new_blog_id );
 521      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 522      $GLOBALS['blog_id']      = $new_blog_id;
 523  
 524      if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
 525          wp_cache_switch_to_blog( $new_blog_id );
 526      } else {
 527          global $wp_object_cache;
 528  
 529          if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
 530              $global_groups = $wp_object_cache->global_groups;
 531          } else {
 532              $global_groups = false;
 533          }
 534          wp_cache_init();
 535  
 536          if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 537              if ( is_array( $global_groups ) ) {
 538                  wp_cache_add_global_groups( $global_groups );
 539              } else {
 540                  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' ) );
 541              }
 542              wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 543          }
 544      }
 545  
 546      /** This filter is documented in wp-includes/ms-blogs.php */
 547      do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
 548      $GLOBALS['switched'] = true;
 549  
 550      return true;
 551  }
 552  
 553  /**
 554   * Restore the current blog, after calling switch_to_blog().
 555   *
 556   * @see switch_to_blog()
 557   * @since MU (3.0.0)
 558   *
 559   * @global wpdb            $wpdb               WordPress database abstraction object.
 560   * @global array           $_wp_switched_stack
 561   * @global int             $blog_id
 562   * @global bool            $switched
 563   * @global string          $table_prefix
 564   * @global WP_Object_Cache $wp_object_cache
 565   *
 566   * @return bool True on success, false if we're already on the current blog.
 567   */
 568  function restore_current_blog() {
 569      global $wpdb;
 570  
 571      if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
 572          return false;
 573      }
 574  
 575      $new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
 576      $prev_blog_id = get_current_blog_id();
 577  
 578      if ( $new_blog_id == $prev_blog_id ) {
 579          /** This filter is documented in wp-includes/ms-blogs.php */
 580          do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
 581          // If we still have items in the switched stack, consider ourselves still 'switched'
 582          $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 583          return true;
 584      }
 585  
 586      $wpdb->set_blog_id( $new_blog_id );
 587      $GLOBALS['blog_id']      = $new_blog_id;
 588      $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
 589  
 590      if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
 591          wp_cache_switch_to_blog( $new_blog_id );
 592      } else {
 593          global $wp_object_cache;
 594  
 595          if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
 596              $global_groups = $wp_object_cache->global_groups;
 597          } else {
 598              $global_groups = false;
 599          }
 600  
 601          wp_cache_init();
 602  
 603          if ( function_exists( 'wp_cache_add_global_groups' ) ) {
 604              if ( is_array( $global_groups ) ) {
 605                  wp_cache_add_global_groups( $global_groups );
 606              } else {
 607                  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' ) );
 608              }
 609              wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 610          }
 611      }
 612  
 613      /** This filter is documented in wp-includes/ms-blogs.php */
 614      do_action( 'switch_blog', $new_blog_id, $prev_blog_id );
 615  
 616      // If we still have items in the switched stack, consider ourselves still 'switched'
 617      $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
 618  
 619      return true;
 620  }
 621  
 622  /**
 623   * Switches the initialized roles and current user capabilities to another site.
 624   *
 625   * @since 4.9.0
 626   *
 627   * @param int $new_site_id New site ID.
 628   * @param int $old_site_id Old site ID.
 629   */
 630  function wp_switch_roles_and_user( $new_site_id, $old_site_id ) {
 631      if ( $new_site_id == $old_site_id ) {
 632          return;
 633      }
 634  
 635      if ( ! did_action( 'init' ) ) {
 636          return;
 637      }
 638  
 639      wp_roles()->for_site( $new_site_id );
 640      wp_get_current_user()->for_site( $new_site_id );
 641  }
 642  
 643  /**
 644   * Determines if switch_to_blog() is in effect
 645   *
 646   * @since 3.5.0
 647   *
 648   * @global array $_wp_switched_stack
 649   *
 650   * @return bool True if switched, false otherwise.
 651   */
 652  function ms_is_switched() {
 653      return ! empty( $GLOBALS['_wp_switched_stack'] );
 654  }
 655  
 656  /**
 657   * Check if a particular blog is archived.
 658   *
 659   * @since MU (3.0.0)
 660   *
 661   * @param int $id Blog ID.
 662   * @return string Whether the blog is archived or not.
 663   */
 664  function is_archived( $id ) {
 665      return get_blog_status( $id, 'archived' );
 666  }
 667  
 668  /**
 669   * Update the 'archived' status of a particular blog.
 670   *
 671   * @since MU (3.0.0)
 672   *
 673   * @param int    $id       Blog ID.
 674   * @param string $archived The new status.
 675   * @return string $archived
 676   */
 677  function update_archived( $id, $archived ) {
 678      update_blog_status( $id, 'archived', $archived );
 679      return $archived;
 680  }
 681  
 682  /**
 683   * Update a blog details field.
 684   *
 685   * @since MU (3.0.0)
 686   * @since 5.1.0 Use wp_update_site() internally.
 687   *
 688   * @global wpdb $wpdb WordPress database abstraction object.
 689   *
 690   * @param int    $blog_id    Blog ID.
 691   * @param string $pref       Field name.
 692   * @param string $value      Field value.
 693   * @param null   $deprecated Not used.
 694   * @return string|false $value
 695   */
 696  function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
 697      global $wpdb;
 698  
 699      if ( null !== $deprecated ) {
 700          _deprecated_argument( __FUNCTION__, '3.1.0' );
 701      }
 702  
 703      if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) ) {
 704          return $value;
 705      }
 706  
 707      $result = wp_update_site(
 708          $blog_id,
 709          array(
 710              $pref => $value,
 711          )
 712      );
 713  
 714      if ( is_wp_error( $result ) ) {
 715          return false;
 716      }
 717  
 718      return $value;
 719  }
 720  
 721  /**
 722   * Get a blog details field.
 723   *
 724   * @since MU (3.0.0)
 725   *
 726   * @global wpdb $wpdb WordPress database abstraction object.
 727   *
 728   * @param int    $id   Blog ID.
 729   * @param string $pref Field name.
 730   * @return bool|string|null $value
 731   */
 732  function get_blog_status( $id, $pref ) {
 733      global $wpdb;
 734  
 735      $details = get_site( $id );
 736      if ( $details ) {
 737          return $details->$pref;
 738      }
 739  
 740      return $wpdb->get_var( $wpdb->prepare( "SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id ) );
 741  }
 742  
 743  /**
 744   * Get a list of most recently updated blogs.
 745   *
 746   * @since MU (3.0.0)
 747   *
 748   * @global wpdb $wpdb WordPress database abstraction object.
 749   *
 750   * @param mixed $deprecated Not used.
 751   * @param int   $start      Optional. Number of blogs to offset the query. Used to build LIMIT clause.
 752   *                          Can be used for pagination. Default 0.
 753   * @param int   $quantity   Optional. The maximum number of blogs to retrieve. Default 40.
 754   * @return array The list of blogs.
 755   */
 756  function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
 757      global $wpdb;
 758  
 759      if ( ! empty( $deprecated ) ) {
 760          _deprecated_argument( __FUNCTION__, 'MU' ); // never used
 761      }
 762  
 763      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 );
 764  }
 765  
 766  /**
 767   * Handler for updating the site's last updated date when a post is published or
 768   * an already published post is changed.
 769   *
 770   * @since 3.3.0
 771   *
 772   * @param string $new_status The new post status
 773   * @param string $old_status The old post status
 774   * @param object $post       Post object
 775   */
 776  function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
 777      $post_type_obj = get_post_type_object( $post->post_type );
 778      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 779          return;
 780      }
 781  
 782      if ( 'publish' != $new_status && 'publish' != $old_status ) {
 783          return;
 784      }
 785  
 786      // Post was freshly published, published post was saved, or published post was unpublished.
 787  
 788      wpmu_update_blogs_date();
 789  }
 790  
 791  /**
 792   * Handler for updating the current site's last updated date when a published
 793   * post is deleted.
 794   *
 795   * @since 3.4.0
 796   *
 797   * @param int $post_id Post ID
 798   */
 799  function _update_blog_date_on_post_delete( $post_id ) {
 800      $post = get_post( $post_id );
 801  
 802      $post_type_obj = get_post_type_object( $post->post_type );
 803      if ( ! $post_type_obj || ! $post_type_obj->public ) {
 804          return;
 805      }
 806  
 807      if ( 'publish' != $post->post_status ) {
 808          return;
 809      }
 810  
 811      wpmu_update_blogs_date();
 812  }
 813  
 814  /**
 815   * Handler for updating the current site's posts count when a post is deleted.
 816   *
 817   * @since 4.0.0
 818   *
 819   * @param int $post_id Post ID.
 820   */
 821  function _update_posts_count_on_delete( $post_id ) {
 822      $post = get_post( $post_id );
 823  
 824      if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
 825          return;
 826      }
 827  
 828      update_posts_count();
 829  }
 830  
 831  /**
 832   * Handler for updating the current site's posts count when a post status changes.
 833   *
 834   * @since 4.0.0
 835   * @since 4.9.0 Added the `$post` parameter.
 836   *
 837   * @param string  $new_status The status the post is changing to.
 838   * @param string  $old_status The status the post is changing from.
 839   * @param WP_Post $post       Post object
 840   */
 841  function _update_posts_count_on_transition_post_status( $new_status, $old_status, $post = null ) {
 842      if ( $new_status === $old_status ) {
 843          return;
 844      }
 845  
 846      if ( 'post' !== get_post_type( $post ) ) {
 847          return;
 848      }
 849  
 850      if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
 851          return;
 852      }
 853  
 854      update_posts_count();
 855  }
 856  
 857  /**
 858   * Count number of sites grouped by site status.
 859   *
 860   * @since 5.3.0
 861   *
 862   * @param int $network_id The network to get counts for.  Default is the current network id.
 863   * @return array Includes a grand total 'all' and an array of counts indexed by
 864   *                status strings: public, archived, mature, spam, deleted.
 865   */
 866  function wp_count_sites( $network_id = null ) {
 867      if ( empty( $network_id ) ) {
 868          $network_id = get_current_network_id();
 869      }
 870  
 871      $counts = array();
 872      $args   = array(
 873          'network_id'    => $network_id,
 874          'number'        => 1,
 875          'fields'        => 'ids',
 876          'no_found_rows' => false,
 877      );
 878  
 879      $q             = new WP_Site_Query( $args );
 880      $counts['all'] = $q->found_sites;
 881  
 882      $_args    = $args;
 883      $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
 884  
 885      foreach ( $statuses as $status ) {
 886          $_args            = $args;
 887          $_args[ $status ] = 1;
 888  
 889          $q                 = new WP_Site_Query( $_args );
 890          $counts[ $status ] = $q->found_sites;
 891      }
 892  
 893      return $counts;
 894  }


Generated: Fri Oct 25 08:20:01 2019 Cross-referenced by PHPXref 0.7