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
2 changes: 1 addition & 1 deletion features/plugin-auto-updates-disable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Disable auto-updates for WordPress plugins

Background:
Given a WP install
And I run `wp plugin install duplicate-post`
And I run `wp plugin install duplicate-post --ignore-requirements`
And I run `wp plugin auto-updates enable --all`

@require-wp-5.5
Expand Down
2 changes: 1 addition & 1 deletion features/plugin-auto-updates-enable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Enable auto-updates for WordPress plugins

Background:
Given a WP install
And I run `wp plugin install duplicate-post`
And I run `wp plugin install duplicate-post --ignore-requirements`

@require-wp-5.5
Scenario: Show an error if required params are missing
Expand Down
2 changes: 1 addition & 1 deletion features/plugin-auto-updates-status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: Show the status of auto-updates for WordPress plugins

Background:
Given a WP install
And I run `wp plugin install duplicate-post`
And I run `wp plugin install duplicate-post --ignore-requirements`

@require-wp-5.5
Scenario: Show an error if required params are missing
Expand Down
35 changes: 34 additions & 1 deletion features/plugin-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Feature: Install WordPress plugins
When I run `rm wp-content/plugins/akismet/akismet.php`
Then the return code should be 0

When I try `wp plugin install akismet`
When I try `wp plugin install akismet --ignore-requirements`
Then STDERR should contain:
"""
Warning: Destination folder already exists. "{WORKING_DIR}/wp-content/plugins/akismet/"
Expand Down Expand Up @@ -237,3 +237,36 @@ Feature: Install WordPress plugins
Plugin 'site-secrets' activated.
Success: Plugin already installed.
"""

@require-php-7
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.
"""
8 changes: 7 additions & 1 deletion features/plugin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Feature: Manage WordPress plugins
When I run `wp plugin activate akismet hello`
Then STDOUT should not be empty

When I run `wp plugin install wordpress-importer`
When I run `wp plugin install wordpress-importer --ignore-requirements`
Then STDOUT should not be empty

When I run `wp plugin activate network-only`
Expand Down Expand Up @@ -373,6 +373,8 @@ Feature: Manage WordPress plugins
| name | status | update |
| wordpress-importer | inactive | none |

# WordPress Importer requires WP 5.2.
@require-wp-5.2
Scenario: Install a plugin when directory doesn't yet exist
Given a WP install

Expand Down Expand Up @@ -448,6 +450,8 @@ Feature: Manage WordPress plugins
must-use
"""

# WordPress Importer requires WP 5.2.
@require-wp-5.2
Scenario: Deactivate and uninstall a plugin, part one
Given a WP install
And these installed and active plugins:
Expand All @@ -472,6 +476,8 @@ Feature: Manage WordPress plugins
And STDOUT should be empty
And the return code should be 1

# WordPress Importer requires WP 5.2.
@require-wp-5.2
Scenario: Deactivate and uninstall a plugin, part two
Given a WP install
And these installed and active plugins:
Expand Down
33 changes: 33 additions & 0 deletions features/theme-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,36 @@ Feature: Install WordPress themes
"""
twentyeleven
"""

@require-php-7
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.
"""
23 changes: 23 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'] );
} elseif ( ! Utils\get_flag_value( $assoc_args, 'ignore-requirements', false ) ) {
$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 Expand Up @@ -896,6 +915,10 @@ protected function filter_item_list( $items, $args ) {
* : If set, the command will overwrite any installed version of the plugin, without prompting
* for confirmation.
*
* [--ignore-requirements]
* :If set, the command will install the plugin while ignoring any WordPress or PHP version requirements
* specified by the plugin authors.
*
* [--activate]
* : If set, the plugin will be activated immediately after install.
*
Expand Down
23 changes: 23 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'] );
} elseif ( ! Utils\get_flag_value( $assoc_args, 'ignore-requirements', false ) ) {
$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 Expand Up @@ -462,6 +481,10 @@ protected function filter_item_list( $items, $args ) {
* : If set, the command will overwrite any installed version of the theme, without prompting
* for confirmation.
*
* [--ignore-requirements]
* : If set, the command will install the theme while ignoring any WordPress or PHP version requirements
* specified by the theme authors.
*
* [--activate]
* : If set, the theme will be activated immediately after install.
*
Expand Down
Loading