[ Index ]

PHP Cross Reference of WordPress

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
  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://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 Used to notify the WordPress version.
 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      include ( 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   *
 436   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 437   */
 438  function wp_update_themes( $extra_stats = array() ) {
 439      if ( wp_installing() ) {
 440          return;
 441      }
 442  
 443      // include an unmodified $wp_version
 444      include ( ABSPATH . WPINC . '/version.php' );
 445  
 446      $installed_themes = wp_get_themes();
 447      $translations     = wp_get_installed_translations( 'themes' );
 448  
 449      $last_update = get_site_transient( 'update_themes' );
 450      if ( ! is_object( $last_update ) ) {
 451          $last_update = new stdClass;
 452      }
 453  
 454      $themes  = array();
 455      $checked = array();
 456      $request = array();
 457  
 458      // Put slug of current theme into request.
 459      $request['active'] = get_option( 'stylesheet' );
 460  
 461      foreach ( $installed_themes as $theme ) {
 462          $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
 463  
 464          $themes[ $theme->get_stylesheet() ] = array(
 465              'Name'       => $theme->get( 'Name' ),
 466              'Title'      => $theme->get( 'Name' ),
 467              'Version'    => $theme->get( 'Version' ),
 468              'Author'     => $theme->get( 'Author' ),
 469              'Author URI' => $theme->get( 'AuthorURI' ),
 470              'Template'   => $theme->get_template(),
 471              'Stylesheet' => $theme->get_stylesheet(),
 472          );
 473      }
 474  
 475      $doing_cron = wp_doing_cron();
 476  
 477      // Check for update on a different schedule, depending on the page.
 478      switch ( current_filter() ) {
 479          case 'upgrader_process_complete':
 480              $timeout = 0;
 481              break;
 482          case 'load-update-core.php':
 483              $timeout = MINUTE_IN_SECONDS;
 484              break;
 485          case 'load-themes.php':
 486          case 'load-update.php':
 487              $timeout = HOUR_IN_SECONDS;
 488              break;
 489          default:
 490              if ( $doing_cron ) {
 491                  $timeout = 2 * HOUR_IN_SECONDS;
 492              } else {
 493                  $timeout = 12 * HOUR_IN_SECONDS;
 494              }
 495      }
 496  
 497      $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
 498  
 499      if ( $time_not_changed && ! $extra_stats ) {
 500          $theme_changed = false;
 501          foreach ( $checked as $slug => $v ) {
 502              if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
 503                  $theme_changed = true;
 504              }
 505          }
 506  
 507          if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
 508              foreach ( $last_update->response as $slug => $update_details ) {
 509                  if ( ! isset( $checked[ $slug ] ) ) {
 510                      $theme_changed = true;
 511                      break;
 512                  }
 513              }
 514          }
 515  
 516          // Bail if we've checked recently and if nothing has changed
 517          if ( ! $theme_changed ) {
 518              return;
 519          }
 520      }
 521  
 522      // Update last_checked for current to prevent multiple blocking requests if request hangs
 523      $last_update->last_checked = time();
 524      set_site_transient( 'update_themes', $last_update );
 525  
 526      $request['themes'] = $themes;
 527  
 528      $locales = array_values( get_available_languages() );
 529  
 530      /**
 531       * Filters the locales requested for theme translations.
 532       *
 533       * @since 3.7.0
 534       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 535       *
 536       * @param array $locales Theme locales. Default is all available locales of the site.
 537       */
 538      $locales = apply_filters( 'themes_update_check_locales', $locales );
 539      $locales = array_unique( $locales );
 540  
 541      if ( $doing_cron ) {
 542          $timeout = 30;
 543      } else {
 544          // Three seconds, plus one extra second for every 10 themes
 545          $timeout = 3 + (int) ( count( $themes ) / 10 );
 546      }
 547  
 548      $options = array(
 549          'timeout'    => $timeout,
 550          'body'       => array(
 551              'themes'       => wp_json_encode( $request ),
 552              'translations' => wp_json_encode( $translations ),
 553              'locale'       => wp_json_encode( $locales ),
 554          ),
 555          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 556      );
 557  
 558      if ( $extra_stats ) {
 559          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 560      }
 561  
 562      $url      = 'http://api.wordpress.org/themes/update-check/1.1/';
 563      $http_url = $url;
 564      $ssl      = wp_http_supports( array( 'ssl' ) );
 565      if ( $ssl ) {
 566          $url = set_url_scheme( $url, 'https' );
 567      }
 568  
 569      $raw_response = wp_remote_post( $url, $options );
 570      if ( $ssl && is_wp_error( $raw_response ) ) {
 571          trigger_error(
 572              sprintf(
 573                  /* translators: %s: Support forums URL. */
 574                  __( '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>.' ),
 575                  __( 'https://wordpress.org/support/forums/' )
 576              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 577              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 578          );
 579          $raw_response = wp_remote_post( $http_url, $options );
 580      }
 581  
 582      if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) {
 583          return;
 584      }
 585  
 586      $new_update               = new stdClass;
 587      $new_update->last_checked = time();
 588      $new_update->checked      = $checked;
 589  
 590      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 591  
 592      if ( is_array( $response ) ) {
 593          $new_update->response     = $response['themes'];
 594          $new_update->translations = $response['translations'];
 595      }
 596  
 597      set_site_transient( 'update_themes', $new_update );
 598  }
 599  
 600  /**
 601   * Performs WordPress automatic background updates.
 602   *
 603   * @since 3.7.0
 604   */
 605  function wp_maybe_auto_update() {
 606      include_once ( ABSPATH . 'wp-admin/includes/admin.php' );
 607      include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
 608  
 609      $upgrader = new WP_Automatic_Updater;
 610      $upgrader->run();
 611  }
 612  
 613  /**
 614   * Retrieves a list of all language updates available.
 615   *
 616   * @since 3.7.0
 617   *
 618   * @return object[] Array of translation objects that have available updates.
 619   */
 620  function wp_get_translation_updates() {
 621      $updates    = array();
 622      $transients = array(
 623          'update_core'    => 'core',
 624          'update_plugins' => 'plugin',
 625          'update_themes'  => 'theme',
 626      );
 627      foreach ( $transients as $transient => $type ) {
 628          $transient = get_site_transient( $transient );
 629          if ( empty( $transient->translations ) ) {
 630              continue;
 631          }
 632  
 633          foreach ( $transient->translations as $translation ) {
 634              $updates[] = (object) $translation;
 635          }
 636      }
 637      return $updates;
 638  }
 639  
 640  /**
 641   * Collect counts and UI strings for available updates
 642   *
 643   * @since 3.3.0
 644   *
 645   * @return array
 646   */
 647  function wp_get_update_data() {
 648      $counts = array(
 649          'plugins'      => 0,
 650          'themes'       => 0,
 651          'wordpress'    => 0,
 652          'translations' => 0,
 653      );
 654  
 655      $plugins = current_user_can( 'update_plugins' );
 656      if ( $plugins ) {
 657          $update_plugins = get_site_transient( 'update_plugins' );
 658          if ( ! empty( $update_plugins->response ) ) {
 659              $counts['plugins'] = count( $update_plugins->response );
 660          }
 661      }
 662  
 663      $themes = current_user_can( 'update_themes' );
 664      if ( $themes ) {
 665          $update_themes = get_site_transient( 'update_themes' );
 666          if ( ! empty( $update_themes->response ) ) {
 667              $counts['themes'] = count( $update_themes->response );
 668          }
 669      }
 670  
 671      $core = current_user_can( 'update_core' );
 672      if ( $core && function_exists( 'get_core_updates' ) ) {
 673          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 674          if ( ! empty( $update_wordpress ) && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ) ) && current_user_can( 'update_core' ) ) {
 675              $counts['wordpress'] = 1;
 676          }
 677      }
 678  
 679      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 680          $counts['translations'] = 1;
 681      }
 682  
 683      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 684      $titles          = array();
 685      if ( $counts['wordpress'] ) {
 686          /* translators: %d: Number of available WordPress updates. */
 687          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 688      }
 689      if ( $counts['plugins'] ) {
 690          /* translators: %d: Number of available plugin updates. */
 691          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 692      }
 693      if ( $counts['themes'] ) {
 694          /* translators: %d: Number of available theme updates. */
 695          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 696      }
 697      if ( $counts['translations'] ) {
 698          $titles['translations'] = __( 'Translation Updates' );
 699      }
 700  
 701      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 702  
 703      $update_data = array(
 704          'counts' => $counts,
 705          'title'  => $update_title,
 706      );
 707      /**
 708       * Filters the returned array of update data for plugins, themes, and WordPress core.
 709       *
 710       * @since 3.5.0
 711       *
 712       * @param array $update_data {
 713       *     Fetched update data.
 714       *
 715       *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
 716       *     @type string  $update_title Titles of available updates.
 717       * }
 718       * @param array $titles An array of update counts and UI strings for available updates.
 719       */
 720      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 721  }
 722  
 723  /**
 724   * Determines whether core should be updated.
 725   *
 726   * @since 2.8.0
 727   *
 728   * @global string $wp_version
 729   */
 730  function _maybe_update_core() {
 731      // include an unmodified $wp_version
 732      include ( ABSPATH . WPINC . '/version.php' );
 733  
 734      $current = get_site_transient( 'update_core' );
 735  
 736      if ( isset( $current->last_checked, $current->version_checked ) &&
 737          12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
 738          $current->version_checked == $wp_version ) {
 739          return;
 740      }
 741      wp_version_check();
 742  }
 743  /**
 744   * Check the last time plugins were run before checking plugin versions.
 745   *
 746   * This might have been backported to WordPress 2.6.1 for performance reasons.
 747   * This is used for the wp-admin to check only so often instead of every page
 748   * load.
 749   *
 750   * @since 2.7.0
 751   * @access private
 752   */
 753  function _maybe_update_plugins() {
 754      $current = get_site_transient( 'update_plugins' );
 755      if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
 756          return;
 757      }
 758      wp_update_plugins();
 759  }
 760  
 761  /**
 762   * Check themes versions only after a duration of time.
 763   *
 764   * This is for performance reasons to make sure that on the theme version
 765   * checker is not run on every page load.
 766   *
 767   * @since 2.7.0
 768   * @access private
 769   */
 770  function _maybe_update_themes() {
 771      $current = get_site_transient( 'update_themes' );
 772      if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
 773          return;
 774      }
 775      wp_update_themes();
 776  }
 777  
 778  /**
 779   * Schedule core, theme, and plugin update checks.
 780   *
 781   * @since 3.1.0
 782   */
 783  function wp_schedule_update_checks() {
 784      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
 785          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
 786      }
 787  
 788      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
 789          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
 790      }
 791  
 792      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
 793          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
 794      }
 795  }
 796  
 797  /**
 798   * Clear existing update caches for plugins, themes, and core.
 799   *
 800   * @since 4.1.0
 801   */
 802  function wp_clean_update_cache() {
 803      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
 804          wp_clean_plugins_cache();
 805      } else {
 806          delete_site_transient( 'update_plugins' );
 807      }
 808      wp_clean_themes_cache();
 809      delete_site_transient( 'update_core' );
 810  }
 811  
 812  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
 813      return;
 814  }
 815  
 816  add_action( 'admin_init', '_maybe_update_core' );
 817  add_action( 'wp_version_check', 'wp_version_check' );
 818  
 819  add_action( 'load-plugins.php', 'wp_update_plugins' );
 820  add_action( 'load-update.php', 'wp_update_plugins' );
 821  add_action( 'load-update-core.php', 'wp_update_plugins' );
 822  add_action( 'admin_init', '_maybe_update_plugins' );
 823  add_action( 'wp_update_plugins', 'wp_update_plugins' );
 824  
 825  add_action( 'load-themes.php', 'wp_update_themes' );
 826  add_action( 'load-update.php', 'wp_update_themes' );
 827  add_action( 'load-update-core.php', 'wp_update_themes' );
 828  add_action( 'admin_init', '_maybe_update_themes' );
 829  add_action( 'wp_update_themes', 'wp_update_themes' );
 830  
 831  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
 832  
 833  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
 834  
 835  add_action( 'init', 'wp_schedule_update_checks' );


Generated: Mon Nov 18 01:00:04 2019 Cross-referenced by PHPXref 0.7.1