Skip to content
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

Cache wp plugin install from GitHub #363

Open
1 task
Luc45 opened this issue Apr 28, 2023 · 2 comments · May be fixed by #385
Open
1 task

Cache wp plugin install from GitHub #363

Luc45 opened this issue Apr 28, 2023 · 2 comments · May be fixed by #385

Comments

@Luc45
Copy link

Luc45 commented Apr 28, 2023

Feature Request

Describe your use case and the problem you are facing

Downloads from wp plugin install <GitHub URL> are not cached.

Describe the solution you'd like

I'd like these downloads to be cached, or maybe optionally cached.

Eg: wp plugin install https://github.com/woocommerce/woocommerce/releases/download/7.6.1/woocommerce.zip

I don't know if this is the best possible solution, but I'm currently using this mu-plugin in my site to cache GitHub downloads:

<?php
/*
 * Plugin name: WP CLI GitHub Cache
 */

if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
	return;
}

/*
 * Cache downloads of "wp plugin install" from GitHub repos.
 */
WP_CLI::add_hook( 'before_run_command', static function ( $args, $assoc_args, $options ) {
	// Early bail: Unexpected value;
	if ( ! is_array( $args ) || count( $args ) < 3 ) {
		return;
	}

	// We want only "plugin install"
	if ( $args[0] !== 'plugin' || $args[1] !== 'install' ) {
		return;
	}

	// We want only plugin installs of GitHub repos.
	if ( stripos( $args[2], 'github.com' ) === false ) {
		return;
	}

	// Invalid URL.
	if ( ! filter_var( $args[2], FILTER_VALIDATE_URL ) ) {
		return;
	}

	$parts = explode( '/', $args[2] );

	$last_part = end( $parts );

	if ( ! str_ends_with( $last_part, '.zip' ) ) {
		return;
	}

	WP_CLI::get_http_cache_manager()->whitelist_package( $args[2], 'plugin', basename( $last_part, '.zip' ), 'github', 86400 );
} );

Result:

wp plugin install https://github.com/woocommerce/woocommerce/releases/download/7.6.1/woocommerce.zip --force
Downloading installation package from https://github.com/woocommerce/woocommerce/releases/download/7.6.1/woocommerce.zip...
Using cached file '/var/cd-cache/.wp-cli/cache/plugin/woocommerce-github.zip'...
Unpacking the package...
Installing the plugin...
Removing the old version of the plugin...
Plugin updated successfully.
Success: Installed 1 of 1 plugins

The problem here seems to be how to invalidate cache if the remote URL changed. The approach that I'm taking is caching for 1 day, which is a short period compared to the default 6 months of TTL.

@danielbachhuber
Copy link
Member

Thanks for the suggestion, @Luc45 !

I'm open to a PR for this. We should make sure to include the full URL in the cache key, though.

@pfefferle pfefferle linked a pull request Nov 16, 2023 that will close this issue
@Souptik2001
Copy link

I am taking this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants