-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-graphql-cloudflare-cache.php
More file actions
276 lines (232 loc) · 7.68 KB
/
wp-graphql-cloudflare-cache.php
File metadata and controls
276 lines (232 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* WPGraphQL Cloudflare Cache
*
* Plugin Name: WPGraphQL Cloudflare Cache
* Plugin URI: https://wordpress.org/plugins/wp-graphql-cloudflare-cache
* GitHub Plugin URI: https://github.com/gdidentity/wp-graphql-cloudflare-cache
* Description: Extends WPGraphQL to add Cloudflare Tags to GraphQL responses and purge them when content is updated.
* Version: 1.1.0
* Author: humet
* Author URI: https://www.github.com/humet
* Text Domain: wp-graphql-cloudflare-cache
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
use WpGraphQLCloudflareCache\Admin\Settings;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
add_action( 'admin_init', 'check_for_wpgraphql_dependency' );
if ( ! class_exists( 'WpGraphQLCloudflareCache' ) ) :
/**
* This is the one true WpGraphQLCloudflareCache class
*/
final class WpGraphQLCloudflareCache {
/**
* Stores the instance of the WpGraphQLCloudflareCache class
*
* @since 0.0.1
*
* @var WpGraphQLCloudflareCache The one true WpGraphQLCloudflareCache
*/
private static $instance;
/**
* The instance of the WpGraphQLCloudflareCache object
*
* @since 0.0.1
*
* @return WpGraphQLCloudflareCache The one true WpGraphQLCloudflareCache
*/
public static function instance(): self {
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, __CLASS__ ) ) ) {
self::$instance = new self();
self::$instance->setup_constants();
if ( self::$instance->includes() ) {
self::$instance->settings();
self::$instance->headers();
self::$instance->pluginLinks();
}
/**
* Fire off init action.
*
* @param WpGraphQLCloudflareCache $instance The instance of the WpGraphQLCloudflareCache class
*/
do_action( 'wp_graphql_cloudflare_cache_init', self::$instance );
}
// Return the WpGraphQLCloudflareCache Instance.
return self::$instance;
}
/**
* Throw error on object clone.
* The whole idea of the singleton design pattern is that there is a single object
* therefore, we don't want the object to be cloned.
*
* @since 0.0.1
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong(
__FUNCTION__,
esc_html__(
'The WpGraphQLCloudflareCache class should not be cloned.',
'wp-graphql-cloudflare-cache'
),
'0.0.1'
);
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong(
__FUNCTION__,
esc_html__(
'De-serializing instances of the WpGraphQLCloudflareCache class is not allowed.',
'wp-graphql-cloudflare-cache'
),
'0.0.1'
);
}
/**
* Setup plugin constants.
*
* @since 0.0.1
*/
private function setup_constants(): void {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Plugin version.
if ( ! defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_VERSION' ) ) {
define( 'WP_GRAPHQL_CLOUDFLARE_CACHE_VERSION', get_plugin_data( __FILE__ )['Version'] );
}
// Plugin Folder Path.
if ( ! defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_DIR' ) ) {
define( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_URL' ) ) {
define( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_FILE' ) ) {
define( 'WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_FILE', __FILE__ );
}
// Whether to autoload the files or not.
if ( ! defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_AUTOLOAD' ) ) {
define( 'WP_GRAPHQL_CLOUDFLARE_CACHE_AUTOLOAD', true );
}
}
/**
* Uses composer's autoload to include required files.
*
* @since 0.0.1
*
* @return bool
*/
private function includes(): bool {
// Autoload Required Classes.
if ( defined( 'WP_GRAPHQL_CLOUDFLARE_CACHE_AUTOLOAD' ) && false !== WP_GRAPHQL_CLOUDFLARE_CACHE_AUTOLOAD ) {
if ( file_exists( WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
require_once WP_GRAPHQL_CLOUDFLARE_CACHE_PLUGIN_DIR . 'vendor/autoload.php';
}
// Bail if installed incorrectly.
if ( ! class_exists( '\WpGraphQLCloudflareCache\Admin\Settings' ) ) {
add_action( 'admin_notices', [ $this, 'missing_notice' ] );
return false;
}
}
return true;
}
/**
* Composer dependencies missing notice.
*
* @since 0.0.1
*/
public function missing_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
} ?>
<div class="notice notice-error">
<p>
<?php esc_html_e( 'WPGraphQL Cloudflare Cache appears to have been installed without its dependencies. It will not work properly until dependencies are installed. This likely means you have cloned the repository from GitHub and need to run the command `composer install`.', 'wp-graphql-cloudflare-cache' ); ?>
</p>
</div>
<?php
}
/**
* Set up settings.
*
* @since 0.0.1
*/
private function settings(): void {
$settings = new Settings();
$settings->init();
}
/**
* Set up Purge.
*
* @since 0.0.1
*/
private function headers(): void {
\WpGraphQLCloudflareCache\ResponseHeaders::init();
\WpGraphQLCloudflareCache\Purge::init();
}
/**
* Set up Action Links.
*
* @since 0.0.1
*/
private function pluginLinks(): void {
// Setup Settings link.
add_filter('plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) {
$url = esc_url( admin_url( 'admin.php?page=wp-graphql-cloudflare-cache' ) );
$links[] = '<a href="' . $url . '">Settings</a>';
return $links;
});
}
}
function check_for_wpgraphql_dependency() {
if ( is_admin() && current_user_can( 'activate_plugins' ) && ! is_plugin_active( 'wp-graphql/wp-graphql.php' ) ) {
add_action( 'admin_notices', 'show_wpgraphql_dependency_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- standard plugin activation cleanup.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
} else {
// Ensure get_plugin_data is available
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/wp-graphql/wp-graphql.php' );
$plugin_version = $plugin_data['Version'];
if ( version_compare( $plugin_version, '1.16.0', '<' ) ) {
add_action( 'admin_notices', 'show_wpgraphql_version_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- standard plugin activation cleanup.
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
}
}
}
function show_wpgraphql_dependency_notice() {
?>
<div class="error">
<p><?php esc_html_e( 'Error: Plugin "WP GraphQL Cloudflare Cache" requires "WPGraphQL" to be activated.', 'wp-graphql-cloudflare-cache' ); ?></p>
</div>
<?php
}
function show_wpgraphql_version_notice() {
?>
<div class="error">
<p><?php esc_html_e( 'Error: Plugin "WP GraphQL Cloudflare Cache" requires "WPGraphQL" version 1.16.0 or higher.', 'wp-graphql-cloudflare-cache' ); ?></p>
</div>
<?php
}
endif;
\WpGraphQLCloudflareCache::instance();