@@ -227,6 +227,9 @@ public function install( $args, $assoc_args ) {
227
227
add_filter ( 'upgrader_source_selection ' , $ filter , 10 );
228
228
}
229
229
230
+ // Add item to cache allowlist if it matches certain URL patterns.
231
+ self ::maybe_cache ( $ slug , $ this ->item_type );
232
+
230
233
if ( $ file_upgrader ->install ( $ slug ) ) {
231
234
$ slug = $ file_upgrader ->result ['destination_name ' ];
232
235
$ result = true ;
@@ -841,6 +844,26 @@ private function parse_url_host_component( $url, $component ) {
841
844
return function_exists ( 'wp_parse_url ' ) ? wp_parse_url ( $ url , $ component ) : parse_url ( $ url , $ component );
842
845
}
843
846
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
+
844
867
/**
845
868
* Get the latest package version based on a given repo slug.
846
869
*
@@ -871,6 +894,13 @@ protected function get_the_latest_github_version( $repo_slug ) {
871
894
);
872
895
}
873
896
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
+
874
904
if ( null === $ decoded_body ) {
875
905
return new \WP_Error ( 500 , 'Empty response received from GitHub.com API ' );
876
906
}
0 commit comments