Skip to content

Commit e0abcdd

Browse files
authored
Merge pull request #436 from mrsdizzie/requirements-check
2 parents 0ef2e34 + 8d9edf4 commit e0abcdd

8 files changed

+123
-5
lines changed

features/plugin-auto-updates-disable.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Feature: Disable auto-updates for WordPress plugins
22

33
Background:
44
Given a WP install
5-
And I run `wp plugin install duplicate-post`
5+
And I run `wp plugin install duplicate-post --ignore-requirements`
66
And I run `wp plugin auto-updates enable --all`
77

88
@require-wp-5.5

features/plugin-auto-updates-enable.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Feature: Enable auto-updates for WordPress plugins
22

33
Background:
44
Given a WP install
5-
And I run `wp plugin install duplicate-post`
5+
And I run `wp plugin install duplicate-post --ignore-requirements`
66

77
@require-wp-5.5
88
Scenario: Show an error if required params are missing

features/plugin-auto-updates-status.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Feature: Show the status of auto-updates for WordPress plugins
22

33
Background:
44
Given a WP install
5-
And I run `wp plugin install duplicate-post`
5+
And I run `wp plugin install duplicate-post --ignore-requirements`
66

77
@require-wp-5.5
88
Scenario: Show an error if required params are missing

features/plugin-install.feature

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Feature: Install WordPress plugins
171171
When I run `rm wp-content/plugins/akismet/akismet.php`
172172
Then the return code should be 0
173173

174-
When I try `wp plugin install akismet`
174+
When I try `wp plugin install akismet --ignore-requirements`
175175
Then STDERR should contain:
176176
"""
177177
Warning: Destination folder already exists. "{WORKING_DIR}/wp-content/plugins/akismet/"
@@ -237,3 +237,36 @@ Feature: Install WordPress plugins
237237
Plugin 'site-secrets' activated.
238238
Success: Plugin already installed.
239239
"""
240+
241+
@require-php-7
242+
Scenario: Can't install plugin that requires a newer version of WordPress
243+
Given a WP install
244+
245+
When I run `wp core download --version=6.4 --force`
246+
And I run `rm -r wp-content/themes/*`
247+
248+
And I try `wp plugin install wp-super-cache`
249+
Then STDERR should contain:
250+
"""
251+
Warning: wp-super-cache: This plugin does not work with your version of WordPress
252+
"""
253+
254+
And STDERR should contain:
255+
"""
256+
Error: No plugins installed.
257+
"""
258+
259+
@less-than-php-7.4 @require-wp-6.6
260+
Scenario: Can't install plugin that requires a newer version of PHP
261+
Given a WP install
262+
263+
And I try `wp plugin install contact-form-7`
264+
Then STDERR should contain:
265+
"""
266+
Warning: contact-form-7: This plugin does not work with your version of PHP
267+
"""
268+
269+
And STDERR should contain:
270+
"""
271+
Error: No plugins installed.
272+
"""

features/plugin.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Feature: Manage WordPress plugins
329329
When I run `wp plugin activate akismet hello`
330330
Then STDOUT should not be empty
331331

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

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

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

@@ -448,6 +450,8 @@ Feature: Manage WordPress plugins
448450
must-use
449451
"""
450452

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

479+
# WordPress Importer requires WP 5.2.
480+
@require-wp-5.2
475481
Scenario: Deactivate and uninstall a plugin, part two
476482
Given a WP install
477483
And these installed and active plugins:

features/theme-install.feature

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

src/Plugin_Command.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ public function path( $args, $assoc_args ) {
582582
}
583583

584584
protected function install_from_repo( $slug, $assoc_args ) {
585+
global $wp_version;
586+
// Extract the major WordPress version (e.g., "6.3") from the full version string
587+
list($wp_core_version) = explode( '-', $wp_version );
588+
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );
589+
585590
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );
586591

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

591596
if ( isset( $assoc_args['version'] ) ) {
592597
self::alter_api_response( $api, $assoc_args['version'] );
598+
} elseif ( ! Utils\get_flag_value( $assoc_args, 'ignore-requirements', false ) ) {
599+
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
600+
$requires_wp = isset( $api->requires ) ? $api->requires : null;
601+
602+
$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
603+
$compatible_wp = empty( $requires_wp ) || version_compare( $wp_core_version, $requires_wp, '>=' );
604+
605+
if ( ! $compatible_wp ) {
606+
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
607+
}
608+
609+
if ( ! $compatible_php ) {
610+
return new WP_Error( 'requirements_not_met', "This plugin does not work with your version of PHP. Minimum PHP required is $compatible_php" );
611+
}
593612
}
594613

595614
$status = install_plugin_install_status( $api );
@@ -896,6 +915,10 @@ protected function filter_item_list( $items, $args ) {
896915
* : If set, the command will overwrite any installed version of the plugin, without prompting
897916
* for confirmation.
898917
*
918+
* [--ignore-requirements]
919+
* :If set, the command will install the plugin while ignoring any WordPress or PHP version requirements
920+
* specified by the plugin authors.
921+
*
899922
* [--activate]
900923
* : If set, the plugin will be activated immediately after install.
901924
*

src/Theme_Command.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ public function path( $args, $assoc_args ) {
395395
}
396396

397397
protected function install_from_repo( $slug, $assoc_args ) {
398+
global $wp_version;
399+
// Extract the major WordPress version (e.g., "6.3") from the full version string
400+
list($wp_core_version) = explode( '-', $wp_version );
401+
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );
402+
398403
$api = themes_api( 'theme_information', array( 'slug' => $slug ) );
399404

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

404409
if ( isset( $assoc_args['version'] ) ) {
405410
self::alter_api_response( $api, $assoc_args['version'] );
411+
} elseif ( ! Utils\get_flag_value( $assoc_args, 'ignore-requirements', false ) ) {
412+
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
413+
$requires_wp = isset( $api->requires ) ? $api->requires : null;
414+
415+
$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
416+
$compatible_wp = empty( $requires_wp ) || version_compare( $wp_core_version, $requires_wp, '>=' );
417+
418+
if ( ! $compatible_wp ) {
419+
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
420+
}
421+
422+
if ( ! $compatible_php ) {
423+
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of PHP. Minimum PHP required is $requires_php" );
424+
}
406425
}
407426

408427
if ( ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
@@ -462,6 +481,10 @@ protected function filter_item_list( $items, $args ) {
462481
* : If set, the command will overwrite any installed version of the theme, without prompting
463482
* for confirmation.
464483
*
484+
* [--ignore-requirements]
485+
* : If set, the command will install the theme while ignoring any WordPress or PHP version requirements
486+
* specified by the theme authors.
487+
*
465488
* [--activate]
466489
* : If set, the theme will be activated immediately after install.
467490
*

0 commit comments

Comments
 (0)