-
Notifications
You must be signed in to change notification settings - Fork 84
Add plugin/theme download subcommands runnable before WordPress load
#527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/add-wp-plugin-download-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da78823
Initial plan
Copilot 298a82d
Add before-WP-load plugin and theme download commands
Copilot 5603469
Fix review comments: slug validation, version URL validation, HTTP st…
Copilot 767d5da
Fix feature test assertions to use captured file path from STDOUT
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.