Skip to content

Commit becf315

Browse files
CopilotswissspidyCopilotRvouillschlessera
authored
Add plugin/theme download subcommands runnable before WordPress load (#527)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Hervé THOMAS <rvouill@gmail.com> Co-authored-by: Alain Schlesser <alain.schlesser@gmail.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent b448f39 commit becf315

8 files changed

Lines changed: 509 additions & 1 deletion

‎composer.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"plugin search",
5858
"plugin status",
5959
"plugin check-update",
60+
"plugin download",
6061
"plugin toggle",
6162
"plugin uninstall",
6263
"plugin update",
@@ -82,6 +83,7 @@
8283
"theme search",
8384
"theme status",
8485
"theme check-update",
86+
"theme download",
8587
"theme update",
8688
"theme mod list",
8789
"theme auto-updates",

‎extension-command.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
if ( file_exists( $wpcli_extension_autoloader ) ) {
99
require_once $wpcli_extension_autoloader;
1010
}
11+
require_once __DIR__ . '/src/Plugin_Download_Command.php';
12+
require_once __DIR__ . '/src/Theme_Download_Command.php';
1113

1214
$wpcli_extension_requires_wp_5_5 = [
1315
'before_invoke' => static function () {
@@ -18,8 +20,10 @@
1820
];
1921

2022
WP_CLI::add_command( 'plugin', 'Plugin_Command' );
23+
WP_CLI::add_command( 'plugin download', 'Plugin_Download_Command' );
2124
WP_CLI::add_command( 'plugin auto-updates', 'Plugin_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 );
2225
WP_CLI::add_command( 'theme', 'Theme_Command' );
26+
WP_CLI::add_command( 'theme download', 'Theme_Download_Command' );
2327
WP_CLI::add_command( 'theme auto-updates', 'Theme_AutoUpdates_Command', $wpcli_extension_requires_wp_5_5 );
2428
WP_CLI::add_command( 'theme mod', 'Theme_Mod_Command' );
2529

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
Feature: Download WordPress.org extensions without loading WordPress
2+
3+
Scenario: Downloading a plugin package works before WordPress is loaded
4+
Given an empty directory
5+
6+
When I run `wp plugin download debug-bar`
7+
Then STDOUT should contain:
8+
"""
9+
Downloading debug-bar
10+
"""
11+
And STDOUT should contain:
12+
"""
13+
Success: Downloaded plugin package to
14+
"""
15+
And save STDOUT 'Success: Downloaded plugin package to (.+)' as {DOWNLOADED_PLUGIN}
16+
And the {DOWNLOADED_PLUGIN} file should exist
17+
And STDERR should be empty
18+
19+
Scenario: Downloading a plugin package to a custom path
20+
Given an empty directory
21+
22+
When I run `wp plugin download debug-bar --target-path=/tmp/wp-cli-download-test-plugin`
23+
Then STDOUT should contain:
24+
"""
25+
Success: Downloaded plugin package to
26+
"""
27+
And save STDOUT 'Success: Downloaded plugin package to (.+)' as {DOWNLOADED_PLUGIN}
28+
And the {DOWNLOADED_PLUGIN} file should exist
29+
And STDERR should be empty
30+
31+
Scenario: Downloading a specific version of a plugin
32+
Given an empty directory
33+
34+
When I run `wp plugin download debug-bar --version=1.1`
35+
Then STDOUT should contain:
36+
"""
37+
Downloading debug-bar (1.1)
38+
"""
39+
And STDOUT should contain:
40+
"""
41+
Success: Downloaded plugin package to
42+
"""
43+
And STDERR should be empty
44+
45+
Scenario: Downloading a non-existent version of a plugin fails with clear error
46+
Given an empty directory
47+
48+
When I try `wp plugin download debug-bar --version=9.9.9`
49+
Then STDERR should contain:
50+
"""
51+
Error: Can't find the requested plugin's version 9.9.9
52+
"""
53+
And the return code should be 1
54+
55+
Scenario: Downloading a plugin with --force overwrites existing file
56+
Given an empty directory
57+
58+
When I run `wp plugin download debug-bar`
59+
And I run `wp plugin download debug-bar --force`
60+
Then STDOUT should contain:
61+
"""
62+
Success: Downloaded plugin package to
63+
"""
64+
And STDERR should be empty
65+
66+
Scenario: Downloading a plugin without --force fails if destination exists
67+
Given an empty directory
68+
69+
When I run `wp plugin download debug-bar`
70+
And I try `wp plugin download debug-bar`
71+
Then STDERR should contain:
72+
"""
73+
Error: Destination file already exists:
74+
"""
75+
And the return code should be 1
76+
77+
Scenario: Downloading an unknown plugin fails with a clear error
78+
Given an empty directory
79+
80+
When I try `wp plugin download this-plugin-does-not-exist-xyz-abc-123`
81+
Then STDERR should contain:
82+
"""
83+
Error: The 'this-plugin-does-not-exist-xyz-abc-123' plugin could not be found.
84+
"""
85+
And the return code should be 1
86+
87+
Scenario: Downloading a theme package works before WordPress is loaded
88+
Given an empty directory
89+
90+
When I run `wp theme download twentytwelve`
91+
Then STDOUT should contain:
92+
"""
93+
Downloading twentytwelve
94+
"""
95+
And STDOUT should contain:
96+
"""
97+
Success: Downloaded theme package to
98+
"""
99+
And save STDOUT 'Success: Downloaded theme package to (.+)' as {DOWNLOADED_THEME}
100+
And the {DOWNLOADED_THEME} file should exist
101+
And STDERR should be empty
102+
103+
Scenario: Downloading a theme package to a custom path
104+
Given an empty directory
105+
106+
When I run `wp theme download twentytwelve --target-path=/tmp/wp-cli-download-test-theme`
107+
Then STDOUT should contain:
108+
"""
109+
Success: Downloaded theme package to
110+
"""
111+
And save STDOUT 'Success: Downloaded theme package to (.+)' as {DOWNLOADED_THEME}
112+
And the {DOWNLOADED_THEME} file should exist
113+
And STDERR should be empty
114+
115+
Scenario: Downloading a specific version of a theme
116+
Given an empty directory
117+
118+
When I run `wp theme download twentytwelve --version=1.3`
119+
Then STDOUT should contain:
120+
"""
121+
Downloading twentytwelve (1.3)
122+
"""
123+
And STDOUT should contain:
124+
"""
125+
Success: Downloaded theme package to
126+
"""
127+
And STDERR should be empty
128+
129+
Scenario: Downloading a non-existent version of a theme fails with clear error
130+
Given an empty directory
131+
132+
When I try `wp theme download twentytwelve --version=9.9.9`
133+
Then STDERR should contain:
134+
"""
135+
Error: Can't find the requested theme's version 9.9.9
136+
"""
137+
And the return code should be 1
138+
139+
Scenario: Downloading a theme with --force overwrites existing file
140+
Given an empty directory
141+
142+
When I run `wp theme download twentytwelve`
143+
And I run `wp theme download twentytwelve --force`
144+
Then STDOUT should contain:
145+
"""
146+
Success: Downloaded theme package to
147+
"""
148+
And STDERR should be empty
149+
150+
Scenario: Downloading a theme without --force fails if destination exists
151+
Given an empty directory
152+
153+
When I run `wp theme download twentytwelve`
154+
And I try `wp theme download twentytwelve`
155+
Then STDERR should contain:
156+
"""
157+
Error: Destination file already exists:
158+
"""
159+
And the return code should be 1
160+
161+
Scenario: Downloading an unknown theme fails with a clear error
162+
Given an empty directory
163+
164+
When I try `wp theme download this-theme-does-not-exist-xyz-abc-123`
165+
Then STDERR should contain:
166+
"""
167+
Error: The 'this-theme-does-not-exist-xyz-abc-123' theme could not be found.
168+
"""
169+
And the return code should be 1

‎phpcs.xml.dist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
<!-- Exclude existing classes from the prefix rule as it would break BC to prefix them now. -->
5555
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
56-
<exclude-pattern>*/src/(Plugin_(AutoUpdates_)?|Theme_(Mod_|AutoUpdates_)?)Command\.php$</exclude-pattern>
56+
<exclude-pattern>*/src/(Plugin_(AutoUpdates_|Download_)?|Theme_(Mod_|AutoUpdates_|Download_)?)Command\.php$</exclude-pattern>
5757
</rule>
5858

5959
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound">

‎phpstan.neon.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- tests/phpstan/scan-files.php
1011
treatPhpDocTypesAsCertain: false
1112
ignoreErrors:
1213
- identifier: missingType.iterableValue

‎src/Plugin_Download_Command.php‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
use WP_CLI\Utils;
4+
use WP_CLI\WpOrgApi;
5+
6+
/**
7+
* Downloads plugin zip files from the WordPress.org repository.
8+
*/
9+
class Plugin_Download_Command {
10+
11+
/**
12+
* Downloads a plugin zip package without loading WordPress.
13+
*
14+
* ## OPTIONS
15+
*
16+
* <slug>
17+
* : Slug of the plugin to download.
18+
*
19+
* [--target-path=<path>]
20+
* : Directory to store the downloaded zip file. Defaults to the current directory.
21+
*
22+
* [--version=<version>]
23+
* : Version to download. Accepts a version number or `dev`.
24+
*
25+
* [--force]
26+
* : Overwrite destination file if it already exists.
27+
*
28+
* [--insecure]
29+
* : Retry download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
30+
*
31+
* ## EXAMPLES
32+
*
33+
* $ wp plugin download bbpress
34+
* Downloading bbpress (2.5.9)...
35+
* Success: Downloaded plugin package to /path/to/bbpress.2.5.9.zip
36+
*
37+
* @when before_wp_load
38+
*
39+
* @param array{0: string} $args Positional arguments.
40+
* @param array{target-path?: string, version?: string, force?: bool, insecure?: bool} $assoc_args Associative arguments.
41+
*/
42+
public function __invoke( $args, $assoc_args ) {
43+
$slug = (string) $args[0];
44+
if ( '' === trim( $slug ) ) {
45+
WP_CLI::error( 'Please provide a plugin slug.' );
46+
}
47+
48+
$insecure = Utils\get_flag_value( $assoc_args, 'insecure', false );
49+
$force = Utils\get_flag_value( $assoc_args, 'force', false );
50+
$requested = Utils\get_flag_value( $assoc_args, 'version', null );
51+
$download_dir = Utils\get_flag_value( $assoc_args, 'target-path', getcwd() ?: '.' );
52+
53+
if ( ! is_dir( $download_dir ) ) {
54+
if ( ! @mkdir( $download_dir, 0755, true ) ) {
55+
WP_CLI::error( "Failed to create directory '{$download_dir}'." );
56+
}
57+
}
58+
59+
if ( ! is_writable( $download_dir ) ) {
60+
WP_CLI::error( "'{$download_dir}' is not writable by current user." );
61+
}
62+
63+
try {
64+
$plugin_data = ( new WpOrgApi( [ 'insecure' => $insecure ] ) )->get_plugin_info( $slug );
65+
} catch ( Exception $exception ) {
66+
WP_CLI::error( "The '{$slug}' plugin could not be found. " . $exception->getMessage() );
67+
}
68+
69+
if ( ! is_array( $plugin_data ) || empty( $plugin_data['download_link'] ) || empty( $plugin_data['version'] ) ) {
70+
WP_CLI::error( "The '{$slug}' plugin could not be found." );
71+
}
72+
73+
$download_url = $plugin_data['download_link'];
74+
$version = $plugin_data['version'];
75+
76+
if ( is_string( $requested ) && '' !== $requested && $requested !== $plugin_data['version'] ) {
77+
$current_zip = basename( (string) Utils\parse_url( $download_url, PHP_URL_PATH ) );
78+
if ( 'dev' === $requested ) {
79+
$download_url = str_replace( $current_zip, $slug . '.zip', $download_url );
80+
$version = 'Development Version';
81+
} else {
82+
$download_url = str_replace( $current_zip, $slug . '.' . $requested . '.zip', $download_url );
83+
$version = $requested;
84+
85+
try {
86+
$head_response = Utils\http_request( 'HEAD', $download_url, null, [], [ 'insecure' => (bool) $insecure ] );
87+
} catch ( Exception $exception ) {
88+
WP_CLI::error( $exception->getMessage() );
89+
}
90+
91+
if ( 200 !== (int) $head_response->status_code ) {
92+
WP_CLI::error(
93+
sprintf(
94+
"Can't find the requested plugin's version %s in the WordPress.org plugin repository (HTTP code %d).",
95+
$requested,
96+
$head_response->status_code
97+
)
98+
);
99+
}
100+
}
101+
}
102+
103+
$zip_name = basename( (string) Utils\parse_url( $download_url, PHP_URL_PATH ) );
104+
if ( '' === $zip_name ) {
105+
$zip_name = "{$slug}.zip";
106+
}
107+
108+
$download_file = rtrim( $download_dir, '/\\' ) . DIRECTORY_SEPARATOR . $zip_name;
109+
110+
if ( ! $force && file_exists( $download_file ) ) {
111+
WP_CLI::error( "Destination file already exists: {$download_file}" );
112+
}
113+
114+
$destination_file = $download_file;
115+
$tmp_file = $download_file;
116+
117+
if ( $force && file_exists( $destination_file ) ) {
118+
$tmp_file = $destination_file . '.tmp.' . uniqid( '', true );
119+
}
120+
121+
WP_CLI::log( "Downloading {$slug} ({$version})..." );
122+
123+
try {
124+
$response = Utils\http_request(
125+
'GET',
126+
$download_url,
127+
null,
128+
[],
129+
[
130+
'filename' => $tmp_file,
131+
'insecure' => (bool) $insecure,
132+
]
133+
);
134+
} catch ( Exception $exception ) {
135+
if ( file_exists( $tmp_file ) ) {
136+
unlink( $tmp_file );
137+
}
138+
WP_CLI::error( $exception->getMessage() );
139+
}
140+
141+
if ( 200 !== (int) $response->status_code ) {
142+
if ( file_exists( $tmp_file ) ) {
143+
unlink( $tmp_file );
144+
}
145+
WP_CLI::error( sprintf( 'Failed to download plugin package (HTTP code %d).', $response->status_code ) );
146+
}
147+
148+
if ( $tmp_file !== $destination_file ) {
149+
if ( file_exists( $destination_file ) && ! @unlink( $destination_file ) ) {
150+
WP_CLI::error( "Failed to remove existing destination file: {$destination_file}" );
151+
}
152+
if ( ! @rename( $tmp_file, $destination_file ) ) {
153+
WP_CLI::error( "Failed to move downloaded file into place: {$destination_file}" );
154+
}
155+
}
156+
157+
WP_CLI::success( "Downloaded plugin package to {$destination_file}" );
158+
}
159+
}

0 commit comments

Comments
 (0)