Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
Cloudflare | |
0.00% |
0 / 43 |
|
0.00% |
0 / 7 |
306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
should_enable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isCoudflareEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCloudflareTier | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
42 | |||
purge_all | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
purge_url | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
purgeRequest | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Performance\Cache\Types; |
4 | |
5 | use NewfoldLabs\WP\Module\Performance\Cache\Purgeable; |
6 | use NewfoldLabs\WP\ModuleLoader\Container; |
7 | |
8 | /** |
9 | * Cloudflare cache type. |
10 | */ |
11 | class Cloudflare extends CacheBase implements Purgeable { |
12 | |
13 | /** |
14 | * Dependency injection container. |
15 | * |
16 | * @var Container |
17 | */ |
18 | protected $container; |
19 | |
20 | /** |
21 | * Cloudflare constructor. |
22 | * |
23 | * @param Container $container Dependency injection container. |
24 | */ |
25 | public function __construct( Container $container ) { |
26 | $this->container = $container; |
27 | } |
28 | |
29 | /** |
30 | * Whether or not the code for this cache type should be loaded. |
31 | * |
32 | * @param Container $container Method container. |
33 | * @return bool True if the cache type should be enabled, false otherwise. |
34 | */ |
35 | public static function should_enable( Container $container ) { |
36 | return (bool) \get_option( 'endurance_cloudflare_enabled', false ); |
37 | } |
38 | |
39 | /** |
40 | * Check if Cloudflare is enabled. |
41 | * |
42 | * @return bool True if Cloudflare is enabled, false otherwise. |
43 | */ |
44 | public function isCoudflareEnabled() { |
45 | return $this->getCloudflareTier() !== 0; |
46 | } |
47 | |
48 | /** |
49 | * Get the Cloudflare tier. |
50 | * |
51 | * @return int|string The Cloudflare tier. |
52 | */ |
53 | public function getCloudflareTier() { |
54 | $tier = \get_option( 'endurance_cloudflare_enabled', false ); |
55 | |
56 | if ( ! $tier ) { |
57 | return 0; |
58 | } |
59 | |
60 | switch ( $tier ) { |
61 | case 'hostgator': |
62 | return 'hostgator'; |
63 | case 'india': |
64 | return 'india'; |
65 | case 'premium': |
66 | return 'premium'; |
67 | default: |
68 | return 'basic'; |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Purge all Cloudflare cache. |
74 | */ |
75 | public function purge_all() { |
76 | if ( $this->isCoudflareEnabled() ) { |
77 | $this->purgeRequest(); |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * Purge a URL from Cloudflare cache. |
83 | * |
84 | * @param string $url URL to purge. |
85 | */ |
86 | public function purge_url( $url ) { |
87 | if ( $this->isCoudflareEnabled() ) { |
88 | $this->purgeRequest( array( $url ) ); |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * Purge multiple URLs from the Cloudflare cache. |
94 | * |
95 | * @link https://confluence.newfold.com/pages/viewpage.action?spaceKey=UDEV&title=Cache+Purge+API |
96 | * |
97 | * @param array $urls URLs to purge. |
98 | */ |
99 | protected function purgeRequest( $urls = array() ) { |
100 | global $wp_version; |
101 | |
102 | $queryString = http_build_query( array( 'cf' => $this->getCloudflareTier() ), '', '&' ); |
103 | $host = wp_parse_url( \home_url(), PHP_URL_HOST ); |
104 | |
105 | $plugin_brand = $this->container->plugin()->get( 'id' ); |
106 | $plugin_version = $this->container->plugin()->version; |
107 | |
108 | $headerName = 'X-' . strtoupper( $plugin_brand ) . '-PLUGIN-PURGE'; |
109 | |
110 | $body = array( |
111 | 'hosts' => array( $host ), |
112 | ); |
113 | |
114 | if ( $urls ) { |
115 | $body['assets'] = $urls; |
116 | } |
117 | |
118 | $args = array( |
119 | 'body' => wp_json_encode( $body ), |
120 | 'compress' => true, |
121 | 'headers' => array( |
122 | $headerName => 1, |
123 | 'Content-Type' => 'application/json', |
124 | ), |
125 | 'sslverify' => false, |
126 | 'user-agent' => "WordPress/{$wp_version}; {$host}; {$plugin_brand}/v{$plugin_version}", |
127 | ); |
128 | |
129 | // If WP_DEBUG is enabled, we want to wait for a response. |
130 | if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
131 | $args['blocking'] = false; |
132 | $args['timeout'] = 0.01; |
133 | } |
134 | |
135 | wp_remote_post( 'https://cachepurge.bluehost.com/v0/purge?' . $queryString, $args ); |
136 | } |
137 | } |