Skip to content

Commit e57258e

Browse files
committed
Add --insecure flag to plugin|theme install & plugin\theme update commands
1 parent 4a018d5 commit e57258e

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

src/Plugin_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ protected function install_from_repo( $slug, $assoc_args ) {
593593
* [--dry-run]
594594
* : Preview which plugins would be updated.
595595
*
596+
* [--insecure]
597+
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
598+
*
596599
* ## EXAMPLES
597600
*
598601
* $ wp plugin update bbpress --version=dev
@@ -737,6 +740,9 @@ protected function filter_item_list( $items, $args ) {
737740
* [--activate-network]
738741
* : If set, the plugin will be network activated immediately after install
739742
*
743+
* [--insecure]
744+
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
745+
*
740746
* ## EXAMPLES
741747
*
742748
* # Install the latest version from wordpress.org and activate

src/Theme_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ protected function filter_item_list( $items, $args ) {
457457
* [--activate]
458458
* : If set, the theme will be activated immediately after install.
459459
*
460+
* [--insecure]
461+
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
462+
*
460463
* ## EXAMPLES
461464
*
462465
* # Install the latest version from wordpress.org and activate
@@ -596,6 +599,9 @@ public function get( $args, $assoc_args ) {
596599
* [--dry-run]
597600
* : Preview which themes would be updated.
598601
*
602+
* [--insecure]
603+
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
604+
*
599605
* ## EXAMPLES
600606
*
601607
* # Update multiple themes

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use Composer\Semver\Comparator;
66
use WP_CLI;
7+
use WP_CLI\Fetchers;
8+
use WP_CLI\Loggers;
79
use WP_CLI\Utils;
10+
use WP_Error;
811

912
abstract class CommandWithUpgrade extends \WP_CLI_Command {
1013

@@ -38,7 +41,7 @@ function () {
3841
999
3942
);
4043

41-
$this->fetcher = new WP_CLI\Fetchers\Plugin();
44+
$this->fetcher = new Fetchers\Plugin();
4245
}
4346

4447
abstract protected function get_upgrader_class( $force );
@@ -95,10 +98,10 @@ private function status_all() {
9598
$line .= ' ' . $details['version'];
9699
}
97100

98-
\WP_CLI::line( \WP_CLI::colorize( $line ) );
101+
WP_CLI::line( WP_CLI::colorize( $line ) );
99102
}
100103

101-
\WP_CLI::line();
104+
WP_CLI::line();
102105

103106
$this->show_legend( $items );
104107
}
@@ -135,7 +138,7 @@ private function show_legend( $items ) {
135138
$legend_line[] = '%yU = Update Available%n';
136139
}
137140

138-
\WP_CLI::line( 'Legend: ' . \WP_CLI::colorize( implode( ', ', $legend_line ) ) );
141+
WP_CLI::line( 'Legend: ' . WP_CLI::colorize( implode( ', ', $legend_line ) ) );
139142
}
140143

141144
public function install( $args, $assoc_args ) {
@@ -185,7 +188,7 @@ public function install( $args, $assoc_args ) {
185188
return $new_path;
186189
}
187190

188-
return new \WP_Error( 'wpcli_install_github', "Couldn't move Github-based project to appropriate directory." );
191+
return new WP_Error( 'wpcli_install_github', "Couldn't move Github-based project to appropriate directory." );
189192
};
190193
add_filter( 'upgrader_source_selection', $filter, 10, 3 );
191194
}
@@ -213,7 +216,7 @@ public function install( $args, $assoc_args ) {
213216
WP_CLI::warning( "Couldn't find '$slug' in the WordPress.org {$this->item_type} directory." );
214217
$errors++;
215218
} else {
216-
\WP_CLI::warning( "$slug: " . $result->get_error_message() );
219+
WP_CLI::warning( "$slug: " . $result->get_error_message() );
217220
if ( 'already_installed' !== $key ) {
218221
$errors++;
219222
}
@@ -237,12 +240,12 @@ public function install( $args, $assoc_args ) {
237240
if ( true === $allow_activation && count( $extension ) > 0 ) {
238241
$this->chained_command = true;
239242
if ( Utils\get_flag_value( $assoc_args, 'activate-network' ) ) {
240-
\WP_CLI::log( "Network-activating '$slug'..." );
243+
WP_CLI::log( "Network-activating '$slug'..." );
241244
$this->activate( array( $slug ), array( 'network' => true ) );
242245
}
243246

244247
if ( Utils\get_flag_value( $assoc_args, 'activate' ) ) {
245-
\WP_CLI::log( "Activating '$slug'..." );
248+
WP_CLI::log( "Activating '$slug'..." );
246249
$this->activate( array( $slug ) );
247250
}
248251
$this->chained_command = false;
@@ -306,20 +309,22 @@ protected static function alter_api_response( $response, $version ) {
306309
}
307310

308311
protected function get_upgrader( $assoc_args ) {
309-
$upgrader_class = $this->get_upgrader_class( Utils\get_flag_value( $assoc_args, 'force' ) );
310-
return Utils\get_upgrader( $upgrader_class );
312+
$force = (bool) Utils\get_flag_value( $assoc_args, 'force', false );
313+
$insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false );
314+
$upgrader_class = $this->get_upgrader_class( $force );
315+
return Utils\get_upgrader( $upgrader_class, $insecure );
311316
}
312317

313318
protected function update_many( $args, $assoc_args ) {
314319
call_user_func( $this->upgrade_refresh );
315320

316321
if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], [ 'json', 'csv' ], true ) ) {
317-
$logger = new \WP_CLI\Loggers\Quiet();
322+
$logger = new Loggers\Quiet();
318323
WP_CLI::set_logger( $logger );
319324
}
320325

