-
Notifications
You must be signed in to change notification settings - Fork 0
/
tt-class.php
422 lines (327 loc) · 12.1 KB
/
tt-class.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
<?php
class TemporalTransients {
public $tt_settings = array();
public $tt_time;
public $tt_version = 0.1;
public $wp_version;
/**
* Start it going!
* Pass in true if its the first load so we are not doubling up and running
* WordPress filters more than required.
*
* @param bool $load
*/
public function __construct($load = false) {
// We make the WordPress version available just in case someone is using a legacy version
global $wp_version;
$this->wp_version = $wp_version;
// Set the default transient life time to 24 hours
$this->tt_time = 60 * 60 * 24;
// Set up the default settings
$this->set_settings();
// Apply TT specific filters!
$this->tt_filters();
if ($load) {
// Do the WordPress filters!
$this->tt_add_filters();
}
if(is_admin()) {
add_action( 'admin_init', array($this, 'tt_admin_init'));
add_action( 'admin_footer', array($this, 'tt_admin_ajax_purge_transients_javascript_css') );
add_action( 'wp_ajax_tt_purge_transients', array($this, 'tt_admin_ajax_purge_transients_action_callback') );
}
}
public function tt_admin_init() {
add_settings_section( 'tt_purge_cache_section', 'Temporal Transients - Purge All Transients', array($this,'tt_admin_purge_transients_section'), 'general' );
add_settings_field( 'tt_purge_cache_field', 'Purge Transients', array($this,'tt_admin_purge_transients_field'), 'general', 'tt_purge_cache_section');
}
/**
* Sets the default settings for the plugin
*
* This can be overridden via the tt_settings filter set up in tt_filters
*/
public function set_settings() {
$this->tt_settings = array(
"navigation" => true,
"the_content" => true
);
}
public function install() {
}
/**
* Custom Filters to allow plugins and themes to manipulate the TT plugin settings
*/
public function tt_filters() {
/**
* Filter whether to change the Transient Default time from 24h
*
* @since 0.1
*
* @param int $this->tt_time An int containing the new default Transient Time
*/
$this->tt_time = apply_filters( 'tt_life_time', $this->tt_time );
/**
* Filter the plugin settings
* Allows users to turn on or off a variety of transients
*
* @since 0.1
*
* @param array $this->tt_settings An array used in tt_add_filters to determine if a transient should be created
*/
$this->tt_settings = apply_filters( 'tt_settings', $this->tt_settings );
}
/**
* Execute the WordPress filters.
*
* This is the grunt of the Plugin where Transients are Stored, Returned and Distroyed based on the rules set out
*/
public function tt_add_filters() {
//These filters should always run
//Delete Transients upon permalink update
add_filter('rewrite_rules_array', array($this, 'tt_rewrite_rules_array'));
// Make sure that our users are running a compatible version of WordPress
if (version_compare($this->wp_version, '3.9', '>=')) {
// Navigation filters
if (!empty($this->tt_settings['navigation'])) {
add_filter( 'pre_wp_nav_menu', array($this, 'tt_pre_wp_nav_menu'), 10, 2);
add_filter( 'wp_nav_menu', array($this,'tt_wp_nav_menu'), 10, 2);
add_action( 'wp_update_nav_menu', array($this,'tt_wp_update_nav_menu'), 10, 1);
add_filter( 'pre_set_theme_mod_nav_menu_locations', array($this, 'tt_pre_set_theme_mod_nav_menu_locations'),10, 2);
}
// Content filters
if (!empty($this->tt_settings['the_content'])) {
add_action( 'save_post', array($this, 'tt_save_post'));
}
}
}
/**
* Retrieve the Menu if it has a existing Transient and is still valid
*
* @param $output
* @param $args
* @return mixed
*/
public function tt_pre_wp_nav_menu($output, $args) {
$hash_generator = new TT_Hash_Generator($args->theme_location);
$hash = $hash_generator->get_hash();
$transient = get_transient('tt_nav_'.$hash);
// Get transient returns false if nothing was found, we need to only return if it has a value that is not false
if ($transient) {
return $transient;
}
// We need to return null if the transient is false otherwise no menu will be rendered.
return null;
}
/**
* Grabs the generated menu and stores it
*
* @param $nav_menu - The generated HTML of the menu
* @param $args - arguments passed into the wp_nav_menu function
* @return $nav_menu
*/
public function tt_wp_nav_menu($nav_menu, $args) {
$hash_generator = new TT_Hash_Generator($args->theme_location);
$hash = $hash_generator->get_hash();
set_transient('tt_nav_'.$hash, $nav_menu, $this->tt_time);
return $nav_menu;
}
/**
* Make sure we clear out the old transient any time we update a particular menu
*
* @param $menu_id
*/
public function tt_wp_update_nav_menu($menu_id) {
$this->tt_purge_transient_type('nav');
}
/**
* When someone updates the Menu Locations than we need to remove the transient for said menu also
*
* @param $value
* @param $old_value
*/
public function tt_pre_set_theme_mod_nav_menu_locations($value, $old_value) {
$this->tt_purge_transient_type('nav');
}
/**
* An Override for the_content
*
* Requires users to edit their themes
*
*/
public function tt_the_content( $more_link_text = null, $strip_teaser = false) {
$hash_generator = new TT_Hash_Generator;
$hash = $hash_generator->get_hash();
if ($this->tt_settings['the_content'] === true) {
$transient = get_transient('tt_content_' . $hash);
// If we get a transient back then echo that out and don't bother generating anything more
if ($transient) {
echo $transient;
return;
}
}
$content = get_the_content( $more_link_text, $strip_teaser );
/**
* Filter the post content.
*
* @since 0.71
*
* @param string $content Content of the current post.
*/
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
if ($this->tt_settings['the_content'] === true) {
set_transient('tt_content_' . $hash, $content, $this->tt_time);
}
echo $content;
}
/**
* Deletes a transient for a post when tt_the_content has been used to call it
*
* @param $post_id
*/
public function tt_save_post($post_id) {
$hash_generator = new TT_Hash_Generator;
$hash = $hash_generator->get_hash();
delete_transient('tt_content_'.$hash);
}
/**
* Purges all TT Transients from the database when the permalinks are updated.
*
* @param $rules
* @return mixed
*/
public function tt_rewrite_rules_array($rules) {
$this->tt_purge_all_transients();
return $rules;
}
/**
* Add in our button to execute our action to purge transients via Ajax
* Added to the General Options page
*
*/
public function tt_admin_purge_transients_field() {
echo '<button id="tt_delete_transients_ajax" class="button button-primary">Purge!</button>';
}
/**
* Add in our Section where out Ajax button will live
* Added to the General Options page
*
*/
public function tt_admin_purge_transients_section() {
echo '<p>Purge all stored Temporal Transients<br/> Use this if your content is not being refreshed after updating your content.</p>';
}
/**
* Adds in required JS and CSS to the admin footer
*
*/
public function tt_admin_ajax_purge_transients_javascript_css() {
$nonce = wp_create_nonce( "tt_purge_transients" );
?>
<style>
@-ms-keyframes spin {
from { -ms-transform: rotate(0deg); }
to { -ms-transform: rotate(360deg); }
}
@-moz-keyframes spin {
from { -moz-transform: rotate(0deg); }
to { -moz-transform: rotate(360deg); }
}
@-webkit-keyframes spin {
from { -webkit-transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
from {
transform:rotate(0deg);
}
to {
transform:rotate(360deg);
}
}
#tt_delete_transients_ajax span.spin {
padding: 3px 0;
-webkit-animation-name: spin;
-webkit-animation-duration: 4000ms;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
-moz-animation-name: spin;
-moz-animation-duration: 4000ms;
-moz-animation-iteration-count: infinite;
-moz-animation-timing-function: linear;
-ms-animation-name: spin;
-ms-animation-duration: 4000ms;
-ms-animation-iteration-count: infinite;
-ms-animation-timing-function: linear;
animation-name: spin;
animation-duration: 4000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
</style>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'tt_purge_transients',
'nonce': '<?php echo $nonce; ?>'
};
$('#tt_delete_transients_ajax').on('click', function(a) {
a.preventDefault();
var e = this;
$(e).html('Purging <span class="dashicons dashicons-update spin"></span>');
$.post(ajaxurl, data, function(response) {
if(response >= 0) {
$(e).html('Purged - Purge again');
} else {
$(e).replaceWith('<div class="error"><p>An Error Occured, Please refresh the page and try again</p></div>');
}
});
});
});
</script>
<?php
}
/**
* Our action for Purging Temporal Transients items
* Uses WordPress nonce to ensure security
*
* returns -1 if the nonce does not clear
* returns number of rows deleted when query is run
*
*/
public function tt_admin_ajax_purge_transients_action_callback() {
check_ajax_referer('tt_purge_transients', 'nonce');
echo $this->tt_purge_all_transients();
wp_die();
}
/**
* Purge All Temporal Transient items
*
* @return mixed
*/
private function tt_purge_all_transients() {
global $wpdb;
$query = "DELETE FROM $wpdb->options WHERE `option_name` LIKE '_transient_tt%'";
$result = $wpdb->query($query);
return $result;
}
/**
* Purgle a Specific Temporal Transient type
*
* Options:
* 'nav'
* 'content'
*
* @param mixed |false $type
* @return mixed
*/
private function tt_purge_transient_type($type = false) {
if(!empty($type)) {
global $wpdb;
$like_string = "'_transient_tt_".$type."%'";
$query = "DELETE FROM $wpdb->options WHERE `option_name` LIKE ".$like_string;
$result = $wpdb->query($query);
return $result;
}
return $type;
}
}