Skip to content

Commit 110989e

Browse files
authored
Merge pull request #385 from pfefferle/fix/363
2 parents da44390 + d27b3b5 commit 110989e

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

features/plugin-install.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Feature: Install WordPress plugins
22

3+
Background:
4+
Given an empty cache
5+
36
Scenario: Branch names should be removed from Github projects
47
Given a WP install
58

features/upgradables.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,35 @@ Feature: Manage WordPress themes and plugins
212212
| type | type_name | item | item_title | version | zip_file | file_to_check |
213213
| theme | Theme | moina | Moina | 1.1.2 | https://wordpress.org/themes/download/moina.1.1.2.zip | {CONTENT_DIR}/moina/style.css |
214214
| plugin | Plugin | category-checklist-tree | Category Checklist Tree | 1.2 | https://downloads.wordpress.org/plugin/category-checklist-tree.1.2.zip | {CONTENT_DIR}/category-checklist-tree/category-checklist-tree.php |
215+
216+
@require-wp-4.5
217+
Scenario Outline: Caches certain GitHub URLs
218+
Given a WP install
219+
And I run `wp plugin delete --all`
220+
221+
When I run `wp plugin install <zip_file>`
222+
Then STDOUT should contain:
223+
"""
224+
Plugin installed successfully
225+
"""
226+
And STDOUT should not contain:
227+
"""
228+
Using cached file '{SUITE_CACHE_DIR}/plugin/<item>-<version>
229+
"""
230+
231+
When I run `wp plugin delete --all`
232+
And I run `wp plugin install <zip_file>`
233+
Then STDOUT should contain:
234+
"""
235+
Plugin installed successfully
236+
"""
237+
And STDOUT should contain:
238+
"""
239+
Using cached file '{SUITE_CACHE_DIR}/plugin/<item>-<version>
240+
"""
241+
242+
Examples:
243+
| item | version | zip_file |
244+
| one-time-login | 0.4.0 | https://github.com/danielbachhuber/one-time-login/releases/latest |
245+
| preferred-languages | 1.8.0 | https://github.com/swissspidy/preferred-languages/releases/download/1.8.0/preferred-languages.zip |
246+
| generic-example-plugin | 0.1.1 | https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.1.zip |

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ public function install( $args, $assoc_args ) {
227227
add_filter( 'upgrader_source_selection', $filter, 10 );
228228
}
229229

230+
// Add item to cache allowlist if it matches certain URL patterns.
231+
self::maybe_cache( $slug, $this->item_type );
232+
230233
if ( $file_upgrader->install( $slug ) ) {
231234
$slug = $file_upgrader->result['destination_name'];
232235
$result = true;
@@ -841,6 +844,26 @@ private function parse_url_host_component( $url, $component ) {
841844
return function_exists( 'wp_parse_url' ) ? wp_parse_url( $url, $component ) : parse_url( $url, $component );
842845
}
843846

847+
/**
848+
* Add versioned GitHub URLs to cache allowlist.
849+
*
850+
* @param string $url The URL to check.
851+
*/
852+
protected static function maybe_cache( $url, $item_type ) {
853+
$matches = [];
854+
855+
// cache release URLs like `https://github.com/wp-cli-test/generic-example-plugin/releases/download/v0.1.0/generic-example-plugin.0.1.0.zip`
856+
if ( preg_match( '#github\.com/[^/]+/([^/]+)/releases/download/v?([^/]+)/.+\.zip#', $url, $matches ) ) {
857+
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[1], $matches[2] );
858+
// cache archive URLs like `https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.0.zip`
859+
} elseif ( preg_match( '#github\.com/[^/]+/([^/]+)/archive/(version/|)v?([^/]+)\.zip#', $url, $matches ) ) {
860+
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[1], $matches[3] );
861+
// cache release URLs like `https://api.github.com/repos/danielbachhuber/one-time-login/zipball/v0.4.0`
862+
} elseif ( preg_match( '#api\.github\.com/repos/[^/]+/([^/]+)/zipball/v?([^/]+)#', $url, $matches ) ) {
863+
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[1], $matches[2] );
864+
}
865+
}
866+
844867
/**
845868
* Get the latest package version based on a given repo slug.
846869
*
@@ -871,6 +894,13 @@ protected function get_the_latest_github_version( $repo_slug ) {
871894
);
872895
}
873896

897+
if ( 404 === wp_remote_retrieve_response_code( $response ) ) {
898+
return new \WP_Error(
899+
$decoded_body->status,
900+
$decoded_body->message
901+
);
902+
}
903+
874904
if ( null === $decoded_body ) {
875905
return new \WP_Error( 500, 'Empty response received from GitHub.com API' );
876906
}

0 commit comments

Comments
 (0)