Created
April 14, 2021 14:14
-
-
Save crstauf/07725140a81b88f331dc8c3c76d8bf1d to your computer and use it in GitHub Desktop.
WordPress mu-plugin that adds a WP CLI command to flush Cloudflare cache.
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
<?php | |
/** | |
* Plugin Name: WP CLI Flush Cloudflare Cache | |
* Description: Register a WP CLI command to flush Cloudflare cache. | |
* Author: Caleb Stauffer | |
* Author URI: https://develop.calebstauffer.com | |
* Plugin URI: https://gist.github.com/crstauf/07725140a81b88f331dc8c3c76d8bf1d | |
*/ | |
if ( !defined( 'WP_CLI' ) || !WP_CLI ) | |
return; | |
if ( !function_exists( 'wpcli_flush_cloudflare_cache' ) ) { | |
function wpcli_flush_cloudflare_cache( array $args, array $assoc_args ) : void { | |
$zone_id = WP_CLI\Utils\get_flag_value( $assoc_args, 'zone_id', constant( 'CLOUDFLARE_ZONE_ID' ) ); | |
$email_address = WP_CLI\Utils\get_flag_value( $assoc_args, 'email', constant( 'CLOUDFLARE_EMAIL_ADDRESS' ) ); | |
$api_key = WP_CLI\Utils\get_flag_value( $assoc_args, 'api_key', constant( 'CLOUDFLARE_API_KEY' ) ); | |
$undefined_count = 0; | |
if ( empty( $zone_id ) ) { | |
WP_CLI::debug( 'Cloudflare zone ID is undefined: define `CLOUDFLARE_ZONE_ID` PHP constant, or pass `zone_id` command argument.' ); | |
$undefined_count++; | |
} | |
if ( empty( $email_address ) ) { | |
WP_CLI::debug( 'Cloudflare account email address is undefined: define `CLOUDFLARE_EMAIL_ADDRESS` PHP constant, or pass `email` command argument.' ); | |
$undefined_count++; | |
} | |
if ( empty( $api_key ) ) { | |
WP_CLI::debug( 'Cloudflare API key is undefined: define `CLOUDFLARE_API_KEY` PHP constant, or pass `api_key` command argument.' ); | |
$undefined_count++; | |
} | |
if ( $undefined_count ) { | |
$error_format = 'Cloudflare API required %s undefined.'; | |
$error = sprintf( $error_format, _n( 'parameter is', 'parameters are', $undefined_count ) ); | |
WP_CLI::error( $error ); | |
} | |
$args = array( | |
'headers' => array( | |
'X-Auth-Email' => $email_address, | |
'X-Auth-Key' => $api_key, | |
'Content-Type' => 'application/json', | |
), | |
'body' => wp_json_encode( array( | |
'purge_everything' => true, | |
) ), | |
); | |
$url = sprintf( 'https://api.cloudflare.com/client/v4/zones/%s/purge_cache', $zone_id ); | |
$response = wp_remote_post( $url, $args ); | |
if ( is_wp_error( $response ) ) { | |
WP_CLI::debug( $response->get_error_message() ); | |
WP_CLI::error( 'Unable to connect to Cloudflare API.' ); | |
} | |
$body = wp_remote_retrieve_body( $response ); | |
$body = json_decode( $body ); | |
if ( !$body->success ) { | |
foreach ( $body->errors as $error ) | |
WP_CLI::debug( $error->message ); | |
WP_CLI::error( 'Cloudflare was unable to flush the cache.' ); | |
} | |
WP_CLI::success( 'Cloudflare cache was flushed.' ); | |
} | |
WP_CLI::add_command( 'cloudflare flush', 'wpcli_flush_cloudflare_cache' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment