Coding Standards: Fix WPCS issues in `wp-includes/update.php`.

Props mukesh27.
Fixes #50130.

git-svn-id: https://develop.svn.wordpress.org/trunk@47778 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-05-09 12:50:43 +00:00
parent efb6e805da
commit 8e3b8c73d5
1 changed files with 56 additions and 12 deletions

View File

@ -22,11 +22,12 @@
* @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. * @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.
*/ */
function wp_version_check( $extra_stats = array(), $force_check = false ) { function wp_version_check( $extra_stats = array(), $force_check = false ) {
global $wpdb, $wp_local_package;
if ( wp_installing() ) { if ( wp_installing() ) {
return; return;
} }
global $wpdb, $wp_local_package;
// Include an unmodified $wp_version. // Include an unmodified $wp_version.
require ABSPATH . WPINC . '/version.php'; require ABSPATH . WPINC . '/version.php';
$php_version = phpversion(); $php_version = phpversion();
@ -35,7 +36,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
$translations = wp_get_installed_translations( 'core' ); $translations = wp_get_installed_translations( 'core' );
// Invalidate the transient when $wp_version changes. // Invalidate the transient when $wp_version changes.
if ( is_object( $current ) && $wp_version != $current->version_checked ) { if ( is_object( $current ) && $wp_version !== $current->version_checked ) {
$current = false; $current = false;
} }
@ -52,6 +53,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
// Wait 1 minute between multiple version check requests. // Wait 1 minute between multiple version check requests.
$timeout = MINUTE_IN_SECONDS; $timeout = MINUTE_IN_SECONDS;
$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked ); $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );
if ( ! $force_check && $time_not_changed ) { if ( ! $force_check && $time_not_changed ) {
return; return;
} }
@ -135,6 +137,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
$url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' ); $url = 'http://api.wordpress.org/core/version-check/1.7/?' . http_build_query( $query, null, '&' );
$http_url = $url; $http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) ); $ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) { if ( $ssl ) {
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
} }
@ -152,6 +155,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
); );
$response = wp_remote_post( $url, $options ); $response = wp_remote_post( $url, $options );
if ( $ssl && is_wp_error( $response ) ) { if ( $ssl && is_wp_error( $response ) ) {
trigger_error( trigger_error(
sprintf( sprintf(
@ -164,7 +168,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
$response = wp_remote_post( $http_url, $options ); $response = wp_remote_post( $http_url, $options );
} }
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return; return;
} }
@ -179,12 +183,12 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
foreach ( $offers as &$offer ) { foreach ( $offers as &$offer ) {
foreach ( $offer as $offer_key => $value ) { foreach ( $offer as $offer_key => $value ) {
if ( 'packages' == $offer_key ) { if ( 'packages' === $offer_key ) {
$offer['packages'] = (object) array_intersect_key( $offer['packages'] = (object) array_intersect_key(
array_map( 'esc_url', $offer['packages'] ), array_map( 'esc_url', $offer['packages'] ),
array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' ) array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
); );
} elseif ( 'download' == $offer_key ) { } elseif ( 'download' === $offer_key ) {
$offer['download'] = esc_url( $value ); $offer['download'] = esc_url( $value );
} else { } else {
$offer[ $offer_key ] = esc_html( $value ); $offer[ $offer_key ] = esc_html( $value );
@ -226,6 +230,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
if ( ! empty( $body['ttl'] ) ) { if ( ! empty( $body['ttl'] ) ) {
$ttl = (int) $body['ttl']; $ttl = (int) $body['ttl'];
if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) { if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
// Queue an event to re-run the update check in $ttl seconds. // Queue an event to re-run the update check in $ttl seconds.
wp_schedule_single_event( time() + $ttl, 'wp_version_check' ); wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
@ -273,6 +278,7 @@ function wp_update_plugins( $extra_stats = array() ) {
$active = get_option( 'active_plugins', array() ); $active = get_option( 'active_plugins', array() );
$current = get_site_transient( 'update_plugins' ); $current = get_site_transient( 'update_plugins' );
if ( ! is_object( $current ) ) { if ( ! is_object( $current ) ) {
$current = new stdClass; $current = new stdClass;
} }
@ -306,6 +312,7 @@ function wp_update_plugins( $extra_stats = array() ) {
if ( $time_not_changed && ! $extra_stats ) { if ( $time_not_changed && ! $extra_stats ) {
$plugin_changed = false; $plugin_changed = false;
foreach ( $plugins as $file => $p ) { foreach ( $plugins as $file => $p ) {
$new_option->checked[ $file ] = $p['Version']; $new_option->checked[ $file ] = $p['Version'];
@ -373,11 +380,13 @@ function wp_update_plugins( $extra_stats = array() ) {
$url = 'http://api.wordpress.org/plugins/update-check/1.1/'; $url = 'http://api.wordpress.org/plugins/update-check/1.1/';
$http_url = $url; $http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) ); $ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) { if ( $ssl ) {
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
} }
$raw_response = wp_remote_post( $url, $options ); $raw_response = wp_remote_post( $url, $options );
if ( $ssl && is_wp_error( $raw_response ) ) { if ( $ssl && is_wp_error( $raw_response ) ) {
trigger_error( trigger_error(
sprintf( sprintf(
@ -390,24 +399,30 @@ function wp_update_plugins( $extra_stats = array() ) {
$raw_response = wp_remote_post( $http_url, $options ); $raw_response = wp_remote_post( $http_url, $options );
} }
if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) { if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
return; return;
} }
$response = json_decode( wp_remote_retrieve_body( $raw_response ), true ); $response = json_decode( wp_remote_retrieve_body( $raw_response ), true );
foreach ( $response['plugins'] as &$plugin ) { foreach ( $response['plugins'] as &$plugin ) {
$plugin = (object) $plugin; $plugin = (object) $plugin;
if ( isset( $plugin->compatibility ) ) { if ( isset( $plugin->compatibility ) ) {
$plugin->compatibility = (object) $plugin->compatibility; $plugin->compatibility = (object) $plugin->compatibility;
foreach ( $plugin->compatibility as &$data ) { foreach ( $plugin->compatibility as &$data ) {
$data = (object) $data; $data = (object) $data;
} }
} }
} }
unset( $plugin, $data ); unset( $plugin, $data );
foreach ( $response['no_update'] as &$plugin ) { foreach ( $response['no_update'] as &$plugin ) {
$plugin = (object) $plugin; $plugin = (object) $plugin;
} }
unset( $plugin ); unset( $plugin );
if ( is_array( $response ) ) { if ( is_array( $response ) ) {
@ -448,6 +463,7 @@ function wp_update_themes( $extra_stats = array() ) {
$translations = wp_get_installed_translations( 'themes' ); $translations = wp_get_installed_translations( 'themes' );
$last_update = get_site_transient( 'update_themes' ); $last_update = get_site_transient( 'update_themes' );
if ( ! is_object( $last_update ) ) { if ( ! is_object( $last_update ) ) {
$last_update = new stdClass; $last_update = new stdClass;
} }
@ -499,6 +515,7 @@ function wp_update_themes( $extra_stats = array() ) {
if ( $time_not_changed && ! $extra_stats ) { if ( $time_not_changed && ! $extra_stats ) {
$theme_changed = false; $theme_changed = false;
foreach ( $checked as $slug => $v ) { foreach ( $checked as $slug => $v ) {
if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) { if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
$theme_changed = true; $theme_changed = true;
@ -563,11 +580,13 @@ function wp_update_themes( $extra_stats = array() ) {
$url = 'http://api.wordpress.org/themes/update-check/1.1/'; $url = 'http://api.wordpress.org/themes/update-check/1.1/';
$http_url = $url; $http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) ); $ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) { if ( $ssl ) {
$url = set_url_scheme( $url, 'https' ); $url = set_url_scheme( $url, 'https' );
} }
$raw_response = wp_remote_post( $url, $options ); $raw_response = wp_remote_post( $url, $options );
if ( $ssl && is_wp_error( $raw_response ) ) { if ( $ssl && is_wp_error( $raw_response ) ) {
trigger_error( trigger_error(
sprintf( sprintf(
@ -580,7 +599,7 @@ function wp_update_themes( $extra_stats = array() ) {
$raw_response = wp_remote_post( $http_url, $options ); $raw_response = wp_remote_post( $http_url, $options );
} }
if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) { if ( is_wp_error( $raw_response ) || 200 !== wp_remote_retrieve_response_code( $raw_response ) ) {
return; return;
} }
@ -625,8 +644,10 @@ function wp_get_translation_updates() {
'update_plugins' => 'plugin', 'update_plugins' => 'plugin',
'update_themes' => 'theme', 'update_themes' => 'theme',
); );
foreach ( $transients as $transient => $type ) { foreach ( $transients as $transient => $type ) {
$transient = get_site_transient( $transient ); $transient = get_site_transient( $transient );
if ( empty( $transient->translations ) ) { if ( empty( $transient->translations ) ) {
continue; continue;
} }
@ -635,6 +656,7 @@ function wp_get_translation_updates() {
$updates[] = (object) $translation; $updates[] = (object) $translation;
} }
} }
return $updates; return $updates;
} }
@ -654,24 +676,30 @@ function wp_get_update_data() {
); );
$plugins = current_user_can( 'update_plugins' ); $plugins = current_user_can( 'update_plugins' );
if ( $plugins ) { if ( $plugins ) {
$update_plugins = get_site_transient( 'update_plugins' ); $update_plugins = get_site_transient( 'update_plugins' );
if ( ! empty( $update_plugins->response ) ) { if ( ! empty( $update_plugins->response ) ) {
$counts['plugins'] = count( $update_plugins->response ); $counts['plugins'] = count( $update_plugins->response );
} }
} }
$themes = current_user_can( 'update_themes' ); $themes = current_user_can( 'update_themes' );
if ( $themes ) { if ( $themes ) {
$update_themes = get_site_transient( 'update_themes' ); $update_themes = get_site_transient( 'update_themes' );
if ( ! empty( $update_themes->response ) ) { if ( ! empty( $update_themes->response ) ) {
$counts['themes'] = count( $update_themes->response ); $counts['themes'] = count( $update_themes->response );
} }
} }
$core = current_user_can( 'update_core' ); $core = current_user_can( 'update_core' );
if ( $core && function_exists( 'get_core_updates' ) ) { if ( $core && function_exists( 'get_core_updates' ) ) {
$update_wordpress = get_core_updates( array( 'dismissed' => false ) ); $update_wordpress = get_core_updates( array( 'dismissed' => false ) );
if ( ! empty( $update_wordpress ) if ( ! empty( $update_wordpress )
&& ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true ) && ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
&& current_user_can( 'update_core' ) && current_user_can( 'update_core' )
@ -686,18 +714,22 @@ function wp_get_update_data() {
$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations']; $counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
$titles = array(); $titles = array();
if ( $counts['wordpress'] ) { if ( $counts['wordpress'] ) {
/* translators: %d: Number of available WordPress updates. */ /* translators: %d: Number of available WordPress updates. */
$titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] ); $titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
} }
if ( $counts['plugins'] ) { if ( $counts['plugins'] ) {
/* translators: %d: Number of available plugin updates. */ /* translators: %d: Number of available plugin updates. */
$titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] ); $titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
} }
if ( $counts['themes'] ) { if ( $counts['themes'] ) {
/* translators: %d: Number of available theme updates. */ /* translators: %d: Number of available theme updates. */
$titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] ); $titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
} }
if ( $counts['translations'] ) { if ( $counts['translations'] ) {
$titles['translations'] = __( 'Translation Updates' ); $titles['translations'] = __( 'Translation Updates' );
} }
@ -737,11 +769,13 @@ function _maybe_update_core() {
$current = get_site_transient( 'update_core' ); $current = get_site_transient( 'update_core' );
if ( isset( $current->last_checked, $current->version_checked ) && if ( isset( $current->last_checked, $current->version_checked )
12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) && && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
$current->version_checked == $wp_version ) { && $current->version_checked === $wp_version
) {
return; return;
} }
wp_version_check(); wp_version_check();
} }
/** /**
@ -756,9 +790,13 @@ function _maybe_update_core() {
*/ */
function _maybe_update_plugins() { function _maybe_update_plugins() {
$current = get_site_transient( 'update_plugins' ); $current = get_site_transient( 'update_plugins' );
if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
if ( isset( $current->last_checked )
&& 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
) {
return; return;
} }
wp_update_plugins(); wp_update_plugins();
} }
@ -773,9 +811,13 @@ function _maybe_update_plugins() {
*/ */
function _maybe_update_themes() { function _maybe_update_themes() {
$current = get_site_transient( 'update_themes' ); $current = get_site_transient( 'update_themes' );
if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ) {
if ( isset( $current->last_checked )
&& 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
) {
return; return;
} }
wp_update_themes(); wp_update_themes();
} }
@ -809,7 +851,9 @@ function wp_clean_update_cache() {
} else { } else {
delete_site_transient( 'update_plugins' ); delete_site_transient( 'update_plugins' );
} }
wp_clean_themes_cache(); wp_clean_themes_cache();
delete_site_transient( 'update_core' ); delete_site_transient( 'update_core' );
} }