-
Notifications
You must be signed in to change notification settings - Fork 14
/
wp-graphql-smart-cache.php
291 lines (246 loc) · 7.68 KB
/
wp-graphql-smart-cache.php
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Plugin Name: WPGraphQL Smart Cache
* Plugin URI: https://github.com/wp-graphql/wp-graphql-smart-cache
* GitHub Plugin URI: https://github.com/wp-graphql/wp-graphql-smart-cache
* Description: Smart Caching and Cache Invalidation for WPGraphQL
* Author: WPGraphQL
* Author URI: http://www.wpgraphql.com
* Requires at least: 5.6
* Tested up to: 6.6.1
* Requires PHP: 7.4
* Text Domain: wp-graphql-smart-cache
* Domain Path: /languages
* Version: 1.3.3
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*
* Persisted Queries and Caching for WPGraphQL
*/
namespace WPGraphQL\SmartCache;
use Appsero\Client;
use WPGraphQL\SmartCache\Cache\Collection;
use WPGraphQL\SmartCache\Cache\Invalidation;
use WPGraphQL\SmartCache\Cache\Results;
use WPGraphQL\SmartCache\Admin\Editor;
use WPGraphQL\SmartCache\Admin\Settings;
use WPGraphQL\SmartCache\Document\Description;
use WPGraphQL\SmartCache\Document\Grant;
use WPGraphQL\SmartCache\Document\Group;
use WPGraphQL\SmartCache\Document\MaxAge;
use WPGraphQL\SmartCache\Document\Loader;
use WPGraphQL\SmartCache\Document\GarbageCollection;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// If the autoload file exists, require it.
// If the plugin was installed from composer, the autoload
// would be required elsewhere in the project
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
if ( ! defined( 'WPGRAPHQL_SMART_CACHE_VERSION' ) ) {
define( 'WPGRAPHQL_SMART_CACHE_VERSION', '1.3.3' );
}
if ( ! defined( 'WPGRAPHQL_SMART_CACHE_WPGRAPHQL_REQUIRED_MIN_VERSION' ) ) {
define( 'WPGRAPHQL_SMART_CACHE_WPGRAPHQL_REQUIRED_MIN_VERSION', '1.12.0' );
}
if ( ! defined( 'WPGRAPHQL_SMART_CACHE_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_SMART_CACHE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
/**
* Check whether WPGraphQL is active, and whether the minimum version requirement has been met,
* and whether the autoloader is working as expected
*
* @return bool
* @since 0.3
*/
function can_load_plugin() {
// Is WPGraphQL active?
if ( ! class_exists( 'WPGraphQL' ) ) {
return false;
}
// Do we have a WPGraphQL version to check against?
if ( ! defined( 'WPGRAPHQL_VERSION' ) ) {
return false;
}
// Have we met the minimum version requirement?
// @phpstan-ignore-next-line
if ( version_compare( WPGRAPHQL_VERSION, WPGRAPHQL_SMART_CACHE_WPGRAPHQL_REQUIRED_MIN_VERSION, 'lt' ) ) {
return false;
}
// If the Document class doesn't exist, then the autoloader failed to load.
// This likely means that the plugin was installed via composer and the parent
// project doesn't have the autoloader setup properly
if ( ! class_exists( Document::class ) ) {
return false;
}
return true;
}
/**
* Set the graphql-php server persistent query loader during server config setup.
* When a queryId is found on the request, the call back is invoked to look up the query string.
*/
add_action(
'graphql_server_config',
function ( \GraphQL\Server\ServerConfig $config ) {
$config->setPersistentQueryLoader(
[ Loader::class, 'by_query_id' ]
);
},
10,
1
);
add_action(
'init',
function () {
/**
* If WPGraphQL is not active, or is an incompatible version, show the admin notice and bail
*/
if ( false === can_load_plugin() ) {
// Show the admin notice
add_action( 'admin_init', __NAMESPACE__ . '\show_admin_notice' );
// Bail
return;
}
$document = new Document();
$document->init();
$description = new Description();
$description->init();
$grant = new Grant();
$grant->init();
$max_age = new MaxAge();
$max_age->init();
$doc_group = new Group();
$doc_group->init();
$errors = new AdminErrors();
$errors->init();
$settings = new Settings();
$settings->init();
}
);
/**
* Show admin notice to admins if this plugin is active but WPGraphQL
* is not active, or doesn't meet version requirements
*
* @return void
*/
function show_admin_notice() {
/**
* For users with lower capabilities, don't show the notice
*/
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
add_action(
'admin_notices',
function () {
?>
<div class="error notice">
<p>
<?php
// translators: placeholder is the version number of the WPGraphQL Plugin that this plugin depends on
$text = sprintf( 'WPGraphQL (v%s+) must be active for "wp-graphql-smart-cache" to work', WPGRAPHQL_SMART_CACHE_WPGRAPHQL_REQUIRED_MIN_VERSION );
// phpcs:ignore
esc_html_e( $text, 'wp-graphql-smart-cache' );
?>
</p>
</div>
<?php
}
);
}
add_action(
'admin_init',
function () {
if ( false === can_load_plugin() ) {
return;
}
$editor = new Editor();
$editor->admin_init();
},
10
);
add_action(
'wp_loaded',
function () {
if ( false === can_load_plugin() ) {
return;
}
// override the query execution with cached results, if present
$results = new Results();
$results->init();
// Start collecting queries for cache
$collection = new Collection();
$collection->init();
// start listening to events that should invalidate caches
$invalidation = new Invalidation( $collection );
$invalidation->init();
}
);
/**
* Initialize the plugin tracker
*
* @return void
*/
function appsero_init_tracker_wpgraphql_smart_cache() {
// If the class doesn't exist, or code is being scanned by PHPSTAN, move on.
if ( ! class_exists( 'Appsero\Client' ) || defined( 'PHPSTAN' ) ) {
return;
}
$client = new Client( '66f03878-3df1-40d7-8be9-0069994480d4', 'WPGraphQL Smart Cache', __FILE__ );
$insights = $client->insights();
// If the Appsero client has the add_plugin_data method, use it
if ( method_exists( $insights, 'add_plugin_data' ) ) {
$insights->add_plugin_data();
}
$insights->init();
}
appsero_init_tracker_wpgraphql_smart_cache();
/**
* The callback function for saved query garbage collection event.
* Look for saved queries to cleanup and schedule a job to do small batches of deletes.
*/
add_action(
'wpgraphql_smart_cache_query_garbage_collect',
function () {
// Check that the clean up toggle is still enabled.
$garbage_toggle = get_graphql_setting( 'query_garbage_collect', null, 'graphql_persisted_queries_section' );
if ( 'on' !== $garbage_toggle ) {
// Remove the scheduled cron job from firing again if the toggle is not on.
wp_clear_scheduled_hook( 'wpgraphql_smart_cache_query_garbage_collect' );
return;
}
// If more posts exist to remove, schedule the removal event
$posts = GarbageCollection::get_documents_by_age( 1 );
if ( $posts ) {
wp_schedule_single_event( time() + 1, 'wpgraphql_smart_cache_query_garbage_collect_deletes' );
}
},
10
);
/**
* The callback function to do the actual deletes of posts that are aged out and need garbage collection.
* This job will run, load a 'batch' number of posts, instead of loading ALL posts to delete.
* After processing the deletes, if more remain, this job is scheduled again to process another batch.
* Do these 'batch' runs of deletes in hope of reducing server load, timeouts, large numbers of deletes in one loop.
*/
add_action(
'wpgraphql_smart_cache_query_garbage_collect_deletes',
function () {
// If posts exist to remove, schedule the removal event
$batch_size = apply_filters( 'wpgraphql_document_garbage_collect_batch_size', 1000 );
$posts = GarbageCollection::get_documents_by_age( $batch_size );
foreach ( $posts as $post_id ) {
// Check if the post is selected to skip garbage collection
wp_delete_post( $post_id );
}
// If more posts exist to remove, schedule the removal event
$posts = GarbageCollection::get_documents_by_age( 1 );
if ( ! empty( $posts ) ) {
wp_schedule_single_event( time() + 1, 'wpgraphql_smart_cache_query_garbage_collect_deletes' );
}
},
10
);