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


Generated: Tue Oct 27 01:00:08 2020 Cross-referenced by PHPXref 0.7.1