add wp-rocket
This commit is contained in:
394
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/APIClient.php
Normal file
394
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/APIClient.php
Normal file
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare;
|
||||
|
||||
use stdClass;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Cloudflare API Client.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class APIClient {
|
||||
const CLOUDFLARE_API = 'https://api.cloudflare.com/client/v4/';
|
||||
|
||||
/**
|
||||
* Email address for API authentication.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
/**
|
||||
* API key for API authentication.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $api_key;
|
||||
|
||||
/**
|
||||
* Zone ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $zone_id;
|
||||
|
||||
/**
|
||||
* An array of arguments for wp_remote_get.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $args = [];
|
||||
|
||||
/**
|
||||
* HTTP headers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $headers = [];
|
||||
|
||||
/**
|
||||
* APIClient constructor.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $useragent The user agent for this plugin or package. For example, "wp-rocket/3.5".
|
||||
*/
|
||||
public function __construct( $useragent ) {
|
||||
$this->args = [
|
||||
'timeout' => 30, // Increase from default of 5 to give extra time for the plugin to process story for exporting.
|
||||
'sslverify' => true,
|
||||
'body' => [],
|
||||
];
|
||||
$this->headers = [
|
||||
'X-Auth-Email' => '',
|
||||
'X-Auth-Key' => '',
|
||||
'User-Agent' => $useragent,
|
||||
'Content-type' => 'application/json',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the API credentials.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $email The email associated with the Cloudflare account.
|
||||
* @param string $api_key The API key for the associated Cloudflare account.
|
||||
* @param string $zone_id The zone ID.
|
||||
*/
|
||||
public function set_api_credentials( $email, $api_key, $zone_id ) {
|
||||
$this->email = $email;
|
||||
$this->api_key = $api_key;
|
||||
$this->zone_id = $zone_id;
|
||||
|
||||
$this->headers['X-Auth-Email'] = $email;
|
||||
$this->headers['X-Auth-Key'] = $api_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get zone data.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function get_zones() {
|
||||
return $this->get( "zones/{$this->zone_id}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the zone's page rules.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function list_pagerules() {
|
||||
return $this->get( "zones/{$this->zone_id}/pagerules?status=active" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the cache.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function purge() {
|
||||
return $this->delete( "zones/{$this->zone_id}/purge_cache", [ 'purge_everything' => true ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purges the given URLs.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array|null $urls An array of URLs that should be removed from cache.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function purge_files( array $urls ) {
|
||||
return $this->delete( "zones/{$this->zone_id}/purge_cache", [ 'files' => $urls ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the zone's browser cache TTL setting.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function change_browser_cache_ttl( $value ) {
|
||||
return $this->change_setting( 'browser_cache_ttl', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the zone's rocket loader setting.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function change_rocket_loader( $value ) {
|
||||
return $this->change_setting( 'rocket_loader', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the zone's minify setting.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function change_minify( $value ) {
|
||||
return $this->change_setting( 'minify', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the zone's cache level.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function change_cache_level( $value ) {
|
||||
return $this->change_setting( 'cache_level', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the zone's development mode.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function change_development_mode( $value ) {
|
||||
return $this->change_setting( 'development_mode', $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the given setting.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $setting Name of the setting to change.
|
||||
* @param string $value New setting's value.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
protected function change_setting( $setting, $value ) {
|
||||
return $this->patch( "zones/{$this->zone_id}/settings/{$setting}", [ 'value' => $value ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all of the Cloudflare settings.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function get_settings() {
|
||||
return $this->get( "zones/{$this->zone_id}/settings" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Cloudflare's IPs.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
public function get_ips() {
|
||||
return $this->get( '/ips' );
|
||||
}
|
||||
|
||||
/**
|
||||
* API call method for sending requests using GET.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $path Path of the endpoint.
|
||||
* @param array $data Data to be sent along with the request.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
protected function get( $path, array $data = [] ) {
|
||||
return $this->request( $path, $data, 'get' );
|
||||
}
|
||||
|
||||
/**
|
||||
* API call method for sending requests using DELETE.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $path Path of the endpoint.
|
||||
* @param array $data Data to be sent along with the request.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
protected function delete( $path, array $data = [] ) {
|
||||
return $this->request( $path, $data, 'delete' );
|
||||
}
|
||||
|
||||
/**
|
||||
* API call method for sending requests using PATCH.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $path Path of the endpoint.
|
||||
* @param array $data Data to be sent along with the request.
|
||||
*
|
||||
* @return stdClass Cloudflare response packet.
|
||||
*/
|
||||
protected function patch( $path, array $data = [] ) {
|
||||
return $this->request( $path, $data, 'patch' );
|
||||
}
|
||||
|
||||
/**
|
||||
* API call method for sending requests using GET, POST, PUT, DELETE OR PATCH.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @author James Bell <james@james-bell.co.uk> - credit for original code adapted for version 1.0.
|
||||
* @author WP Media
|
||||
*
|
||||
* @param string $path Path of the endpoint.
|
||||
* @param array $data Data to be sent along with the request.
|
||||
* @param string $method Type of method that should be used ('GET', 'DELETE', 'PATCH').
|
||||
*
|
||||
* @return stdClass response object.
|
||||
* @throws AuthenticationException When email or api key are not set.
|
||||
* @throws UnauthorizedException When Cloudflare's API returns a 401 or 403.
|
||||
*/
|
||||
protected function request( $path, array $data = [], $method = 'get' ) {
|
||||
if ( '/ips' !== $path && ! $this->is_authorized() ) {
|
||||
throw new AuthenticationException( 'Authentication information must be provided.' );
|
||||
}
|
||||
|
||||
$response = $this->do_remote_request( $path, $data, $method );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
throw new Exception( $response->get_error_message() );
|
||||
}
|
||||
|
||||
$data = wp_remote_retrieve_body( $response );
|
||||
|
||||
if ( empty( $data ) ) {
|
||||
throw new Exception( __( 'Cloudflare did not provide any reply. Please try again later.', 'rocket' ) );
|
||||
}
|
||||
|
||||
$data = json_decode( $data );
|
||||
|
||||
if ( empty( $data->success ) ) {
|
||||
$errors = [];
|
||||
foreach ( $data->errors as $error ) {
|
||||
if ( 6003 === $error->code || 9103 === $error->code ) {
|
||||
$msg = __( 'Incorrect Cloudflare email address or API key.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
throw new Exception( $msg );
|
||||
}
|
||||
if ( 7003 === $error->code ) {
|
||||
$msg = __( 'Incorrect Cloudflare Zone ID.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
throw new Exception( $msg );
|
||||
}
|
||||
$errors[] = $error->message;
|
||||
}
|
||||
throw new Exception( wp_sprintf_l( '%l ', $errors ) );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the email and API key for the API credentials are set.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return bool true if authorized; else false.
|
||||
*/
|
||||
private function is_authorized() {
|
||||
return (
|
||||
isset( $this->email, $this->api_key )
|
||||
&&
|
||||
false !== filter_var( $this->email, FILTER_VALIDATE_EMAIL )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the request remote cURL request.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $path Path of the endpoint.
|
||||
* @param array $data Data to be sent along with the request.
|
||||
* @param string $method Type of method that should be used ('GET', 'DELETE', 'PATCH').
|
||||
*
|
||||
* @return array curl response packet.
|
||||
*/
|
||||
private function do_remote_request( $path, array $data, $method ) {
|
||||
$this->args['method'] = isset( $method ) ? strtoupper( $method ) : 'GET';
|
||||
|
||||
if ( '/ips' !== $path ) {
|
||||
$this->args['headers'] = $this->headers;
|
||||
}
|
||||
|
||||
$this->args['body'] = [];
|
||||
|
||||
if ( ! empty( $data ) ) {
|
||||
$this->args['body'] = wp_json_encode( $data );
|
||||
}
|
||||
|
||||
$response = wp_remote_request( self::CLOUDFLARE_API . $path, $this->args );
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class AuthenticationException extends RuntimeException {}
|
481
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Cloudflare.php
Normal file
481
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Cloudflare.php
Normal file
@@ -0,0 +1,481 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare;
|
||||
|
||||
use Exception;
|
||||
use stdClass;
|
||||
use WP_Error;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
|
||||
/**
|
||||
* Cloudflare
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class Cloudflare {
|
||||
|
||||
/**
|
||||
* Options Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Cloudflare API instance.
|
||||
*
|
||||
* @var APIClient
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* WP_Error if Cloudflare Credentials are not valid.
|
||||
*
|
||||
* @var WP_Error
|
||||
*/
|
||||
private $cloudflare_api_error;
|
||||
|
||||
/**
|
||||
* Creates an instance of Cloudflare Addon.
|
||||
*
|
||||
* @param Options_Data $options WP Rocket options instance.
|
||||
* @param APIClient $api Cloudflare API instance.
|
||||
*/
|
||||
public function __construct( Options_Data $options, APIClient $api ) {
|
||||
$this->options = $options;
|
||||
$this->cloudflare_api_error = null;
|
||||
$this->api = $api;
|
||||
// Update api_error with WP_Error if credentials are not valid.
|
||||
// Update API with Cloudflare instance with correct auth data.
|
||||
$this->get_cloudflare_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Cloudflare\Api instance & the zone_id corresponding to the domain.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return Object Cloudflare instance & zone_id if credentials are correct, WP_Error otherwise.
|
||||
*/
|
||||
public function get_cloudflare_instance() {
|
||||
$cf_email = $this->options->get( 'cloudflare_email', null );
|
||||
$cf_api_key = defined( 'WP_ROCKET_CF_API_KEY' ) ? WP_ROCKET_CF_API_KEY : $this->options->get( 'cloudflare_api_key', null );
|
||||
$cf_zone_id = $this->options->get( 'cloudflare_zone_id', null );
|
||||
$is_api_keys_valid_cf = get_transient( 'rocket_cloudflare_is_api_keys_valid' );
|
||||
|
||||
if ( false === $is_api_keys_valid_cf ) {
|
||||
$is_api_keys_valid_cf = $this->is_api_keys_valid( $cf_email, $cf_api_key, $cf_zone_id );
|
||||
set_transient( 'rocket_cloudflare_is_api_keys_valid', $is_api_keys_valid_cf, 2 * WEEK_IN_SECONDS );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $is_api_keys_valid_cf ) ) {
|
||||
// Sets Cloudflare API as WP_Error if credentials are not valid.
|
||||
$this->cloudflare_api_error = $is_api_keys_valid_cf;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Sets Cloudflare Valid Credentials and User Agent.
|
||||
$this->api->set_api_credentials( $cf_email, $cf_api_key, $cf_zone_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Cloudflare input data.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $cf_email Cloudflare email.
|
||||
* @param string $cf_api_key Cloudflare API key.
|
||||
* @param string $cf_zone_id Cloudflare zone ID.
|
||||
*
|
||||
* @return stdClass true if credentials are ok, WP_Error otherwise.
|
||||
*/
|
||||
public function is_api_keys_valid( $cf_email, $cf_api_key, $cf_zone_id ) {
|
||||
if ( empty( $cf_email ) || empty( $cf_api_key ) ) {
|
||||
return new WP_Error(
|
||||
'cloudflare_credentials_empty',
|
||||
sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Cloudflare email and/or API key are not set. Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( empty( $cf_zone_id ) ) {
|
||||
$msg = __( 'Missing Cloudflare Zone ID.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_no_zone_id', $msg );
|
||||
}
|
||||
|
||||
try {
|
||||
$this->api->set_api_credentials( $cf_email, $cf_api_key, $cf_zone_id );
|
||||
|
||||
$cf_zone = $this->api->get_zones();
|
||||
$zone_found = false;
|
||||
$site_url = get_site_url();
|
||||
|
||||
if ( function_exists( 'domain_mapping_siteurl' ) ) {
|
||||
$site_url = domain_mapping_siteurl( $site_url );
|
||||
}
|
||||
|
||||
if ( ! empty( $cf_zone->result ) ) {
|
||||
$parsed_url = wp_parse_url( $site_url );
|
||||
if ( false !== strpos( strtolower( $parsed_url['host'] ), $cf_zone->result->name ) ) {
|
||||
$zone_found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $zone_found ) {
|
||||
$msg = __( 'It looks like your domain is not set up on Cloudflare.', 'rocket' );
|
||||
|
||||
$msg .= ' ' . sprintf(
|
||||
/* translators: %1$s = opening link; %2$s = closing link */
|
||||
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
|
||||
// translators: Documentation exists in EN, FR; use localized URL if applicable.
|
||||
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
return new WP_Error( 'cloudflare_wrong_zone_id', $msg );
|
||||
}
|
||||
|
||||
$this->cloudflare_api_error = null;
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_invalid_auth', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if CF has the $action_value set as a Page Rule.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $action_value Cache_everything.
|
||||
*
|
||||
* @return mixed Object|bool true / false if $action_value was found or not, WP_Error otherwise.
|
||||
*/
|
||||
public function has_page_rule( $action_value ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_page_rule = $this->api->list_pagerules();
|
||||
$cf_page_rule_arr = wp_json_encode( $cf_page_rule );
|
||||
|
||||
return preg_match( '/' . $action_value . '/', $cf_page_rule_arr );
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_page_rule_failed', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Cloudflare cache.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return mixed Object|bool true if the purge is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function purge_cloudflare() {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_purge = $this->api->purge();
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_purge_failed', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Cloudflare Cache by URL
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param WP_Post $post The post object.
|
||||
* @param array $purge_urls URLs cache files to remove.
|
||||
* @param string $lang The post language.
|
||||
*
|
||||
* @return mixed Object|bool true if the purge is successful, WP_Error otherwise
|
||||
*/
|
||||
public function purge_by_url( $post, $purge_urls, $lang ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_purge = $this->api->purge_files( $purge_urls );
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_purge_failed', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Browser Cache TTL in Cloudflare.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $mode Value for Cloudflare browser cache TTL.
|
||||
*
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function set_browser_cache_ttl( $mode ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_return = $this->api->change_browser_cache_ttl( (int) $mode );
|
||||
return $mode;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_browser_cache', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Rocket Loader.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $mode Value for Cloudflare Rocket Loader.
|
||||
*
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function set_rocket_loader( $mode ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_return = $this->api->change_rocket_loader( $mode );
|
||||
return $mode;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_rocket_loader', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Minification.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $mode Value for Cloudflare minification.
|
||||
*
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function set_minify( $mode ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
$cf_minify_settings = [
|
||||
'css' => $mode,
|
||||
'html' => $mode,
|
||||
'js' => $mode,
|
||||
];
|
||||
|
||||
try {
|
||||
$cf_return = $this->api->change_minify( $cf_minify_settings );
|
||||
return $mode;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_minification', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Caching level.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $mode Value for Cloudflare caching level.
|
||||
*
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function set_cache_level( $mode ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_return = $this->api->change_cache_level( $mode );
|
||||
return $mode;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_cache_level', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Cloudflare Development mode.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $mode Value for Cloudflare development mode.
|
||||
*
|
||||
* @return mixed Object|String Mode value if the update is successful, WP_Error otherwise.
|
||||
*/
|
||||
public function set_devmode( $mode ) {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
if ( 0 === (int) $mode ) {
|
||||
$value = 'off';
|
||||
} else {
|
||||
$value = 'on';
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_return = $this->api->change_development_mode( $value );
|
||||
|
||||
if ( 'on' === $value ) {
|
||||
wp_schedule_single_event( time() + 3 * HOUR_IN_SECONDS, 'rocket_cron_deactivate_cloudflare_devmode' );
|
||||
}
|
||||
|
||||
return $value;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_dev_mode', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the current Cloudflare settings for a given domain.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return mixed bool|Array Array of Cloudflare settings, false if any error connection to Cloudflare.
|
||||
*/
|
||||
public function get_settings() {
|
||||
if ( is_wp_error( $this->cloudflare_api_error ) ) {
|
||||
return $this->cloudflare_api_error;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_settings = $this->api->get_settings();
|
||||
|
||||
foreach ( $cf_settings->result as $cloudflare_option ) {
|
||||
switch ( $cloudflare_option->id ) {
|
||||
case 'browser_cache_ttl':
|
||||
$browser_cache_ttl = $cloudflare_option->value;
|
||||
break;
|
||||
case 'cache_level':
|
||||
$cache_level = $cloudflare_option->value;
|
||||
break;
|
||||
case 'rocket_loader':
|
||||
$rocket_loader = $cloudflare_option->value;
|
||||
break;
|
||||
case 'minify':
|
||||
$cf_minify = $cloudflare_option->value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$cf_minify_value = 'on';
|
||||
|
||||
if ( 'off' === $cf_minify->js || 'off' === $cf_minify->css || 'off' === $cf_minify->html ) {
|
||||
$cf_minify_value = 'off';
|
||||
}
|
||||
|
||||
$cf_settings_array = [
|
||||
'cache_level' => $cache_level,
|
||||
'minify' => $cf_minify_value,
|
||||
'rocket_loader' => $rocket_loader,
|
||||
'browser_cache_ttl' => $browser_cache_ttl,
|
||||
];
|
||||
|
||||
return $cf_settings_array;
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'cloudflare_current_settings', $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cloudflare IPs. No API validation needed, all exceptions returns the default CF IPs array.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return Object Result of API request if successful, default CF IPs otherwise.
|
||||
*/
|
||||
public function get_cloudflare_ips() {
|
||||
$cf_ips = get_transient( 'rocket_cloudflare_ips' );
|
||||
if ( false !== $cf_ips ) {
|
||||
return $cf_ips;
|
||||
}
|
||||
|
||||
try {
|
||||
$cf_ips = $this->api->get_ips();
|
||||
|
||||
if ( empty( $cf_ips->success ) ) {
|
||||
// Set default IPs from Cloudflare if call to Cloudflare /ips API does not contain a success.
|
||||
// Prevents from making API calls on each page load.
|
||||
$cf_ips = $this->get_default_ips();
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
// Set default IPs from Cloudflare if call to Cloudflare /ips API fails.
|
||||
// Prevents from making API calls on each page load.
|
||||
$cf_ips = $this->get_default_ips();
|
||||
}
|
||||
|
||||
set_transient( 'rocket_cloudflare_ips', $cf_ips, 2 * WEEK_IN_SECONDS );
|
||||
|
||||
return $cf_ips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default Cloudflare IPs.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return stdClass Default Cloudflare connecting IPs.
|
||||
*/
|
||||
private function get_default_ips() {
|
||||
$cf_ips = (object) [
|
||||
'result' => (object) [],
|
||||
'success' => true,
|
||||
'errors' => [],
|
||||
'messages' => [],
|
||||
];
|
||||
|
||||
$cf_ips->result->ipv4_cidrs = [
|
||||
'173.245.48.0/20',
|
||||
'103.21.244.0/22',
|
||||
'103.22.200.0/22',
|
||||
'103.31.4.0/22',
|
||||
'141.101.64.0/18',
|
||||
'108.162.192.0/18',
|
||||
'190.93.240.0/20',
|
||||
'188.114.96.0/20',
|
||||
'197.234.240.0/22',
|
||||
'198.41.128.0/17',
|
||||
'162.158.0.0/15',
|
||||
'104.16.0.0/12',
|
||||
'172.64.0.0/13',
|
||||
'131.0.72.0/22',
|
||||
];
|
||||
|
||||
$cf_ips->result->ipv6_cidrs = [
|
||||
'2400:cb00::/32',
|
||||
'2606:4700::/32',
|
||||
'2803:f800::/32',
|
||||
'2405:b500::/32',
|
||||
'2405:8100::/32',
|
||||
'2a06:98c0::/29',
|
||||
'2c0f:f248::/32',
|
||||
];
|
||||
|
||||
return $cf_ips;
|
||||
}
|
||||
}
|
608
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Subscriber.php
Normal file
608
wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/Subscriber.php
Normal file
@@ -0,0 +1,608 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Admin\Options;
|
||||
|
||||
/**
|
||||
* Cloudflare Subscriber.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
|
||||
/**
|
||||
* Cloudflare instance.
|
||||
*
|
||||
* @var Cloudflare
|
||||
*/
|
||||
private $cloudflare;
|
||||
|
||||
/**
|
||||
* Options Data instance.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Options instance.
|
||||
*
|
||||
* @var Options
|
||||
*/
|
||||
private $options_api;
|
||||
|
||||
/**
|
||||
* Creates an instance of the Cloudflare Subscriber.
|
||||
*
|
||||
* @param Cloudflare $cloudflare Cloudflare instance.
|
||||
* @param Options_Data $options WP Rocket options instance.
|
||||
* @param Options $options_api Options instance.
|
||||
*/
|
||||
public function __construct( Cloudflare $cloudflare, Options_Data $options, Options $options_api ) {
|
||||
$this->options = $options;
|
||||
$this->options_api = $options_api;
|
||||
$this->cloudflare = $cloudflare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the subscribed events.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return array subscribed events => callbacks.
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
$slug = rocket_get_constant( 'WP_ROCKET_SLUG', 'wp_rocket_settings' );
|
||||
|
||||
return [
|
||||
'rocket_varnish_ip' => 'set_varnish_localhost',
|
||||
'rocket_varnish_purge_request_host' => 'set_varnish_purge_request_host',
|
||||
'rocket_cron_deactivate_cloudflare_devmode' => 'deactivate_devmode',
|
||||
'after_rocket_clean_domain' => 'auto_purge',
|
||||
'after_rocket_clean_post' => [ 'auto_purge_by_url', 10, 3 ],
|
||||
'admin_post_rocket_purge_cloudflare' => 'purge_cache',
|
||||
'init' => [ 'set_real_ip', 1 ],
|
||||
'update_option_' . $slug => [ 'save_cloudflare_options', 10, 2 ],
|
||||
'pre_update_option_' . $slug => [ 'save_cloudflare_old_settings', 10, 2 ],
|
||||
'admin_notices' => [
|
||||
[ 'maybe_display_purge_notice' ],
|
||||
[ 'maybe_print_update_settings_notice' ],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Varnish IP to localhost if Cloudflare is active.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string|array $varnish_ip Varnish IP.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function set_varnish_localhost( $varnish_ip ) {
|
||||
if ( ! $this->should_filter_varnish() ) {
|
||||
return $varnish_ip;
|
||||
}
|
||||
|
||||
if ( is_string( $varnish_ip ) ) {
|
||||
$varnish_ip = (array) $varnish_ip;
|
||||
}
|
||||
|
||||
$varnish_ip[] = 'localhost';
|
||||
|
||||
return $varnish_ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Host header to the website domain if Cloudflare is active.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param string $host the host header value.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function set_varnish_purge_request_host( $host ) {
|
||||
if ( ! $this->should_filter_varnish() ) {
|
||||
return $host;
|
||||
}
|
||||
|
||||
return wp_parse_url( home_url(), PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we should filter the value for the Varnish purge.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function should_filter_varnish() {
|
||||
// This filter is documented in inc/classes/subscriber/Addons/Varnish/VarnishSubscriber.php.
|
||||
if ( ! apply_filters( 'do_rocket_varnish_http_purge', false ) && ! $this->options->get( 'varnish_auto_purge', 0 ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Automatically set Cloudflare development mode value to off after 3 hours to reflect Cloudflare behaviour.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function deactivate_devmode() {
|
||||
$this->options->set( 'cloudflare_devmode', 'off' );
|
||||
$this->options_api->set( 'settings', $this->options->get_options() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Cloudflare cache automatically if Cache Everything is set as a Page Rule.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function auto_purge() {
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cf_cache_everything = $this->cloudflare->has_page_rule( 'cache_everything' );
|
||||
|
||||
if ( is_wp_error( $cf_cache_everything ) || ! $cf_cache_everything ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Purge CloudFlare.
|
||||
$this->cloudflare->purge_cloudflare();
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge Cloudflare cache URLs automatically if Cache Everything is set as a Page Rule.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param WP_Post $post The post object.
|
||||
* @param array $purge_urls URLs cache files to remove.
|
||||
* @param string $lang The post language.
|
||||
*/
|
||||
public function auto_purge_by_url( $post, $purge_urls, $lang ) {
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cf_cache_everything = $this->cloudflare->has_page_rule( 'cache_everything' );
|
||||
|
||||
if ( is_wp_error( $cf_cache_everything ) || ! $cf_cache_everything ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add home URL and feeds URLs to Cloudflare clean cache URLs list.
|
||||
$purge_urls[] = get_rocket_i18n_home_url( $lang );
|
||||
$feed_urls = [];
|
||||
$feed_urls[] = get_feed_link();
|
||||
$feed_urls[] = get_feed_link( 'comments_' );
|
||||
|
||||
// this filter is documented in inc/functions/files.php.
|
||||
$feed_urls = apply_filters( 'rocket_clean_home_feeds', $feed_urls );
|
||||
$purge_urls = array_unique( array_merge( $purge_urls, $feed_urls ) );
|
||||
|
||||
// Purge CloudFlare.
|
||||
$this->cloudflare->purge_by_url( $post, $purge_urls, $lang );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge CloudFlare cache.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function purge_cache_no_die() {
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Purge CloudFlare.
|
||||
$cf_purge = $this->cloudflare->purge_cloudflare();
|
||||
|
||||
if ( is_wp_error( $cf_purge ) ) {
|
||||
$cf_purge_result = [
|
||||
'result' => 'error',
|
||||
// translators: %s = CloudFare API return message.
|
||||
'message' => sprintf( __( '<strong>WP Rocket:</strong> %s', 'rocket' ), $cf_purge->get_error_message() ),
|
||||
];
|
||||
} else {
|
||||
$cf_purge_result = [
|
||||
'result' => 'success',
|
||||
'message' => __( '<strong>WP Rocket:</strong> Cloudflare cache successfully purged.', 'rocket' ),
|
||||
];
|
||||
}
|
||||
|
||||
set_transient( get_current_user_id() . '_cloudflare_purge_result', $cf_purge_result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge CloudFlare cache.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function purge_cache() {
|
||||
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'rocket_purge_cloudflare' ) ) {
|
||||
wp_nonce_ays( '' );
|
||||
}
|
||||
|
||||
$this->purge_cache_no_die();
|
||||
|
||||
wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
|
||||
defined( 'WPMEDIA_IS_TESTING' ) ? wp_die() : exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Real IP from CloudFlare.
|
||||
*
|
||||
* @since 1.0
|
||||
* @source cloudflare.php - https://wordpress.org/plugins/cloudflare/
|
||||
*/
|
||||
public function set_real_ip() {
|
||||
// only run this logic if the REMOTE_ADDR is populated, to avoid causing notices in CLI mode.
|
||||
if ( ! isset( $_SERVER['HTTP_CF_CONNECTING_IP'], $_SERVER['REMOTE_ADDR'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cf_ips_values = $this->cloudflare->get_cloudflare_ips();
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv6_cidrs;
|
||||
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) );
|
||||
$ipv6 = get_rocket_ipv6_full( $ip );
|
||||
if ( false === strpos( $ip, ':' ) ) {
|
||||
// IPV4: Update the REMOTE_ADDR value if the current REMOTE_ADDR value is in the specified range.
|
||||
$cf_ip_ranges = $cf_ips_values->result->ipv4_cidrs;
|
||||
}
|
||||
|
||||
foreach ( $cf_ip_ranges as $range ) {
|
||||
if (
|
||||
( strpos( $ip, ':' ) && rocket_ipv6_in_range( $ipv6, $range ) )
|
||||
||
|
||||
( false === strpos( $ip, ':' ) && rocket_ipv4_in_range( $ip, $range ) )
|
||||
) {
|
||||
$_SERVER['REMOTE_ADDR'] = sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_CONNECTING_IP'] ) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed after purging the CloudFlare cache.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function maybe_display_purge_notice() {
|
||||
if ( ! current_user_can( 'rocket_purge_cloudflare_cache' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
$notice = get_transient( $user_id . '_cloudflare_purge_result' );
|
||||
if ( ! $notice ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_transient( $user_id . '_cloudflare_purge_result' );
|
||||
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => $notice['result'],
|
||||
'message' => $notice['message'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This notice is displayed after modifying the CloudFlare settings.
|
||||
*
|
||||
* @since 1.0
|
||||
*/
|
||||
public function maybe_print_update_settings_notice() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) || 'settings_page_wprocket' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
$notices = get_transient( $user_id . '_cloudflare_update_settings' );
|
||||
if ( ! $notices ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$errors = '';
|
||||
$success = '';
|
||||
delete_transient( $user_id . '_cloudflare_update_settings' );
|
||||
foreach ( $notices as $notice ) {
|
||||
if ( 'error' === $notice['result'] ) {
|
||||
$errors .= $notice['message'] . '<br>';
|
||||
} elseif ( 'success' === $notice['result'] ) {
|
||||
$success .= $notice['message'] . '<br>';
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $success ) ) {
|
||||
rocket_notice_html(
|
||||
[
|
||||
'message' => $success,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
rocket_notice_html(
|
||||
[
|
||||
'status' => 'error',
|
||||
'message' => $errors,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare dev mode admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $devmode New value for Cloudflare dev mode.
|
||||
*/
|
||||
private function save_cloudflare_devmode( $devmode ) {
|
||||
$cloudflare_dev_mode_return = $this->cloudflare->set_devmode( $devmode );
|
||||
if ( is_wp_error( $cloudflare_dev_mode_return ) ) {
|
||||
return [
|
||||
'result' => 'error',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare development mode error: %s', 'rocket' ), $cloudflare_dev_mode_return->get_error_message() ),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'result' => 'success',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare development mode %s', 'rocket' ), $cloudflare_dev_mode_return ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare cache_level admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $cache_level New value for Cloudflare cache_level.
|
||||
*/
|
||||
private function save_cache_level( $cache_level ) {
|
||||
// Set Cache Level to Aggressive.
|
||||
$cf_cache_level_return = $this->cloudflare->set_cache_level( $cache_level );
|
||||
|
||||
if ( is_wp_error( $cf_cache_level_return ) ) {
|
||||
return [
|
||||
'result' => 'error',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare cache level error: %s', 'rocket' ), $cf_cache_level_return->get_error_message() ),
|
||||
];
|
||||
}
|
||||
|
||||
if ( 'aggressive' === $cf_cache_level_return ) {
|
||||
$cf_cache_level_return = _x( 'Standard', 'Cloudflare caching level', 'rocket' );
|
||||
}
|
||||
|
||||
return [
|
||||
'result' => 'success',
|
||||
// translators: %s is the caching level returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare cache level set to %s', 'rocket' ), $cf_cache_level_return ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare minify admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $minify New value for Cloudflare minify.
|
||||
*/
|
||||
private function save_minify( $minify ) {
|
||||
$cf_minify_return = $this->cloudflare->set_minify( $minify );
|
||||
|
||||
if ( is_wp_error( $cf_minify_return ) ) {
|
||||
return [
|
||||
'result' => 'error',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare minification error: %s', 'rocket' ), $cf_minify_return->get_error_message() ),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'result' => 'success',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare minification %s', 'rocket' ), $cf_minify_return ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare rocket loader admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param string $rocket_loader New value for Cloudflare rocket loader.
|
||||
*/
|
||||
private function save_rocket_loader( $rocket_loader ) {
|
||||
$cf_rocket_loader_return = $this->cloudflare->set_rocket_loader( $rocket_loader );
|
||||
|
||||
if ( is_wp_error( $cf_rocket_loader_return ) ) {
|
||||
return [
|
||||
'result' => 'error',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare rocket loader error: %s', 'rocket' ), $cf_rocket_loader_return->get_error_message() ),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'result' => 'success',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare rocket loader %s', 'rocket' ), $cf_rocket_loader_return ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare browser cache ttl admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param int $browser_cache_ttl New value for Cloudflare browser cache ttl.
|
||||
*/
|
||||
private function save_browser_cache_ttl( $browser_cache_ttl ) {
|
||||
$cf_browser_cache_return = $this->cloudflare->set_browser_cache_ttl( $browser_cache_ttl );
|
||||
|
||||
if ( is_wp_error( $cf_browser_cache_return ) ) {
|
||||
return [
|
||||
'result' => 'error',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare browser cache error: %s', 'rocket' ), $cf_browser_cache_return->get_error_message() ),
|
||||
];
|
||||
}
|
||||
return [
|
||||
'result' => 'success',
|
||||
// translators: %s is the message returned by the CloudFlare API.
|
||||
'message' => '<strong>' . __( 'WP Rocket: ', 'rocket' ) . '</strong>' . sprintf( __( 'Cloudflare browser cache set to %s seconds', 'rocket' ), $cf_browser_cache_return ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare auto settings admin option.
|
||||
*
|
||||
* @since 3.5.2
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param array $auto_settings New value for Cloudflare auto_settings.
|
||||
* @param array $old_settings Cloudflare cloudflare_old_settings.
|
||||
*/
|
||||
private function save_cloudflare_auto_settings( $auto_settings, $old_settings ) {
|
||||
$cf_old_settings = explode( ',', $old_settings );
|
||||
$cloudflare_update_result = [];
|
||||
|
||||
// Set Cache Level to Aggressive.
|
||||
$cf_cache_level = isset( $cf_old_settings[0] ) && 0 === $auto_settings ? 'basic' : 'aggressive';
|
||||
$cloudflare_update_result[] = $this->save_cache_level( $cf_cache_level );
|
||||
|
||||
// Active Minification for HTML, CSS & JS.
|
||||
$cf_minify = isset( $cf_old_settings[1] ) && 0 === $auto_settings ? $cf_old_settings[1] : 'on';
|
||||
$cloudflare_update_result[] = $this->save_minify( $cf_minify );
|
||||
|
||||
// Deactivate Rocket Loader to prevent conflicts.
|
||||
$cf_rocket_loader = isset( $cf_old_settings[2] ) && 0 === $auto_settings ? $cf_old_settings[2] : 'off';
|
||||
$cloudflare_update_result[] = $this->save_rocket_loader( $cf_rocket_loader );
|
||||
|
||||
// Set Browser cache to 1 year.
|
||||
$cf_browser_cache_ttl = isset( $cf_old_settings[3] ) && 0 === $auto_settings ? $cf_old_settings[3] : '31536000';
|
||||
$cloudflare_update_result[] = $this->save_browser_cache_ttl( $cf_browser_cache_ttl );
|
||||
|
||||
return $cloudflare_update_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare admin options.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $old_value An array of previous values for the settings.
|
||||
* @param array $value An array of submitted values for the settings.
|
||||
*/
|
||||
public function save_cloudflare_options( $old_value, $value ) {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$is_api_keys_valid_cloudflare = get_transient( 'rocket_cloudflare_is_api_keys_valid' );
|
||||
$submit_cloudflare_view = false;
|
||||
if (
|
||||
( isset( $old_value['cloudflare_email'], $value['cloudflare_email'] ) && $old_value['cloudflare_email'] !== $value['cloudflare_email'] )
|
||||
||
|
||||
( isset( $old_value['cloudflare_api_key'], $value['cloudflare_api_key'] ) && $old_value['cloudflare_api_key'] !== $value['cloudflare_api_key'] )
|
||||
||
|
||||
( isset( $old_value['cloudflare_zone_id'], $value['cloudflare_zone_id'] ) && $old_value['cloudflare_zone_id'] !== $value['cloudflare_zone_id'] )
|
||||
) {
|
||||
delete_transient( 'rocket_cloudflare_is_api_keys_valid' );
|
||||
$is_api_keys_valid_cloudflare = $this->cloudflare->is_api_keys_valid( $value['cloudflare_email'], $value['cloudflare_api_key'], $value['cloudflare_zone_id'], true );
|
||||
set_transient( 'rocket_cloudflare_is_api_keys_valid', $is_api_keys_valid_cloudflare, 2 * WEEK_IN_SECONDS );
|
||||
$submit_cloudflare_view = true;
|
||||
}
|
||||
|
||||
if ( ( isset( $old_value['cloudflare_devmode'], $value['cloudflare_devmode'] ) && (int) $old_value['cloudflare_devmode'] !== (int) $value['cloudflare_devmode'] ) ||
|
||||
( isset( $old_value['cloudflare_auto_settings'], $value['cloudflare_auto_settings'] ) && (int) $old_value['cloudflare_auto_settings'] !== (int) $value['cloudflare_auto_settings'] ) ) {
|
||||
$submit_cloudflare_view = true;
|
||||
}
|
||||
|
||||
// Revalidate Cloudflare credentials if transient is false.
|
||||
if ( false === $is_api_keys_valid_cloudflare ) {
|
||||
if ( isset( $value['cloudflare_email'], $value['cloudflare_api_key'], $value['cloudflare_zone_id'] ) ) {
|
||||
$is_api_keys_valid_cloudflare = $this->cloudflare->is_api_keys_valid( $value['cloudflare_email'], $value['cloudflare_api_key'], $value['cloudflare_zone_id'] );
|
||||
} else {
|
||||
$is_api_keys_valid_cloudflare = false;
|
||||
}
|
||||
set_transient( 'rocket_cloudflare_is_api_keys_valid', $is_api_keys_valid_cloudflare, 2 * WEEK_IN_SECONDS );
|
||||
}
|
||||
|
||||
// If is submit CF view & CF Credentials are invalid, display error and bail out.
|
||||
if ( is_wp_error( $is_api_keys_valid_cloudflare ) && $submit_cloudflare_view ) {
|
||||
$cloudflare_error_message = $is_api_keys_valid_cloudflare->get_error_message();
|
||||
add_settings_error( 'general', 'cloudflare_api_key_invalid', __( 'WP Rocket: ', 'rocket' ) . '</strong>' . $cloudflare_error_message . '<strong>', 'error' );
|
||||
set_transient( get_current_user_id() . '_cloudflare_update_settings', [] );
|
||||
return;
|
||||
}
|
||||
|
||||
// Update CloudFlare Development Mode.
|
||||
$cloudflare_update_result = [];
|
||||
if ( isset( $old_value['cloudflare_devmode'], $value['cloudflare_devmode'] ) && (int) $old_value['cloudflare_devmode'] !== (int) $value['cloudflare_devmode'] ) {
|
||||
$cloudflare_update_result[] = $this->save_cloudflare_devmode( $value['cloudflare_devmode'] );
|
||||
}
|
||||
|
||||
// Update CloudFlare settings.
|
||||
if ( isset( $old_value['cloudflare_auto_settings'], $value['cloudflare_auto_settings'] ) && (int) $old_value['cloudflare_auto_settings'] !== (int) $value['cloudflare_auto_settings'] ) {
|
||||
$cloudflare_update_result = array_merge( $cloudflare_update_result, $this->save_cloudflare_auto_settings( $value['cloudflare_auto_settings'], $value['cloudflare_old_settings'] ) );
|
||||
}
|
||||
|
||||
set_transient( get_current_user_id() . '_cloudflare_update_settings', $cloudflare_update_result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Cloudflare old settings when the auto settings option is enabled.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param array $value An array of previous values for the settings.
|
||||
* @param array $old_value An array of submitted values for the settings.
|
||||
*
|
||||
* @return array settings with old settings.
|
||||
*/
|
||||
public function save_cloudflare_old_settings( $value, $old_value ) {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Save old CloudFlare settings.
|
||||
if (
|
||||
isset( $value['cloudflare_auto_settings'], $old_value ['cloudflare_auto_settings'] )
|
||||
&&
|
||||
$value['cloudflare_auto_settings'] !== $old_value ['cloudflare_auto_settings']
|
||||
&&
|
||||
1 === $value['cloudflare_auto_settings']
|
||||
) {
|
||||
$cf_settings = $this->cloudflare->get_settings();
|
||||
$value['cloudflare_old_settings'] = ! is_wp_error( $cf_settings )
|
||||
? implode( ',', array_filter( $cf_settings ) )
|
||||
: '';
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace WPMedia\Cloudflare;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class UnauthorizedException extends RuntimeException {}
|
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "wp-media/cloudflare",
|
||||
"description": "Cloudflare Addon",
|
||||
"homepage": "https://github.com/wp-media/cloudflare",
|
||||
"license": "GPL-2.0+",
|
||||
"authors": [
|
||||
{
|
||||
"name": "WP Media",
|
||||
"email": "contact@wp-media.me",
|
||||
"homepage": "https://wp-media.me"
|
||||
}
|
||||
],
|
||||
"type": "library",
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/wp-media/cloudflare/issues",
|
||||
"source": "https://github.com/wp-media/cloudflare"
|
||||
},
|
||||
"require-dev": {
|
||||
"php": "^5.6 || ^7",
|
||||
"brain/monkey": "^2.0",
|
||||
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
|
||||
"phpcompatibility/phpcompatibility-wp": "^2.0",
|
||||
"phpunit/phpunit": "^5.7 || ^7",
|
||||
"wp-coding-standards/wpcs": "^2",
|
||||
"wp-media/event-manager": "^3.1",
|
||||
"wp-media/options": "^3.0",
|
||||
"wp-media/phpunit": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "WPMedia\\Cloudflare\\": "." },
|
||||
"exclude-from-classmap": [ "/Tests/" ]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": { "WPMedia\\Cloudflare\\Tests\\": "Tests/" }
|
||||
},
|
||||
"scripts": {
|
||||
"test-unit": "\"vendor/bin/wpmedia-phpunit\" unit path=Tests/Unit",
|
||||
"test-integration": "\"vendor/bin/wpmedia-phpunit\" integration path=Tests/Integration/",
|
||||
"run-tests": [
|
||||
"@test-unit",
|
||||
"@test-integration"
|
||||
],
|
||||
"install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
|
||||
"phpcs": "phpcs --basepath=.",
|
||||
"phpcs-changed": "./bin/phpcs-changed.sh",
|
||||
"phpcs:fix": "phpcbf"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user