[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * A simple set of functions to check the WordPress.org Version Update service.
   4   *
   5   * @package WordPress
   6   * @since 2.3.0
   7   */
   8  
   9  // Don't load directly.
  10  if ( ! defined( 'ABSPATH' ) ) {
  11      die( '-1' );
  12  }
  13  
  14  /**
  15   * Checks WordPress version against the newest version.
  16   *
  17   * The WordPress version, PHP version, and locale is sent to api.wordpress.org.
  18   *
  19   * Checks against the WordPress server at api.wordpress.org. Will only check
  20   * if WordPress isn't installing.
  21   *
  22   * @since 2.3.0
  23   *
  24   * @global wpdb   $wpdb             WordPress database abstraction object.
  25   * @global string $wp_local_package Locale code of the package.
  26   *
  27   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  28   * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check.
  29   *                           Defaults to false, true if $extra_stats is set.
  30   */
  31  function wp_version_check( $extra_stats = array(), $force_check = false ) {
  32      global $wpdb, $wp_local_package;
  33  
  34      if ( wp_installing() ) {
  35          return;
  36      }
  37  
  38      $php_version = PHP_VERSION;
  39  
  40      $current      = get_site_transient( 'update_core' );
  41      $translations = wp_get_installed_translations( 'core' );
  42  
  43      // Invalidate the transient when $wp_version changes.
  44      if ( is_object( $current ) && wp_get_wp_version() !== $current->version_checked ) {
  45          $current = false;
  46      }
  47  
  48      if ( ! is_object( $current ) ) {
  49          $current                  = new stdClass();
  50          $current->updates         = array();
  51          $current->version_checked = wp_get_wp_version();
  52      }
  53  
  54      if ( ! empty( $extra_stats ) ) {
  55          $force_check = true;
  56      }
  57  
  58      // Wait 1 minute between multiple version check requests.
  59      $timeout          = MINUTE_IN_SECONDS;
  60      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  61  
  62      if ( ! $force_check && $time_not_changed ) {
  63          return;
  64      }
  65  
  66      /**
  67       * Filters the locale requested for WordPress core translations.
  68       *
  69       * @since 2.8.0
  70       *
  71       * @param string $locale Current locale.
  72       */
  73      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  74  
  75      // Update last_checked for current to prevent multiple blocking requests if request hangs.
  76      $current->last_checked = time();
  77      set_site_transient( 'update_core', $current );
  78  
  79      if ( method_exists( $wpdb, 'db_server_info' ) ) {
  80          $mysql_version = $wpdb->db_server_info();
  81      } elseif ( method_exists( $wpdb, 'db_version' ) ) {
  82          $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
  83      } else {
  84          $mysql_version = 'N/A';
  85      }
  86  
  87      if ( is_multisite() ) {
  88          $num_blogs         = get_blog_count();
  89          $wp_install        = network_site_url();
  90          $multisite_enabled = 1;
  91      } else {
  92          $multisite_enabled = 0;
  93          $num_blogs         = 1;
  94          $wp_install        = home_url( '/' );
  95      }
  96  
  97      $extensions = get_loaded_extensions();
  98      sort( $extensions, SORT_STRING | SORT_FLAG_CASE );
  99      $query = array(
 100          'version'            => wp_get_wp_version(),
 101          'php'                => $php_version,
 102          'locale'             => $locale,
 103          'mysql'              => $mysql_version,
 104          'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
 105          'blogs'              => $num_blogs,
 106          'users'              => get_user_count(),
 107          'multisite_enabled'  => $multisite_enabled,
 108          'initial_db_version' => get_site_option( 'initial_db_version' ),
 109          'extensions'         => array_combine( $extensions, array_map( 'phpversion', $extensions ) ),
 110          'platform_flags'     => array(
 111              'os'   => PHP_OS,
 112              'bits' => PHP_INT_SIZE === 4 ? 32 : 64,
 113          ),
 114          'image_support'      => array(),
 115      );
 116  
 117      if ( function_exists( 'gd_info' ) ) {
 118          $gd_info = gd_info();
 119          // Filter to supported values.
 120          $gd_info = array_filter( $gd_info );
 121  
 122          // Add data for GD WebP, AVIF, HEIC and JPEG XL support.
 123          $query['image_support']['gd'] = array_keys(
 124              array_filter(
 125                  array(
 126                      'webp' => isset( $gd_info['WebP Support'] ),
 127                      'avif' => isset( $gd_info['AVIF Support'] ),
 128                      'heic' => isset( $gd_info['HEIC Support'] ),
 129                      'jxl'  => isset( $gd_info['JXL Support'] ),
 130                  )
 131              )
 132          );
 133      }
 134  
 135      if ( class_exists( 'Imagick' ) ) {
 136          // Add data for Imagick WebP, AVIF, HEIC and JPEG XL support.
 137          $query['image_support']['imagick'] = array_keys(
 138              array_filter(
 139                  array(
 140                      'webp' => ! empty( Imagick::queryFormats( 'WEBP' ) ),
 141                      'avif' => ! empty( Imagick::queryFormats( 'AVIF' ) ),
 142                      'heic' => ! empty( Imagick::queryFormats( 'HEIC' ) ),
 143                      'jxl'  => ! empty( Imagick::queryFormats( 'JXL' ) ),
 144                  )
 145              )
 146          );
 147      }
 148  
 149      /**
 150       * Filters the query arguments sent as part of the core version check.
 151       *
 152       * WARNING: Changing this data may result in your site not receiving security updates.
 153       * Please exercise extreme caution.
 154       *
 155       * @since 4.9.0
 156       * @since 6.1.0 Added `$extensions`, `$platform_flags`, and `$image_support` to the `$query` parameter.
 157       *
 158       * @param array $query {
 159       *     Version check query arguments.
 160       *
 161       *     @type string $version            WordPress version number.
 162       *     @type string $php                PHP version number.
 163       *     @type string $locale             The locale to retrieve updates for.
 164       *     @type string $mysql              MySQL version number.
 165       *     @type string $local_package      The value of the $wp_local_package global, when set.
 166       *     @type int    $blogs              Number of sites on this WordPress installation.
 167       *     @type int    $users              Number of users on this WordPress installation.
 168       *     @type int    $multisite_enabled  Whether this WordPress installation uses Multisite.
 169       *     @type int    $initial_db_version Database version of WordPress at time of installation.
 170       *     @type array  $extensions         List of PHP extensions and their versions.
 171       *     @type array  $platform_flags     List containing the operating system name and bit support.
 172       *     @type array  $image_support      List of image formats supported by GD and Imagick.
 173       * }
 174       */
 175      $query = apply_filters( 'core_version_check_query_args', $query );
 176  
 177      $post_body = array(
 178          'translations' => wp_json_encode( $translations ),
 179      );
 180  
 181      if ( is_array( $extra_stats ) ) {
 182          $post_body = array_merge( $post_body, $extra_stats );
 183      }
 184  
 185      // Allow for WP_AUTO_UPDATE_CORE to specify beta/RC/development releases.
 186      if ( defined( 'WP_AUTO_UPDATE_CORE' )
 187          && in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true )
 188      ) {
 189          $query['channel'] = WP_AUTO_UPDATE_CORE;
 190      }
 191  
 192      $url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, '', '&' );
 193      $http_url = $url;
 194      $ssl      = wp_http_supports( array( 'ssl' ) );
 195  
 196      if ( $ssl ) {
 197          $url = set_url_scheme( $url, 'https' );
 198      }
 199  
 200      $doing_cron = wp_doing_cron();
 201  
 202      $options = array(
 203          'timeout'    => $doing_cron ? 30 : 3,
 204          'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 205          'headers'    => array(
 206              'wp_install' => $wp_install,
 207              'wp_blog'    => home_url( '/' ),
 208          ),
 209          'body'       => $post_body,
 210      );
 211  
 212      $response = wp_remote_post( $url, $options );
 213  
 214      if ( $ssl && is_wp_error( $response ) ) {
 215          wp_trigger_error(
 216              __FUNCTION__,
 217              sprintf(
 218                  /* translators: %s: Support forums URL. */
 219                  __( '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>.' ),
 220                  __( 'https://wordpress.org/support/forums/' )
 221              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 222              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 223          );
 224          $response = wp_remote_post( $http_url, $options );
 225      }
 226  
 227      if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
 228          return;
 229      }
 230  
 231      $body = trim( wp_remote_retrieve_body( $response ) );
 232      $body = json_decode( $body, true );
 233  
 234      if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
 235          return;
 236      }
 237  
 238      $offers = $body['offers'];
 239  
 240      foreach ( $offers as &$offer ) {
 241          foreach ( $offer as $offer_key => $value ) {
 242              if ( 'packages' === $offer_key ) {
 243                  $offer['packages'] = (object) array_intersect_key(
 244                      array_map( 'esc_url', $offer['packages'] ),
 245                      array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
 246                  );
 247              } elseif ( 'download' === $offer_key ) {
 248                  $offer['download'] = esc_url( $value );
 249              } else {
 250                  $offer[ $offer_key ] = esc_html( $value );
 251              }
 252          }
 253          $offer = (object) array_intersect_key(
 254              $offer,
 255              array_fill_keys(
 256                  array(
 257                      'response',
 258                      'download',
 259                      'locale',
 260                      'packages',
 261                      'current',
 262                      'version',
 263                      'php_version',
 264                      'mysql_version',
 265                      'new_bundled',
 266                      'partial_version',
 267                      'notify_email',
 268                      'support_email',
 269                      'new_files',
 270                  ),
 271                  ''
 272              )
 273          );
 274      }
 275  
 276      $updates                  = new stdClass();
 277      $updates->updates         = $offers;
 278      $updates->last_checked    = time();
 279      $updates->version_checked = wp_get_wp_version();
 280  
 281      if ( isset( $body['translations'] ) ) {
 282          $updates->translations = $body['translations'];
 283      }
 284  
 285      set_site_transient( 'update_core', $updates );
 286  
 287      if ( ! empty( $body['ttl'] ) ) {
 288          $ttl = (int) $body['ttl'];
 289  
 290          if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
 291              // Queue an event to re-run the update check in $ttl seconds.
 292              wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
 293          }
 294      }
 295  
 296      // Trigger background updates if running non-interactively, and we weren't called from the update handler.
 297      if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
 298          /**
 299           * Fires during wp_cron, starting the auto-update process.
 300           *
 301           * @since 3.9.0
 302           */
 303          do_action( 'wp_maybe_auto_update' );
 304      }
 305  }
 306  
 307  /**
 308   * Checks for available updates to plugins based on the latest versions hosted on WordPress.org.
 309   *
 310   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 311   *
 312   * A list of all plugins installed is sent to api.wordpress.org, along with the site locale.
 313   *
 314   * Checks against the WordPress server at api.wordpress.org. Will only check
 315   * if WordPress isn't installing.
 316   *
 317   * @since 2.3.0
 318   *
 319   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 320   */
 321  function wp_update_plugins( $extra_stats = array() ) {
 322      if ( wp_installing() ) {
 323          return;
 324      }
 325  
 326      // If running blog-side, bail unless we've not checked in the last 12 hours.
 327      if ( ! function_exists( 'get_plugins' ) ) {
 328          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 329      }
 330  
 331      $plugins      = get_plugins();
 332      $translations = wp_get_installed_translations( 'plugins' );
 333  
 334      $active  = get_option( 'active_plugins', array() );
 335      $current = get_site_transient( 'update_plugins' );
 336  
 337      if ( ! is_object( $current ) ) {
 338          $current = new stdClass();
 339      }
 340  
 341      $doing_cron = wp_doing_cron();
 342  
 343      // Check for update on a different schedule, depending on the page.
 344      switch ( current_filter() ) {
 345          case 'upgrader_process_complete':
 346              $timeout = 0;
 347              break;
 348          case 'load-update-core.php':
 349              $timeout = MINUTE_IN_SECONDS;
 350              break;
 351          case 'load-plugins.php':
 352          case 'load-update.php':
 353              $timeout = HOUR_IN_SECONDS;
 354              break;
 355          default:
 356              if ( $doing_cron ) {
 357                  $timeout = 2 * HOUR_IN_SECONDS;
 358              } else {
 359                  $timeout = 12 * HOUR_IN_SECONDS;
 360              }
 361      }
 362  
 363      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 364  
 365      if ( $time_not_changed && ! $extra_stats ) {
 366          $plugin_changed = false;
 367  
 368          foreach ( $plugins as $file => $p ) {
 369              if ( ! isset( $current->checked[ $file ] ) || (string) $current->checked[ $file ] !== (string) $p['Version'] ) {
 370                  $plugin_changed = true;
 371              }
 372          }
 373  
 374          if ( isset( $current->response ) && is_array( $current->response ) ) {
 375              foreach ( $current->response as $plugin_file => $update_details ) {
 376                  if ( ! isset( $plugins[ $plugin_file ] ) ) {
 377                      $plugin_changed = true;
 378                      break;
 379                  }
 380              }
 381          }
 382  
 383          // Bail if we've checked recently and if nothing has changed.
 384          if ( ! $plugin_changed ) {
 385              return;
 386          }
 387      }
 388  
 389      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 390      $current->last_checked = time();
 391      set_site_transient( 'update_plugins', $current );
 392  
 393      $to_send = compact( 'plugins', 'active' );
 394  
 395      $locales = array_values( get_available_languages() );
 396  
 397      /**
 398       * Filters the locales requested for plugin translations.
 399       *
 400       * @since 3.7.0
 401       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 402       *
 403       * @param string[] $locales Plugin locales. Default is all available locales of the site.
 404       */
 405      $locales = apply_filters( 'plugins_update_check_locales', $locales );
 406      $locales = array_unique( $locales );
 407  
 408      if ( $doing_cron ) {
 409          $timeout = 30; // 30 seconds.
 410      } else {
 411          // Three seconds, plus one extra second for every 10 plugins.
 412          $timeout = 3 + (int) ( count( $plugins ) / 10 );
 413      }
 414  
 415      $options = array(
 416          'timeout'    => $timeout,
 417          'body'       => array(
 418              'plugins'      => wp_json_encode( $to_send ),
 419              'translations' => wp_json_encode( $translations ),
 420              'locale'       => wp_json_encode( $locales ),
 421              'all'          => wp_json_encode( true ),
 422          ),
 423          'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 424      );
 425  
 426      if ( $extra_stats ) {
 427          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 428      }
 429  
 430      $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
 431      $http_url = $url;
 432      $ssl      = wp_http_supports( array( 'ssl' ) );
 433  
 434      if ( $ssl ) {
 435          $url = set_url_scheme( $url, 'https' );
 436      }
 437  
 438      $raw_response = wp_remote_post( $url, $options );
 439  
 440      if ( $ssl && is_wp_error( $raw_response ) ) {
 441          wp_trigger_error(
 442              __FUNCTION__,
 443              sprintf(
 444                  /* translators: %s: Support forums URL. */
 445                  __( '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>.' ),
 446                  __( 'https://wordpress.org/support/forums/' )
 447              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 448              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 449          );
 450          $raw_response = wp_remote_post( $http_url, $options );
 451      }
 452  
 453      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 454          return;
 455      }
 456  
 457      $updates               = new stdClass();
 458      $updates->last_checked = time();
 459      $updates->response     = array();
 460      $updates->translations = array();
 461      $updates->no_update    = array();
 462      foreach ( $plugins as $file => $p ) {
 463          $updates->checked[ $file ] = $p['Version'];
 464      }
 465  
 466      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 467  
 468      if ( $response && is_array( $response ) ) {
 469          $updates->response     = $response['plugins'];
 470          $updates->translations = $response['translations'];
 471          $updates->no_update    = $response['no_update'];
 472      }
 473  
 474      // Support updates for any plugins using the `Update URI` header field.
 475      foreach ( $plugins as $plugin_file => $plugin_data ) {
 476          if ( ! $plugin_data['UpdateURI'] || isset( $updates->response[ $plugin_file ] ) ) {
 477              continue;
 478          }
 479  
 480          $hostname = wp_parse_url( sanitize_url( $plugin_data['UpdateURI'] ), PHP_URL_HOST );
 481  
 482          /**
 483           * Filters the update response for a given plugin hostname.
 484           *
 485           * The dynamic portion of the hook name, `$hostname`, refers to the hostname
 486           * of the URI specified in the `Update URI` header field.
 487           *
 488           * @since 5.8.0
 489           *
 490           * @param array|false $update {
 491           *     The plugin update data with the latest details. Default false.
 492           *
 493           *     @type string   $id           Optional. ID of the plugin for update purposes, should be a URI
 494           *                                  specified in the `Update URI` header field.
 495           *     @type string   $slug         Slug of the plugin.
 496           *     @type string   $version      The version of the plugin.
 497           *     @type string   $url          The URL for details of the plugin.
 498           *     @type string   $package      Optional. The update ZIP for the plugin.
 499           *     @type string   $tested       Optional. The version of WordPress the plugin is tested against.
 500           *     @type string   $requires_php Optional. The version of PHP which the plugin requires.
 501           *     @type bool     $autoupdate   Optional. Whether the plugin should automatically update.
 502           *     @type string[] $icons        Optional. Array of plugin icons.
 503           *     @type string[] $banners      Optional. Array of plugin banners.
 504           *     @type string[] $banners_rtl  Optional. Array of plugin RTL banners.
 505           *     @type array    $translations {
 506           *         Optional. List of translation updates for the plugin.
 507           *
 508           *         @type string $language   The language the translation update is for.
 509           *         @type string $version    The version of the plugin this translation is for.
 510           *                                  This is not the version of the language file.
 511           *         @type string $updated    The update timestamp of the translation file.
 512           *                                  Should be a date in the `YYYY-MM-DD HH:MM:SS` format.
 513           *         @type string $package    The ZIP location containing the translation update.
 514           *         @type string $autoupdate Whether the translation should be automatically installed.
 515           *     }
 516           * }
 517           * @param array       $plugin_data      Plugin headers.
 518           * @param string      $plugin_file      Plugin filename.
 519           * @param string[]    $locales          Installed locales to look up translations for.
 520           */
 521          $update = apply_filters( "update_plugins_{$hostname}", false, $plugin_data, $plugin_file, $locales );
 522  
 523          if ( ! $update ) {
 524              continue;
 525          }
 526  
 527          $update = (object) $update;
 528  
 529          // Is it valid? We require at least a version.
 530          if ( ! isset( $update->version ) ) {
 531              continue;
 532          }
 533  
 534          // These should remain constant.
 535          $update->id     = $plugin_data['UpdateURI'];
 536          $update->plugin = $plugin_file;
 537  
 538          // WordPress needs the version field specified as 'new_version'.
 539          if ( ! isset( $update->new_version ) ) {
 540              $update->new_version = $update->version;
 541          }
 542  
 543          // Handle any translation updates.
 544          if ( ! empty( $update->translations ) ) {
 545              foreach ( $update->translations as $translation ) {
 546                  if ( isset( $translation['language'], $translation['package'] ) ) {
 547                      $translation['type'] = 'plugin';
 548                      $translation['slug'] = isset( $update->slug ) ? $update->slug : $update->id;
 549  
 550                      $updates->translations[] = $translation;
 551                  }
 552              }
 553          }
 554  
 555          unset( $updates->no_update[ $plugin_file ], $updates->response[ $plugin_file ] );
 556  
 557          if ( version_compare( $update->new_version, $plugin_data['Version'], '>' ) ) {
 558              $updates->response[ $plugin_file ] = $update;
 559          } else {
 560              $updates->no_update[ $plugin_file ] = $update;
 561          }
 562      }
 563  
 564      $sanitize_plugin_update_payload = static function ( &$item ) {
 565          $item = (object) $item;
 566  
 567          unset( $item->translations, $item->compatibility );
 568  
 569          return $item;
 570      };
 571  
 572      array_walk( $updates->response, $sanitize_plugin_update_payload );
 573      array_walk( $updates->no_update, $sanitize_plugin_update_payload );
 574  
 575      set_site_transient( 'update_plugins', $updates );
 576  }
 577  
 578  /**
 579   * Checks for available updates to themes based on the latest versions hosted on WordPress.org.
 580   *
 581   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 582   *
 583   * A list of all themes installed is sent to api.wordpress.org, along with the site locale.
 584   *
 585   * Checks against the WordPress server at api.wordpress.org. Will only check
 586   * if WordPress isn't installing.
 587   *
 588   * @since 2.7.0
 589   *
 590   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 591   */
 592  function wp_update_themes( $extra_stats = array() ) {
 593      if ( wp_installing() ) {
 594          return;
 595      }
 596  
 597      $installed_themes = wp_get_themes();
 598      $translations     = wp_get_installed_translations( 'themes' );
 599  
 600      $last_update = get_site_transient( 'update_themes' );
 601  
 602      if ( ! is_object( $last_update ) ) {
 603          $last_update = new stdClass();
 604      }
 605  
 606      $themes  = array();
 607      $checked = array();
 608      $request = array();
 609  
 610      // Put slug of active theme into request.
 611      $request['active'] = get_option( 'stylesheet' );
 612  
 613      foreach ( $installed_themes as $theme ) {
 614          $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
 615  
 616          $themes[ $theme->get_stylesheet() ] = array(
 617              'Name'       => $theme->get( 'Name' ),
 618              'Title'      => $theme->get( 'Name' ),
 619              'Version'    => $theme->get( 'Version' ),
 620              'Author'     => $theme->get( 'Author' ),
 621              'Author URI' => $theme->get( 'AuthorURI' ),
 622              'UpdateURI'  => $theme->get( 'UpdateURI' ),
 623              'Template'   => $theme->get_template(),
 624              'Stylesheet' => $theme->get_stylesheet(),
 625          );
 626      }
 627  
 628      $doing_cron = wp_doing_cron();
 629  
 630      // Check for update on a different schedule, depending on the page.
 631      switch ( current_filter() ) {
 632          case 'upgrader_process_complete':
 633              $timeout = 0;
 634              break;
 635          case 'load-update-core.php':
 636              $timeout = MINUTE_IN_SECONDS;
 637              break;
 638          case 'load-themes.php':
 639          case 'load-update.php':
 640              $timeout = HOUR_IN_SECONDS;
 641              break;
 642          default:
 643              if ( $doing_cron ) {
 644                  $timeout = 2 * HOUR_IN_SECONDS;
 645              } else {
 646                  $timeout = 12 * HOUR_IN_SECONDS;
 647              }
 648      }
 649  
 650      $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
 651  
 652      if ( $time_not_changed && ! $extra_stats ) {
 653          $theme_changed = false;
 654  
 655          foreach ( $checked as $slug => $v ) {
 656              if ( ! isset( $last_update->checked[ $slug ] ) || (string) $last_update->checked[ $slug ] !== (string) $v ) {
 657                  $theme_changed = true;
 658              }
 659          }
 660  
 661          if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
 662              foreach ( $last_update->response as $slug => $update_details ) {
 663                  if ( ! isset( $checked[ $slug ] ) ) {
 664                      $theme_changed = true;
 665                      break;
 666                  }
 667              }
 668          }
 669  
 670          // Bail if we've checked recently and if nothing has changed.
 671          if ( ! $theme_changed ) {
 672              return;
 673          }
 674      }
 675  
 676      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 677      $last_update->last_checked = time();
 678      set_site_transient( 'update_themes', $last_update );
 679  
 680      $request['themes'] = $themes;
 681  
 682      $locales = array_values( get_available_languages() );
 683  
 684      /**
 685       * Filters the locales requested for theme translations.
 686       *
 687       * @since 3.7.0
 688       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 689       *
 690       * @param string[] $locales Theme locales. Default is all available locales of the site.
 691       */
 692      $locales = apply_filters( 'themes_update_check_locales', $locales );
 693      $locales = array_unique( $locales );
 694  
 695      if ( $doing_cron ) {
 696          $timeout = 30; // 30 seconds.
 697      } else {
 698          // Three seconds, plus one extra second for every 10 themes.
 699          $timeout = 3 + (int) ( count( $themes ) / 10 );
 700      }
 701  
 702      $options = array(
 703          'timeout'    => $timeout,
 704          'body'       => array(
 705              'themes'       => wp_json_encode( $request ),
 706              'translations' => wp_json_encode( $translations ),
 707              'locale'       => wp_json_encode( $locales ),
 708          ),
 709          'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 710      );
 711  
 712      if ( $extra_stats ) {
 713          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 714      }
 715  
 716      $url      = 'http://api.wordpress.org/themes/update-check/1.1/';
 717      $http_url = $url;
 718      $ssl      = wp_http_supports( array( 'ssl' ) );
 719  
 720      if ( $ssl ) {
 721          $url = set_url_scheme( $url, 'https' );
 722      }
 723  
 724      $raw_response = wp_remote_post( $url, $options );
 725  
 726      if ( $ssl && is_wp_error( $raw_response ) ) {
 727          wp_trigger_error(
 728              __FUNCTION__,
 729              sprintf(
 730                  /* translators: %s: Support forums URL. */
 731                  __( '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>.' ),
 732                  __( 'https://wordpress.org/support/forums/' )
 733              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 734              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 735          );
 736          $raw_response = wp_remote_post( $http_url, $options );
 737      }
 738  
 739      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 740          return;
 741      }
 742  
 743      $new_update               = new stdClass();
 744      $new_update->last_checked = time();
 745      $new_update->checked      = $checked;
 746  
 747      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 748  
 749      if ( is_array( $response ) ) {
 750          $new_update->response     = $response['themes'];
 751          $new_update->no_update    = $response['no_update'];
 752          $new_update->translations = $response['translations'];
 753      }
 754  
 755      // Support updates for any themes using the `Update URI` header field.
 756      foreach ( $themes as $theme_stylesheet => $theme_data ) {
 757          if ( ! $theme_data['UpdateURI'] || isset( $new_update->response[ $theme_stylesheet ] ) ) {
 758              continue;
 759          }
 760  
 761          $hostname = wp_parse_url( sanitize_url( $theme_data['UpdateURI'] ), PHP_URL_HOST );
 762  
 763          /**
 764           * Filters the update response for a given theme hostname.
 765           *
 766           * The dynamic portion of the hook name, `$hostname`, refers to the hostname
 767           * of the URI specified in the `Update URI` header field.
 768           *
 769           * @since 6.1.0
 770           *
 771           * @param array|false $update {
 772           *     The theme update data with the latest details. Default false.
 773           *
 774           *     @type string $id           Optional. ID of the theme for update purposes, should be a URI
 775           *                                specified in the `Update URI` header field.
 776           *     @type string $theme        Directory name of the theme.
 777           *     @type string $version      The version of the theme.
 778           *     @type string $url          The URL for details of the theme.
 779           *     @type string $package      Optional. The update ZIP for the theme.
 780           *     @type string $tested       Optional. The version of WordPress the theme is tested against.
 781           *     @type string $requires_php Optional. The version of PHP which the theme requires.
 782           *     @type bool   $autoupdate   Optional. Whether the theme should automatically update.
 783           *     @type array  $translations {
 784           *         Optional. List of translation updates for the theme.
 785           *
 786           *         @type string $language   The language the translation update is for.
 787           *         @type string $version    The version of the theme this translation is for.
 788           *                                  This is not the version of the language file.
 789           *         @type string $updated    The update timestamp of the translation file.
 790           *                                  Should be a date in the `YYYY-MM-DD HH:MM:SS` format.
 791           *         @type string $package    The ZIP location containing the translation update.
 792           *         @type string $autoupdate Whether the translation should be automatically installed.
 793           *     }
 794           * }
 795           * @param array       $theme_data       Theme headers.
 796           * @param string      $theme_stylesheet Theme stylesheet.
 797           * @param string[]    $locales          Installed locales to look up translations for.
 798           */
 799          $update = apply_filters( "update_themes_{$hostname}", false, $theme_data, $theme_stylesheet, $locales );
 800  
 801          if ( ! $update ) {
 802              continue;
 803          }
 804  
 805          $update = (object) $update;
 806  
 807          // Is it valid? We require at least a version.
 808          if ( ! isset( $update->version ) ) {
 809              continue;
 810          }
 811  
 812          // This should remain constant.
 813          $update->id = $theme_data['UpdateURI'];
 814  
 815          // WordPress needs the version field specified as 'new_version'.
 816          if ( ! isset( $update->new_version ) ) {
 817              $update->new_version = $update->version;
 818          }
 819  
 820          // Handle any translation updates.
 821          if ( ! empty( $update->translations ) ) {
 822              foreach ( $update->translations as $translation ) {
 823                  if ( isset( $translation['language'], $translation['package'] ) ) {
 824                      $translation['type'] = 'theme';
 825                      $translation['slug'] = isset( $update->theme ) ? $update->theme : $update->id;
 826  
 827                      $new_update->translations[] = $translation;
 828                  }
 829              }
 830          }
 831  
 832          unset( $new_update->no_update[ $theme_stylesheet ], $new_update->response[ $theme_stylesheet ] );
 833  
 834          if ( version_compare( $update->new_version, $theme_data['Version'], '>' ) ) {
 835              $new_update->response[ $theme_stylesheet ] = (array) $update;
 836          } else {
 837              $new_update->no_update[ $theme_stylesheet ] = (array) $update;
 838          }
 839      }
 840  
 841      set_site_transient( 'update_themes', $new_update );
 842  }
 843  
 844  /**
 845   * Performs WordPress automatic background updates.
 846   *
 847   * Updates WordPress core plus any plugins and themes that have automatic updates enabled.
 848   *
 849   * @since 3.7.0
 850   */
 851  function wp_maybe_auto_update() {
 852      require_once  ABSPATH . 'wp-admin/includes/admin.php';
 853      require_once  ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 854  
 855      $upgrader = new WP_Automatic_Updater();
 856      $upgrader->run();
 857  }
 858  
 859  /**
 860   * Retrieves a list of all language updates available.
 861   *
 862   * @since 3.7.0
 863   *
 864   * @return object[] Array of translation objects that have available updates.
 865   */
 866  function wp_get_translation_updates() {
 867      $updates    = array();
 868      $transients = array(
 869          'update_core'    => 'core',
 870          'update_plugins' => 'plugin',
 871          'update_themes'  => 'theme',
 872      );
 873  
 874      foreach ( $transients as $transient => $type ) {
 875          $transient = get_site_transient( $transient );
 876  
 877          if ( empty( $transient->translations ) ) {
 878              continue;
 879          }
 880  
 881          foreach ( $transient->translations as $translation ) {
 882              $updates[] = (object) $translation;
 883          }
 884      }
 885  
 886      return $updates;
 887  }
 888  
 889  /**
 890   * Collects counts and UI strings for available updates.
 891   *
 892   * @since 3.3.0
 893   *
 894   * @return array {
 895   *     Fetched update data.
 896   *
 897   *     @type int[]   $counts       An array of counts for available plugin, theme, and WordPress updates.
 898   *     @type string  $update_title Titles of available updates.
 899   * }
 900   */
 901  function wp_get_update_data() {
 902      $counts = array(
 903          'plugins'      => 0,
 904          'themes'       => 0,
 905          'wordpress'    => 0,
 906          'translations' => 0,
 907      );
 908  
 909      $plugins = current_user_can( 'update_plugins' );
 910  
 911      if ( $plugins ) {
 912          $update_plugins = get_site_transient( 'update_plugins' );
 913  
 914          if ( ! empty( $update_plugins->response ) ) {
 915              $counts['plugins'] = count( $update_plugins->response );
 916          }
 917      }
 918  
 919      $themes = current_user_can( 'update_themes' );
 920  
 921      if ( $themes ) {
 922          $update_themes = get_site_transient( 'update_themes' );
 923  
 924          if ( ! empty( $update_themes->response ) ) {
 925              $counts['themes'] = count( $update_themes->response );
 926          }
 927      }
 928  
 929      $core = current_user_can( 'update_core' );
 930  
 931      if ( $core && function_exists( 'get_core_updates' ) ) {
 932          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 933  
 934          if ( ! empty( $update_wordpress )
 935              && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
 936              && current_user_can( 'update_core' )
 937          ) {
 938              $counts['wordpress'] = 1;
 939          }
 940      }
 941  
 942      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 943          $counts['translations'] = 1;
 944      }
 945  
 946      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 947      $titles          = array();
 948  
 949      if ( $counts['wordpress'] ) {
 950          /* translators: %d: Number of available WordPress updates. */
 951          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 952      }
 953  
 954      if ( $counts['plugins'] ) {
 955          /* translators: %d: Number of available plugin updates. */
 956          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 957      }
 958  
 959      if ( $counts['themes'] ) {
 960          /* translators: %d: Number of available theme updates. */
 961          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 962      }
 963  
 964      if ( $counts['translations'] ) {
 965          $titles['translations'] = __( 'Translation Updates' );
 966      }
 967  
 968      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 969  
 970      $update_data = array(
 971          'counts' => $counts,
 972          'title'  => $update_title,
 973      );
 974      /**
 975       * Filters the returned array of update data for plugins, themes, and WordPress core.
 976       *
 977       * @since 3.5.0
 978       *
 979       * @param array $update_data {
 980       *     Fetched update data.
 981       *
 982       *     @type int[]   $counts       An array of counts for available plugin, theme, and WordPress updates.
 983       *     @type string  $update_title Titles of available updates.
 984       * }
 985       * @param array $titles An array of update counts and UI strings for available updates.
 986       */
 987      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 988  }
 989  
 990  /**
 991   * Determines whether core should be updated.
 992   *
 993   * @since 2.8.0
 994   */
 995  function _maybe_update_core() {
 996      $current = get_site_transient( 'update_core' );
 997  
 998      if ( isset( $current->last_checked, $current->version_checked )
 999          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
1000          && wp_get_wp_version() === $current->version_checked
1001      ) {
1002          return;
1003      }
1004  
1005      wp_version_check();
1006  }
1007  /**
1008   * Checks the last time plugins were run before checking plugin versions.
1009   *
1010   * This might have been backported to WordPress 2.6.1 for performance reasons.
1011   * This is used for the wp-admin to check only so often instead of every page
1012   * load.
1013   *
1014   * @since 2.7.0
1015   * @access private
1016   */
1017  function _maybe_update_plugins() {
1018      $current = get_site_transient( 'update_plugins' );
1019  
1020      if ( isset( $current->last_checked )
1021          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
1022      ) {
1023          return;
1024      }
1025  
1026      wp_update_plugins();
1027  }
1028  
1029  /**
1030   * Checks themes versions only after a duration of time.
1031   *
1032   * This is for performance reasons to make sure that on the theme version
1033   * checker is not run on every page load.
1034   *
1035   * @since 2.7.0
1036   * @access private
1037   */
1038  function _maybe_update_themes() {
1039      $current = get_site_transient( 'update_themes' );
1040  
1041      if ( isset( $current->last_checked )
1042          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
1043      ) {
1044          return;
1045      }
1046  
1047      wp_update_themes();
1048  }
1049  
1050  /**
1051   * Schedules core, theme, and plugin update checks.
1052   *
1053   * @since 3.1.0
1054   */
1055  function wp_schedule_update_checks() {
1056      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
1057          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
1058      }
1059  
1060      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
1061          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
1062      }
1063  
1064      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
1065          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
1066      }
1067  }
1068  
1069  /**
1070   * Clears existing update caches for plugins, themes, and core.
1071   *
1072   * @since 4.1.0
1073   */
1074  function wp_clean_update_cache() {
1075      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
1076          wp_clean_plugins_cache();
1077      } else {
1078          delete_site_transient( 'update_plugins' );
1079      }
1080  
1081      wp_clean_themes_cache();
1082  
1083      delete_site_transient( 'update_core' );
1084  }
1085  
1086  /**
1087   * Schedules the removal of all contents in the temporary backup directory.
1088   *
1089   * @since 6.3.0
1090   */
1091  function wp_delete_all_temp_backups() {
1092      /*
1093       * Check if there is a lock, or if currently performing an Ajax request,
1094       * in which case there is a chance an update is running.
1095       * Reschedule for an hour from now and exit early.
1096       */
1097      if ( get_option( 'core_updater.lock' ) || get_option( 'auto_updater.lock' ) || wp_doing_ajax() ) {
1098          wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_delete_temp_updater_backups' );
1099          return;
1100      }
1101  
1102      // This action runs on shutdown to make sure there are no plugin updates currently running.
1103      add_action( 'shutdown', '_wp_delete_all_temp_backups' );
1104  }
1105  
1106  /**
1107   * Deletes all contents in the temporary backup directory.
1108   *
1109   * @since 6.3.0
1110   *
1111   * @access private
1112   *
1113   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1114   */
1115  function _wp_delete_all_temp_backups() {
1116      global $wp_filesystem;
1117  
1118      if ( ! function_exists( 'WP_Filesystem' ) ) {
1119          require_once  ABSPATH . 'wp-admin/includes/file.php';
1120      }
1121  
1122      ob_start();
1123      $credentials = request_filesystem_credentials( '' );
1124      ob_end_clean();
1125  
1126      if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
1127          wp_trigger_error( __FUNCTION__, __( 'Could not access filesystem.' ) );
1128          return;
1129      }
1130  
1131      if ( ! $wp_filesystem->wp_content_dir() ) {
1132          wp_trigger_error(
1133              __FUNCTION__,
1134              /* translators: %s: Directory name. */
1135              sprintf( __( 'Unable to locate WordPress content directory (%s).' ), 'wp-content' )
1136          );
1137          return;
1138      }
1139  
1140      $temp_backup_dir = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/';
1141      $dirlist         = $wp_filesystem->dirlist( $temp_backup_dir );
1142      $dirlist         = $dirlist ? $dirlist : array();
1143  
1144      foreach ( array_keys( $dirlist ) as $dir ) {
1145          if ( '.' === $dir || '..' === $dir ) {
1146              continue;
1147          }
1148  
1149          $wp_filesystem->delete( $temp_backup_dir . $dir, true );
1150      }
1151  }
1152  
1153  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
1154      return;
1155  }
1156  
1157  add_action( 'admin_init', '_maybe_update_core' );
1158  add_action( 'wp_version_check', 'wp_version_check' );
1159  
1160  add_action( 'load-plugins.php', 'wp_update_plugins' );
1161  add_action( 'load-update.php', 'wp_update_plugins' );
1162  add_action( 'load-update-core.php', 'wp_update_plugins' );
1163  add_action( 'admin_init', '_maybe_update_plugins' );
1164  add_action( 'wp_update_plugins', 'wp_update_plugins' );
1165  
1166  add_action( 'load-themes.php', 'wp_update_themes' );
1167  add_action( 'load-update.php', 'wp_update_themes' );
1168  add_action( 'load-update-core.php', 'wp_update_themes' );
1169  add_action( 'admin_init', '_maybe_update_themes' );
1170  add_action( 'wp_update_themes', 'wp_update_themes' );
1171  
1172  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
1173  
1174  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
1175  
1176  add_action( 'init', 'wp_schedule_update_checks' );
1177  
1178  add_action( 'wp_delete_temp_updater_backups', 'wp_delete_all_temp_backups' );


Generated : Sat Jul 26 08:20:01 2025 Cross-referenced by PHPXref