Skip to content

Commit 29b31a4

Browse files
swissspidyclaude
andauthored
Honor --ignore-requirements for WordPress core package checks (#558)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a234561 commit 29b31a4

3 files changed

Lines changed: 241 additions & 2 deletions

File tree

‎features/plugin-install.feature‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,98 @@ Feature: Install WordPress plugins
550550
Error: The --slug option can only be used when installing a single item.
551551
"""
552552
And the return code should be 1
553+
554+
@require-wp-5.5
555+
Scenario Outline: Install plugin from a zip file with an unmet <requirement> requirement using --ignore-requirements
556+
Given a WP install
557+
And a requires-plugin/requires-plugin.php file:
558+
"""
559+
<?php
560+
/**
561+
* Plugin Name: Requires Plugin
562+
* Version: 1.0.0
563+
* <requirement>: 99.0
564+
*/
565+
"""
566+
And a make-zip.php file:
567+
"""
568+
<?php
569+
$zip = new ZipArchive();
570+
$zip->open( 'requires-plugin.zip', ZipArchive::CREATE );
571+
$zip->addFile( 'requires-plugin/requires-plugin.php' );
572+
$zip->close();
573+
"""
574+
575+
When I run `wp eval-file make-zip.php --skip-wordpress`
576+
Then the return code should be 0
577+
578+
When I try `wp plugin install requires-plugin.zip`
579+
Then STDERR should contain:
580+
"""
581+
<message>
582+
"""
583+
And STDERR should contain:
584+
"""
585+
however the uploaded plugin requires 99.0.
586+
"""
587+
And STDERR should contain:
588+
"""
589+
Error: No plugins installed.
590+
"""
591+
And the wp-content/plugins/requires-plugin directory should not exist
592+
And the return code should be 1
593+
594+
When I run `wp plugin install requires-plugin.zip --ignore-requirements`
595+
Then STDOUT should contain:
596+
"""
597+
Plugin installed successfully.
598+
"""
599+
And the wp-content/plugins/requires-plugin/requires-plugin.php file should exist
600+
601+
When I run `wp plugin install requires-plugin.zip --ignore-requirements --slug=custom-requires-plugin`
602+
Then STDOUT should contain:
603+
"""
604+
Renamed 'requires-plugin' to 'custom-requires-plugin'.
605+
"""
606+
And STDOUT should contain:
607+
"""
608+
Plugin installed successfully.
609+
"""
610+
And the wp-content/plugins/custom-requires-plugin/requires-plugin.php file should exist
611+
612+
Examples:
613+
| requirement | message |
614+
| Requires at least | Your WordPress version is |
615+
| Requires PHP | The PHP version on your server is |
616+
617+
@require-wp-5.5
618+
Scenario: Install invalid plugin zip file using --ignore-requirements
619+
Given a WP install
620+
And a not-a-plugin/not-a-plugin.php file:
621+
"""
622+
<?php
623+
// Not a plugin.
624+
"""
625+
And a make-zip.php file:
626+
"""
627+
<?php
628+
$zip = new ZipArchive();
629+
$zip->open( 'not-a-plugin.zip', ZipArchive::CREATE );
630+
$zip->addFile( 'not-a-plugin/not-a-plugin.php' );
631+
$zip->close();
632+
"""
633+
634+
When I run `wp eval-file make-zip.php --skip-wordpress`
635+
Then the return code should be 0
636+
637+
When I try `wp plugin install not-a-plugin.zip --ignore-requirements`
638+
Then STDERR should contain:
639+
"""
640+
No valid plugins were found.
641+
"""
642+
And STDERR should contain:
643+
"""
644+
Error: No plugins installed.
645+
"""
646+
And the wp-content/plugins/not-a-plugin directory should not exist
647+
And the return code should be 1

‎features/theme-install.feature‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,86 @@ Feature: Install WordPress themes
211211
Error: The --slug option can only be used when installing a single item.
212212
"""
213213
And the return code should be 1
214+
215+
@require-wp-5.5
216+
Scenario Outline: Install theme from a zip file with an unmet <requirement> requirement using --ignore-requirements
217+
Given a WP install
218+
And a requires-theme/style.css file:
219+
"""
220+
/*
221+
Theme Name: Requires Theme
222+
Version: 1.0.0
223+
<requirement>: 99.0
224+
*/
225+
"""
226+
And a requires-theme/index.php file:
227+
"""
228+
<?php
229+
"""
230+
And a make-zip.php file:
231+
"""
232+
<?php
233+
$zip = new ZipArchive();
234+
$zip->open( 'requires-theme.zip', ZipArchive::CREATE );
235+
$zip->addFile( 'requires-theme/style.css' );
236+
$zip->addFile( 'requires-theme/index.php' );
237+
$zip->close();
238+
"""
239+
240+
When I run `wp eval-file make-zip.php --skip-wordpress`
241+
Then the return code should be 0
242+
243+
When I try `wp theme install requires-theme.zip`
244+
Then STDERR should contain:
245+
"""
246+
<message>
247+
"""
248+
And STDERR should contain:
249+
"""
250+
however the uploaded theme requires 99.0.
251+
"""
252+
And STDERR should contain:
253+
"""
254+
Error: No themes installed.
255+
"""
256+
And the wp-content/themes/requires-theme directory should not exist
257+
And the return code should be 1
258+
259+
When I run `wp theme install requires-theme.zip --ignore-requirements`
260+
Then STDOUT should contain:
261+
"""
262+
Theme installed successfully.
263+
"""
264+
And the wp-content/themes/requires-theme/style.css file should exist
265+
266+
Examples:
267+
| requirement | message |
268+
| Requires at least | Your WordPress version is |
269+
| Requires PHP | The PHP version on your server is |
270+
271+
@require-wp-5.5
272+
Scenario: Install invalid theme zip file using --ignore-requirements
273+
Given a WP install
274+
And a not-a-theme/index.php file:
275+
"""
276+
<?php
277+
"""
278+
And a make-zip.php file:
279+
"""
280+
<?php
281+
$zip = new ZipArchive();
282+
$zip->open( 'not-a-theme.zip', ZipArchive::CREATE );
283+
$zip->addFile( 'not-a-theme/index.php' );
284+
$zip->close();
285+
"""
286+
287+
When I run `wp eval-file make-zip.php --skip-wordpress`
288+
Then the return code should be 0
289+
290+
When I try `wp theme install not-a-theme.zip --ignore-requirements`
291+
Then STDERR should contain:
292+
"""
293+
Error: No themes installed.
294+
"""
295+
And the wp-content/themes/not-a-theme directory should not exist
296+
And the return code should be 1

‎src/WP_CLI/CommandWithUpgrade.php‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public function install( $args, $assoc_args ) {
197197
WP_CLI::error( 'The --slug option can only be used when installing a single item.' );
198198
}
199199

200+
$ignore_requirements = Utils\get_flag_value( $assoc_args, 'ignore-requirements', false );
201+
200202
foreach ( $args as $slug ) {
201203

202204
if ( empty( $slug ) ) {
@@ -342,7 +344,14 @@ public function install( $args, $assoc_args ) {
342344
// Add item to cache allowlist if it matches certain URL patterns.
343345
self::maybe_cache( $slug, $this->item_type );
344346

345-
if ( $file_upgrader->install( $slug ) ) {
347+
$installed = $this->maybe_ignore_requirements(
348+
$ignore_requirements,
349+
static function () use ( $file_upgrader, $slug ) {
350+
return $file_upgrader->install( $slug );
351+
}
352+
);
353+
354+
if ( $installed ) {
346355
$slug = $file_upgrader->result['destination_name'];
347356
$result = true;
348357
if ( $filter ) {
@@ -354,7 +363,12 @@ public function install( $args, $assoc_args ) {
354363
}
355364
} else {
356365
// Assume a plugin/theme slug from the WordPress.org repository has been specified.
357-
$result = $this->install_from_repo( $slug, $assoc_args );
366+
$result = $this->maybe_ignore_requirements(
367+
$ignore_requirements,
368+
function () use ( $slug, $assoc_args ) {
369+
return $this->install_from_repo( $slug, $assoc_args );
370+
}
371+
);
358372

359373
if ( is_null( $result ) ) {
360374
++$errors;
@@ -417,6 +431,53 @@ public function install( $args, $assoc_args ) {
417431
Utils\report_batch_operation_results( $this->item_type, 'install', count( $args ), $successes, $errors );
418432
}
419433

434+
/**
435+
* Runs an install callback, optionally bypassing the WordPress and PHP
436+
* version checks that the core upgraders run in `check_package()`.
437+
*
438+
* The callback must trigger the upgrader's `install()`, which registers
439+
* `check_package()` at priority 10. The capturing callback is registered
440+
* right before that, so it runs after any directory renaming filters and
441+
* before `check_package()`. If the package was rejected only because of
442+
* version requirements, the captured source is restored. Other validation
443+
* errors, like a missing plugin header, are kept.
444+
*
445+
* @param bool $ignore_requirements Whether to bypass the version checks.
446+
* @param callable $callback Install callback.
447+
* @return mixed Return value of the callback.
448+
*/
449+
private function maybe_ignore_requirements( $ignore_requirements, $callback ) {
450+
if ( ! $ignore_requirements ) {
451+
return $callback();
452+
}
453+
454+
$original_source = null;
455+
456+
$capture = static function ( $source ) use ( &$original_source ) {
457+
$original_source = $source;
458+
return $source;
459+
};
460+
461+
$restore = static function ( $source ) use ( &$original_source ) {
462+
if ( is_wp_error( $source )
463+
&& in_array( $source->get_error_code(), array( 'incompatible_wp_required_version', 'incompatible_php_required_version' ), true )
464+
&& is_string( $original_source ) ) {
465+
return $original_source;
466+
}
467+
return $source;
468+
};
469+
470+
add_filter( 'upgrader_source_selection', $capture, 10 );
471+
add_filter( 'upgrader_source_selection', $restore, 11 );
472+
473+
try {
474+
return $callback();
475+
} finally {
476+
remove_filter( 'upgrader_source_selection', $capture, 10 );
477+
remove_filter( 'upgrader_source_selection', $restore, 11 );
478+
}
479+
}
480+
420481
/**
421482
* Install a plugin from a single PHP file URL.
422483
*

0 commit comments

Comments
 (0)