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


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref