PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.0.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.0.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / Extensions / Cloudflare / Cloudflare.php

Cloudflare.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 2.0.2, at includes/classes/Extensions/Cloudflare/Cloudflare.php

119 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cloudflare Extension
4 *
5 * @package PoweredCache\Extensions
6 */
7
8 namespace PoweredCache\Extensions\Cloudflare;
9
10 /**
11 * Class Cloudflare
12 */
13 class Cloudflare {
14
15 /**
16 * CF API Class
17 *
18 * @var $cf_api
19 */
20 private $cf_api;
21
22 /**
23 * Plugin settings
24 *
25 * @var $settings
26 */
27 private $settings;
28
29 /**
30 * Placeholder constructor
31 */
32 public function __construct() {
33 }
34
35
36 /**
37 * Return an instance of the current class, create one if it doesn't exist
38 *
39 * @return object
40 * @since 1.0
41 */
42 public static function factory() {
43
44 static $instance;
45
46 if ( ! $instance ) {
47 $instance = new self();
48 $instance->setup();
49 }
50
51 return $instance;
52 }
53
54 /**
55 * Setup hooks
56 */
57 public function setup() {
58 $this->settings = \PoweredCache\Utils\get_settings();
59 $this->cf_api = API::factory( $this->settings['cloudflare_email'], $this->settings['cloudflare_api_key'] );
60
61 // Fixes Flexible SSL
62 if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
63 $_SERVER['HTTPS'] = 'on';
64 }
65
66 // real user ip
67 if ( isset( $_SERVER['HTTP_CF_CONNECTING_IP'] ) ) {
68 $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
69 }
70
71 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
72 add_action( 'powered_cache_flushed', [ $this, 'delete_cloudflare_cache_on_flush' ] );
73 add_action( 'admin_post_powered_cache_purge_cf_cache', [ $this, 'delete_cloudflare_cache' ] );
74
75 }
76
77 /**
78 * Add admin bar CF flush menu
79 *
80 * @param Object $wp_admin_bar admin bar object
81 */
82 public function admin_bar_menu( $wp_admin_bar ) {
83 $wp_admin_bar->add_menu(
84 array(
85 'id' => 'cf-purge-cache',
86 'title' => esc_html__( 'Purge Cloudflare Cache', 'powered-cache' ),
87 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_cf_cache' ), 'powered_cache_purge_cf_cache' ),
88 'parent' => 'powered-cache',
89 )
90 );
91 }
92
93 /**
94 * Delete CF cache when deleting all cache
95 */
96 public function delete_cloudflare_cache_on_flush() {
97 $this->cf_api->purge( $this->settings['zone'] );
98 }
99
100 /**
101 * Delete CF cache when it triggered from admin menu
102 */
103 public function delete_cloudflare_cache() {
104 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_cf_cache' ) ) {
105 wp_nonce_ays( '' );
106 }
107
108 if ( $this->cf_api->purge( $this->settings['zone'] ) ) {
109 $redirect_url = add_query_arg( 'pc_action', 'flush_cf_cache', wp_get_referer() );
110 wp_safe_redirect( $redirect_url );
111 exit;
112 }
113 $redirect_url = add_query_arg( 'pc_action', 'flush_cf_cache_failed', wp_get_referer() );
114 wp_safe_redirect( $redirect_url );
115 exit;
116 }
117
118 }
119