[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> update.php (source)

   1  <?php
   2  /**
   3   * A simple set of functions to check our version 1.0 update service.
   4   *
   5   * @package WordPress
   6   * @since 2.3.0
   7   */
   8  
   9  /**
  10   * Check WordPress version against the newest version.
  11   *
  12   * The WordPress version, PHP version, and Locale is sent. Checks against the
  13   * WordPress server at api.wordpress.org server. Will only check if WordPress
  14   * isn't installing.
  15   *
  16   * @since 2.3.0
  17   * @global string $wp_version       Used to check against the newest WordPress version.
  18   * @global wpdb   $wpdb             WordPress database abstraction object.
  19   * @global string $wp_local_package Locale code of the package.
  20   *
  21   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  22   * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set.
  23   */
  24  function wp_version_check( $extra_stats = array(), $force_check = false ) {
  25      if ( wp_installing() ) {
  26          return;
  27      }
  28  
  29      global $wpdb, $wp_local_package;
  30      // Include an unmodified $wp_version.
  31      require  ABSPATH . WPINC . '/version.php';
  32      $php_version = phpversion();
  33  
  34      $current      = get_site_transient( 'update_core' );
  35      $translations = wp_get_installed_translations( 'core' );
  36  
  37      // Invalidate the transient when $wp_version changes.
  38      if ( is_object( $current ) && $wp_version != $current->version_checked ) {
  39          $current = false;
  40      }
  41  
  42      if ( ! is_object( $current ) ) {
  43          $current                  = new stdClass;
  44          $current->updates         = array();
  45          $current->version_checked = $wp_version;
  46      }
  47  
  48      if ( ! empty( $extra_stats ) ) {
  49          $force_check = true;
  50      }
  51  
  52      // Wait 1 minute between multiple version check requests.
  53      $timeout          = MINUTE_IN_SECONDS;
  54      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  55      if ( ! $force_check && $time_not_changed ) {
  56          return;
  57      }
  58  
  59      /**
  60       * Filters the locale requested for WordPress core translations.
  61       *
  62       * @since 2.8.0
  63       *
  64       * @param string $locale Current locale.
  65       */
  66      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  67  
  68      // Update last_checked for current to prevent multiple blocking requests if request hangs.
  69      $current->last_checked = time();
  70      set_site_transient( 'update_core', $current );
  71  
  72      if ( method_exists( $wpdb, 'db_version' ) ) {
  73          $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
  74      } else {
  75          $mysql_version = 'N/A';
  76      }
  77  
  78      if ( is_multisite() ) {
  79          $user_count        = get_user_count();
  80          $num_blogs         = get_blog_count();
  81          $wp_install        = network_site_url();
  82          $multisite_enabled = 1;
  83      } else {
  84          $user_count        = count_users();
  85          $user_count        = $user_count['total_users'];
  86          $multisite_enabled = 0;
  87          $num_blogs         = 1;
  88          $wp_install        = home_url( '/' );
  89      }
  90  
  91      $query = array(
  92          'version'            => $wp_version,
  93          'php'                => $php_version,
  94          'locale'             => $locale,
  95          'mysql'              => $mysql_version,
  96          'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
  97          'blogs'              => $num_blogs,
  98          'users'              => $user_count,
  99          'multisite_enabled'  => $multisite_enabled,
 100          'initial_db_version' => get_site_option( 'initial_db_version' ),
 101      );
 102  
 103      /**
 104       * Filter the query arguments sent as part of the core version check.
 105       *
 106       * WARNING: Changing this data may result in your site not receiving security updates.
 107       * Please exercise extreme caution.
 108       *
 109       * @since 4.9.0
 110       *
 111       * @param array $query {
 112       *     Version check query arguments.
 113       *
 114       *     @type string $version            WordPress version number.
 115       *     @type string $php                PHP version number.
 116       *     @type string $locale             The locale to retrieve updates for.
 117       *     @type string $mysql              MySQL version number.
 118       *     @type string $local_package      The value of the $wp_local_package global, when set.
 119       *     @type int    $blogs              Number of sites on this WordPress installation.
 120       *     @type int    $users              Number of users on this WordPress installation.
 121       *     @type int    $multisite_enabled  Whether this WordPress installation uses Multisite.
 122       *     @type int    $initial_db_version Database version of WordPress at time of installation.
 123       * }
 124       */
 125      $query = apply_filters( 'core_version_check_query_args', $query );
 126  
 127      $post_body = array(
 128          'translations' => wp_json_encode( $translations ),
 129      );
 130  
 131      if ( is_array( $extra_stats ) ) {
 132          $post_body = array_merge( $post_body, $extra_stats );
 133      }
 134  
 135      $url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
 136      $http_url = $url;
 137      $ssl      = wp_http_supports( array( 'ssl' ) );
 138      if ( $ssl ) {
 139          $url = set_url_scheme( $url, 'https' );
 140      }
 141  
 142      $doing_cron = wp_doing_cron();
 143  
 144      $options = array(
 145          'timeout'    => $doing_cron ? 30 : 3,
 146          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 147          'headers'    => array(
 148              'wp_install' => $wp_install,
 149              'wp_blog'    => home_url( '/' ),
 150          ),
 151          'body'       => $post_body,
 152      );
 153  
 154      $response = wp_remote_post( $url, $options );
 155      if ( $ssl && is_wp_error( $response ) ) {
 156          trigger_error(
 157              sprintf(
 158                  /* translators: %s: Support forums URL. */
 159                  __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 160                  __( 'https://wordpress.org/support/forums/' )
 161              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 162              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 163          );
 164          $response = wp_remote_post( $http_url, $options );
 165      }
 166  
 167      if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
 168          return;
 169      }
 170  
 171      $body = trim( wp_remote_retrieve_body( $response ) );
 172      $body = json_decode( $body, true );
 173  
 174      if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
 175          return;
 176      }
 177  
 178      $offers = $body['offers'];
 179  
 180      foreach ( $offers as &$offer ) {
 181          foreach ( $offer as $offer_key => $value ) {
 182              if ( 'packages' == $offer_key ) {
 183                  $offer['packages'] = (object) array_intersect_key(
 184                      array_map( 'esc_url', $offer['packages'] ),
 185                      array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
 186                  );
 187              } elseif ( 'download' == $offer_key ) {
 188                  $offer['download'] = esc_url( $value );
 189              } else {
 190                  $offer[ $offer_key ] = esc_html( $value );
 191              }
 192          }
 193          $offer = (object) array_intersect_key(
 194              $offer,
 195              array_fill_keys(
 196                  array(
 197                      'response',
 198                      'download',
 199                      'locale',
 200                      'packages',
 201                      'current',
 202                      'version',
 203                      'php_version',
 204                      'mysql_version',
 205                      'new_bundled',
 206                      'partial_version',
 207                      'notify_email',
 208                      'support_email',
 209                      'new_files',
 210                  ),
 211                  ''
 212              )
 213          );
 214      }
 215  
 216      $updates                  = new stdClass();
 217      $updates->updates         = $offers;
 218      $updates->last_checked    = time();
 219      $updates->version_checked = $wp_version;
 220  
 221      if ( isset( $body['translations'] ) ) {
 222          $updates->translations = $body['translations'];
 223      }
 224  
 225      set_site_transient( 'update_core', $updates );
 226  
 227      if ( ! empty( $body['ttl'] ) ) {
 228          $ttl = (int) $body['ttl'];
 229          if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
 230              // Queue an event to re-run the update check in $ttl seconds.
 231              wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
 232          }
 233      }
 234  
 235      // Trigger background updates if running non-interactively, and we weren't called from the update handler.
 236      if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
 237          /**
 238           * Fires during wp_cron, starting the auto update process.
 239           *
 240           * @since 3.9.0
 241           */
 242          do_action( 'wp_maybe_auto_update' );
 243      }
 244  }
 245  
 246  /**
 247   * Check plugin versions against the latest versions hosted on WordPress.org.
 248   *
 249   * The WordPress version, PHP version, and Locale is sent along with a list of
 250   * all plugins installed. Checks against the WordPress server at
 251   * api.wordpress.org. Will only check if WordPress isn't installing.
 252   *
 253   * @since 2.3.0
 254   * @global string $wp_version The WordPress version string.
 255   *
 256   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 257   */
 258  function wp_update_plugins( $extra_stats = array() ) {
 259      if ( wp_installing() ) {
 260          return;
 261      }
 262  
 263      // Include an unmodified $wp_version.
 264      require  ABSPATH . WPINC . '/version.php';
 265  
 266      // If running blog-side, bail unless we've not checked in the last 12 hours.
 267      if ( ! function_exists( 'get_plugins' ) ) {
 268          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 269      }
 270  
 271      $plugins      = get_plugins();
 272      $translations = wp_get_installed_translations( 'plugins' );
 273  
 274      $active  = get_option( 'active_plugins', array() );
 275      $current = get_site_transient( 'update_plugins' );
 276      if ( ! is_object( $current ) ) {
 277          $current = new stdClass;
 278      }
 279  
 280      $new_option               = new stdClass;
 281      $new_option->last_checked = time();
 282  
 283      $doing_cron = wp_doing_cron();
 284  
 285      // Check for update on a different schedule, depending on the page.
 286      switch ( current_filter() ) {
 287          case 'upgrader_process_complete':
 288              $timeout = 0;
 289              break;
 290          case 'load-update-core.php':
 291              $timeout = MINUTE_IN_SECONDS;
 292              break;
 293          case 'load-plugins.php':
 294          case 'load-update.php':
 295              $timeout = HOUR_IN_SECONDS;
 296              break;
 297          default:
 298              if ( $doing_cron ) {
 299                  $timeout = 2 * HOUR_IN_SECONDS;
 300              } else {
 301                  $timeout = 12 * HOUR_IN_SECONDS;
 302              }
 303      }
 304  
 305      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 306  
 307      if ( $time_not_changed && ! $extra_stats ) {
 308          $plugin_changed = false;
 309          foreach ( $plugins as $file => $p ) {
 310              $new_option->checked[ $file ] = $p['Version'];
 311  
 312              if ( ! isset( $current->checked[ $file ] ) || strval( $current->checked[ $file ] ) !== strval( $p['Version'] ) ) {
 313                  $plugin_changed = true;
 314              }
 315          }
 316  
 317          if ( isset( $current->response ) && is_array( $current->response ) ) {
 318              foreach ( $current->response as $plugin_file => $update_details ) {
 319                  if ( ! isset( $plugins[ $plugin_file ] ) ) {
 320                      $plugin_changed = true;
 321                      break;
 322                  }
 323              }
 324          }
 325  
 326          // Bail if we've checked recently and if nothing has changed.
 327          if ( ! $plugin_changed ) {
 328              return;
 329          }
 330      }
 331  
 332      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 333      $current->last_checked = time();
 334      set_site_transient( 'update_plugins', $current );
 335  
 336      $to_send = compact( 'plugins', 'active' );
 337  
 338      $locales = array_values( get_available_languages() );
 339  
 340      /**
 341       * Filters the locales requested for plugin translations.
 342       *
 343       * @since 3.7.0
 344       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 345       *
 346       * @param array $locales Plugin locales. Default is all available locales of the site.
 347       */
 348      $locales = apply_filters( 'plugins_update_check_locales', $locales );
 349      $locales = array_unique( $locales );
 350  
 351      if ( $doing_cron ) {
 352          $timeout = 30;
 353      } else {
 354          // Three seconds, plus one extra second for every 10 plugins.
 355          $timeout = 3 + (int) ( count( $plugins ) / 10 );
 356      }
 357  
 358      $options = array(
 359          'timeout'    => $timeout,
 360          'body'       => array(
 361              'plugins'      => wp_json_encode( $to_send ),
 362              'translations' => wp_json_encode( $translations ),
 363              'locale'       => wp_json_encode( $locales ),
 364              'all'          => wp_json_encode( true ),
 365          ),
 366          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 367      );
 368  
 369      if ( $extra_stats ) {
 370          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 371      }
 372  
 373      $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
 374      $http_url = $url;
 375      $ssl      = wp_http_supports( array( 'ssl' ) );
 376      if ( $ssl ) {
 377          $url = set_url_scheme( $url, 'https' );
 378      }
 379  
 380      $raw_response = wp_remote_post( $url, $options );
 381      if ( $ssl && is_wp_error( $raw_response ) ) {
 382          trigger_error(
 383              sprintf(
 384                  /* translators: %s: Support forums URL. */
 385                  __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 386                  __( 'https://wordpress.org/support/forums/' )
 387              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 388              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 389          );
 390          $raw_response = wp_remote_post( $http_url, $options );
 391      }
 392  
 393      if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
 394          return;
 395      }
 396  
 397      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 398      foreach ( $response['plugins'] as &$plugin ) {
 399          $plugin = (object) $plugin;
 400          if ( isset( $plugin->compatibility ) ) {
 401              $plugin->compatibility = (object) $plugin->compatibility;
 402              foreach ( $plugin->compatibility as &$data ) {
 403                  $data = (object) $data;
 404              }
 405          }
 406      }
 407      unset( $plugin, $data );
 408      foreach ( $response['no_update'] as &$plugin ) {
 409          $plugin = (object) $plugin;
 410      }
 411      unset( $plugin );
 412  
 413      if ( is_array( $response ) ) {
 414          $new_option->response     = $response['plugins'];
 415          $new_option->translations = $response['translations'];
 416          // TODO: Perhaps better to store no_update in a separate transient with an expiry?
 417          $new_option->no_update = $response['no_update'];
 418      } else {
 419          $new_option->response     = array();
 420          $new_option->translations = array();
 421          $new_option->no_update    = array();
 422      }
 423  
 424      set_site_transient( 'update_plugins', $new_option );
 425  }
 426  
 427  /**
 428   * Check theme versions against the latest versions hosted on WordPress.org.
 429   *
 430   * A list of all themes installed in sent to WP. Checks against the
 431   * WordPress server at api.wordpress.org. Will only check if WordPress isn't
 432   * installing.
 433   *
 434   * @since 2.7.0
 435   * @global string $wp_version The WordPress version string.
 436   *
 437   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 438   */
 439  function wp_update_themes( $extra_stats = array() ) {
 440      if ( wp_installing() ) {
 441          return;
 442      }
 443  
 444      // Include an unmodified $wp_version.
 445      require  ABSPATH . WPINC . '/version.php';
 446  
 447      $installed_themes = wp_get_themes();
 448      $translations     = wp_get_installed_translations( 'themes' );
 449  
 450      $last_update = get_site_transient( 'update_themes' );
 451      if ( ! is_object( $last_update ) ) {
 452          $last_update = new stdClass;
 453      }
 454  
 455      $themes  = array();
 456      $checked = array();
 457      $request = array();
 458  
 459      // Put slug of current theme into request.
 460      $request['active'] = get_option( 'stylesheet' );
 461  
 462      foreach ( $installed_themes as $theme ) {
 463          $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
 464  
 465          $themes[ $theme->get_stylesheet() ] = array(
 466              'Name'       => $theme->get( 'Name' ),
 467              'Title'      => $theme->get( 'Name' ),
 468              'Version'    => $theme->get( 'Version' ),
 469              'Author'     => $theme->get( 'Author' ),
 470              'Author URI' => $theme->get( 'AuthorURI' ),
 471              'Template'   => $theme->get_template(),
 472              'Stylesheet' => $theme->get_stylesheet(),
 473          );
 474      }
 475  
 476      $doing_cron = wp_doing_cron();
 477  
 478      // Check for update on a different schedule, depending on the page.
 479      switch ( current_filter() ) {
 480          case 'upgrader_process_complete':
 481              $timeout = 0;
 482              break;
 483          case 'load-update-core.php':
 484              $timeout = MINUTE_IN_SECONDS;
 485              break;
 486          case 'load-themes.php':
 487          case 'load-update.php':
 488              $timeout = HOUR_IN_SECONDS;
 489              break;
 490          default:
 491              if ( $doing_cron ) {
 492                  $timeout = 2 * HOUR_IN_SECONDS;
 493              } else {
 494                  $timeout = 12 * HOUR_IN_SECONDS;
 495              }
 496      }
 497  
 498      $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
 499  
 500      if ( $time_not_changed && ! $extra_stats ) {
 501          $theme_changed = false;
 502          foreach ( $checked as $slug => $v ) {
 503              if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
 504                  $theme_changed = true;
 505              }
 506          }
 507  
 508          if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
 509              foreach ( $last_update->response as $slug => $update_details ) {
 510                  if ( ! isset( $checked[ $slug ] ) ) {
 511                      $theme_changed = true;
 512                      break;
 513                  }
 514              }
 515          }
 516  
 517          // Bail if we've checked recently and if nothing has changed.
 518          if ( ! $theme_changed ) {
 519              return;
 520          }
 521      }
 522  
 523      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 524      $last_update->last_checked = time();
 525      set_site_transient( 'update_themes', $last_update );
 526  
 527      $request['themes'] = $themes;
 528  
 529      $locales = array_values( get_available_languages() );
 530  
 531      /**
 532       * Filters the locales requested for theme translations.
 533       *
 534       * @since 3.7.0
 535       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 536       *
 537       * @param array $locales Theme locales. Default is all available locales of the site.
 538       */
 539      $locales = apply_filters( 'themes_update_check_locales', $locales );
 540      $locales = array_unique( $locales );
 541  
 542      if ( $doing_cron ) {
 543          $timeout = 30;
 544      } else {
 545          // Three seconds, plus one extra second for every 10 themes.
 546          $timeout = 3 + (int) ( count( $themes ) / 10 );
 547      }
 548  
 549      $options = array(
 550          'timeout'    => $timeout,
 551          'body'       => array(
 552              'themes'       => wp_json_encode( $request ),
 553              'translations' => wp_json_encode( $translations ),
 554              'locale'       => wp_json_encode( $locales ),
 555          ),
 556          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 557      );
 558  
 559      if ( $extra_stats ) {
 560          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 561      }
 562  
 563      $url      = 'http://api.wordpress.org/themes/update-check/1.1/';
 564      $http_url = $url;
 565      $ssl      = wp_http_supports( array( 'ssl' ) );
 566      if ( $ssl ) {
 567          $url = set_url_scheme( $url, 'https' );
 568      }
 569  
 570      $raw_response = wp_remote_post( $url, $options );
 571      if ( $ssl && is_wp_error( $raw_response ) ) {
 572          trigger_error(
 573              sprintf(
 574                  /* translators: %s: Support forums URL. */
 575                  __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 576                  __( 'https://wordpress.org/support/forums/' )
 577              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 578              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 579          );
 580          $raw_response = wp_remote_post( $http_url, $options );
 581      }
 582  
 583      if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
 584          return;
 585      }
 586  
 587      $new_update               = new stdClass;
 588      $new_update->last_checked = time();
 589      $new_update->checked      = $checked;
 590  
 591      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 592  
 593      if ( is_array( $response ) ) {
 594          $new_update->response     = $response['themes'];
 595          $new_update->translations = $response['translations'];
 596      }
 597  
 598      set_site_transient( 'update_themes', $new_update );
 599  }
 600  
 601  /**
 602   * Performs WordPress automatic background updates.
 603   *
 604   * @since 3.7.0
 605   */
 606  function wp_maybe_auto_update() {
 607      include_once  ABSPATH . 'wp-admin/includes/admin.php';
 608      require_once  ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 609  
 610      $upgrader = new WP_Automatic_Updater;
 611      $upgrader->run();
 612  }
 613  
 614  /**
 615   * Retrieves a list of all language updates available.
 616   *
 617   * @since 3.7.0
 618   *
 619   * @return object[] Array of translation objects that have available updates.
 620   */
 621  function wp_get_translation_updates() {
 622      $updates    = array();
 623      $transients = array(
 624          'update_core'    => 'core',
 625          'update_plugins' => 'plugin',
 626          'update_themes'  => 'theme',
 627      );
 628      foreach ( $transients as $transient => $type ) {
 629          $transient = get_site_transient( $transient );
 630          if ( empty( $transient->translations ) ) {
 631              continue;
 632          }
 633  
 634          foreach ( $transient->translations as $translation ) {
 635              $updates[] = (object) $translation;
 636          }
 637      }
 638      return $updates;
 639  }
 640  
 641  /**
 642   * Collect counts and UI strings for available updates
 643   *
 644   * @since 3.3.0
 645   *
 646   * @return array
 647   */
 648  function wp_get_update_data() {
 649      $counts = array(
 650          'plugins'      => 0,
 651          'themes'       => 0,
 652          'wordpress'    => 0,
 653          'translations' => 0,
 654      );
 655  
 656      $plugins = current_user_can( 'update_plugins' );
 657      if ( $plugins ) {
 658          $update_plugins = get_site_transient( 'update_plugins' );
 659          if ( ! empty( $update_plugins->response ) ) {
 660              $counts['plugins'] = count( $update_plugins->response );
 661          }
 662      }
 663  
 664      $themes = current_user_can( 'update_themes' );
 665      if ( $themes ) {
 666          $update_themes = get_site_transient( 'update_themes' );
 667          if ( ! empty( $update_themes->response ) ) {
 668              $counts['themes'] = count( $update_themes->response );
 669          }
 670      }
 671  
 672      $core = current_user_can( 'update_core' );
 673      if ( $core && function_exists( 'get_core_updates' ) ) {
 674          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 675          if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ) ) && current_user_can( 'update_core' ) ) {
 676              $counts['wordpress'] = 1;
 677          }
 678      }
 679  
 680      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 681          $counts['translations'] = 1;
 682      }
 683  
 684      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 685      $titles          = array();
 686      if ( $counts['wordpress'] ) {
 687          /* translators: %d: Number of available WordPress updates. */
 688          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 689      }
 690      if ( $counts['plugins'] ) {
 691          /* translators: %d: Number of available plugin updates. */
 692          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 693      }
 694      if ( $counts['themes'] ) {
 695          /* translators: %d: Number of available theme updates. */
 696          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 697      }
 698      if ( $counts['translations'] ) {
 699          $titles['translations'] = __( 'Translation Updates' );
 700      }
 701  
 702      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 703  
 704      $update_data = array(
 705          'counts' => $counts,
 706          'title'  => $update_title,
 707      );
 708      /**
 709       * Filters the returned array of update data for plugins, themes, and WordPress core.
 710       *
 711       * @since 3.5.0
 712       *
 713       * @param array $update_data {
 714       *     Fetched update data.
 715       *
 716       *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
 717       *     @type string  $update_title Titles of available updates.
 718       * }
 719       * @param array $titles An array of update counts and UI strings for available updates.
 720       */
 721      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 722  }
 723  
 724  /**
 725   * Determines whether core should be updated.
 726   *
 727   * @since 2.8.0
 728   *
 729   * @global string $wp_version The WordPress version string.
 730   */
 731  function _maybe_update_core() {
 732      // Include an unmodified $wp_version.
 733      require  ABSPATH . WPINC . '/version.php';
 734  
 735      $current = get_site_transient( 'update_core' );
 736  
 737      if ( isset( $current->last_checked, $current->version_checked ) &&
 738          12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
 739          $current->version_checked == $wp_version ) {
 740          return;
 741      }
 742      wp_version_check();
 743  }
 744  /**
 745   * Check the last time plugins were run before checking plugin versions.
 746   *
 747   * This might have been backported to WordPress 2.6.1 for performance reasons.
 748   * This is used for the wp-admin to check only so often instead of every page
 749   * load.
 750   *
 751   * @since 2.7.0
 752   * @access private
 753   */
 754  function _maybe_update_plugins() {
 755      $current = get_site_transient( 'update_plugins' );
 756      if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
 757          return;
 758      }
 759      wp_update_plugins();
 760  }
 761  
 762  /**
 763   * Check themes versions only after a duration of time.
 764   *
 765   * This is for performance reasons to make sure that on the theme version
 766   * checker is not run on every page load.
 767   *
 768   * @since 2.7.0
 769   * @access private
 770   */
 771  function _maybe_update_themes() {
 772      $current = get_site_transient( 'update_themes' );
 773      if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
 774          return;
 775      }
 776      wp_update_themes();
 777  }
 778  
 779  /**
 780   * Schedule core, theme, and plugin update checks.
 781   *
 782   * @since 3.1.0
 783   */
 784  function wp_schedule_update_checks() {
 785      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
 786          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
 787      }
 788  
 789      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
 790          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
 791      }
 792  
 793      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
 794          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
 795      }
 796  }
 797  
 798  /**
 799   * Clear existing update caches for plugins, themes, and core.
 800   *
 801   * @since 4.1.0
 802   */
 803  function wp_clean_update_cache() {
 804      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
 805          wp_clean_plugins_cache();
 806      } else {
 807          delete_site_transient( 'update_plugins' );
 808      }
 809      wp_clean_themes_cache();
 810      delete_site_transient( 'update_core' );
 811  }
 812  
 813  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
 814      return;
 815  }
 816  
 817  add_action( 'admin_init', '_maybe_update_core' );
 818  add_action( 'wp_version_check', 'wp_version_check' );
 819  
 820  add_action( 'load-plugins.php', 'wp_update_plugins' );
 821  add_action( 'load-update.php', 'wp_update_plugins' );
 822  add_action( 'load-update-core.php', 'wp_update_plugins' );
 823  add_action( 'admin_init', '_maybe_update_plugins' );
 824  add_action( 'wp_update_plugins', 'wp_update_plugins' );
 825  
 826  add_action( 'load-themes.php', 'wp_update_themes' );
 827  add_action( 'load-update.php', 'wp_update_themes' );
 828  add_action( 'load-update-core.php', 'wp_update_themes' );
 829  add_action( 'admin_init', '_maybe_update_themes' );
 830  add_action( 'wp_update_themes', 'wp_update_themes' );
 831  
 832  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
 833  
 834  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
 835  
 836  add_action( 'init', 'wp_schedule_update_checks' );


Generated : Wed Apr 1 08:20:01 2020 Cross-referenced by PHPXref