Upgrade/Install: Simplify compatibility checks for uploaded plugins and themes for better readability.

Use `$new_plugin_data` and `$new_theme_data` as a shorthand for the corresponding `$this->upgrader` properties.

Follow-up to [48390], [48448].

Props afragen.
See #9757.

git-svn-id: https://develop.svn.wordpress.org/trunk@48455 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-13 13:33:32 +00:00
parent dd50193d6a
commit 4171c2485c
3 changed files with 38 additions and 35 deletions

View File

@ -186,7 +186,9 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
$folder = ltrim( substr( $folder, strlen( WP_PLUGIN_DIR ) ), '/' );
$current_plugin_data = false;
foreach ( get_plugins() as $plugin => $plugin_data ) {
$all_plugins = get_plugins();
foreach ( $all_plugins as $plugin => $plugin_data ) {
if ( strrpos( $plugin, $folder ) !== 0 ) {
continue;
}
@ -194,13 +196,15 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
$current_plugin_data = $plugin_data;
}
if ( empty( $current_plugin_data ) || empty( $this->upgrader->new_plugin_data ) ) {
$new_plugin_data = $this->upgrader->new_plugin_data;
if ( ! $current_plugin_data || ! $new_plugin_data ) {
return false;
}
echo '<h2 class="update-from-upload-heading">' . esc_html( __( 'This plugin is already installed.' ) ) . '</h2>';
$this->is_downgrading = version_compare( $current_plugin_data['Version'], $this->upgrader->new_plugin_data['Version'], '>' );
$this->is_downgrading = version_compare( $current_plugin_data['Version'], $new_plugin_data['Version'], '>' );
$rows = array(
'Name' => __( 'Plugin name' ),
@ -218,7 +222,7 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
foreach ( $rows as $field => $label ) {
$old_value = ! empty( $current_plugin_data[ $field ] ) ? (string) $current_plugin_data[ $field ] : '-';
$new_value = ! empty( $this->upgrader->new_plugin_data[ $field ] ) ? (string) $this->upgrader->new_plugin_data[ $field ] : '-';
$new_value = ! empty( $new_plugin_data[ $field ] ) ? (string) $new_plugin_data[ $field ] : '-';
$is_same_plugin = $is_same_plugin && ( $old_value === $new_value );
@ -241,7 +245,7 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
* @param array $current_plugin_data Array with current plugin data.
* @param array $new_plugin_data Array with uploaded plugin data.
*/
echo apply_filters( 'install_plugin_ovewrite_comparison', $table, $current_plugin_data, $this->upgrader->new_plugin_data );
echo apply_filters( 'install_plugin_ovewrite_comparison', $table, $current_plugin_data, $new_plugin_data );
$install_actions = array();
$can_update = true;
@ -249,28 +253,27 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
$blocked_message = '<p>' . esc_html( __( 'The plugin cannot be updated due to the following:' ) ) . '</p>';
$blocked_message .= '<ul class="ul-disc">';
if ( ! empty( $this->upgrader->new_plugin_data['RequiresPHP'] )
&& ! is_php_version_compatible( $this->upgrader->new_plugin_data['RequiresPHP'] )
) {
$requires_php = isset( $new_plugin_data['RequiresPHP'] ) ? $new_plugin_data['RequiresPHP'] : null;
$requires_wp = isset( $new_plugin_data['RequiresWP'] ) ? $new_plugin_data['RequiresWP'] : null;
if ( ! is_php_version_compatible( $requires_php ) ) {
$error = sprintf(
/* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
__( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
phpversion(),
$this->upgrader->new_plugin_data['RequiresPHP']
$requires_php
);
$blocked_message .= '<li>' . esc_html( $error ) . '</li>';
$can_update = false;
}
if ( ! empty( $this->upgrader->new_plugin_data['RequiresWP'] )
&& ! is_wp_version_compatible( $this->upgrader->new_plugin_data['RequiresWP'] )
) {
if ( ! is_wp_version_compatible( $requires_wp ) ) {
$error = sprintf(
/* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
__( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
$GLOBALS['wp_version'],
$this->upgrader->new_plugin_data['RequiresWP']
get_bloginfo( 'version' ),
$requires_wp
);
$blocked_message .= '<li>' . esc_html( $error ) . '</li>';
@ -324,7 +327,7 @@ class Plugin_Installer_Skin extends WP_Upgrader_Skin {
* @param object $api Object containing WordPress.org API plugin data.
* @param array $new_plugin_data Array with uploaded plugin data.
*/
$install_actions = apply_filters( 'install_plugin_ovewrite_actions', $install_actions, $this->api, $this->upgrader->new_plugin_data );
$install_actions = apply_filters( 'install_plugin_ovewrite_actions', $install_actions, $this->api, $new_plugin_data );
if ( ! empty( $install_actions ) ) {
printf(

View File

@ -207,7 +207,9 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
$current_theme_data = $theme;
}
if ( empty( $current_theme_data ) || empty( $this->upgrader->new_theme_data ) ) {
$new_theme_data = $this->upgrader->new_theme_data;
if ( ! $current_theme_data || ! $new_theme_data ) {
return false;
}
@ -218,11 +220,11 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
$this->feedback( 'current_theme_has_errors', $current_theme_data->errors()->get_error_message() );
}
$this->is_downgrading = version_compare( $current_theme_data['Version'], $this->upgrader->new_theme_data['Version'], '>' );
$this->is_downgrading = version_compare( $current_theme_data['Version'], $new_theme_data['Version'], '>' );
$is_invalid_parent = false;
if ( ! empty( $this->upgrader->new_theme_data['Template'] ) ) {
$is_invalid_parent = ! in_array( $this->upgrader->new_theme_data['Template'], array_keys( $all_themes ), true );
if ( ! empty( $new_theme_data['Template'] ) ) {
$is_invalid_parent = ! in_array( $new_theme_data['Template'], array_keys( $all_themes ), true );
}
$rows = array(
@ -243,7 +245,7 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
$old_value = $current_theme_data->display( $field, false );
$old_value = $old_value ? (string) $old_value : '-';
$new_value = ! empty( $this->upgrader->new_theme_data[ $field ] ) ? (string) $this->upgrader->new_theme_data[ $field ] : '-';
$new_value = ! empty( $new_theme_data[ $field ] ) ? (string) $new_theme_data[ $field ] : '-';
if ( $old_value === $new_value && '-' === $new_value && 'Template' === $field ) {
continue;
@ -276,7 +278,7 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
* @param array $current_theme_data Array with current theme data.
* @param array $new_theme_data Array with uploaded theme data.
*/
echo apply_filters( 'install_theme_overwrite_comparison', $table, $current_theme_data, $this->upgrader->new_theme_data );
echo apply_filters( 'install_theme_overwrite_comparison', $table, $current_theme_data, $new_theme_data );
$install_actions = array();
$can_update = true;
@ -284,28 +286,27 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
$blocked_message = '<p>' . esc_html( __( 'The theme cannot be updated due to the following:' ) ) . '</p>';
$blocked_message .= '<ul class="ul-disc">';
if ( ! empty( $this->upgrader->new_theme_data['RequiresPHP'] )
&& ! is_php_version_compatible( $this->upgrader->new_theme_data['RequiresPHP'] )
) {
$requires_php = isset( $new_theme_data['RequiresPHP'] ) ? $new_theme_data['RequiresPHP'] : null;
$requires_wp = isset( $new_theme_data['RequiresWP'] ) ? $new_theme_data['RequiresWP'] : null;
if ( ! is_php_version_compatible( $requires_php ) ) {
$error = sprintf(
/* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
__( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
phpversion(),
$this->upgrader->new_theme_data['RequiresPHP']
$requires_php
);
$blocked_message .= '<li>' . esc_html( $error ) . '</li>';
$can_update = false;
}
if ( ! empty( $this->upgrader->new_theme_data['RequiresWP'] )
&& ! is_wp_version_compatible( $this->upgrader->new_theme_data['RequiresWP'] )
) {
if ( ! is_wp_version_compatible( $requires_wp ) ) {
$error = sprintf(
/* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
__( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
$GLOBALS['wp_version'],
$this->upgrader->new_theme_data['RequiresWP']
get_bloginfo( 'version' ),
$requires_wp
);
$blocked_message .= '<li>' . esc_html( $error ) . '</li>';
@ -359,7 +360,7 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
* @param object $api Object containing WordPress.org API theme data.
* @param array $new_theme_data Array with uploaded theme data.
*/
$install_actions = apply_filters( 'install_theme_ovewrite_actions', $install_actions, $this->api, $this->upgrader->new_theme_data );
$install_actions = apply_filters( 'install_theme_ovewrite_actions', $install_actions, $this->api, $new_theme_data );
if ( ! empty( $install_actions ) ) {
printf(
@ -371,5 +372,4 @@ class Theme_Installer_Skin extends WP_Upgrader_Skin {
return true;
}
}

View File

@ -273,9 +273,9 @@ class Theme_Upgrader extends WP_Upgrader {
*
* @since 5.5.0
*
* @param string $package The package file.
* @param array $new_plugin_data The new theme data.
* @param string $package_type The package type (plugin or theme).
* @param string $package The package file.
* @param array $new_theme_data The new theme data.
* @param string $package_type The package type (plugin or theme).
*/
do_action( 'upgrader_overwrote_package', $package, $this->new_theme_data, 'theme' );
}