[ 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.
  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 string $wp_version       Used to check against the newest WordPress version.
  25   * @global wpdb   $wpdb             WordPress database abstraction object.
  26   * @global string $wp_local_package Locale code of the package.
  27   *
  28   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  29   * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check.
  30   *                           Defaults to false, true if $extra_stats is set.
  31   */
  32  function wp_version_check( $extra_stats = array(), $force_check = false ) {
  33      global $wpdb, $wp_local_package;
  34  
  35      if ( wp_installing() ) {
  36          return;
  37      }
  38  
  39      $php_version = PHP_VERSION;
  40  
  41      $current      = get_site_transient( 'update_core' );
  42      $translations = wp_get_installed_translations( 'core' );
  43  
  44      // Invalidate the transient when $wp_version changes.
  45      if ( is_object( $current ) && wp_get_wp_version() !== $current->version_checked ) {
  46          $current = false;
  47      }
  48  
  49      if ( ! is_object( $current ) ) {
  50          $current                  = new stdClass();
  51          $current->updates         = array();
  52          $current->version_checked = wp_get_wp_version();
  53      }
  54  
  55      if ( ! empty( $extra_stats ) ) {
  56          $force_check = true;
  57      }
  58  
  59      // Wait 1 minute between multiple version check requests.
  60      $timeout          = MINUTE_IN_SECONDS;
  61      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  62  
  63      if ( ! $force_check && $time_not_changed ) {
  64          return;
  65      }
  66  
  67      /**
  68       * Filters the locale requested for WordPress core translations.
  69       *
  70       * @since 2.8.0
  71       *
  72       * @param string $locale Current locale.
  73       */
  74      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  75  
  76      // Update last_checked for current to prevent multiple blocking requests if request hangs.
  77      $current->last_checked = time();
  78      set_site_transient( 'update_core', $current );
  79  
  80      if ( method_exists( $wpdb, 'db_server_info' ) ) {
  81          $mysql_version = $wpdb->db_server_info();
  82      } elseif ( method_exists( $wpdb, 'db_version' ) ) {
  83          $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
  84      } else {
  85          $mysql_version = 'N/A';
  86      }
  87  
  88      if ( is_multisite() ) {
  89          $num_blogs         = get_blog_count();
  90          $wp_install        = network_site_url();
  91          $multisite_enabled = 1;
  92      } else {
  93          $multisite_enabled = 0;
  94          $num_blogs         = 1;
  95          $wp_install        = home_url( '/' );
  96      }
  97  
  98      $extensions = get_loaded_extensions();
  99      sort( $extensions, SORT_STRING | SORT_FLAG_CASE );
 100      $query = array(
 101          'version'            => wp_get_wp_version(),
 102          'php'                => $php_version,
 103          'locale'             => $locale,
 104          'mysql'              => $mysql_version,
 105          'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
 106          'blogs'              => $num_blogs,
 107          'users'              => get_user_count(),
 108          'multisite_enabled'  => $multisite_enabled,
 109          'initial_db_version' => get_site_option( 'initial_db_version' ),
 110          'extensions'         => array_combine( $extensions, array_map( 'phpversion', $extensions ) ),
 111          'platform_flags'     => array(
 112              'os'   => PHP_OS,
 113              'bits' => PHP_INT_SIZE === 4 ? 32 : 64,
 114          ),
 115          'image_support'      => array(),
 116      );
 117  
 118      if ( function_exists( 'gd_info' ) ) {
 119          $gd_info = gd_info();
 120          // Filter to supported values.
 121          $gd_info = array_filter( $gd_info );
 122  
 123          // Add data for GD WebP, AVIF, HEIC and JPEG XL support.
 124          $query['image_support']['gd'] = array_keys(
 125              array_filter(
 126                  array(
 127                      'webp' => isset( $gd_info['WebP Support'] ),
 128                      'avif' => isset( $gd_info['AVIF Support'] ),
 129                      'heic' => isset( $gd_info['HEIC Support'] ),
 130                      'jxl'  => isset( $gd_info['JXL Support'] ),
 131                  )
 132              )
 133          );
 134      }
 135  
 136      if ( class_exists( 'Imagick' ) ) {
 137          // Add data for Imagick WebP, AVIF, HEIC and JPEG XL support.
 138          $query['image_support']['imagick'] = array_keys(
 139              array_filter(
 140                  array(
 141                      'webp' => ! empty( Imagick::queryFormats( 'WEBP' ) ),
 142                      'avif' => ! empty( Imagick::queryFormats( 'AVIF' ) ),
 143                      'heic' => ! empty( Imagick::queryFormats( 'HEIC' ) ),
 144                      'jxl'  => ! empty( Imagick::queryFormats( 'JXL' ) ),
 145                  )
 146              )
 147          );
 148      }
 149  
 150      /**
 151       * Filters the query arguments sent as part of the core version check.
 152       *
 153       * WARNING: Changing this data may result in your site not receiving security updates.
 154       * Please exercise extreme caution.
 155       *
 156       * @since 4.9.0
 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       * }
 171       */
 172      $query = apply_filters( 'core_version_check_query_args', $query );
 173  
 174      $post_body = array(
 175          'translations' => wp_json_encode( $translations ),
 176      );
 177  
 178      if ( is_array( $extra_stats ) ) {
 179          $post_body = array_merge( $post_body, $extra_stats );
 180      }
 181  
 182      // Allow for WP_AUTO_UPDATE_CORE to specify beta/RC/development releases.
 183      if ( defined( 'WP_AUTO_UPDATE_CORE' )
 184          && in_array( WP_AUTO_UPDATE_CORE, array( 'beta', 'rc', 'development', 'branch-development' ), true )
 185      ) {
 186          $query['channel'] = WP_AUTO_UPDATE_CORE;
 187      }
 188  
 189      $url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, '', '&' );
 190      $http_url = $url;
 191      $ssl      = wp_http_supports( array( 'ssl' ) );
 192  
 193      if ( $ssl ) {
 194          $url = set_url_scheme( $url, 'https' );
 195      }
 196  
 197      $doing_cron = wp_doing_cron();
 198  
 199      $options = array(
 200          'timeout'    => $doing_cron ? 30 : 3,
 201          'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 202          'headers'    => array(
 203              'wp_install' => $wp_install,
 204              'wp_blog'    => home_url( '/' ),
 205          ),
 206          'body'       => $post_body,
 207      );
 208  
 209      $response = wp_remote_post( $url, $options );
 210  
 211      if ( $ssl && is_wp_error( $response ) ) {
 212          wp_trigger_error(
 213              __FUNCTION__,
 214              sprintf(
 215                  /* translators: %s: Support forums URL. */
 216                  __( '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>.' ),
 217                  __( 'https://wordpress.org/support/forums/' )
 218              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 219              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 220          );
 221          $response = wp_remote_post( $http_url, $options );
 222      }
 223  
 224      if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
 225          return;
 226      }
 227  
 228      $body = trim( wp_remote_retrieve_body( $response ) );
 229      $body = json_decode( $body, true );
 230  
 231      if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
 232          return;
 233      }
 234  
 235      $offers = $body['offers'];
 236  
 237      foreach ( $offers as &$offer ) {
 238          foreach ( $offer as $offer_key => $value ) {
 239              if ( 'packages' === $offer_key ) {
 240                  $offer['packages'] = (object) array_intersect_key(
 241                      array_map( 'esc_url', $offer['packages'] ),
 242                      array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
 243                  );
 244              } elseif ( 'download' === $offer_key ) {
 245                  $offer['download'] = esc_url( $value );
 246              } else {
 247                  $offer[ $offer_key ] = esc_html( $value );
 248              }
 249          }
 250          $offer = (object) array_intersect_key(
 251              $offer,
 252              array_fill_keys(
 253                  array(
 254                      'response',
 255                      'download',
 256                      'locale',
 257                      'packages',
 258                      'current',
 259                      'version',
 260                      'php_version',
 261                      'mysql_version',
 262                      'new_bundled',
 263                      'partial_version',
 264                      'notify_email',
 265                      'support_email',
 266                      'new_files',
 267                  ),
 268                  ''
 269              )
 270          );
 271      }
 272  
 273      $updates                  = new stdClass();
 274      $updates->updates         = $offers;
 275      $updates->last_checked    = time();
 276      $updates->version_checked = wp_get_wp_version();
 277  
 278      if ( isset( $body['translations'] ) ) {
 279          $updates->translations = $body['translations'];
 280      }
 281  
 282      set_site_transient( 'update_core', $updates );
 283  
 284      if ( ! empty( $body['ttl'] ) ) {
 285          $ttl = (int) $body['ttl'];
 286  
 287          if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
 288              // Queue an event to re-run the update check in $ttl seconds.
 289              wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
 290          }
 291      }
 292  
 293      // Trigger background updates if running non-interactively, and we weren't called from the update handler.
 294      if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
 295          /**
 296           * Fires during wp_cron, starting the auto-update process.
 297           *
 298           * @since 3.9.0
 299           */
 300          do_action( 'wp_maybe_auto_update' );
 301      }
 302  }
 303  
 304  /**
 305   * Checks for available updates to plugins based on the latest versions hosted on WordPress.org.
 306   *
 307   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 308   *
 309   * A list of all plugins installed is sent to WP, along with the site locale.
 310   *
 311   * Checks against the WordPress server at api.wordpress.org. Will only check
 312   * if WordPress isn't installing.
 313   *
 314   * @since 2.3.0
 315   *
 316   * @global string $wp_version The WordPress version string.
 317   *
 318   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 319   */
 320  function wp_update_plugins( $extra_stats = array() ) {
 321      if ( wp_installing() ) {
 322          return;
 323      }
 324  
 325      // If running blog-side, bail unless we've not checked in the last 12 hours.
 326      if ( ! function_exists( 'get_plugins' ) ) {
 327          require_once  ABSPATH . 'wp-admin/includes/plugin.php';
 328      }
 329  
 330      $plugins      = get_plugins();
 331      $translations = wp_get_installed_translations( 'plugins' );
 332  
 333      $active  = get_option( 'active_plugins', array() );
 334      $current = get_site_transient( 'update_plugins' );
 335  
 336      if ( ! is_object( $current ) ) {
 337          $current = new stdClass();
 338      }
 339  
 340      $updates               = new stdClass();
 341      $updates->last_checked = time();
 342      $updates->response     = array();
 343      $updates->translations = array();
 344      $updates->no_update    = array();
 345  
 346      $doing_cron = wp_doing_cron();
 347  
 348      // Check for update on a different schedule, depending on the page.
 349      switch ( current_filter() ) {
 350          case 'upgrader_process_complete':
 351              $timeout = 0;
 352              break;
 353          case 'load-update-core.php':
 354              $timeout = MINUTE_IN_SECONDS;
 355              break;
 356          case 'load-plugins.php':
 357          case 'load-update.php':
 358              $timeout = HOUR_IN_SECONDS;
 359              break;
 360          default:
 361              if ( $doing_cron ) {
 362                  $timeout = 2 * HOUR_IN_SECONDS;
 363              } else {
 364                  $timeout = 12 * HOUR_IN_SECONDS;
 365              }
 366      }
 367  
 368      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 369  
 370      if ( $time_not_changed && ! $extra_stats ) {
 371          $plugin_changed = false;
 372  
 373          foreach ( $plugins as $file => $p ) {
 374              $updates->checked[ $file ] = $p['Version'];
 375  
 376              if ( ! isset( $current->checked[ $file ] ) || (string) $current->checked[ $file ] !== (string) $p['Version'] ) {
 377                  $plugin_changed = true;
 378              }
 379          }
 380  
 381          if ( isset( $current->response ) && is_array( $current->response ) ) {
 382              foreach ( $current->response as $plugin_file => $update_details ) {
 383                  if ( ! isset( $plugins[ $plugin_file ] ) ) {
 384                      $plugin_changed = true;
 385                      break;
 386                  }
 387              }
 388          }
 389  
 390          // Bail if we've checked recently and if nothing has changed.
 391          if ( ! $plugin_changed ) {
 392              return;
 393          }
 394      }
 395  
 396      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 397      $current->last_checked = time();
 398      set_site_transient( 'update_plugins', $current );
 399  
 400      $to_send = compact( 'plugins', 'active' );
 401  
 402      $locales = array_values( get_available_languages() );
 403  
 404      /**
 405       * Filters the locales requested for plugin translations.
 406       *
 407       * @since 3.7.0
 408       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 409       *
 410       * @param string[] $locales Plugin locales. Default is all available locales of the site.
 411       */
 412      $locales = apply_filters( 'plugins_update_check_locales', $locales );
 413      $locales = array_unique( $locales );
 414  
 415      if ( $doing_cron ) {
 416          $timeout = 30; // 30 seconds.
 417      } else {
 418          // Three seconds, plus one extra second for every 10 plugins.
 419          $timeout = 3 + (int) ( count( $plugins ) / 10 );
 420      }
 421  
 422      $options = array(
 423          'timeout'    => $timeout,
 424          'body'       => array(
 425              'plugins'      => wp_json_encode( $to_send ),
 426              'translations' => wp_json_encode( $translations ),
 427              'locale'       => wp_json_encode( $locales ),
 428              'all'          => wp_json_encode( true ),
 429          ),
 430          'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 431      );
 432  
 433      if ( $extra_stats ) {
 434          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 435      }
 436  
 437      $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
 438      $http_url = $url;
 439      $ssl      = wp_http_supports( array( 'ssl' ) );
 440  
 441      if ( $ssl ) {
 442          $url = set_url_scheme( $url, 'https' );
 443      }
 444  
 445      $raw_response = wp_remote_post( $url, $options );
 446  
 447      if ( $ssl && is_wp_error( $raw_response ) ) {
 448          wp_trigger_error(
 449              __FUNCTION__,
 450              sprintf(
 451                  /* translators: %s: Support forums URL. */
 452                  __( '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>.' ),
 453                  __( 'https://wordpress.org/support/forums/' )
 454              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 455              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 456          );
 457          $raw_response = wp_remote_post( $http_url, $options );
 458      }
 459  
 460      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 461          return;
 462      }
 463  
 464      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 465  
 466      if ( $response && is_array( $response ) ) {
 467          $updates->response     = $response['plugins'];
 468          $updates->translations = $response['translations'];
 469          $updates->no_update    = $response['no_update'];
 470      }
 471  
 472      // Support updates for any plugins using the `Update URI` header field.
 473      foreach ( $plugins as $plugin_file => $plugin_data ) {
 474          if ( ! $plugin_data['UpdateURI'] || isset( $updates->response[ $plugin_file ] ) ) {
 475              continue;
 476          }
 477  
 478          $hostname = wp_parse_url( sanitize_url( $plugin_data['UpdateURI'] ), PHP_URL_HOST );
 479  
 480          /**
 481           * Filters the update response for a given plugin hostname.
 482           *
 483           * The dynamic portion of the hook name, `$hostname`, refers to the hostname
 484           * of the URI specified in the `Update URI` header field.
 485           *
 486           * @since 5.8.0
 487           *
 488           * @param array|false $update {
 489           *     The plugin update data with the latest details. Default false.
 490           *
 491           *     @type string $id           Optional. ID of the plugin for update purposes, should be a URI
 492           *                                specified in the `Update URI` header field.
 493           *     @type string $slug         Slug of the plugin.
 494           *     @type string $version      The version of the plugin.
 495           *     @type string $url          The URL for details of the plugin.
 496           *     @type string $package      Optional. The update ZIP for the plugin.
 497           *     @type string $tested       Optional. The version of WordPress the plugin is tested against.
 498           *     @type string $requires_php Optional. The version of PHP which the plugin requires.
 499           *     @type bool   $autoupdate   Optional. Whether the plugin should automatically update.
 500           *     @type array  $icons        Optional. Array of plugin icons.
 501           *     @type array  $banners      Optional. Array of plugin banners.
 502           *     @type array  $banners_rtl  Optional. Array of plugin RTL banners.
 503           *     @type array  $translations {
 504           *         Optional. List of translation updates for the plugin.
 505           *
 506           *         @type string $language   The language the translation update is for.
 507           *         @type string $version    The version of the plugin this translation is for.
 508           *                                  This is not the version of the language file.
 509           *         @type string $updated    The update timestamp of the translation file.
 510           *                                  Should be a date in the `YYYY-MM-DD HH:MM:SS` format.
 511           *         @type string $package    The ZIP location containing the translation update.
 512           *         @type string $autoupdate Whether the translation should be automatically installed.
 513           *     }
 514           * }
 515           * @param array       $plugin_data      Plugin headers.
 516           * @param string      $plugin_file      Plugin filename.
 517           * @param string[]    $locales          Installed locales to look up translations for.
 518           */
 519          $update = apply_filters( "update_plugins_{$hostname}", false, $plugin_data, $plugin_file, $locales );
 520  
 521          if ( ! $update ) {
 522              continue;
 523          }
 524  
 525          $update = (object) $update;
 526  
 527          // Is it valid? We require at least a version.
 528          if ( ! isset( $update->version ) ) {
 529              continue;
 530          }
 531  
 532          // These should remain constant.
 533          $update->id     = $plugin_data['UpdateURI'];
 534          $update->plugin = $plugin_file;
 535  
 536          // WordPress needs the version field specified as 'new_version'.
 537          if ( ! isset( $update->new_version ) ) {
 538              $update->new_version = $update->version;
 539          }
 540  
 541          // Handle any translation updates.
 542          if ( ! empty( $update->translations ) ) {
 543              foreach ( $update->translations as $translation ) {
 544                  if ( isset( $translation['language'], $translation['package'] ) ) {
 545                      $translation['type'] = 'plugin';
 546                      $translation['slug'] = isset( $update->slug ) ? $update->slug : $update->id;
 547  
 548                      $updates->translations[] = $translation;
 549                  }
 550              }
 551          }
 552  
 553          unset( $updates->no_update[ $plugin_file ], $updates->response[ $plugin_file ] );
 554  
 555          if ( version_compare( $update->new_version, $plugin_data['Version'], '>' ) ) {
 556              $updates->response[ $plugin_file ] = $update;
 557          } else {
 558              $updates->no_update[ $plugin_file ] = $update;
 559          }
 560      }
 561  
 562      $sanitize_plugin_update_payload = static function ( &$item ) {
 563          $item = (object) $item;
 564  
 565          unset( $item->translations, $item->compatibility );
 566  
 567          return $item;
 568      };
 569  
 570      array_walk( $updates->response, $sanitize_plugin_update_payload );
 571      array_walk( $updates->no_update, $sanitize_plugin_update_payload );
 572  
 573      set_site_transient( 'update_plugins', $updates );
 574  }
 575  
 576  /**
 577   * Checks for available updates to themes based on the latest versions hosted on WordPress.org.
 578   *
 579   * Despite its name this function does not actually perform any updates, it only checks for available updates.
 580   *
 581   * A list of all themes installed is sent to WP, along with the site locale.
 582   *
 583   * Checks against the WordPress server at api.wordpress.org. Will only check
 584   * if WordPress isn't installing.
 585   *
 586   * @since 2.7.0
 587   *
 588   * @global string $wp_version The WordPress version string.
 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   */
 896  function wp_get_update_data() {
 897      $counts = array(
 898          'plugins'      => 0,
 899          'themes'       => 0,
 900          'wordpress'    => 0,
 901          'translations' => 0,
 902      );
 903  
 904      $plugins = current_user_can( 'update_plugins' );
 905  
 906      if ( $plugins ) {
 907          $update_plugins = get_site_transient( 'update_plugins' );
 908  
 909          if ( ! empty( $update_plugins->response ) ) {
 910              $counts['plugins'] = count( $update_plugins->response );
 911          }
 912      }
 913  
 914      $themes = current_user_can( 'update_themes' );
 915  
 916      if ( $themes ) {
 917          $update_themes = get_site_transient( 'update_themes' );
 918  
 919          if ( ! empty( $update_themes->response ) ) {
 920              $counts['themes'] = count( $update_themes->response );
 921          }
 922      }
 923  
 924      $core = current_user_can( 'update_core' );
 925  
 926      if ( $core && function_exists( 'get_core_updates' ) ) {
 927          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 928  
 929          if ( ! empty( $update_wordpress )
 930              && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
 931              && current_user_can( 'update_core' )
 932          ) {
 933              $counts['wordpress'] = 1;
 934          }
 935      }
 936  
 937      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 938          $counts['translations'] = 1;
 939      }
 940  
 941      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 942      $titles          = array();
 943  
 944      if ( $counts['wordpress'] ) {
 945          /* translators: %d: Number of available WordPress updates. */
 946          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 947      }
 948  
 949      if ( $counts['plugins'] ) {
 950          /* translators: %d: Number of available plugin updates. */
 951          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 952      }
 953  
 954      if ( $counts['themes'] ) {
 955          /* translators: %d: Number of available theme updates. */
 956          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 957      }
 958  
 959      if ( $counts['translations'] ) {
 960          $titles['translations'] = __( 'Translation Updates' );
 961      }
 962  
 963      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 964  
 965      $update_data = array(
 966          'counts' => $counts,
 967          'title'  => $update_title,
 968      );
 969      /**
 970       * Filters the returned array of update data for plugins, themes, and WordPress core.
 971       *
 972       * @since 3.5.0
 973       *
 974       * @param array $update_data {
 975       *     Fetched update data.
 976       *
 977       *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
 978       *     @type string  $update_title Titles of available updates.
 979       * }
 980       * @param array $titles An array of update counts and UI strings for available updates.
 981       */
 982      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 983  }
 984  
 985  /**
 986   * Determines whether core should be updated.
 987   *
 988   * @since 2.8.0
 989   *
 990   * @global string $wp_version The WordPress version string.
 991   */
 992  function _maybe_update_core() {
 993      $current = get_site_transient( 'update_core' );
 994  
 995      if ( isset( $current->last_checked, $current->version_checked )
 996          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 997          && wp_get_wp_version() === $current->version_checked
 998      ) {
 999          return;
1000      }
1001  
1002      wp_version_check();
1003  }
1004  /**
1005   * Checks the last time plugins were run before checking plugin versions.
1006   *
1007   * This might have been backported to WordPress 2.6.1 for performance reasons.
1008   * This is used for the wp-admin to check only so often instead of every page
1009   * load.
1010   *
1011   * @since 2.7.0
1012   * @access private
1013   */
1014  function _maybe_update_plugins() {
1015      $current = get_site_transient( 'update_plugins' );
1016  
1017      if ( isset( $current->last_checked )
1018          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
1019      ) {
1020          return;
1021      }
1022  
1023      wp_update_plugins();
1024  }
1025  
1026  /**
1027   * Checks themes versions only after a duration of time.
1028   *
1029   * This is for performance reasons to make sure that on the theme version
1030   * checker is not run on every page load.
1031   *
1032   * @since 2.7.0
1033   * @access private
1034   */
1035  function _maybe_update_themes() {
1036      $current = get_site_transient( 'update_themes' );
1037  
1038      if ( isset( $current->last_checked )
1039          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
1040      ) {
1041          return;
1042      }
1043  
1044      wp_update_themes();
1045  }
1046  
1047  /**
1048   * Schedules core, theme, and plugin update checks.
1049   *
1050   * @since 3.1.0
1051   */
1052  function wp_schedule_update_checks() {
1053      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
1054          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
1055      }
1056  
1057      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
1058          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
1059      }
1060  
1061      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
1062          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
1063      }
1064  }
1065  
1066  /**
1067   * Clears existing update caches for plugins, themes, and core.
1068   *
1069   * @since 4.1.0
1070   */
1071  function wp_clean_update_cache() {
1072      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
1073          wp_clean_plugins_cache();
1074      } else {
1075          delete_site_transient( 'update_plugins' );
1076      }
1077  
1078      wp_clean_themes_cache();
1079  
1080      delete_site_transient( 'update_core' );
1081  }
1082  
1083  /**
1084   * Schedules the removal of all contents in the temporary backup directory.
1085   *
1086   * @since 6.3.0
1087   */
1088  function wp_delete_all_temp_backups() {
1089      /*
1090       * Check if there is a lock, or if currently performing an Ajax request,
1091       * in which case there is a chance an update is running.
1092       * Reschedule for an hour from now and exit early.
1093       */
1094      if ( get_option( 'core_updater.lock' ) || get_option( 'auto_updater.lock' ) || wp_doing_ajax() ) {
1095          wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_delete_temp_updater_backups' );
1096          return;
1097      }
1098  
1099      // This action runs on shutdown to make sure there are no plugin updates currently running.
1100      add_action( 'shutdown', '_wp_delete_all_temp_backups' );
1101  }
1102  
1103  /**
1104   * Deletes all contents in the temporary backup directory.
1105   *
1106   * @since 6.3.0
1107   *
1108   * @access private
1109   *
1110   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1111   */
1112  function _wp_delete_all_temp_backups() {
1113      global $wp_filesystem;
1114  
1115      if ( ! function_exists( 'WP_Filesystem' ) ) {
1116          require_once  ABSPATH . '/wp-admin/includes/file.php';
1117      }
1118  
1119      ob_start();
1120      $credentials = request_filesystem_credentials( '' );
1121      ob_end_clean();
1122  
1123      if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
1124          wp_trigger_error( __FUNCTION__, __( 'Could not access filesystem.' ) );
1125          return;
1126      }
1127  
1128      if ( ! $wp_filesystem->wp_content_dir() ) {
1129          wp_trigger_error(
1130              __FUNCTION__,
1131              /* translators: %s: Directory name. */
1132              sprintf( __( 'Unable to locate WordPress content directory (%s).' ), 'wp-content' )
1133          );
1134          return;
1135      }
1136  
1137      $temp_backup_dir = $wp_filesystem->wp_content_dir() . 'upgrade-temp-backup/';
1138      $dirlist         = $wp_filesystem->dirlist( $temp_backup_dir );
1139      $dirlist         = $dirlist ? $dirlist : array();
1140  
1141      foreach ( array_keys( $dirlist ) as $dir ) {
1142          if ( '.' === $dir || '..' === $dir ) {
1143              continue;
1144          }
1145  
1146          $wp_filesystem->delete( $temp_backup_dir . $dir, true );
1147      }
1148  }
1149  
1150  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
1151      return;
1152  }
1153  
1154  add_action( 'admin_init', '_maybe_update_core' );
1155  add_action( 'wp_version_check', 'wp_version_check' );
1156  
1157  add_action( 'load-plugins.php', 'wp_update_plugins' );
1158  add_action( 'load-update.php', 'wp_update_plugins' );
1159  add_action( 'load-update-core.php', 'wp_update_plugins' );
1160  add_action( 'admin_init', '_maybe_update_plugins' );
1161  add_action( 'wp_update_plugins', 'wp_update_plugins' );
1162  
1163  add_action( 'load-themes.php', 'wp_update_themes' );
1164  add_action( 'load-update.php', 'wp_update_themes' );
1165  add_action( 'load-update-core.php', 'wp_update_themes' );
1166  add_action( 'admin_init', '_maybe_update_themes' );
1167  add_action( 'wp_update_themes', 'wp_update_themes' );
1168  
1169  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
1170  
1171  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
1172  
1173  add_action( 'init', 'wp_schedule_update_checks' );
1174  
1175  add_action( 'wp_delete_temp_updater_backups', 'wp_delete_all_temp_backups' );


Generated : Sat Feb 22 08:20:01 2025 Cross-referenced by PHPXref