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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7