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


Generated : Tue Mar 19 08:20:01 2024 Cross-referenced by PHPXref