Skip to content

Commit d0dc756

Browse files
committed
cache certain GitHub URLs
1 parent 7118325 commit d0dc756

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ public function install( $args, $assoc_args ) {
196196
add_filter( 'upgrader_source_selection', $filter, 10, 3 );
197197
}
198198

199+
// Check if the URL the URL is cachable and whitelist it then.
200+
self::maybe_cache( $slug, $this->item_type );
201+
199202
if ( $file_upgrader->install( $slug ) ) {
200203
$slug = $file_upgrader->result['destination_name'];
201204
$result = true;
@@ -771,4 +774,21 @@ private function parse_url_host_component( $url, $component ) {
771774
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
772775
return function_exists( 'wp_parse_url' ) ? wp_parse_url( $url, $component ) : parse_url( $url, $component );
773776
}
777+
778+
/**
779+
* Whitelist GitHub URLs for caching.
780+
*
781+
* @param string $url The URL to check.
782+
*/
783+
public static function maybe_cache( $url, $item_type ) {
784+
$matches = [];
785+
786+
// 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`
787+
if ( preg_match( '#github\.com/[^/]+/([^/]+)/releases/download/tags/([^/]+)/(.+)\.zip#', $url, $matches ) ) {
788+
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[2], $matches[3] );
789+
// cache archive URLs like `https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.0.zip`
790+
} elseif ( preg_match( '#github\.com/[^/]+/([^/]+)/archive/(version/|)([^/]+)\.zip#', $url, $matches ) ) {
791+
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[1], $matches[3] );
792+
}
793+
}
774794
}

0 commit comments

Comments
 (0)