321326
if ( ! Utils\get_flag_value( $assoc_args, 'all' ) && empty( $args ) ) {
322-
\WP_CLI::error( "Please specify one or more {$this->item_type}s, or use --all." );
327+
WP_CLI::error( "Please specify one or more {$this->item_type}s, or use --all." );
323328
}
324329

325330
if ( Utils\get_flag_value( $assoc_args, 'minor' ) && Utils\get_flag_value( $assoc_args, 'patch' ) ) {
@@ -336,14 +341,18 @@ protected function update_many( $args, $assoc_args ) {
336341

337342
$items_to_update = wp_list_filter( $items, [ 'update' => true ] );
338343

344+
$minor = (bool) Utils\get_flag_value( $assoc_args, 'minor', false );
345+
$patch = (bool) Utils\get_flag_value( $assoc_args, 'patch', false );
346+
339347
if ( 'plugin' === $this->item_type
340-
&& ( Utils\get_flag_value( $assoc_args, 'minor' )
341-
|| Utils\get_flag_value( $assoc_args, 'patch' ) ) ) {
342-
$type = Utils\get_flag_value( $assoc_args, 'minor' ) ? 'minor' : 'patch';
343-
$items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type );
348+
&& ( $minor || $patch ) ) {
349+
$type = $minor ? 'minor' : 'patch';
350+
$insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false );
351+
352+
$items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type, $insecure );
344353
}
345354

346-
$exclude = WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude' );
355+
$exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
347356
if ( isset( $exclude ) ) {
348357
$exclude_items = explode( ',', trim( $assoc_args['exclude'], ',' ) );
349358
unset( $assoc_args['exclude'] );
@@ -369,29 +378,29 @@ protected function update_many( $args, $assoc_args ) {
369378

370379
if ( Utils\get_flag_value( $assoc_args, 'dry-run' ) ) {
371380
if ( empty( $items_to_update ) ) {
372-
\WP_CLI::log( "No {$this->item_type} updates available." );
381+
WP_CLI::log( "No {$this->item_type} updates available." );
373382

374383
if ( null !== $exclude ) {
375-
\WP_CLI::log( "Skipped updates for: $exclude" );
384+
WP_CLI::log( "Skipped updates for: $exclude" );
376385
}
377386

378387
return;
379388
}
380389

381390
if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], [ 'json', 'csv' ], true ) ) {
382-
WP_CLI\Utils\format_items( $assoc_args['format'], $items_to_update, [ 'name', 'status', 'version', 'update_version' ] );
391+
Utils\format_items( $assoc_args['format'], $items_to_update, [ 'name', 'status', 'version', 'update_version' ] );
383392
} elseif ( ! empty( $assoc_args['format'] ) && 'summary' === $assoc_args['format'] ) {
384-
\WP_CLI::log( "Available {$this->item_type} updates:" );
393+
WP_CLI::log( "Available {$this->item_type} updates:" );
385394
foreach ( $items_to_update as $item_to_update => $info ) {
386-
\WP_CLI::log( "{$info['title']} update from version {$info['version']} to version {$info['update_version']}" );
395+
WP_CLI::log( "{$info['title']} update from version {$info['version']} to version {$info['update_version']}" );
387396
}
388397
} else {
389-
\WP_CLI::log( "Available {$this->item_type} updates:" );
398+
WP_CLI::log( "Available {$this->item_type} updates:" );
390399
Utils\format_items( 'table', $items_to_update, [ 'name', 'status', 'version', 'update_version' ] );
391400
}
392401

