-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1286 from greenpeace/cache-clear-command
PLANET-5870: Cache clear command
- Loading branch information
Showing
8 changed files
with
303 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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,27 @@ | ||
<?php | ||
/** | ||
* Commands. | ||
*/ | ||
|
||
namespace P4\MasterTheme; | ||
|
||
use P4\MasterTheme\Commands\CloudflarePurge; | ||
use P4\MasterTheme\Commands\RunActivator; | ||
use P4\MasterTheme\Commands\SaveCloudflareKey; | ||
|
||
/** | ||
* Class with a static function just because PHP can't autoload functions. | ||
*/ | ||
class Commands { | ||
/** | ||
* Add some WP_CLI commands if we're in CLI. | ||
*/ | ||
public static function load() { | ||
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { | ||
return; | ||
} | ||
RunActivator::register(); | ||
SaveCloudflareKey::register(); | ||
CloudflarePurge::register(); | ||
} | ||
} |
This file contains 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,122 @@ | ||
<?php | ||
|
||
namespace P4\MasterTheme\Commands; | ||
|
||
use CF\Integration\DefaultConfig; | ||
use CF\Integration\DefaultIntegration; | ||
use CF\Integration\DefaultLogger; | ||
use CF\WordPress\DataStore; | ||
use CF\WordPress\WordPressAPI; | ||
use CF\WordPress\WordPressClientAPI; | ||
use P4\MasterTheme\Features; | ||
use WP_CLI; | ||
|
||
/** | ||
* Class CloudflarePurge | ||
*/ | ||
class CloudflarePurge extends Command { | ||
|
||
/** | ||
* The name to access the command. | ||
* | ||
* @return string The command name. | ||
*/ | ||
protected static function get_name(): string { | ||
return 'p4-cf-purge'; | ||
} | ||
|
||
/** | ||
* The description shown in the argument's help. | ||
* | ||
* @return string The description text. | ||
*/ | ||
protected static function get_short_description(): string { | ||
return 'Purge urls from Cloudflare cache'; | ||
} | ||
|
||
/** | ||
* The logic of the command. Has WP_CLI command signature. | ||
* | ||
* @param array|null $args Positional arguments. | ||
* @param array|null $assoc_args Named arguments. | ||
*/ | ||
public static function execute( ?array $args, ?array $assoc_args ): void { | ||
if ( ! Features::is_active( Features::CLOUDFLARE_DEPLOY_PURGE ) ) { | ||
WP_CLI::warning( 'Purge on deploy is not enabled, not purging.' ); | ||
|
||
return; | ||
} | ||
|
||
if ( ! defined( 'CLOUDFLARE_PLUGIN_DIR' ) ) { | ||
define( 'CLOUDFLARE_PLUGIN_DIR', WP_PLUGIN_DIR . '/cloudflare/' ); | ||
} | ||
require_once CLOUDFLARE_PLUGIN_DIR . 'vendor/autoload.php'; | ||
|
||
// The following is just a copy of the plugin's dependency chain, can probably be improved. | ||
$config = new DefaultConfig( file_get_contents( CLOUDFLARE_PLUGIN_DIR . 'config.json', true ) ); | ||
$logger = new DefaultLogger( $config->getValue( 'debug' ) ); | ||
$data_store = new DataStore( $logger ); | ||
$integration_api = new WordPressAPI( $data_store ); | ||
$integration = new DefaultIntegration( $config, $integration_api, $data_store, $logger ); | ||
$api = new WordPressClientAPI( $integration ); | ||
|
||
$zone_id = $api->getZoneTag( get_option( 'cloudflare_cached_domain_name' ) ); | ||
|
||
$urls = self::get_urls( $assoc_args ); | ||
WP_CLI::log( 'About to purge ' . count( $urls ) . ' urls.' ); | ||
|
||
// 30 is Cloudflare's purge api limit. | ||
$chunks = array_chunk( $urls, 30 ); | ||
|
||
foreach ( $chunks as $i => $chunk ) { | ||
// We only use $i to be human readable, increment it immediately. | ||
++ $i; | ||
|
||
$ok = $api->zonePurgeFiles( $zone_id, $chunk ); | ||
// It's unlikely that only some of the chunks will fail, as Cloudflare's API responds with success | ||
// for any url, even if on non-existent domains. Giving a warning per chunk anyway, just in case. | ||
if ( ! $ok ) { | ||
$joined = implode( $chunk, "\n" ); | ||
WP_CLI::warning( "Chunk $i failed, one or more of these didn't work out: \n$joined" ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Determine which urls to purge. Throws error if right args were not passed. | ||
* | ||
* @param array|null $assoc_args The named args passed to the command. | ||
* | ||
* @throws \RuntimeException If you don't provide the right args. | ||
* | ||
* @return array The urls to purge | ||
*/ | ||
private static function get_urls( ?array $assoc_args ): array { | ||
if ( isset( $assoc_args['urls'] ) ) { | ||
return explode( ',', $assoc_args['urls'] ); | ||
} | ||
|
||
if ( isset( $assoc_args['all'] ) ) { | ||
$post_types = isset( $assoc_args['post-types'] ) | ||
? explode( ',', $assoc_args['post-types'] ) | ||
: [ | ||
'post', | ||
'page', | ||
'campaign', | ||
]; | ||
$query_args = [ | ||
'post_type' => $post_types, | ||
'posts_per_page' => - 1, | ||
'post_status' => 'publish', | ||
'ignore_sticky_posts' => 1, | ||
'fields' => 'ids', | ||
]; | ||
|
||
$ids = get_posts( $query_args ); | ||
|
||
return array_map( 'get_permalink', $ids ); | ||
} | ||
|
||
throw new \RuntimeException( 'Please provide either --urls, or purge all urls with --all.' ); | ||
} | ||
} |
This file contains 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,47 @@ | ||
<?php | ||
|
||
namespace P4\MasterTheme\Commands; | ||
|
||
use WP_CLI; | ||
|
||
/** | ||
* Base class for WP_CLI commands. | ||
*/ | ||
abstract class Command { | ||
/** | ||
* Registers the command. | ||
* | ||
* @throws \Exception If WP_CLI doesn't like what we register. | ||
*/ | ||
public static function register(): void { | ||
WP_CLI::add_command( | ||
static::get_name(), | ||
[ static::class, 'execute' ], | ||
[ | ||
'shortdesc' => static::get_short_description(), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* The name to access the command. | ||
* | ||
* @return string The command name. | ||
*/ | ||
abstract protected static function get_name(): string; | ||
|
||
/** | ||
* The description shown in the argument's help. | ||
* | ||
* @return string The description text. | ||
*/ | ||
abstract protected static function get_short_description(): string; | ||
|
||
/** | ||
* The logic of the command. Has WP_CLI command signature. | ||
* | ||
* @param array|null $args Positional arguments. | ||
* @param array|null $assoc_args Named arguments. | ||
*/ | ||
abstract public static function execute( ?array $args, ?array $assoc_args ): void; | ||
} |
This file contains 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,39 @@ | ||
<?php | ||
|
||
namespace P4\MasterTheme\Commands; | ||
|
||
use P4\MasterTheme\Activator; | ||
|
||
/** | ||
* Class RunActivator | ||
*/ | ||
class RunActivator extends Command { | ||
|
||
/** | ||
* The name to access the command. | ||
* | ||
* @return string The command name. | ||
*/ | ||
protected static function get_name(): string { | ||
return 'p4-run-activator'; | ||
} | ||
|
||
/** | ||
* The description shown in the argument's help. | ||
* | ||
* @return string The description text. | ||
*/ | ||
protected static function get_short_description(): string { | ||
return 'Update roles in DB and run migrations scripts'; | ||
} | ||
|
||
/** | ||
* The logic of the command. Has WP_CLI command signature. | ||
* | ||
* @param array|null $args Positional arguments. | ||
* @param array|null $assoc_args Named arguments. | ||
*/ | ||
public static function execute( ?array $args, ?array $assoc_args ): void { | ||
Activator::run(); | ||
} | ||
} |
This file contains 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,55 @@ | ||
<?php | ||
|
||
namespace P4\MasterTheme\Commands; | ||
|
||
use WP_CLI; | ||
|
||
/** | ||
* Class SaveCloudflareKey | ||
*/ | ||
class SaveCloudflareKey extends Command { | ||
|
||
/** | ||
* The name to access the command. | ||
* | ||
* @return string The command name. | ||
*/ | ||
protected static function get_name(): string { | ||
return 'p4-cf-key-in-db'; | ||
} | ||
|
||
/** | ||
* The description shown in the argument's help. | ||
* | ||
* @return string The description text. | ||
*/ | ||
protected static function get_short_description(): string { | ||
return 'Put Cloudflare key in DB from config file'; | ||
} | ||
|
||
/** | ||
* The logic of the command. Has WP_CLI command signature. | ||
* | ||
* @param array|null $args Positional arguments. | ||
* @param array|null $assoc_args Named arguments. | ||
* | ||
* @throws WP_CLI\ExitException If no hostname or Cloudflare key is not present. | ||
*/ | ||
public static function execute( ?array $args, ?array $assoc_args ): void { | ||
$hostname = $args[0] ?? null; | ||
if ( empty( $hostname ) ) { | ||
WP_CLI::error( 'Please specify the hostname.' ); | ||
} | ||
|
||
if ( ! defined( 'CLOUDFLARE_API_KEY' ) || empty( CLOUDFLARE_API_KEY ) ) { | ||
WP_CLI::error( 'CLOUDFLARE_API_KEY constant is not set.' ); | ||
} | ||
|
||
$domain_parts = explode( '.', $hostname ); | ||
|
||
$root_domain = implode( '.', array_slice( $domain_parts, - 2 ) ); | ||
update_option( 'cloudflare_api_key', CLOUDFLARE_API_KEY ); | ||
update_option( 'automatic_platform_optimization', [ 'value' => 1 ] ); | ||
update_option( 'cloudflare_cached_domain_name', $root_domain ); | ||
} | ||
} |
This file contains 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 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