Skip to content

Commit

Permalink
Coding Standards: Fix WPCS issues in wp-includes/update.php.
Browse files Browse the repository at this point in the history
Props mukesh27.
Fixes #50130.

git-svn-id: https://develop.svn.wordpress.org/trunk@47778 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed May 9, 2020
1 parent efb6e80 commit 8e3b8c7
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions src/wp-includes/update.php
Expand Up @@ -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.
*/
function wp_version_check( $extra_stats = array(), $force_check = false ) {
global $wpdb, $wp_local_package;

if ( wp_installing() ) {
return;
}

global $wpdb, $wp_local_package;
// Include an unmodified $wp_version.
require ABSPATH . WPINC . '/version.php';
$php_version = phpversion();
Expand All @@ -35,7 +36,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
$translations = wp_get_installed_translations( 'core' );

// 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;
}

Expand All @@ -52,6 +53,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
// Wait 1 minute between multiple version check requests.
$timeout = MINUTE_IN_SECONDS;
$time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked );

if ( ! $force_check && $time_not_changed ) {
return;
}
Expand Down Expand Up @@ -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, '&' );
$http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) );

if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}
Expand All @@ -152,6 +155,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
);

$response = wp_remote_post( $url, $options );

if ( $ssl && is_wp_error( $response ) ) {
trigger_error(
sprintf(
Expand All @@ -164,7 +168,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {
$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;
}

Expand All @@ -179,12 +183,12 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {

foreach ( $offers as &$offer ) {
foreach ( $offer as $offer_key => $value ) {
if ( 'packages' == $offer_key ) {
if ( 'packages' === $offer_key ) {
$offer['packages'] = (object) array_intersect_key(
array_map( 'esc_url', $offer['packages'] ),
array_fill_keys( array( 'full', 'no_content', 'new_bundled', 'partial', 'rollback' ), '' )
);
} elseif ( 'download' == $offer_key ) {
} elseif ( 'download' === $offer_key ) {
$offer['download'] = esc_url( $value );
} else {
$offer[ $offer_key ] = esc_html( $value );
Expand Down Expand Up @@ -226,6 +230,7 @@ function wp_version_check( $extra_stats = array(), $force_check = false ) {

if ( ! empty( $body['ttl'] ) ) {
$ttl = (int) $body['ttl'];

if ( $ttl && ( time() + $ttl < wp_next_scheduled( 'wp_version_check' ) ) ) {
// Queue an event to re-run the update check in $ttl seconds.
wp_schedule_single_event( time() + $ttl, 'wp_version_check' );
Expand Down Expand Up @@ -273,6 +278,7 @@ function wp_update_plugins( $extra_stats = array() ) {

$active = get_option( 'active_plugins', array() );
$current = get_site_transient( 'update_plugins' );

if ( ! is_object( $current ) ) {
$current = new stdClass;
}
Expand Down Expand Up @@ -306,6 +312,7 @@ function wp_update_plugins( $extra_stats = array() ) {

if ( $time_not_changed && ! $extra_stats ) {
$plugin_changed = false;

foreach ( $plugins as $file => $p ) {
$new_option->checked[ $file ] = $p['Version'];

Expand Down Expand Up @@ -373,11 +380,13 @@ function wp_update_plugins( $extra_stats = array() ) {
$url = 'http://api.wordpress.org/plugins/update-check/1.1/';
$http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) );

if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}

$raw_response = wp_remote_post( $url, $options );

if ( $ssl && is_wp_error( $raw_response ) ) {
trigger_error(
sprintf(
Expand All @@ -390,24 +399,30 @@ function wp_update_plugins( $extra_stats = array() ) {
$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;
}

$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );

foreach ( $response['plugins'] as &$plugin ) {
$plugin = (object) $plugin;

if ( isset( $plugin->compatibility ) ) {
$plugin->compatibility = (object) $plugin->compatibility;

foreach ( $plugin->compatibility as &$data ) {
$data = (object) $data;
}
}
}

unset( $plugin, $data );

foreach ( $response['no_update'] as &$plugin ) {
$plugin = (object) $plugin;
}

unset( $plugin );

if ( is_array( $response ) ) {
Expand Down Expand Up @@ -448,6 +463,7 @@ function wp_update_themes( $extra_stats = array() ) {
$translations = wp_get_installed_translations( 'themes' );

$last_update = get_site_transient( 'update_themes' );

if ( ! is_object( $last_update ) ) {
$last_update = new stdClass;
}
Expand Down Expand Up @@ -499,6 +515,7 @@ function wp_update_themes( $extra_stats = array() ) {

if ( $time_not_changed && ! $extra_stats ) {
$theme_changed = false;

foreach ( $checked as $slug => $v ) {
if ( ! isset( $last_update->checked[ $slug ] ) || strval( $last_update->checked[ $slug ] ) !== strval( $v ) ) {
$theme_changed = true;
Expand Down Expand Up @@ -563,11 +580,13 @@ function wp_update_themes( $extra_stats = array() ) {
$url = 'http://api.wordpress.org/themes/update-check/1.1/';
$http_url = $url;
$ssl = wp_http_supports( array( 'ssl' ) );

if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}

$raw_response = wp_remote_post( $url, $options );

if ( $ssl && is_wp_error( $raw_response ) ) {
trigger_error(
sprintf(
Expand All @@ -580,7 +599,7 @@ function wp_update_themes( $extra_stats = array() ) {
$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;
}

Expand Down Expand Up @@ -625,8 +644,10 @@ function wp_get_translation_updates() {
'update_plugins' => 'plugin',
'update_themes' => 'theme',
);

foreach ( $transients as $transient => $type ) {
$transient = get_site_transient( $transient );

if ( empty( $transient->translations ) ) {
continue;
}
Expand All @@ -635,6 +656,7 @@ function wp_get_translation_updates() {
$updates[] = (object) $translation;
}
}

return $updates;
}

Expand All @@ -654,24 +676,30 @@ function wp_get_update_data() {
);

$plugins = current_user_can( 'update_plugins' );

if ( $plugins ) {
$update_plugins = get_site_transient( 'update_plugins' );

if ( ! empty( $update_plugins->response ) ) {
$counts['plugins'] = count( $update_plugins->response );
}
}

$themes = current_user_can( 'update_themes' );

if ( $themes ) {
$update_themes = get_site_transient( 'update_themes' );

if ( ! empty( $update_themes->response ) ) {
$counts['themes'] = count( $update_themes->response );
}
}

$core = current_user_can( 'update_core' );

if ( $core && function_exists( 'get_core_updates' ) ) {
$update_wordpress = get_core_updates( array( 'dismissed' => false ) );

if ( ! empty( $update_wordpress )
&& ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
&& current_user_can( 'update_core' )
Expand All @@ -686,18 +714,22 @@ function wp_get_update_data() {

$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
$titles = array();

if ( $counts['wordpress'] ) {
/* translators: %d: Number of available WordPress updates. */
$titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
}

if ( $counts['plugins'] ) {
/* translators: %d: Number of available plugin updates. */
$titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
}

if ( $counts['themes'] ) {
/* translators: %d: Number of available theme updates. */
$titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
}

if ( $counts['translations'] ) {
$titles['translations'] = __( 'Translation Updates' );
}
Expand Down Expand Up @@ -737,11 +769,13 @@ function _maybe_update_core() {

$current = get_site_transient( 'update_core' );

if ( isset( $current->last_checked, $current->version_checked ) &&
12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) &&
$current->version_checked == $wp_version ) {
if ( isset( $current->last_checked, $current->version_checked )
&& 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked )
&& $current->version_checked === $wp_version
) {
return;
}

wp_version_check();
}
/**
Expand All @@ -756,9 +790,13 @@ function _maybe_update_core() {
*/
function _maybe_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;
}

wp_update_plugins();
}

Expand All @@ -773,9 +811,13 @@ function _maybe_update_plugins() {
*/
function _maybe_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;
}

wp_update_themes();
}

Expand Down Expand Up @@ -809,7 +851,9 @@ function wp_clean_update_cache() {
} else {
delete_site_transient( 'update_plugins' );
}

wp_clean_themes_cache();

delete_site_transient( 'update_core' );
}

Expand Down

0 comments on commit 8e3b8c7

Please sign in to comment.