[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


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