-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl2x-remote-purge.php
204 lines (172 loc) · 5.14 KB
/
pl2x-remote-purge.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
<?php
/**
* Plugin Name: PL2X Remote Purge
* Author: Evan Mattson
* Version: 0.2
* Description: A utility to allow for PL2X cache to be purged remotely using a dynamic url.
* GitHub Plugin URI: aaemnnosttv/pl2x-remote-purge
*/
namespace PL2X;
use stdClass;
use OptEngine;
class RemotePurge
{
// pl settings option key
const PL_OPT_KEY = 'remote_purge_key';
// purge query var
const PURGE_QUERY_VAR = 'pl2x_purge';
// regenerate purge key query var
const PURGE_REGEN_KEY = 'pl2x_purge_regen';
// purge key regenerated status query var
const PURGE_REGEN_KEY_STATUS = 'purge_key_regenerated';
public static function init()
{
self::add_filter('pagelines_options_array', 'add_panel');
self::add_action('pagelines_options_pl2x_purge_field', 'render_purge_key_field', 10, 2);
self::add_action('admin_menu', 'admin_menu', 20);
self::add_action('admin_notices', 'regenerated_notice');
// set initial value if there isn't one
if ( ! self::get_purge_key() )
self::regenerate_purge_key();
self::listen();
}
public static function admin_menu()
{
global $_pagelines_options_page_hook; // yuck
self::add_action("load-{$_pagelines_options_page_hook}", 'maybe_regenerate_key');
}
private static function listen()
{
if ( $key = filter_input(INPUT_GET, self::PURGE_QUERY_VAR) )
{
$response = new stdClass;
if ( $key === self::get_purge_key() )
{
delete_transient(EXTEND_UPDATE);
delete_transient('pagelines_extend_themes');
delete_transient('pagelines_extend_sections');
delete_transient('pagelines_extend_plugins');
delete_transient('pagelines_extend_integrations');
delete_transient('pagelines_sections_cache');
remove_theme_mod('available_updates');
remove_theme_mod('pending_updates');
do_action('extend_flush');
$response->status = 'success';
$response->message = 'Cache purged.';
$status_code = 200;
}
else
{
$response->status = 'fail';
$response->message = 'Invalid key.';
$status_code = 422;
}
if ( ! headers_sent() )
{
nocache_headers();
@header("Content-type: application/json");
status_header( $status_code );
echo json_encode( $response );
exit;
}
else
{
wp_die( $response->message, $response->status, array('response' => $status_code) );
}
// silence
}
}
public static function maybe_regenerate_key()
{
if ( $nonce = filter_input(INPUT_GET, self::PURGE_REGEN_KEY) )
{
if ( wp_verify_nonce( $nonce, self::PURGE_REGEN_KEY ) )
{
self::regenerate_purge_key();
wp_redirect( add_query_arg(array(
self::PURGE_REGEN_KEY => false,
self::PURGE_REGEN_KEY_STATUS => true
)) );
exit;
}
else
{
wp_redirect( add_query_arg(array(
self::PURGE_REGEN_KEY => false,
self::PURGE_REGEN_KEY_STATUS => 0,
'error' => 'invalid_nonce'
)) );
exit;
}
}
}
public static function regenerated_notice()
{
if ( filter_input(INPUT_GET, self::PURGE_REGEN_KEY_STATUS) )
{
?>
<div class="updated">
<p>PL2X purge key regenerated successfully!</p>
</div>
<?php
}
}
public static function add_panel( $opts )
{
$opts['remote_purge'] = (array) self::get_panel_config();
return $opts;
}
public static function get_panel_config()
{
$config = new stdClass;
$config->remote_purge_key = array(
'title' => 'Remote Purge Key',
'shortexp' => 'For purging theme cache remotely. Especially useful for regenerating styles after deploying new changes.',
'type' => 'pl2x_purge_field',
'exp' => sprintf('<div><a href="%s" style="text-decoration:none;" onclick="return false;" disabled><span class="dashicons dashicons-admin-links"></span> Copy URL</a></div>
<div><em>%s</em></div>',
self::get_purge_url(),
'Right-click and choose Copy Link Address'
)
);
return $config;
}
public static function render_purge_key_field( $oid, $o )
{
?>
<pre style="max-width: 100%; overflow-x: auto;"><?php echo self::get_purge_url() ?></pre>
<div><a href="<?php echo self::get_regen_url() ?>" class="button">Regenerate key</a></div>
<em>This will invalidate the current key and replace it with a new one.</em>
<?php echo OptEngine::input_hidden('pl2x_purge_key', $o['input_name'], $o['val']) ?>
<?php
}
private static function get_purge_url()
{
return add_query_arg(array(self::PURGE_QUERY_VAR => self::get_purge_key()), home_url());
}
private static function get_regen_url()
{
return add_query_arg(array(self::PURGE_REGEN_KEY => wp_create_nonce(self::PURGE_REGEN_KEY)));
}
private static function get_purge_key()
{
return ploption(self::PL_OPT_KEY);
}
private static function regenerate_purge_key()
{
plupop(self::PL_OPT_KEY, self::generate_purge_key());
}
public static function generate_purge_key()
{
return wp_hash( uniqid() );
}
public static function add_action($handle, $method, $priority = 10, $args = 1)
{
add_action( $handle, array(__CLASS__, $method), $priority, $args);
}
public static function add_filter($handle, $method, $priority = 10, $args = 1)
{
add_filter( $handle, array(__CLASS__, $method), $priority, $args);
}
}
add_action('pagelines_hook_init', 'PL2X\\RemotePurge::init');