This commit is contained in:
2026-10-06 18:51:23 -06:00
parent 8424dacd03
commit e2c26b7f21
4223 changed files with 737986 additions and 53 deletions
@@ -0,0 +1,226 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Addon\Cloudflare\API;
use WP_Error;
use WP_Rocket\Addon\Cloudflare\Auth\AuthInterface;
class Client {
const CLOUDFLARE_API = 'https://api.cloudflare.com/client/v4/';
/**
* Auth object
*
* @var AuthInterface
*/
private $auth;
/**
* An array of arguments for wp_remote_request()
*
* @var mixed[]
*/
protected $args = [];
/**
* Constructor.
*
* @param AuthInterface $auth Auth implementation.
*/
public function __construct( AuthInterface $auth ) {
$this->auth = $auth;
$this->args = [
'sslverify' => true,
'body' => [],
'headers' => [],
];
}
/**
* Change client auth.
*
* @param AuthInterface $auth Client auth.
*
* @return void
*/
public function set_auth( AuthInterface $auth ) {
$this->auth = $auth;
}
/**
* API call method for sending requests using GET.
*
* @param string $path Path of the endpoint.
* @param mixed[] $data Data to be sent along with the request.
*
* @return object
*/
public function get( $path, array $data = [] ) {
return $this->request( $path, 'get', $data );
}
/**
* API call method for sending requests using POST.
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return object
*/
public function post( $path, array $data = [] ) {
return $this->request( $path, 'post', $data );
}
/**
* API call method for sending requests using DELETE.
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return object
*/
public function delete( $path, array $data = [] ) {
return $this->request( $path, 'delete', $data );
}
/**
* API call method for sending requests using PATCH.
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return object
*/
public function patch( $path, array $data = [] ) {
return $this->request( $path, 'patch', $data );
}
/**
* API call method for sending requests
*
* @param string $path Path of the endpoint.
* @param string $method Type of method that should be used.
* @param array $data Data to be sent along with the request.
*
* @return object|WP_Error
*/
protected function request( $path, $method = 'get', array $data = [] ) {
if ( '/ips' !== $path ) {
$valid = $this->auth->is_valid_credentials();
if ( is_wp_error( $valid ) ) {
return $valid;
}
if ( ! $valid ) {
return new WP_Error( 'cloudflare_invalid_credentials', 'Cloudflare credentials are invalid.' );
}
}
$response = $this->do_remote_request( $path, $method, $data );
if ( is_wp_error( $response ) ) {
return $response;
}
$content = wp_remote_retrieve_body( $response );
if ( empty( $content ) ) {
return new WP_Error( 'cloudflare_no_reply', __( 'Cloudflare did not provide any reply. Please try again later.', 'rocket' ) );
}
$content = json_decode( $content );
if ( ! is_object( $content ) ) {
return new WP_Error( 'cloudflare_content_error', __( 'Cloudflare unexpected response', 'rocket' ) );
}
if ( empty( $content->success ) ) {
return $this->set_request_error( $content );
}
if ( ! property_exists( $content, 'result' ) ) {
return new WP_Error( 'cloudflare_no_reply', __( 'Missing Cloudflare result.', 'rocket' ) );
}
return $content->result;
}
/**
* Does the request remote request.
*
* @param string $path Path of the endpoint.
* @param string $method Type of method that should be used.
* @param array $data Data to be sent along with the request.
*
* @return array|WP_Error
*/
private function do_remote_request( string $path, string $method = 'GET', array $data = [] ) {
$this->args['method'] = strtoupper( $method );
$headers = [
'User-Agent' => 'wp-rocket/' . rocket_get_constant( 'WP_ROCKET_VERSION' ),
'Content-Type' => 'application/json',
];
if ( '/ips' !== $path ) {
$this->args['headers'] = array_merge( $headers, $this->auth->get_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;
}
/**
* Sets the WP_Error when request is not successful
*
* @param object $content Response object.
*
* @return WP_Error
*/
private function set_request_error( $content ) {
$errors = [];
foreach ( $content->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>'
);
return new WP_Error( 'cloudflare_incorrect_credentials', $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>'
);
return new WP_Error( 'cloudflare_incorrect_zone_id', $msg );
}
$errors[] = $error->message;
}
return new WP_Error( 'cloudflare_request_error', wp_sprintf_l( '%l ', $errors ) );
}
}
@@ -0,0 +1,174 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Addon\Cloudflare\API;
use WP_Rocket\Addon\Cloudflare\Auth\AuthInterface;
class Endpoints {
/**
* Client instance
*
* @var Client
*/
private $client;
/**
* Constructor
*
* @param Client $client Client instance.
*/
public function __construct( Client $client ) {
$this->client = $client;
}
/**
* Get zone data.
*
* @param string $zone_id Zone ID.
*
* @return object
*/
public function get_zones( string $zone_id ) {
return $this->client->get( "zones/{$zone_id}" );
}
/**
* Get the zone's page rules.
*
* @param string $zone_id Zone ID.
* @param string $status Rule status.
*
* @return object
*/
public function list_pagerules( string $zone_id, string $status ) {
return $this->client->get( "zones/{$zone_id}/pagerules?status={$status}" );
}
/**
* Purges the cache.
*
* @param string $zone_id Zone ID.
*
* @return object
*/
public function purge( string $zone_id ) {
return $this->client->post( "zones/{$zone_id}/purge_cache", [ 'purge_everything' => true ] );
}
/**
* Purges the given URLs.
*
* @param string $zone_id Zone ID.
* @param array $urls An array of URLs that should be removed from cache.
*
* @return object
*/
public function purge_files( string $zone_id, array $urls = [] ) {
return $this->client->post( "zones/{$zone_id}/purge_cache", [ 'files' => $urls ] );
}
/**
* Updates the zone's browser cache TTL setting
*
* @param string $zone_id Zone ID.
* @param string $value Cache TTL value.
*
* @return object
*/
public function update_browser_cache_ttl( string $zone_id, $value ) {
return $this->update_setting( $zone_id, 'browser_cache_ttl', $value );
}
/**
* Updates the zone's rocket loader setting.
*
* @param string $zone_id Zone ID.
* @param string $value Rocket Loader value.
*
* @return object
*/
public function update_rocket_loader( string $zone_id, $value ) {
return $this->update_setting( $zone_id, 'rocket_loader', $value );
}
/**
* Updates the zone's minify setting.
*
* @param string $zone_id Zone ID.
* @param string[] $value Minify value.
*
* @return object
*/
public function update_minify( string $zone_id, $value ) {
return $this->update_setting( $zone_id, 'minify', $value );
}
/**
* Updates the zone's cache level.
*
* @param string $zone_id Zone ID.
* @param string $value Cache level value.
*
* @return object
*/
public function change_cache_level( string $zone_id, $value ) {
return $this->update_setting( $zone_id, 'cache_level', $value );
}
/**
* Changes the zone's development mode.
*
* @param string $zone_id Zone ID.
* @param string $value Development mode value.
*
* @return object
*/
public function change_development_mode( string $zone_id, $value ) {
return $this->update_setting( $zone_id, 'development_mode', $value );
}
/**
* Updates the given setting.
*
* @param string $zone_id Zone ID.
* @param string $setting Name of the setting to change.
* @param mixed $value Setting value.
*
* @return object
*/
protected function update_setting( string $zone_id, $setting, $value ) {
return $this->client->patch( "zones/{$zone_id}/settings/{$setting}", [ 'value' => $value ] );
}
/**
* Gets all of the Cloudflare settings.
*
* @param string $zone_id Zone ID.
*
* @return object
*/
public function get_settings( string $zone_id ) {
return $this->client->get( "zones/{$zone_id}/settings" );
}
/**
* Gets Cloudflare's IPs.
*
* @return object
*/
public function get_ips() {
return $this->client->get( '/ips' );
}
/**
* Change client auth.
*
* @param AuthInterface $auth Client auth.
*
* @return void
*/
public function change_auth( AuthInterface $auth ) {
$this->client->set_auth( $auth );
}
}