-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-stellate.php
543 lines (467 loc) · 15.6 KB
/
wp-stellate.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/**
* Plugin Name: Stellate
* Plugin URI: https://github.com/StellateHQ/wp-stellate
* GitHub Plugin URI: https://github.com/StellateHQ/wp-stellate
* Description: Stellate for your WordPress GraphQL API
* Author: Stellate
* Author URI: https://stellate.co
* Version: 0.1.8
* Requires at least: 5.0
* Tested up to: 6.4.0
* Requires PHP: 7.1
* License: GPL-3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*
* @package Stellate
* @author Stellate
* @version 0.1.8
*/
/**
* Exit if accessed directly.
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Register the settings
*/
add_action('admin_init', function () {
register_setting('stellate', 'stellate_service_name');
register_setting('stellate', 'stellate_purging_token', [
'sanitize_callback' => function ($token) {
$hasChanged = (bool) $_POST["stellate_touched_purging_token"];
return $hasChanged ? $token : get_option('stellate_purging_token');
}
]);
register_setting('stellate', 'stellate_soft_purge');
});
/**
* Add a "Settings" page for Stellate (part of the "GraphQL" Menu provided by WPGraphQL)
*/
add_action('admin_menu', function () {
add_submenu_page(
'graphiql-ide',
__('WPGraphQL Settings', 'wp-graphql'),
'Caching',
'manage_options',
'graphql-caching',
'stellate_render_caching_page'
);
});
function stellate_render_caching_page()
{
$service_name = get_option('stellate_service_name');
$concealed_token = str_repeat('*', strlen(get_option('stellate_purging_token')));
$soft_purge = get_option('stellate_soft_purge') === 'on' ? 'checked' : '';
?>
<?php echo get_option('stellate_purging_token') ?>
<div class="wrap">
<h2>GraphQL Edge Caching with Stellate</h2>
<h3>Settings</h3>
<form action="options.php" method="POST" autocomplete="off">
<?php
settings_fields('stellate');
do_settings_sections('stellate');
?>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">Service name</th>
<td>
<input type="text" name="stellate_service_name" class="regular-text" value="<?php echo esc_attr($service_name) ?>" />
<p><?php esc_attr_e('Enter the name of your Stellate service. Without this the Stellate plugin will do nothing.', 'WpAdminStyle'); ?></p>
</td>
</tr>
<tr>
<th scope="row">Purging token</th>
<td>
<input type="password" name="stellate_purging_token" class="regular-text" value="<?php echo esc_attr($concealed_token) ?>" />
<input type="hidden" name="stellate_touched_purging_token" value="0" />
<noscript>
<input type="hidden" name="stellate_touched_purging_token" value="1" />
</noscript>
<p><?php esc_attr_e('Enter a purging token created for the Stellate service entered above. Without this the Stellate plugin will do nothing.', 'WpAdminStyle'); ?></p>
</td>
</tr>
<tr>
<th scope="row">Use soft purging</th>
<td>
<label>
<input name="stellate_soft_purge" type="checkbox" <?php echo esc_attr($soft_purge) ?> />
<span><?php esc_attr_e('When using soft purging, the cache continues to serve stale data while it is revalidated in the background.', 'WpAdminStyle'); ?></span>
</label>
</td>
</tr>
</tbody>
</table>
<?php submit_button() ?>
</form>
<script>
document.querySelector('input[name="stellate_purging_token"]').addEventListener('input', function() {
document.querySelector('input[name="stellate_touched_purging_token"]').value = 1;
});
</script>
<h3>Purge the entire cache</h3>
<p>By clicking the following button you purge all contents from the cache of your Stellate service.</p>
<form action="admin-post.php" method="POST">
<input type="hidden" name="action" value="stellate_purge_all" />
<?php submit_button('Purge', 'secondary') ?>
</form>
<?php if (isset($_GET['success'])) { ?>
<div class="notice notice-success inline">
<p>Purging succeeded!</p>
</div>
<?php } ?>
<?php if (isset($_GET['failure'])) { ?>
<div class="notice notice-error inline">
<p>
<?php
printf(
esc_attr__('Purging did not succeed: %1$s', 'WpAdminStyle'),
'<code>' . esc_html($_GET['failure']) . '</code>'
);
?>
</p>
</div>
<?php } ?>
</div>
<?php
}
/**
* Handle submitting the form to purge the entire cache (part of the settings page)
*/
add_action('admin_post_stellate_purge_all', function () {
$err = stellate_purge_all();
if (!$err) do_action('stellate_purge', ['has_purged_all' => true]);
$query = $err ? 'failure=' . urlencode($err) : 'success=true';
wp_redirect('admin.php?page=graphql-caching&' . $query);
exit;
});
/**
* This global array stores all the stuff that we want to purge.
*/
$GLOBALS['gcdn_purges'] = [
'has_purged_all' => false,
'purged_types' => [],
'Post' => [],
'Page' => [],
'Category' => [],
'Tag' => [],
'Comment' => [],
'Menu' => [],
'MenuItem' => [],
'User' => []
];
$GLOBALS['gcdn_typename_map'] = [
'post' => 'Post',
'page' => 'Page',
'nav_menu_item' => 'MenuItem'
];
$GLOBALS['gcdn_id_prefix_map'] = [
'Post' => 'post',
'Page' => 'post',
'Category' => 'term',
'Tag' => 'term',
'Comment' => 'comment',
'Menu' => 'term',
'MenuItem' => 'post',
'User' => 'user'
];
function stellate_add_purge_entity(string $key, $value)
{
if (!in_array($value, $GLOBALS['gcdn_purges'][$key], true))
$GLOBALS['gcdn_purges'][$key][] = $value;
}
add_action('registered_post_type', function (string $post_type, WP_Post_Type $post_type_object) {
/**
* Noting to do if the type is not exposed over GraphQL, or if the type
* names are not specified.
*/
if (
!isset($post_type_object->show_in_graphql)
|| !isset($post_type_object->graphql_single_name)
|| !$post_type_object->show_in_graphql
) return;
/** Add an array to collect purges for this custom post type. */
$GLOBALS['gcdn_purges'][$post_type_object->graphql_single_name] = [];
/** Extend the mapping from post type to GraphQL typename. */
$GLOBALS['gcdn_typename_map'][$post_type] = $post_type_object->graphql_single_name;
/** Extend the mapping from GraphQL typename to id prefix. */
$GLOBALS['gcdn_id_prefix_map'][$post_type_object->graphql_single_name] = 'post';
}, 10, 2);
add_action('registered_taxonomy', function (string $taxonomy, $object_type, array $args) {
/**
* Noting to do if the type is not exposed over GraphQL, or if the type
* names are not specified.
*/
if (
!isset($args['show_in_graphql'])
|| !isset($args['graphql_single_name'])
|| !$args['show_in_graphql']
) return;
/** Add an array to collect purges for this custom post type. */
$GLOBALS['gcdn_purges'][$args['graphql_single_name']] = [];
/** Extend the mapping from post type to GraphQL typename. */
$GLOBALS['gcdn_typename_map'][$taxonomy] = $args['graphql_single_name'];
/** Extend the mapping from GraphQL typename to id prefix. */
$GLOBALS['gcdn_id_prefix_map'][$args['graphql_single_name']] = 'term';
/**
* This runs when creating a new term.
*/
add_action("created_{$taxonomy}", function () use ($args) {
stellate_add_purge_entity('purged_types', $args['graphql_single_name']);
});
/**
* This runs when updating an existing term.
*/
add_action("edited_{$taxonomy}", function (int $term_id) use ($args) {
stellate_add_purge_entity($args['graphql_single_name'], $term_id);
});
/**
* This runs when deleting a term.
*/
add_action("delete_${taxonomy}", function (int $term_id) use ($args) {
stellate_add_purge_entity($args['graphql_single_name'], $term_id);
});
}, 10, 3);
/**
* This runs when inserting or updating any post type. This also includes
* pages and menu items.
*/
add_action('wp_insert_post', function (int $post_id, WP_Post $post, bool $update) {
if (!array_key_exists($post->post_type, $GLOBALS['gcdn_typename_map'])) return;
$type = $GLOBALS['gcdn_typename_map'][$post->post_type];
if ($update) {
/**
* When a post or page has been updated, purge just this one post
*/
stellate_add_purge_entity($type, $post_id);
} else {
/**
* When a new post or page has been created, purge all things related to
* that entity
*/
stellate_add_purge_entity('purged_types', $type);
}
/**
* The "edit_category" action does not seem to be called when adding or
* removing a categories to posts. Same story for tags. But we do need
* to purge the cache for these types, because the count of linked posts
* might have changed. So to be safe, we purge aggressively here.
*
* TODO: Implement a more fine-grained purging for this case.
*/
if ($type === 'Post') {
stellate_add_purge_entity('purged_types', 'Category');
stellate_add_purge_entity('purged_types', 'Tag');
}
}, 10, 3);
/**
* This runs when deleting a post type. This also includes pages and menu
* items.
*/
add_action('deleted_post', function (int $post_id, WP_Post $post) {
if (!array_key_exists($post->post_type, $GLOBALS['gcdn_typename_map'])) return;
$type = $GLOBALS['gcdn_typename_map'][$post->post_type];
stellate_add_purge_entity($type, $post_id);
}, 10, 2);
/**
* This runs when creating a new category.
*/
add_action('created_category', function () {
stellate_add_purge_entity('purged_types', 'Category');
});
/**
* This runs when updating an existing category.
*/
add_action('edited_category', function (int $category_id) {
stellate_add_purge_entity('Category', $category_id);
});
/**
* This runs when deleting a category.
*/
add_action('delete_category', function (int $category_id) {
stellate_add_purge_entity('Category', $category_id);
});
/**
* This runs when creating a new tag.
*/
add_action('created_post_tag', function () {
stellate_add_purge_entity('purged_types', 'Tag');
});
/**
* This runs when updating an existing tag.
*/
add_action('edited_post_tag', function (int $tag_id) {
stellate_add_purge_entity('Tag', $tag_id);
});
/**
* This runs when deleting a tag.
*/
add_action('delete_post_tag', function (int $tag_id) {
stellate_add_purge_entity('Tag', $tag_id);
});
/**
* This runs when a new comment is created.
*/
add_action('wp_insert_comment', function () {
stellate_add_purge_entity('purged_types', 'Comment');
});
/**
* This runs when the status of a comment is updated. This catches all of
* the following actions:
* - Approving or un-approvind a comment
* - Marking a comment as spam or not-spam
* - Moving a comment to the trash
* - Deleting a comment irreversibly
*/
add_action('wp_set_comment_status', function (int $comment_id) {
stellate_add_purge_entity('Comment', $comment_id);
});
/**
* This runs when the content of a comment is updated.
*/
add_action('edit_comment', function (int $comment_id) {
stellate_add_purge_entity('Comment', $comment_id);
});
/**
* This runs when the count of comments for a post is updated.
*/
add_action('wp_update_comment_count', function (int $post_id) {
stellate_add_purge_entity('Post', $post_id);
});
/**
* This runs when a new menu is created.
*/
add_action('wp_create_nav_menu', function () {
stellate_add_purge_entity('purged_types', 'Menu');
});
/**
* This runs when a menu is deleted.
*/
add_action('wp_delete_nav_menu', function (int $menu_id) {
stellate_add_purge_entity('Menu', $menu_id);
});
/**
* This runs when a new user is created.
*/
add_action('user_register', function () {
stellate_add_purge_entity('purged_types', 'User');
});
/**
* This runs when an existing user is updated.
*/
add_action('profile_update', function (int $user_id) {
stellate_add_purge_entity('User', $user_id);
});
/**
* This runs when a user is deleted.
*/
add_action('delete_user', function (int $user_id) {
stellate_add_purge_entity('User', $user_id);
});
/**
* When all is done, call the admin API and purge all the entities that we
* collected previously.
*/
function stellate_encode_ids_as_key_field_inputs(string $key, array $ids)
{
$type_prefix = $GLOBALS['gcdn_id_prefix_map'][$key];
$type = ucfirst($key);
return array_map(function ($id) use ($type_prefix) {
$encoded_id = base64_encode($type_prefix . ':' . $id);
return array("name"=>"id", "value"=>$encoded_id);
}, $ids);
};
add_action('shutdown', function () {
$variable_definitions = '$soft: Boolean';
$selection_set = '';
$variable_values = [];
foreach ($GLOBALS['gcdn_purges'] as $key => $value) {
switch ($key) {
case 'has_purged_all':
if ($value) {
$selection_set .= '_purgeAll(soft: $soft)';
}
break;
case 'purged_types':
/** Handle types where all entities should by purged. */
foreach ($value as $type) {
$uppercase_type = ucfirst($type);
$selection_set .= "purge{$uppercase_type}: _purgeType(type: \"{$uppercase_type}\", soft: \$soft)\n";
}
break;
default:
if (count($value) > 0) {
/** Handle purging individual entities by their id. */
$uppercase_key = ucfirst($key);
$variable_name = "{$key}Ids";
$variable_definitions .= " \${$variable_name}: [KeyFieldInput!]";
$selection_set .= "purge{$uppercase_key}ById: _purgeType(type: \"{$uppercase_key}\", soft: \$soft, keyFields: \${$variable_name})\n";
$variable_values[$variable_name] = stellate_encode_ids_as_key_field_inputs($key, $value);
}
break;
}
}
/** Skip sending any request if there is nothing to purge. */
if ($selection_set === '') return;
$query = "mutation WPStellateIntegration({$variable_definitions}) {
{$selection_set}
}";
$err = stellate_call_admin_api($query, $variable_values);
if ($err) {
// Something went wrong, fall back to purging everything
$GLOBALS['gcdn_purges']['has_purged_all'] = true;
stellate_purge_all();
}
do_action('stellate_purge', $GLOBALS['gcdn_purges']);
});
/**
* Helper functions for purging via admin-api
*/
/**
* @return null|string Returns `null` on success, an error message on failure
*/
function stellate_purge_all()
{
return stellate_call_admin_api('mutation WPStellateIntegration_PurgeAll ($soft: Boolean) { _purgeAll(soft: $soft) }', []);
}
/**
* @param string $query The GraphQL operation
* @param array $variables The variables passed with the operation
* @return null|string Returns `null` on success, an error message on failure
*/
function stellate_call_admin_api($query, $variables)
{
$service_name = get_option('stellate_service_name');
$token = get_option('stellate_purging_token');
// If we don't have service name and token, we can't invalidate
if (!$service_name || !$token) return false;
$variables['soft'] = get_option('stellate_soft_purge') === 'on';
$res = wp_remote_post('https://admin.stellate.co/' . $service_name, array(
'headers' => array(
'content-type' => 'application/json',
'stellate-token' => $token
),
'body' => json_encode(array('query' => $query, 'variables' => $variables))
));
if ($res instanceof WP_Error) {
return $res->get_error_message();
}
$is_success = $res['response']['code'] < 300;
$body = json_decode($res['body']);
if ($body === null) {
// Body contains no JSON, success depends on the status code
return $is_success ? null : $res['body'];
}
if (property_exists($body, 'errors')) {
// return the first error
return $body->errors[0]->message;
}
if (!$is_success) {
// Not successful, but also no errors in body
return "Unknown error";
}
return null;
}