-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-bypass-transients.php
229 lines (191 loc) · 5.87 KB
/
class-bypass-transients.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
<?php
namespace bypassTransients;
class Bypass_Transients {
protected $_known_transients = array();
protected $_found_transients = array();
protected $_option_key = 'st_known_transients';
protected $_transients_bypassed = array();
protected $_is_bypassing = false;
/**
* Entry point
*/
public function init() {
if ( isset( $_GET['bypass-transients'] ) ) {
$this->_is_bypassing = true;
add_action( 'after_setup_theme', [ $this, 'filter_all_known_transients' ] );
}
if ( isset( $_GET['flush-transients'] ) ) {
add_action( 'after_setup_theme', [ $this, 'flush_transients' ] );
}
add_action( 'setted_transient', [ $this, 'setted_callback' ] );
add_action( 'shutdown', [ $this, 'save_found_transients' ] );
add_action( 'admin_bar_menu', [ $this, 'inject_admin_bar_button' ] );
add_action( 'admin_bar_menu', [ $this, 'inject_admin_bar_button_scan' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'add_admin_bar_css' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'add_admin_bar_css' ] );
}
function add_admin_bar_css() {
$min = ( defined( 'SCRIPT_DEBUG' ) & true === SCRIPT_DEBUG ) ? 'src' : 'min';
wp_enqueue_style( 'bypass-transients', plugins_url( 'assets/css/bypass-transients.' . $min . '.css', dirname( __FILE__ ) ) );
}
/**
* Add a filter for each known transient to return it as false.
*/
public function filter_all_known_transients() {
$this->verify_user_intent( 'bypass_transients' );
$this->_known_transients = $this->get_known_transients();
foreach ( $this->get_known_transients() as $transient ) {
add_filter( 'transient_' . $transient , [ $this, 'count_and_return_false' ], 10, 2 );
}
}
public function count_and_return_false( $value, $transient ) {
$this->_transients_bypassed[] = $transient;
return false;
}
/**
* Any set_transient call will add the key to the array
* @param $transient_name
*/
public function setted_callback( $transient_name ) {
if ( is_array( $this->_known_transients ) && ! in_array( $transient_name, $this->_known_transients, true ) ) {
$this->_found_transients[] = $transient_name;
}
}
/**
* Let's add any new transients that were found during the page load.
*/
public function save_found_transients() {
if ( ! empty( $this->_found_transients ) ) {
$known = $this->get_known_transients();
update_option( $this->_option_key, array_merge( $known, $this->_found_transients ) );
}
}
/**
* Callback to save all of the known options very late in the WP load
* to avoid race conditions.
*/
public function save_known_transients() {
update_option( $this->_option_key, $this->_known_transients );
}
/**
* Helper to retrieve the known transients.
* @return mixed|void
*/
public function get_known_transients() {
return get_option( $this->_option_key , array() );
}
/**
* Helper to retrieve suspended transients.
* @return array
*/
public function get_bypassed_transients() {
return $this->_transients_bypassed;
}
/**
* Helper to retrieve found transients
* @return array
*/
public function get_found_transients() {
return $this->_found_transients;
}
/**
* Flush the object cache
*/
public function flush_transients() {
$this->verify_user_intent( 'flush_transients' );
wp_cache_flush();
}
/**
* Helper to DRY out the nonce checks
* @param $nonce
*/
protected function verify_user_intent( $nonce ) {
if ( ! current_user_can( 'manage_options' ) || false === wp_verify_nonce( $_GET['wp_nonce'], $nonce ) ) {
wp_die( 'You don\'t have the correct permissions to do that' );
}
}
/**
* On some on activation stuff.
*/
public function on_activate() {}
/**
* Inject the Bypass button.
*/
public function inject_admin_bar_button() {
global $wp_admin_bar;
$classes = 'bypass-transients';
if ( $this->_is_bypassing ) {
$classes .= ' active';
} elseif ( 0 < count( $this->_found_transients ) ) {
$classes .= ' found';
}
$title = ( $this->_is_bypassing ) ? 'Re-activate Transients' : 'Bypass Transients';
$href = ( $this->_is_bypassing ) ? '/' :'?bypass-transients=true&wp_nonce=' . wp_create_nonce( 'bypass_transients' );
$wp_admin_bar->add_menu(
array(
'id' => 'bypass-transients',
'parent' => 'top-secondary',
'title' => $title,
'href' => $href,
'meta' => array( 'class' => $classes ),
)
);
if ( $this->_is_bypassing ) {
$wp_admin_bar->add_menu(
array(
'id' => 'bypassed-transients',
'parent' => 'bypass-transients',
'title' => 'Bypassed Transients: ' . count( $this->_transients_bypassed ),
)
);
foreach ( $this->_transients_bypassed as $key => $transient ) {
$wp_admin_bar->add_menu(
array(
'id' => $key . '_' . $transient,
'parent' => 'bypassed-transients',
'title' => $transient,
)
);
}
}
$wp_admin_bar->add_menu(
array(
'id' => 'known-transients',
'parent' => 'bypass-transients',
'title' => 'Known Transients: ' . count( $this->get_known_transients() ),
)
);
if ( 0 < count( $this->get_found_transients() ) ) {
$wp_admin_bar->add_menu(
array(
'id' => 'found-transients',
'parent' => 'bypass-transients',
'title' => 'Found Transients: ' . count( $this->get_found_transients() ),
)
);
foreach ( $this->_found_transients as $key => $transient ) {
$wp_admin_bar->add_menu(
array(
'id' => $key . '_' . $transient,
'parent' => 'found-transients',
'title' => $transient,
)
);
}
}
}
/**
* Add the Flush transients button
*/
public function inject_admin_bar_button_scan() {
global $wp_admin_bar;
$wp_admin_bar->add_menu(
array(
'id' => 'flush-transients',
'parent' => 'bypass-transients',
'title' => 'Flush Transients',
'href' => '?flush-transients=true&wp_nonce=' . wp_create_nonce( 'flush_transients' ),
)
);
}
}