Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check WordPress and PHP requirements before installing a theme or plugin #436

Merged
merged 10 commits into from
Dec 13, 2024
32 changes: 32 additions & 0 deletions features/plugin-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,35 @@ Feature: Install WordPress plugins
Plugin 'site-secrets' activated.
Success: Plugin already installed.
"""

Scenario: Can't install plugin that requires a newer version of WordPress
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
Given a WP install

When I run `wp core download --version=6.4 --force`
And I run `rm -r wp-content/themes/*`

And I try `wp plugin install wp-super-cache`
Then STDERR should contain:
"""
Warning: wp-super-cache: This plugin does not work with your version of WordPress
"""

And STDERR should contain:
"""
Error: No plugins installed.
"""

@less-than-php-7.4 @require-wp-6.6
Scenario: Can't install plugin that requires a newer version of PHP
Given a WP install

And I try `wp plugin install contact-form-7`
Then STDERR should contain:
"""
Warning: contact-form-7: This plugin does not work with your version of PHP
"""

And STDERR should contain:
"""
Error: No plugins installed.
"""
32 changes: 32 additions & 0 deletions features/theme-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,35 @@ Feature: Install WordPress themes
"""
twentyeleven
"""

Scenario: Can't install theme that requires a newer version of WordPress
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
Given a WP install

When I run `wp core download --version=6.4 --force`
And I run `rm -r wp-content/themes/*`

And I try `wp theme install twentytwentyfive`
Then STDERR should contain:
"""
Warning: twentytwentyfive: This theme does not work with your version of WordPress.
"""

And STDERR should contain:
"""
Error: No themes installed.
"""

@less-than-php-7.4 @require-wp-5.6
mrsdizzie marked this conversation as resolved.
Show resolved Hide resolved
Scenario: Can't install theme that requires a newer version of PHP
Given a WP install

And I try `wp theme install oceanwp`
Then STDERR should contain:
"""
Warning: oceanwp: This theme does not work with your version of PHP.
"""

And STDERR should contain:
"""
Error: No themes installed.
"""
19 changes: 19 additions & 0 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@ public function path( $args, $assoc_args ) {
}

protected function install_from_repo( $slug, $assoc_args ) {
global $wp_version;
// Extract the major WordPress version (e.g., "6.3") from the full version string
list($wp_core_version) = explode( '-', $wp_version );
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );

$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );

if ( is_wp_error( $api ) ) {
Expand All @@ -590,6 +595,20 @@ protected function install_from_repo( $slug, $assoc_args ) {

if ( isset( $assoc_args['version'] ) ) {
self::alter_api_response( $api, $assoc_args['version'] );
} else {
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
$requires_wp = isset( $api->requires ) ? $api->requires : null;

$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
$compatible_wp = empty( $requires_wp ) || version_compare( $wp_core_version, $requires_wp, '>=' );

if ( ! $compatible_wp ) {
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
}

if ( ! $compatible_php ) {
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of PHP. Minimum PHP required is $compatible_php" );
}
}

$status = install_plugin_install_status( $api );
Expand Down
19 changes: 19 additions & 0 deletions src/Theme_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ public function path( $args, $assoc_args ) {
}

protected function install_from_repo( $slug, $assoc_args ) {
global $wp_version;
// Extract the major WordPress version (e.g., "6.3") from the full version string
list($wp_core_version) = explode( '-', $wp_version );
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );

$api = themes_api( 'theme_information', array( 'slug' => $slug ) );

if ( is_wp_error( $api ) ) {
Expand All @@ -403,6 +408,20 @@ protected function install_from_repo( $slug, $assoc_args ) {

if ( isset( $assoc_args['version'] ) ) {
self::alter_api_response( $api, $assoc_args['version'] );
} else {
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
$requires_wp = isset( $api->requires ) ? $api->requires : null;

$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
$compatible_wp = empty( $requires_wp ) || version_compare( $wp_core_version, $requires_wp, '>=' );

if ( ! $compatible_wp ) {
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
}

if ( ! $compatible_php ) {
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of PHP. Minimum PHP required is $requires_php" );
}
}

if ( ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
Expand Down
Loading