Skip to content

Commit d75ff32

Browse files
committed
Check WordPress and PHP requirements before installing a theme or plugin
This matches the behavior of WordPress core which will refuse to install a plugin if the local copy of WordPress or PHP don't meet the minimum requirements for the most recent version as listed by the plugin authors. Unfortunately the api is limited and only provides these requirement details for the most recent version, so it isn't possible to find an older version that might work. As a compromise, this code doesn't check requirements if a user provides a specific --version since we can't know the requirements for anything other than the latest version and assume if somebody specifies a version they know it will work or want to try anyway.
1 parent 5710a18 commit d75ff32

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

features/plugin-install.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,35 @@ Feature: Install WordPress plugins
237237
Plugin 'site-secrets' activated.
238238
Success: Plugin already installed.
239239
"""
240+
241+
Scenario: Can't install plugin that requires a newer version of WordPress
242+
Given a WP install
243+
244+
When I run `wp core download --version=6.4 --force`
245+
And I run `rm -r wp-content/themes/*`
246+
247+
And I try `wp plugin install wp-super-cache`
248+
Then STDERR should contain:
249+
"""
250+
Warning: wp-super-cache: This plugin does not work with your version of WordPress
251+
"""
252+
253+
And STDERR should contain:
254+
"""
255+
Error: No plugins installed.
256+
"""
257+
258+
@less-than-php-7.4
259+
Scenario: Can't install plugin that requires a newer version of PHP
260+
Given a WP install
261+
262+
And I try `wp plugin install contact-form-7`
263+
Then STDERR should contain:
264+
"""
265+
Warning: contact-form-7: This plugin does not work with your version of PHP
266+
"""
267+
268+
And STDERR should contain:
269+
"""
270+
Error: No plugins installed.
271+
"""

features/theme-install.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,35 @@ Feature: Install WordPress themes
125125
"""
126126
twentyeleven
127127
"""
128+
129+
Scenario: Can't install theme that requires a newer version of WordPress
130+
Given a WP install
131+
132+
When I run `wp core download --version=6.4 --force`
133+
And I run `rm -r wp-content/themes/*`
134+
135+
And I try `wp theme install twentytwentyfive`
136+
Then STDERR should contain:
137+
"""
138+
Warning: twentytwentyfive: This theme does not work with your version of WordPress.
139+
"""
140+
141+
And STDERR should contain:
142+
"""
143+
Error: No themes installed.
144+
"""
145+
146+
@less-than-php-7.4
147+
Scenario: Can't install theme that requires a newer version of PHP
148+
Given a WP install
149+
150+
And I try `wp theme install oceanwp`
151+
Then STDERR should contain:
152+
"""
153+
Warning: oceanwp: This theme does not work with your version of PHP.
154+
"""
155+
156+
And STDERR should contain:
157+
"""
158+
Error: No themes installed.
159+
"""

src/Plugin_Command.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ protected function install_from_repo( $slug, $assoc_args ) {
590590

591591
if ( isset( $assoc_args['version'] ) ) {
592592
self::alter_api_response( $api, $assoc_args['version'] );
593+
} else {
594+
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
595+
$requires_wp = isset( $api->requires ) ? $api->requires : null;
596+
597+
$compatible_php = is_php_version_compatible( $requires_php );
598+
$compatible_wp = is_wp_version_compatible( $requires_wp );
599+
600+
if ( ! $compatible_wp ) {
601+
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
602+
}
603+
604+
if ( ! $compatible_php ) {
605+
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of PHP. Minimum PHP required is $compatible_php" );
606+
}
593607
}
594608

595609
$status = install_plugin_install_status( $api );

src/Theme_Command.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,20 @@ protected function install_from_repo( $slug, $assoc_args ) {
403403

404404
if ( isset( $assoc_args['version'] ) ) {
405405
self::alter_api_response( $api, $assoc_args['version'] );
406+
} else {
407+
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
408+
$requires_wp = isset( $api->requires ) ? $api->requires : null;
409+
410+
$compatible_php = is_php_version_compatible( $requires_php );
411+
$compatible_wp = is_wp_version_compatible( $requires_wp );
412+
413+
if ( ! $compatible_wp ) {
414+
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
415+
}
416+
417+
if ( ! $compatible_php ) {
418+
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of PHP. Minimum PHP required is $requires_php" );
419+
}
406420
}
407421

408422
if ( ! Utils\get_flag_value( $assoc_args, 'force' ) ) {

0 commit comments

Comments
 (0)