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

Closes #5729 Refactor Cloudflare implementation #5884

Merged
merged 159 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
159 commits
Select commit Hold shift + click to select a range
adc12f0
remove unused file
remyperona Feb 10, 2023
d55b912
remove old class
remyperona Feb 10, 2023
6a700e8
Add new API and Auth classes
remyperona Feb 10, 2023
021b871
move exceptions
remyperona Feb 15, 2023
bd6c749
move is_authorized method
remyperona Feb 15, 2023
1d469ac
add is_authorized() method signature
remyperona Feb 15, 2023
db64de0
Add admin subscriber
remyperona Feb 15, 2023
e5a3222
move methods to admin subscribers
remyperona Feb 15, 2023
4662cbb
add post method
remyperona Feb 15, 2023
9d02486
update methods declarations
remyperona Feb 15, 2023
2cabcb0
add credentialException usage
remyperona Feb 20, 2023
cb51a6c
throw exception if empty credentials
remyperona Feb 20, 2023
e3316df
update method name
remyperona Feb 20, 2023
4c33e44
add CredentialsException
remyperona Feb 20, 2023
a7dd84d
update class with new structure
remyperona Feb 20, 2023
16d5b83
fix spacing
remyperona Feb 20, 2023
d299c1f
code improvements
remyperona Feb 20, 2023
0e0a53f
don't import psr/container
remyperona Feb 21, 2023
f069085
exclude psr/container from mozart
remyperona Feb 21, 2023
005bba3
update service provider for cloudflare
remyperona Feb 21, 2023
49d9736
set cache TTL value to 14400 (4 hours)
remyperona Feb 22, 2023
e1fcb2b
add intro text for notice
remyperona Feb 27, 2023
58d4a1c
update text message for CF notice
remyperona Feb 27, 2023
a76ec67
convert browser cache TTL value to human-readable value
remyperona Feb 27, 2023
4ca5e52
npm update
remyperona Feb 28, 2023
ad6a2f9
hide CF api key and Zone ID except last 4 characters
remyperona Feb 28, 2023
8011b45
add provided to the array
remyperona Mar 2, 2023
768450d
add auth unit tests
remyperona Mar 9, 2023
0fd01f6
use constant if it exists
remyperona Mar 9, 2023
4ba2bdf
remove unused variable
remyperona Mar 9, 2023
92750be
update type of exception
remyperona Mar 9, 2023
9eb38f1
update variable name
remyperona Mar 15, 2023
7f49f6e
add tests for get() method
remyperona Mar 15, 2023
074d377
add tests for delete, patch, post
remyperona Mar 16, 2023
c875655
add test for is_auth_valid()
remyperona Mar 17, 2023
beeeda6
update fixture
remyperona Mar 20, 2023
dabdbae
update variable name
remyperona Mar 20, 2023
c87f388
add tests for has_page_rule()
remyperona Mar 20, 2023
c9ef0bf
update return
remyperona Mar 20, 2023
6a9ba01
update fixture name
remyperona Mar 20, 2023
8764dd0
add tests for purge_cloudflare()
remyperona Mar 20, 2023
6a15cad
update return
remyperona Mar 20, 2023
baa2fd8
add tests for purge_by_url
remyperona Mar 20, 2023
d4c040e
add tests for set methods
remyperona Mar 21, 2023
05cc83f
update code
remyperona Mar 22, 2023
8ed6346
add tests for get_settings() and get_cloudflare_ips()
remyperona Mar 22, 2023
f8b378c
update method name
remyperona Mar 22, 2023
f2bfb01
add tests for notices methods
remyperona Mar 22, 2023
c0b41b4
add test for set_varnish_localhost()
remyperona Mar 23, 2023
f0db1c7
add test for deactivate_devmode
remyperona Mar 24, 2023
2c58cf2
add tests for auto_purge and purge_cache
remyperona Mar 24, 2023
c7c5ce1
Merge branch 'develop' into enhancement/update-cloudflare
remyperona Mar 28, 2023
5bcfde8
add test for save_cloudflare_old_settings()
remyperona Mar 28, 2023
84c0e65
update formatting
remyperona Mar 30, 2023
066cfe3
update formatting
remyperona Mar 30, 2023
1f19c99
add test for set_real_ip()
remyperona Mar 30, 2023
fa17d58
add tests for save_cloudflare_options()
remyperona Mar 30, 2023
092a7d9
Merge branch 'develop' into enhancement/update-cloudflare
remyperona Mar 30, 2023
f6c32be
migrate and deprecate protocol rewrite functions
remyperona Mar 30, 2023
b49d08f
add tests for protocol rewrite methods
remyperona Apr 3, 2023
7e0ab7e
update class name
remyperona Apr 3, 2023
2229dc2
remove unused method
remyperona Apr 3, 2023
eefc333
add translations stub
remyperona Apr 12, 2023
1347057
update stub
remyperona Apr 12, 2023
e5443ac
update fixture
remyperona Apr 12, 2023
6f4a66d
update stub
remyperona Apr 12, 2023
977ff82
update fixtures
remyperona Apr 12, 2023
c97b2fc
add integration tests for client methods
remyperona Apr 12, 2023
a5e5921
update instantiation
remyperona Apr 14, 2023
aaafe20
add cf-ip-rewrite package
remyperona Apr 21, 2023
176ad91
remove check
remyperona Apr 21, 2023
2cc0507
remove stub
remyperona Apr 21, 2023
3136ac1
remove tests
remyperona Apr 21, 2023
d063768
add usage of new method for ip rewrite
remyperona Apr 21, 2023
c11e91b
deprecate functions and remove old files
remyperona Apr 21, 2023
7a89454
Merge branch 'develop' into enhancement/update-cloudflare
remyperona Apr 21, 2023
ab22c8e
update regex
remyperona Apr 21, 2023
ac55877
remove old test
remyperona Apr 21, 2023
0bbdb52
fix PHPCS
remyperona Apr 21, 2023
b63ac6a
fix PHPCS
remyperona Apr 21, 2023
2db935e
fix skipped test
remyperona Apr 21, 2023
0a4da91
fix namespace capitalize
remyperona Apr 21, 2023
93cd812
use correct option name
remyperona Apr 21, 2023
f29fd36
update bootstrap for CF
remyperona Apr 21, 2023
eb55a23
update bootstrap
remyperona Apr 21, 2023
8f6c6d1
fix methods name in callback
remyperona Apr 21, 2023
b9df0a8
fix protocol rewrite disabling
remyperona Apr 21, 2023
d7dc614
fix test
remyperona Apr 21, 2023
143bba0
re-compile admin JS
remyperona Apr 24, 2023
c77238b
update cloudflare classes loading
remyperona Apr 24, 2023
2979a3b
change parameters order to put optional after mandatory
remyperona Apr 24, 2023
52e5fc2
use shouldReceive
remyperona Apr 24, 2023
ef2d0c8
update docblock
remyperona Apr 25, 2023
cea717d
remove exceptions usage by WP_Error
remyperona Apr 25, 2023
efa4ce9
update WP_Error fixture construct
remyperona Apr 25, 2023
3326c26
update error code and message
remyperona Apr 26, 2023
c8a33f3
remove return hint
remyperona Apr 26, 2023
e95a52c
update tests
remyperona Apr 26, 2023
67dda8e
remove extra spacing
remyperona Apr 26, 2023
f5930e1
remove input partial hide
remyperona Apr 27, 2023
f57a53a
update integration tests
remyperona Apr 27, 2023
e75f517
move psr/container to require
remyperona Apr 28, 2023
4617046
add a check and update if transient is an error
remyperona Apr 28, 2023
28d1347
update tests
remyperona Apr 28, 2023
6493977
define $level variable
remyperona May 1, 2023
6692554
update fixtures
remyperona May 1, 2023
8a93d3b
load the CF admin subscriber
remyperona May 1, 2023
5239f7d
add group for CF admin
remyperona May 1, 2023
4ee806c
add test for maybe_display_purge_notice()
remyperona May 1, 2023
5bc9428
add cloudflareadmin group test
remyperona May 1, 2023
1d55846
add tests for maybe_display_update_settings_notice()
remyperona May 1, 2023
1f021b4
add missing parent setUp()
remyperona May 1, 2023
4d3f26b
add integration test for get_cloudflare_ips()
remyperona May 1, 2023
a17cf0f
re-namespace psr/container
remyperona May 2, 2023
f075e39
re-factor request method
remyperona May 2, 2023
aaf9089
fix PHPCS
remyperona May 2, 2023
8066084
add get_settings() integration test
remyperona May 2, 2023
015ad4a
add cloudflareadmin to the run tests command
remyperona May 2, 2023
e360377
add tests for Cloudflare class
remyperona May 2, 2023
3b8c404
fix value
remyperona May 3, 2023
37c9288
update subscriber tests
remyperona May 3, 2023
3546a62
Add Cloudflare Service Provider
remyperona May 4, 2023
e6b9c9d
Merge branch 'develop' into enhancement/update-cloudflare
remyperona May 4, 2023
f8e0b63
remove event when deactivating dev mode
remyperona May 4, 2023
a948117
fix PHPCS
remyperona May 4, 2023
c2ef59e
improve notice message
remyperona May 4, 2023
40329dd
fix notice message
remyperona May 4, 2023
d357ddd
update service provider loading
remyperona May 5, 2023
f39744f
Merge branch 'develop' into enhancement/update-cloudflare
remyperona May 8, 2023
f26b0fe
force value to be a int
remyperona May 8, 2023
8f52d6e
set HTML minify value and browser TTL back to 1 year
remyperona May 9, 2023
ee99dbf
update tests
remyperona May 9, 2023
2ea430e
Merge branch 'develop' into enhancement/update-cloudflare
remyperona May 11, 2023
58a6d34
test PHP 7.2
remyperona May 11, 2023
eff0e5b
fix cap name
remyperona May 11, 2023
77d5908
move CF inputs validation
remyperona May 15, 2023
1253a71
add default value
remyperona May 15, 2023
6bad859
update connection check
remyperona May 16, 2023
2ccc4a4
Delete the CF connection transient when changing settings
remyperona May 17, 2023
d4bf42f
fix PHPCS
remyperona May 17, 2023
49c6c2f
update dev mode method
remyperona May 24, 2023
16f7993
Merge branch 'develop' into enhancement/update-cloudflare
remyperona May 24, 2023
cab4eda
check for settings change before checking for API connection
remyperona May 26, 2023
4bad76c
Merge branch 'develop' into enhancement/update-cloudflare
remyperona Jun 2, 2023
5452147
display error when purging CF if API connection is not correct
remyperona Jun 2, 2023
5986189
Fixed an issue with the empty zone ID
CrochetFeve0251 Jun 5, 2023
59f4d77
Swapped callbacks
CrochetFeve0251 Jun 6, 2023
214b3a6
Fixed error warning issue
CrochetFeve0251 Jun 6, 2023
7e0bfc4
Added a way to change client auth during execution
CrochetFeve0251 Jun 7, 2023
5b19d50
Added a way to change client auth during execution
CrochetFeve0251 Jun 7, 2023
25a14b2
Return a value on changing auth
CrochetFeve0251 Jun 7, 2023
6ec9286
Swapped values and old values
CrochetFeve0251 Jun 7, 2023
bb5e780
Added return value on notice callback
CrochetFeve0251 Jun 7, 2023
b0e21d1
Merge remote-tracking branch 'origin/enhancement/update-cloudflare' i…
CrochetFeve0251 Jun 7, 2023
96c3f58
Added fix for initial state
CrochetFeve0251 Jun 7, 2023
0eee8f2
Fixed code standards
CrochetFeve0251 Jun 8, 2023
9fb0279
Fixed unit tests
CrochetFeve0251 Jun 8, 2023
76f3c70
Closes #5951 Add compatibility with Cloudflare plugin & APO (#5953)
remyperona Jun 12, 2023
420bd97
Merge branch 'develop' into enhancement/update-cloudflare
remyperona Jun 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint_phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- develop
- branch-*
- feature/*
- enhancement/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_wprocket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- develop
- branch-*
- feature/*
- enhancement/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_wprocket_php8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- develop
- branch-*
- feature/*
- enhancement/*

jobs:
run:
Expand Down
2 changes: 1 addition & 1 deletion assets/js/wpr-admin.js.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/wpr-admin.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
],
"require": {
"php": ">=7.3",
"cloudflare/cf-ip-rewrite": "^1.0",
"composer/installers": "^1.0 || ^2.0",
"monolog/monolog": "^1.0 || ^2.0"
},
Expand Down Expand Up @@ -115,10 +116,11 @@
},
"scripts": {
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,DoCloudflare,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,WPGeotargeting",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly,BeaverBuilder,Elementor,Hummingbird,WithSmush,WithWoo,WithAmp,WithAmpAndCloudflare,WithSCCSS,Cloudways,Dreampress,Cloudflare,CloudflareAdmin,Multisite,WPEngine,SpinUpWP,WordPressCom,O2Switch,PDFEmbedder,PDFEmbedderPremium,PDFEmbedderSecure,Godaddy,LiteSpeed,RevolutionSlider,WordFence,ConvertPlug,Kinsta,Jetpack,RankMathSEO,AllInOneSeoPack,SEOPress,TheSEOFramework,OneCom,RocketLazyLoad,WPXCloud,TheEventsCalendar,Perfmatters,RapidLoad,ProIsp,WPGeotargeting",
"test-integration-adminonly": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly",
"test-integration-bb": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group BeaverBuilder",
"test-integration-cloudflare": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group DoCloudflare",
"test-integration-cloudflare": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Cloudflare",
"test-integration-cloudflareadmin": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group CloudflareAdmin",
"test-integration-cloudways": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Cloudways",
"test-integration-elementor": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Elementor",
"test-integration-hummingbird": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group Hummingbird",
Expand Down Expand Up @@ -160,6 +162,7 @@
"@test-integration",
"@test-integration-adminonly",
"@test-integration-cloudflare",
"@test-integration-cloudflareadmin",
"@test-integration-bb",
"@test-integration-elementor",
"@test-integration-hummingbird",
Expand Down
218 changes: 218 additions & 0 deletions inc/Addon/Cloudflare/API/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?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 array
*/
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 array $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 ( empty( $content->success ) ) {
return $this->set_request_error( $content );
}

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, array $data ) {
$this->args['method'] = isset( $method ) ? strtoupper( $method ) : 'GET';

$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 ) );
}
}
Loading