Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"plugin search",
"plugin status",
"plugin check-update",
"plugin download",
Comment thread
swissspidy marked this conversation as resolved.
"plugin toggle",
"plugin uninstall",
"plugin update",
Expand All @@ -82,6 +83,7 @@
"theme search",
"theme status",
"theme check-update",
"theme download",
"theme update",
"theme mod list",
"theme auto-updates",
Expand Down
16 changes: 16 additions & 0 deletions extension-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
if ( file_exists( $wpcli_extension_autoloader ) ) {
require_once $wpcli_extension_autoloader;
}
require_once __DIR__ . '/src/Plugin_Download_Command.php';
require_once __DIR__ . '/src/Theme_Download_Command.php';

$wpcli_extension_requires_wp_5_5 = [
'before_invoke' => static function () {
Expand All @@ -18,8 +20,22 @@
];

WP_CLI::add_command( 'plugin', 'Plugin_Command' );
WP_CLI::add_command(
'plugin download',
'Plugin_Download_Command',
[
'when' => 'before_wp_load',
]
);
WP_CLI::add_command( 'plugin auto-updates', 'Plugin_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 );
WP_CLI::add_command( 'theme', 'Theme_Command' );
WP_CLI::add_command(
'theme download',
'Theme_Download_Command',
[
'when' => 'before_wp_load',
]
);
WP_CLI::add_command( 'theme auto-updates', 'Theme_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 );
WP_CLI::add_command( 'theme mod', 'Theme_Mod_Command' );

Expand Down
169 changes: 169 additions & 0 deletions features/extension-download.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
Feature: Download WordPress.org extensions without loading WordPress

Scenario: Downloading a plugin package works before WordPress is loaded
Given an empty directory

When I run `wp plugin download debug-bar`
Then STDOUT should contain:
"""
Downloading debug-bar
"""
And STDOUT should contain:
"""
Success: Downloaded plugin package to
"""
And save STDOUT 'Success: Downloaded plugin package to (.+)' as {DOWNLOADED_PLUGIN}
And the {DOWNLOADED_PLUGIN} file should exist
And STDERR should be empty

Scenario: Downloading a plugin package to a custom path
Given an empty directory

When I run `wp plugin download debug-bar --path=/tmp/wp-cli-download-test-plugin`
Then STDOUT should contain:
"""
Success: Downloaded plugin package to
"""
And save STDOUT 'Success: Downloaded plugin package to (.+)' as {DOWNLOADED_PLUGIN}
And the {DOWNLOADED_PLUGIN} file should exist
And STDERR should be empty

Scenario: Downloading a specific version of a plugin
Given an empty directory

When I run `wp plugin download debug-bar --version=1.4`
Then STDOUT should contain:
"""
Downloading debug-bar (1.4)
"""
And STDOUT should contain:
"""
Success: Downloaded plugin package to
"""
And STDERR should be empty

Scenario: Downloading a non-existent version of a plugin fails with clear error
Given an empty directory

When I try `wp plugin download debug-bar --version=9.9.9`
Then STDERR should contain:
"""
Error: Can't find the requested plugin's version 9.9.9
"""
And the return code should be 1

Scenario: Downloading a plugin with --force overwrites existing file
Given an empty directory

When I run `wp plugin download debug-bar`
And I run `wp plugin download debug-bar --force`
Then STDOUT should contain:
"""
Success: Downloaded plugin package to
"""
And STDERR should be empty

Scenario: Downloading a plugin without --force fails if destination exists
Given an empty directory

When I run `wp plugin download debug-bar`
And I try `wp plugin download debug-bar`
Then STDERR should contain:
"""
Error: Destination file already exists:
"""
And the return code should be 1

Scenario: Downloading an unknown plugin fails with a clear error
Given an empty directory

When I try `wp plugin download this-plugin-does-not-exist-xyz-abc-123`
Then STDERR should contain:
"""
Error: The 'this-plugin-does-not-exist-xyz-abc-123' plugin could not be found.
"""
And the return code should be 1

Scenario: Downloading a theme package works before WordPress is loaded
Given an empty directory

When I run `wp theme download twentytwelve`
Then STDOUT should contain:
"""
Downloading twentytwelve
"""
And STDOUT should contain:
"""
Success: Downloaded theme package to
"""
And save STDOUT 'Success: Downloaded theme package to (.+)' as {DOWNLOADED_THEME}
And the {DOWNLOADED_THEME} file should exist
And STDERR should be empty
Comment on lines +1 to +101

Scenario: Downloading a theme package to a custom path
Given an empty directory

When I run `wp theme download twentytwelve --path=/tmp/wp-cli-download-test-theme`
Then STDOUT should contain:
"""
Success: Downloaded theme package to
"""
And save STDOUT 'Success: Downloaded theme package to (.+)' as {DOWNLOADED_THEME}
And the {DOWNLOADED_THEME} file should exist
And STDERR should be empty

Scenario: Downloading a specific version of a theme
Given an empty directory

When I run `wp theme download twentytwelve --version=1.3`
Then STDOUT should contain:
"""
Downloading twentytwelve (1.3)
"""
And STDOUT should contain:
"""
Success: Downloaded theme package to
"""
And STDERR should be empty

Scenario: Downloading a non-existent version of a theme fails with clear error
Given an empty directory

When I try `wp theme download twentytwelve --version=9.9.9`
Then STDERR should contain:
"""
Error: Can't find the requested theme's version 9.9.9
"""
And the return code should be 1

Scenario: Downloading a theme with --force overwrites existing file
Given an empty directory

When I run `wp theme download twentytwelve`
And I run `wp theme download twentytwelve --force`
Then STDOUT should contain:
"""
Success: Downloaded theme package to
"""
And STDERR should be empty

Scenario: Downloading a theme without --force fails if destination exists
Given an empty directory

When I run `wp theme download twentytwelve`
And I try `wp theme download twentytwelve`
Then STDERR should contain:
"""
Error: Destination file already exists:
"""
And the return code should be 1

Scenario: Downloading an unknown theme fails with a clear error
Given an empty directory

When I try `wp theme download this-theme-does-not-exist-xyz-abc-123`
Then STDERR should contain:
"""
Error: The 'this-theme-does-not-exist-xyz-abc-123' theme could not be found.
"""
And the return code should be 1
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
- vendor/wp-cli/wp-cli/utils/phpstan/scan-files.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
Expand Down
141 changes: 141 additions & 0 deletions src/Plugin_Download_Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

use WP_CLI\Utils;
use WP_CLI\WpOrgApi;

/**
* Downloads plugin zip files from the WordPress.org repository.
*
* ## OPTIONS
*
* <slug>
* : Slug of the plugin to download.
*
* [--path=<path>]
* : Directory to store the downloaded zip file. Defaults to the current directory.
*
* [--version=<version>]
* : Version to download. Accepts a version number or `dev`.
*
* [--force]
* : Overwrite destination file if it already exists.
*
* [--insecure]
* : Retry download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* ## EXAMPLES
*
* $ wp plugin download bbpress
* Downloading bbpress (2.5.9)...
* Success: Downloaded plugin package to /path/to/bbpress.2.5.9.zip
*
* @when before_wp_load
*/
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
class Plugin_Download_Command {

/**
* Downloads a plugin zip package without loading WordPress.
*
* @param array{0: string} $args Positional arguments.
* @param array{path?: string, version?: string, force?: bool, insecure?: bool} $assoc_args Associative arguments.
*/
public function __invoke( $args, $assoc_args ) {
$slug = (string) $args[0];
if ( '' === $slug ) {
WP_CLI::error( 'Please provide a plugin slug.' );
}

$insecure = Utils\get_flag_value( $assoc_args, 'insecure', false );
$force = Utils\get_flag_value( $assoc_args, 'force', false );
$requested = Utils\get_flag_value( $assoc_args, 'version', null );
$download_dir = Utils\get_flag_value( $assoc_args, 'path', getcwd() );

if ( ! is_dir( $download_dir ) ) {
if ( ! @mkdir( $download_dir, 0755, true ) ) {
WP_CLI::error( "Failed to create directory '{$download_dir}'." );
}
}

if ( ! is_writable( $download_dir ) ) {
WP_CLI::error( "'{$download_dir}' is not writable by current user." );
}
Comment on lines +43 to +62

try {
$plugin_data = ( new WpOrgApi( [ 'insecure' => $insecure ] ) )->get_plugin_info( $slug );
} catch ( Exception $exception ) {
WP_CLI::error( $exception->getMessage() );
}

if ( ! is_array( $plugin_data ) || empty( $plugin_data['download_link'] ) || empty( $plugin_data['version'] ) ) {
WP_CLI::error( "The '{$slug}' plugin could not be found." );
}

Comment on lines +70 to +73
$download_url = $plugin_data['download_link'];
$version = $plugin_data['version'];

if ( is_string( $requested ) && '' !== $requested && $requested !== $plugin_data['version'] ) {
$current_zip = basename( (string) Utils\parse_url( $download_url, PHP_URL_PATH ) );
if ( 'dev' === $requested ) {
$download_url = str_replace( $current_zip, $slug . '.zip', $download_url );
$version = 'Development Version';
} else {
$download_url = str_replace( $current_zip, $slug . '.' . $requested . '.zip', $download_url );
$version = $requested;

try {
$head_response = Utils\http_request( 'HEAD', $download_url, null, [], [ 'insecure' => (bool) $insecure ] );
} catch ( Exception $exception ) {
WP_CLI::error( $exception->getMessage() );
}

if ( 200 !== (int) $head_response->status_code ) {
WP_CLI::error(
sprintf(
"Can't find the requested plugin's version %s in the WordPress.org plugin repository (HTTP code %d).",
$requested,
$head_response->status_code
)
);
}
}
}
Comment on lines +77 to +102

$zip_name = basename( (string) Utils\parse_url( $download_url, PHP_URL_PATH ) );
if ( '' === $zip_name ) {
$zip_name = "{$slug}.zip";
}

$download_file = rtrim( $download_dir, '/\\' ) . DIRECTORY_SEPARATOR . $zip_name;

if ( ! $force && file_exists( $download_file ) ) {
WP_CLI::error( "Destination file already exists: {$download_file}" );
}

WP_CLI::log( "Downloading {$slug} ({$version})..." );

try {
$response = Utils\http_request(
'GET',
$download_url,
null,
[],
[
'filename' => $download_file,
'insecure' => (bool) $insecure,
]
);
} catch ( Exception $exception ) {
WP_CLI::error( $exception->getMessage() );
}

if ( 200 !== (int) $response->status_code ) {
if ( file_exists( $download_file ) ) {
unlink( $download_file );
}
WP_CLI::error( sprintf( 'Failed to download plugin package (HTTP code %d).', $response->status_code ) );
}

WP_CLI::success( "Downloaded plugin package to {$download_file}" );
Comment on lines +117 to +139
}
}
Loading
Loading