393402
if ( null !== $exclude ) {
394-
\WP_CLI::log( "Skipped updates for: $exclude" );
403+
WP_CLI::log( "Skipped updates for: $exclude" );
395404
}
396405

397406
return;
@@ -401,7 +410,7 @@ protected function update_many( $args, $assoc_args ) {
401410

402411
// Only attempt to update if there is something to update.
403412
if ( ! empty( $items_to_update ) ) {
404-
$cache_manager = \WP_CLI::get_http_cache_manager();
413+
$cache_manager = WP_CLI::get_http_cache_manager();
405414
foreach ( $items_to_update as $item ) {
406415
$cache_manager->whitelist_package( $item['update_package'], $this->item_type, $item['name'], $item['update_version'] );
407416
}
@@ -429,7 +438,7 @@ protected function update_many( $args, $assoc_args ) {
429438
if ( ! empty( $assoc_args['format'] ) && 'summary' === $assoc_args['format'] ) {
430439
foreach ( $items_to_update as $item_to_update => $info ) {
431440
$message = null !== $result[ $info['update_id'] ] ? 'updated successfully' : 'did not update';
432-
\WP_CLI::log( "{$info['title']} {$message} from version {$info['version']} to version {$info['update_version']}" );
441+
WP_CLI::log( "{$info['title']} {$message} from version {$info['version']} to version {$info['update_version']}" );
433442
}
434443
} else {
435444
$status = array();
@@ -457,7 +466,7 @@ protected function update_many( $args, $assoc_args ) {
457466
$total_updated = Utils\get_flag_value( $assoc_args, 'all' ) ? $num_to_update : count( $args );
458467
Utils\report_batch_operation_results( $this->item_type, 'update', $total_updated, $num_updated, $errors );
459468
if ( null !== $exclude ) {
460-
\WP_CLI::log( "Skipped updates for: $exclude" );
469+
WP_CLI::log( "Skipped updates for: $exclude" );
461470
}
462471
}
463472

@@ -469,7 +478,7 @@ protected function _list( $_, $assoc_args ) {
469478
$all_items = $this->get_all_items();
470479

471480
if ( ! is_array( $all_items ) ) {
472-
\WP_CLI::error( "No {$this->item_type}s found." );
481+
WP_CLI::error( "No {$this->item_type}s found." );
473482
}
474483

475484
foreach ( $all_items as $key => &$item ) {
@@ -568,14 +577,15 @@ private function get_color( $status ) {
568577
/**
569578
* Get the minor or patch version for plugins with available updates
570579
*
571-
* @param array $items Plugins with updates.
572-
* @param string $type Either 'minor' or 'patch'
580+
* @param array $items Plugins with updates.
581+
* @param string $type Either 'minor' or 'patch'.
582+
* @param bool $insecure Whether to retry without certificate validation on TLS handshake failure.
573583
* @return array
574584
*/
575-
private function get_minor_or_patch_updates( $items, $type ) {
585+
private function get_minor_or_patch_updates( $items, $type, $insecure ) {
576586
foreach ( $items as $i => $item ) {
577587
$wporg_url = sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s.json', $item['name'] );
578-
$response = Utils\http_request( 'GET', $wporg_url );
588+
$response = Utils\http_request( 'GET', $wporg_url, null, [], [ 'insecure' => $insecure ] );
579589
// Must not be hosted on wp.org
580590
if ( 20 !== absint( substr( $response->status_code, 0, 2 ) ) ) {
581591
unset( $items[ $i ] );
@@ -658,13 +668,13 @@ protected function _search( $args, $assoc_args ) {
658668
}
659669

660670
if ( is_wp_error( $api ) ) {
661-
\WP_CLI::error( $api->get_error_message() . __( ' Try again' ) );
671+
WP_CLI::error( $api->get_error_message() . __( ' Try again' ) );
662672
}
663673

664674
$plural = $this->item_type . 's';
665675

666676
if ( ! isset( $api->$plural ) ) {
667-
\WP_CLI::error( __( 'API error. Try Again.' ) );
677+
WP_CLI::error( __( 'API error. Try Again.' ) );
668678
}
669679

670680
$items = $api->$plural;
@@ -678,7 +688,7 @@ protected function _search( $args, $assoc_args ) {
678688

679689
if ( 'table' === $format ) {
680690
$count = Utils\get_flag_value( $api->info, 'results', 'unknown' );
681-
\WP_CLI::success( sprintf( 'Showing %s of %s %s.', count( $items ), $count, $plural ) );
691+
WP_CLI::success( sprintf( 'Showing %s of %s %s.', count( $items ), $count, $plural ) );
682692
}
683693

684694
$formatter->display_items( $items );

0 commit comments

Comments
 (0)