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


Generated : Mon Jul 15 08:20:02 2024 Cross-referenced by PHPXref