@@ -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