[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * A simple set of functions to check our version 1.0 update service.
   4   *
   5   * @package WordPress
   6   * @since 2.3.0
   7   */
   8  
   9  /**
  10   * Check WordPress version against the newest version.
  11   *
  12   * The WordPress version, PHP version, and Locale is sent. Checks against the
  13   * WordPress server at api.wordpress.org server. Will only check if WordPress
  14   * isn't installing.
  15   *
  16   * @since 2.3.0
  17   * @global string $wp_version       Used to check against the newest WordPress version.
  18   * @global wpdb   $wpdb             WordPress database abstraction object.
  19   * @global string $wp_local_package Locale code of the package.
  20   *
  21   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
  22   * @param bool  $force_check Whether to bypass the transient cache and force a fresh update check. Defaults to false, true if $extra_stats is set.
  23   */
  24  function wp_version_check( $extra_stats = array(), $force_check = false ) {
  25      global $wpdb, $wp_local_package;
  26  
  27      if ( wp_installing() ) {
  28          return;
  29      }
  30  
  31      // Include an unmodified $wp_version.
  32      require  ABSPATH . WPINC . '/version.php';
  33      $php_version = phpversion();
  34  
  35      $current      = get_site_transient( 'update_core' );
  36      $translations = wp_get_installed_translations( 'core' );
  37  
  38      // Invalidate the transient when $wp_version changes.
  39      if ( is_object( $current ) && $wp_version !== $current->version_checked ) {
  40          $current = false;
  41      }
  42  
  43      if ( ! is_object( $current ) ) {
  44          $current                  = new stdClass;
  45          $current->updates         = array();
  46          $current->version_checked = $wp_version;
  47      }
  48  
  49      if ( ! empty( $extra_stats ) ) {
  50          $force_check = true;
  51      }
  52  
  53      // Wait 1 minute between multiple version check requests.
  54      $timeout          = MINUTE_IN_SECONDS;
  55      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
  56  
  57      if ( ! $force_check && $time_not_changed ) {
  58          return;
  59      }
  60  
  61      /**
  62       * Filters the locale requested for WordPress core translations.
  63       *
  64       * @since 2.8.0
  65       *
  66       * @param string $locale Current locale.
  67       */
  68      $locale = apply_filters( 'core_version_check_locale', get_locale() );
  69  
  70      // Update last_checked for current to prevent multiple blocking requests if request hangs.
  71      $current->last_checked = time();
  72      set_site_transient( 'update_core', $current );
  73  
  74      if ( method_exists( $wpdb, 'db_version' ) ) {
  75          $mysql_version = preg_replace( '/[^0-9.].*/', '', $wpdb->db_version() );
  76      } else {
  77          $mysql_version = 'N/A';
  78      }
  79  
  80      if ( is_multisite() ) {
  81          $user_count        = get_user_count();
  82          $num_blogs         = get_blog_count();
  83          $wp_install        = network_site_url();
  84          $multisite_enabled = 1;
  85      } else {
  86          $user_count        = count_users();
  87          $user_count        = $user_count['total_users'];
  88          $multisite_enabled = 0;
  89          $num_blogs         = 1;
  90          $wp_install        = home_url( '/' );
  91      }
  92  
  93      $query = array(
  94          'version'            => $wp_version,
  95          'php'                => $php_version,
  96          'locale'             => $locale,
  97          'mysql'              => $mysql_version,
  98          'local_package'      => isset( $wp_local_package ) ? $wp_local_package : '',
  99          'blogs'              => $num_blogs,
 100          'users'              => $user_count,
 101          'multisite_enabled'  => $multisite_enabled,
 102          'initial_db_version' => get_site_option( 'initial_db_version' ),
 103      );
 104  
 105      /**
 106       * Filter the query arguments sent as part of the core version check.
 107       *
 108       * WARNING: Changing this data may result in your site not receiving security updates.
 109       * Please exercise extreme caution.
 110       *
 111       * @since 4.9.0
 112       *
 113       * @param array $query {
 114       *     Version check query arguments.
 115       *
 116       *     @type string $version            WordPress version number.
 117       *     @type string $php                PHP version number.
 118       *     @type string $locale             The locale to retrieve updates for.
 119       *     @type string $mysql              MySQL version number.
 120       *     @type string $local_package      The value of the $wp_local_package global, when set.
 121       *     @type int    $blogs              Number of sites on this WordPress installation.
 122       *     @type int    $users              Number of users on this WordPress installation.
 123       *     @type int    $multisite_enabled  Whether this WordPress installation uses Multisite.
 124       *     @type int    $initial_db_version Database version of WordPress at time of installation.
 125       * }
 126       */
 127      $query = apply_filters( 'core_version_check_query_args', $query );
 128  
 129      $post_body = array(
 130          'translations' => wp_json_encode( $translations ),
 131      );
 132  
 133      if ( is_array( $extra_stats ) ) {
 134          $post_body = array_merge( $post_body, $extra_stats );
 135      }
 136  
 137      $url      = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
 138      $http_url = $url;
 139      $ssl      = wp_http_supports( array( 'ssl' ) );
 140  
 141      if ( $ssl ) {
 142          $url = set_url_scheme( $url, 'https' );
 143      }
 144  
 145      $doing_cron = wp_doing_cron();
 146  
 147      $options = array(
 148          'timeout'    => $doing_cron ? 30 : 3,
 149          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 150          'headers'    => array(
 151              'wp_install' => $wp_install,
 152              'wp_blog'    => home_url( '/' ),
 153          ),
 154          'body'       => $post_body,
 155      );
 156  
 157      $response = wp_remote_post( $url, $options );
 158  
 159      if ( $ssl && is_wp_error( $response ) ) {
 160          trigger_error(
 161              sprintf(
 162                  /* translators: %s: Support forums URL. */
 163                  __( '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>.' ),
 164                  __( 'https://wordpress.org/support/forums/' )
 165              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 166              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 167          );
 168          $response = wp_remote_post( $http_url, $options );
 169      }
 170  
 171      if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
 172          return;
 173      }
 174  
 175      $body = trim( wp_remote_retrieve_body( $response ) );
 176      $body = json_decode( $body, true );
 177  
 178      if ( ! is_array( $body ) || ! isset( $body['offers'] ) ) {
 179          return;
 180      }
 181  
 182      $offers = $body['offers'];
 183  
 184      foreach ( $offers as &$offer ) {
 185          foreach ( $offer as $offer_key => $value ) {
 186              if ( 'packages' === $offer_key ) {
 187                  $offer['packages'] = (object) array_intersect_key(
 188                      array_map( 'esc_url', $offer['packages'] ),
 189                      array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
 190                  );
 191              } elseif ( 'download' === $offer_key ) {
 192                  $offer['download'] = esc_url( $value );
 193              } else {
 194                  $offer[ $offer_key ] = esc_html( $value );
 195              }
 196          }
 197          $offer = (object) array_intersect_key(
 198              $offer,
 199              array_fill_keys(
 200                  array(
 201                      'response',
 202                      'download',
 203                      'locale',
 204                      'packages',
 205                      'current',
 206                      'version',
 207                      'php_version',
 208                      'mysql_version',
 209                      'new_bundled',
 210                      'partial_version',
 211                      'notify_email',
 212                      'support_email',
 213                      'new_files',
 214                  ),
 215                  ''
 216              )
 217          );
 218      }
 219  
 220      $updates                  = new stdClass();
 221      $updates->updates         = $offers;
 222      $updates->last_checked    = time();
 223      $updates->version_checked = $wp_version;
 224  
 225      if ( isset( $body['translations'] ) ) {
 226          $updates->translations = $body['translations'];
 227      }
 228  
 229      set_site_transient( 'update_core', $updates );
 230  
 231      if ( ! empty( $body['ttl'] ) ) {
 232          $ttl = (int) $body['ttl'];
 233  
 234          if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
 235              // Queue an event to re-run the update check in $ttl seconds.
 236              wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
 237          }
 238      }
 239  
 240      // Trigger background updates if running non-interactively, and we weren't called from the update handler.
 241      if ( $doing_cron && ! doing_action( 'wp_maybe_auto_update' ) ) {
 242          /**
 243           * Fires during wp_cron, starting the auto update process.
 244           *
 245           * @since 3.9.0
 246           */
 247          do_action( 'wp_maybe_auto_update' );
 248      }
 249  }
 250  
 251  /**
 252   * Check plugin versions against the latest versions hosted on WordPress.org.
 253   *
 254   * The WordPress version, PHP version, and Locale is sent along with a list of
 255   * all plugins installed. Checks against the WordPress server at
 256   * api.wordpress.org. Will only check if WordPress isn't installing.
 257   *
 258   * @since 2.3.0
 259   * @global string $wp_version The WordPress version string.
 260   *
 261   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 262   */
 263  function wp_update_plugins( $extra_stats = array() ) {
 264      if ( wp_installing() ) {
 265          return;
 266      }
 267  
 268      // Include an unmodified $wp_version.
 269      require  ABSPATH . WPINC . '/version.php';
 270  
 271      // If running blog-side, bail unless we've not checked in the last 12 hours.
 272      if ( ! function_exists( 'get_plugins' ) ) {
 273          require_once ABSPATH . 'wp-admin/includes/plugin.php';
 274      }
 275  
 276      $plugins      = get_plugins();
 277      $translations = wp_get_installed_translations( 'plugins' );
 278  
 279      $active  = get_option( 'active_plugins', array() );
 280      $current = get_site_transient( 'update_plugins' );
 281  
 282      if ( ! is_object( $current ) ) {
 283          $current = new stdClass;
 284      }
 285  
 286      $new_option               = new stdClass;
 287      $new_option->last_checked = time();
 288  
 289      $doing_cron = wp_doing_cron();
 290  
 291      // Check for update on a different schedule, depending on the page.
 292      switch ( current_filter() ) {
 293          case 'upgrader_process_complete':
 294              $timeout = 0;
 295              break;
 296          case 'load-update-core.php':
 297              $timeout = MINUTE_IN_SECONDS;
 298              break;
 299          case 'load-plugins.php':
 300          case 'load-update.php':
 301              $timeout = HOUR_IN_SECONDS;
 302              break;
 303          default:
 304              if ( $doing_cron ) {
 305                  $timeout = 2 * HOUR_IN_SECONDS;
 306              } else {
 307                  $timeout = 12 * HOUR_IN_SECONDS;
 308              }
 309      }
 310  
 311      $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
 312  
 313      if ( $time_not_changed && ! $extra_stats ) {
 314          $plugin_changed = false;
 315  
 316          foreach ( $plugins as $file => $p ) {
 317              $new_option->checked[ $file ] = $p['Version'];
 318  
 319              if ( ! isset( $current->checked[ $file ] ) || strval( $current->checked[ $file ] ) !== strval( $p['Version'] ) ) {
 320                  $plugin_changed = true;
 321              }
 322          }
 323  
 324          if ( isset( $current->response ) && is_array( $current->response ) ) {
 325              foreach ( $current->response as $plugin_file => $update_details ) {
 326                  if ( ! isset( $plugins[ $plugin_file ] ) ) {
 327                      $plugin_changed = true;
 328                      break;
 329                  }
 330              }
 331          }
 332  
 333          // Bail if we've checked recently and if nothing has changed.
 334          if ( ! $plugin_changed ) {
 335              return;
 336          }
 337      }
 338  
 339      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 340      $current->last_checked = time();
 341      set_site_transient( 'update_plugins', $current );
 342  
 343      $to_send = compact( 'plugins', 'active' );
 344  
 345      $locales = array_values( get_available_languages() );
 346  
 347      /**
 348       * Filters the locales requested for plugin translations.
 349       *
 350       * @since 3.7.0
 351       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 352       *
 353       * @param array $locales Plugin locales. Default is all available locales of the site.
 354       */
 355      $locales = apply_filters( 'plugins_update_check_locales', $locales );
 356      $locales = array_unique( $locales );
 357  
 358      if ( $doing_cron ) {
 359          $timeout = 30;
 360      } else {
 361          // Three seconds, plus one extra second for every 10 plugins.
 362          $timeout = 3 + (int) ( count( $plugins ) / 10 );
 363      }
 364  
 365      $options = array(
 366          'timeout'    => $timeout,
 367          'body'       => array(
 368              'plugins'      => wp_json_encode( $to_send ),
 369              'translations' => wp_json_encode( $translations ),
 370              'locale'       => wp_json_encode( $locales ),
 371              'all'          => wp_json_encode( true ),
 372          ),
 373          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 374      );
 375  
 376      if ( $extra_stats ) {
 377          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 378      }
 379  
 380      $url      = 'http://api.wordpress.org/plugins/update-check/1.1/';
 381      $http_url = $url;
 382      $ssl      = wp_http_supports( array( 'ssl' ) );
 383  
 384      if ( $ssl ) {
 385          $url = set_url_scheme( $url, 'https' );
 386      }
 387  
 388      $raw_response = wp_remote_post( $url, $options );
 389  
 390      if ( $ssl && is_wp_error( $raw_response ) ) {
 391          trigger_error(
 392              sprintf(
 393                  /* translators: %s: Support forums URL. */
 394                  __( '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>.' ),
 395                  __( 'https://wordpress.org/support/forums/' )
 396              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 397              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 398          );
 399          $raw_response = wp_remote_post( $http_url, $options );
 400      }
 401  
 402      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 403          return;
 404      }
 405  
 406      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 407  
 408      foreach ( $response['plugins'] as &$plugin ) {
 409          $plugin = (object) $plugin;
 410  
 411          if ( isset( $plugin->compatibility ) ) {
 412              $plugin->compatibility = (object) $plugin->compatibility;
 413  
 414              foreach ( $plugin->compatibility as &$data ) {
 415                  $data = (object) $data;
 416              }
 417          }
 418      }
 419  
 420      unset( $plugin, $data );
 421  
 422      foreach ( $response['no_update'] as &$plugin ) {
 423          $plugin = (object) $plugin;
 424      }
 425  
 426      unset( $plugin );
 427  
 428      if ( is_array( $response ) ) {
 429          $new_option->response     = $response['plugins'];
 430          $new_option->translations = $response['translations'];
 431          // TODO: Perhaps better to store no_update in a separate transient with an expiry?
 432          $new_option->no_update = $response['no_update'];
 433      } else {
 434          $new_option->response     = array();
 435          $new_option->translations = array();
 436          $new_option->no_update    = array();
 437      }
 438  
 439      set_site_transient( 'update_plugins', $new_option );
 440  }
 441  
 442  /**
 443   * Check theme versions against the latest versions hosted on WordPress.org.
 444   *
 445   * A list of all themes installed in sent to WP. Checks against the
 446   * WordPress server at api.wordpress.org. Will only check if WordPress isn't
 447   * installing.
 448   *
 449   * @since 2.7.0
 450   * @global string $wp_version The WordPress version string.
 451   *
 452   * @param array $extra_stats Extra statistics to report to the WordPress.org API.
 453   */
 454  function wp_update_themes( $extra_stats = array() ) {
 455      if ( wp_installing() ) {
 456          return;
 457      }
 458  
 459      // Include an unmodified $wp_version.
 460      require  ABSPATH . WPINC . '/version.php';
 461  
 462      $installed_themes = wp_get_themes();
 463      $translations     = wp_get_installed_translations( 'themes' );
 464  
 465      $last_update = get_site_transient( 'update_themes' );
 466  
 467      if ( ! is_object( $last_update ) ) {
 468          $last_update = new stdClass;
 469      }
 470  
 471      $themes  = array();
 472      $checked = array();
 473      $request = array();
 474  
 475      // Put slug of current theme into request.
 476      $request['active'] = get_option( 'stylesheet' );
 477  
 478      foreach ( $installed_themes as $theme ) {
 479          $checked[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
 480  
 481          $themes[ $theme->get_stylesheet() ] = array(
 482              'Name'       => $theme->get( 'Name' ),
 483              'Title'      => $theme->get( 'Name' ),
 484              'Version'    => $theme->get( 'Version' ),
 485              'Author'     => $theme->get( 'Author' ),
 486              'Author URI' => $theme->get( 'AuthorURI' ),
 487              'Template'   => $theme->get_template(),
 488              'Stylesheet' => $theme->get_stylesheet(),
 489          );
 490      }
 491  
 492      $doing_cron = wp_doing_cron();
 493  
 494      // Check for update on a different schedule, depending on the page.
 495      switch ( current_filter() ) {
 496          case 'upgrader_process_complete':
 497              $timeout = 0;
 498              break;
 499          case 'load-update-core.php':
 500              $timeout = MINUTE_IN_SECONDS;
 501              break;
 502          case 'load-themes.php':
 503          case 'load-update.php':
 504              $timeout = HOUR_IN_SECONDS;
 505              break;
 506          default:
 507              if ( $doing_cron ) {
 508                  $timeout = 2 * HOUR_IN_SECONDS;
 509              } else {
 510                  $timeout = 12 * HOUR_IN_SECONDS;
 511              }
 512      }
 513  
 514      $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked );
 515  
 516      if ( $time_not_changed && ! $extra_stats ) {
 517          $theme_changed = false;
 518  
 519          foreach ( $checked as $slug => $v ) {
 520              if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
 521                  $theme_changed = true;
 522              }
 523          }
 524  
 525          if ( isset( $last_update->response ) && is_array( $last_update->response ) ) {
 526              foreach ( $last_update->response as $slug => $update_details ) {
 527                  if ( ! isset( $checked[ $slug ] ) ) {
 528                      $theme_changed = true;
 529                      break;
 530                  }
 531              }
 532          }
 533  
 534          // Bail if we've checked recently and if nothing has changed.
 535          if ( ! $theme_changed ) {
 536              return;
 537          }
 538      }
 539  
 540      // Update last_checked for current to prevent multiple blocking requests if request hangs.
 541      $last_update->last_checked = time();
 542      set_site_transient( 'update_themes', $last_update );
 543  
 544      $request['themes'] = $themes;
 545  
 546      $locales = array_values( get_available_languages() );
 547  
 548      /**
 549       * Filters the locales requested for theme translations.
 550       *
 551       * @since 3.7.0
 552       * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales.
 553       *
 554       * @param array $locales Theme locales. Default is all available locales of the site.
 555       */
 556      $locales = apply_filters( 'themes_update_check_locales', $locales );
 557      $locales = array_unique( $locales );
 558  
 559      if ( $doing_cron ) {
 560          $timeout = 30;
 561      } else {
 562          // Three seconds, plus one extra second for every 10 themes.
 563          $timeout = 3 + (int) ( count( $themes ) / 10 );
 564      }
 565  
 566      $options = array(
 567          'timeout'    => $timeout,
 568          'body'       => array(
 569              'themes'       => wp_json_encode( $request ),
 570              'translations' => wp_json_encode( $translations ),
 571              'locale'       => wp_json_encode( $locales ),
 572          ),
 573          'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
 574      );
 575  
 576      if ( $extra_stats ) {
 577          $options['body']['update_stats'] = wp_json_encode( $extra_stats );
 578      }
 579  
 580      $url      = 'http://api.wordpress.org/themes/update-check/1.1/';
 581      $http_url = $url;
 582      $ssl      = wp_http_supports( array( 'ssl' ) );
 583  
 584      if ( $ssl ) {
 585          $url = set_url_scheme( $url, 'https' );
 586      }
 587  
 588      $raw_response = wp_remote_post( $url, $options );
 589  
 590      if ( $ssl && is_wp_error( $raw_response ) ) {
 591          trigger_error(
 592              sprintf(
 593                  /* translators: %s: Support forums URL. */
 594                  __( '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>.' ),
 595                  __( 'https://wordpress.org/support/forums/' )
 596              ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 597              headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 598          );
 599          $raw_response = wp_remote_post( $http_url, $options );
 600      }
 601  
 602      if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
 603          return;
 604      }
 605  
 606      $new_update               = new stdClass;
 607      $new_update->last_checked = time();
 608      $new_update->checked      = $checked;
 609  
 610      $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
 611  
 612      if ( is_array( $response ) ) {
 613          $new_update->response     = $response['themes'];
 614          $new_update->translations = $response['translations'];
 615      }
 616  
 617      set_site_transient( 'update_themes', $new_update );
 618  }
 619  
 620  /**
 621   * Performs WordPress automatic background updates.
 622   *
 623   * @since 3.7.0
 624   */
 625  function wp_maybe_auto_update() {
 626      include_once  ABSPATH . 'wp-admin/includes/admin.php';
 627      require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
 628  
 629      $upgrader = new WP_Automatic_Updater;
 630      $upgrader->run();
 631  }
 632  
 633  /**
 634   * Retrieves a list of all language updates available.
 635   *
 636   * @since 3.7.0
 637   *
 638   * @return object[] Array of translation objects that have available updates.
 639   */
 640  function wp_get_translation_updates() {
 641      $updates    = array();
 642      $transients = array(
 643          'update_core'    => 'core',
 644          'update_plugins' => 'plugin',
 645          'update_themes'  => 'theme',
 646      );
 647  
 648      foreach ( $transients as $transient => $type ) {
 649          $transient = get_site_transient( $transient );
 650  
 651          if ( empty( $transient->translations ) ) {
 652              continue;
 653          }
 654  
 655          foreach ( $transient->translations as $translation ) {
 656              $updates[] = (object) $translation;
 657          }
 658      }
 659  
 660      return $updates;
 661  }
 662  
 663  /**
 664   * Collect counts and UI strings for available updates
 665   *
 666   * @since 3.3.0
 667   *
 668   * @return array
 669   */
 670  function wp_get_update_data() {
 671      $counts = array(
 672          'plugins'      => 0,
 673          'themes'       => 0,
 674          'wordpress'    => 0,
 675          'translations' => 0,
 676      );
 677  
 678      $plugins = current_user_can( 'update_plugins' );
 679  
 680      if ( $plugins ) {
 681          $update_plugins = get_site_transient( 'update_plugins' );
 682  
 683          if ( ! empty( $update_plugins->response ) ) {
 684              $counts['plugins'] = count( $update_plugins->response );
 685          }
 686      }
 687  
 688      $themes = current_user_can( 'update_themes' );
 689  
 690      if ( $themes ) {
 691          $update_themes = get_site_transient( 'update_themes' );
 692  
 693          if ( ! empty( $update_themes->response ) ) {
 694              $counts['themes'] = count( $update_themes->response );
 695          }
 696      }
 697  
 698      $core = current_user_can( 'update_core' );
 699  
 700      if ( $core && function_exists( 'get_core_updates' ) ) {
 701          $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
 702  
 703          if ( ! empty( $update_wordpress )
 704              && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
 705              && current_user_can( 'update_core' )
 706          ) {
 707              $counts['wordpress'] = 1;
 708          }
 709      }
 710  
 711      if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
 712          $counts['translations'] = 1;
 713      }
 714  
 715      $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
 716      $titles          = array();
 717  
 718      if ( $counts['wordpress'] ) {
 719          /* translators: %d: Number of available WordPress updates. */
 720          $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
 721      }
 722  
 723      if ( $counts['plugins'] ) {
 724          /* translators: %d: Number of available plugin updates. */
 725          $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
 726      }
 727  
 728      if ( $counts['themes'] ) {
 729          /* translators: %d: Number of available theme updates. */
 730          $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
 731      }
 732  
 733      if ( $counts['translations'] ) {
 734          $titles['translations'] = __( 'Translation Updates' );
 735      }
 736  
 737      $update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
 738  
 739      $update_data = array(
 740          'counts' => $counts,
 741          'title'  => $update_title,
 742      );
 743      /**
 744       * Filters the returned array of update data for plugins, themes, and WordPress core.
 745       *
 746       * @since 3.5.0
 747       *
 748       * @param array $update_data {
 749       *     Fetched update data.
 750       *
 751       *     @type array   $counts       An array of counts for available plugin, theme, and WordPress updates.
 752       *     @type string  $update_title Titles of available updates.
 753       * }
 754       * @param array $titles An array of update counts and UI strings for available updates.
 755       */
 756      return apply_filters( 'wp_get_update_data', $update_data, $titles );
 757  }
 758  
 759  /**
 760   * Determines whether core should be updated.
 761   *
 762   * @since 2.8.0
 763   *
 764   * @global string $wp_version The WordPress version string.
 765   */
 766  function _maybe_update_core() {
 767      // Include an unmodified $wp_version.
 768      require  ABSPATH . WPINC . '/version.php';
 769  
 770      $current = get_site_transient( 'update_core' );
 771  
 772      if ( isset( $current->last_checked, $current->version_checked )
 773          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 774          && $current->version_checked === $wp_version
 775      ) {
 776          return;
 777      }
 778  
 779      wp_version_check();
 780  }
 781  /**
 782   * Check the last time plugins were run before checking plugin versions.
 783   *
 784   * This might have been backported to WordPress 2.6.1 for performance reasons.
 785   * This is used for the wp-admin to check only so often instead of every page
 786   * load.
 787   *
 788   * @since 2.7.0
 789   * @access private
 790   */
 791  function _maybe_update_plugins() {
 792      $current = get_site_transient( 'update_plugins' );
 793  
 794      if ( isset( $current->last_checked )
 795          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 796      ) {
 797          return;
 798      }
 799  
 800      wp_update_plugins();
 801  }
 802  
 803  /**
 804   * Check themes versions only after a duration of time.
 805   *
 806   * This is for performance reasons to make sure that on the theme version
 807   * checker is not run on every page load.
 808   *
 809   * @since 2.7.0
 810   * @access private
 811   */
 812  function _maybe_update_themes() {
 813      $current = get_site_transient( 'update_themes' );
 814  
 815      if ( isset( $current->last_checked )
 816          && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
 817      ) {
 818          return;
 819      }
 820  
 821      wp_update_themes();
 822  }
 823  
 824  /**
 825   * Schedule core, theme, and plugin update checks.
 826   *
 827   * @since 3.1.0
 828   */
 829  function wp_schedule_update_checks() {
 830      if ( ! wp_next_scheduled( 'wp_version_check' ) && ! wp_installing() ) {
 831          wp_schedule_event( time(), 'twicedaily', 'wp_version_check' );
 832      }
 833  
 834      if ( ! wp_next_scheduled( 'wp_update_plugins' ) && ! wp_installing() ) {
 835          wp_schedule_event( time(), 'twicedaily', 'wp_update_plugins' );
 836      }
 837  
 838      if ( ! wp_next_scheduled( 'wp_update_themes' ) && ! wp_installing() ) {
 839          wp_schedule_event( time(), 'twicedaily', 'wp_update_themes' );
 840      }
 841  }
 842  
 843  /**
 844   * Clear existing update caches for plugins, themes, and core.
 845   *
 846   * @since 4.1.0
 847   */
 848  function wp_clean_update_cache() {
 849      if ( function_exists( 'wp_clean_plugins_cache' ) ) {
 850          wp_clean_plugins_cache();
 851      } else {
 852          delete_site_transient( 'update_plugins' );
 853      }
 854  
 855      wp_clean_themes_cache();
 856  
 857      delete_site_transient( 'update_core' );
 858  }
 859  
 860  if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
 861      return;
 862  }
 863  
 864  add_action( 'admin_init', '_maybe_update_core' );
 865  add_action( 'wp_version_check', 'wp_version_check' );
 866  
 867  add_action( 'load-plugins.php', 'wp_update_plugins' );
 868  add_action( 'load-update.php', 'wp_update_plugins' );
 869  add_action( 'load-update-core.php', 'wp_update_plugins' );
 870  add_action( 'admin_init', '_maybe_update_plugins' );
 871  add_action( 'wp_update_plugins', 'wp_update_plugins' );
 872  
 873  add_action( 'load-themes.php', 'wp_update_themes' );
 874  add_action( 'load-update.php', 'wp_update_themes' );
 875  add_action( 'load-update-core.php', 'wp_update_themes' );
 876  add_action( 'admin_init', '_maybe_update_themes' );
 877  add_action( 'wp_update_themes', 'wp_update_themes' );
 878  
 879  add_action( 'update_option_WPLANG', 'wp_clean_update_cache', 10, 0 );
 880  
 881  add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
 882  
 883  add_action( 'init', 'wp_schedule_update_checks' );


Generated: Mon May 25 01:00:03 2020 Cross-referenced by PHPXref 0.7.1