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
|
<?php
if (!defined('ABSPATH')) die('No direct access allowed');
/** * Page caching rules and exceptions */
if (!class_exists('WPO_Cache_Config')) require_once('class-wpo-cache-config.php');
require_once dirname(__FILE__) . '/file-based-page-cache-functions.php';
if (!class_exists('WPO_Cache_Rules')) :
class WPO_Cache_Rules {
/** * Cache config object * * @var mixed */ public $config;
/** * Instance of this class * * @var mixed */ public static $instance;
public function __construct() { $this->config = WPO_Cache_Config::instance()->get(); $this->setup_hooks(); }
/** * Setup hooks/filters */ public function setup_hooks() { add_action('save_post', array($this, 'purge_post_on_update'), 10, 1); add_action('save_post', array($this, 'purge_archive_pages_on_post_update'), 10, 1); add_action('wp_trash_post', array($this, 'purge_post_on_update'), 10, 1); add_action('comment_post', array($this, 'purge_post_on_comment'), 10, 3); add_action('wp_set_comment_status', array($this, 'purge_post_on_comment_status_change'), 10, 1); add_action('edit_terms', array($this, 'purge_related_elements_on_term_updated'), 10, 2); add_action('set_object_terms', array($this, 'purge_related_elements_on_post_terms_change'), 10, 6); add_action('wpo_cache_config_updated', array($this, 'cache_config_updated'), 10, 1); add_action('wp_insert_comment', array($this, 'comment_inserted'), 10, 2);
/** * List of hooks for which when executed, the cache will be purged * * @param array $actions The actions */ $purge_on_action = apply_filters('wpo_purge_cache_hooks', array('after_switch_theme', 'wp_update_nav_menu', 'customize_save_after', array('wp_ajax_save-widget', 0), array('wp_ajax_update-widget', 0), 'autoptimize_action_cachepurged', 'upgrader_overwrote_package', 'wpo_active_plugin_or_theme_updated')); foreach ($purge_on_action as $action) { if (is_array($action)) { add_action($action[0], array($this, 'purge_cache'), $action[1]); } else { add_action($action, array($this, 'purge_cache')); } } }
/** * Purge post cache when there is a new approved comment * * @param int $comment_id Comment ID. * @param int|string $approved Comment approved status. can be 0, 1 or 'spam'. * @param array $commentdata Comment data array. Always sent be WP core, but a plugin was found that does not send it - https://wordpress.org/support/topic/critical-problems-with-version-3-0-10/ */ public function purge_post_on_comment($comment_id, $approved, $commentdata = array()) { if (1 !== $approved) { return; }
if (!empty($this->config['enable_page_caching']) && !empty($commentdata['comment_post_ID'])) { $post_id = $commentdata['comment_post_ID'];
WPO_Page_Cache::delete_single_post_cache($post_id); } }
/** * Every time a comment's status changes, purge it's parent posts cache * * @param int $comment_id Comment ID. */ public function purge_post_on_comment_status_change($comment_id) { if (!empty($this->config['enable_page_caching'])) { $comment = get_comment($comment_id); if (is_object($comment) && !empty($comment->comment_post_ID)) WPO_Page_Cache::delete_single_post_cache($comment->comment_post_ID); } }
/** * Action when a comment is inserted * * @param integer $comment_id - The comment ID * @param boolean|WP_Comment $comment - The comment object (from WP 4.4) * @return void */ public function comment_inserted($comment_id, $comment = false) { if ($comment && is_a($comment, 'WP_Comment')) { /** * Filters whether to add a cookie when a comment is posted, in order to exclude the page from caching. * Regular comments have the property comment_type set to '' or 'comment'. So by default, only add the cookie in those cases. * * @param boolean $add_cookie * @param WP_Comment $comment * @return boolean */ $add_cookie = apply_filters('wpo_add_commented_post_cookie', '' == $comment->comment_type || 'comment' == $comment->comment_type, $comment); if (!$add_cookie) return;
$url = get_permalink($comment->comment_post_ID); $url_info = parse_url($url); setcookie('wpo_commented_post', 1, time() + WEEK_IN_SECONDS, isset($url_info['path']) ? $url_info['path'] : '/'); } }
/** * Automatically purge all file based page cache on post changes * We want the whole cache purged here as different parts * of the site could potentially change on post updates * * @param Integer $post_id - WP post id */ public function purge_post_on_update($post_id) { $post_type = get_post_type($post_id); $post_type_object = get_post_type_object($post_type);
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type || !$post_type_object->public) { return; }
/** * Purge the whole cache if set to true, only the edited post otherwise. Default is false. * * @param boolean $purge_all_cache The default filter value * @param integer $post_id The saved post ID */ if (apply_filters('wpo_purge_all_cache_on_update', false, $post_id)) { $this->purge_cache(); return; } else { if (apply_filters('wpo_delete_cached_homepage_on_post_update', true, $post_id)) WPO_Page_Cache::delete_homepage_cache(); WPO_Page_Cache::delete_single_post_cache($post_id); } }
/** * Purge archive pages on post update. * * @param integer $post_id */ public function purge_archive_pages_on_post_update($post_id) { $post_type = get_post_type($post_id);
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type) { return; }
$post_obj = get_post_type_object($post_type);
if ('post' == $post_type) { // delete all archive pages for post. $post_date = get_post_time('Y-m-j', false, $post_id); list($year, $month, $day) = $post_date;
$archive_links = array( get_year_link($year), get_month_link($year, $month), get_day_link($year, $month, $day), );
foreach ($archive_links as $link) { WPO_Page_Cache::delete_cache_by_url($link, true); } } elseif ($post_obj->has_archive) { // delete archive page for custom post type. WPO_Page_Cache::delete_cache_by_url(get_post_type_archive_link($post_type), true); }
}
/** * We use it with edit_terms action filter to purge cached elements related * to updated term when term updated. * * @param int $term_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ public function purge_related_elements_on_term_updated($term_id, $taxonomy) { // purge cached page for term. $term = get_term($term_id, $taxonomy, ARRAY_A); if (is_array($term)) { $term_permalink = get_term_link($term['term_id']); if (!is_wp_error($term_permalink)) { WPO_Page_Cache::delete_cache_by_url($term_permalink, true); } }
// get posts which belongs to updated term. $posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'any', 'fields' => 'ids', 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $term_id, ) ), ));
if (!empty($posts)) { foreach ($posts as $post_id) { WPO_Page_Cache::delete_single_post_cache($post_id); } } }
/** * Triggered by set_object_terms action. Used to clear all the terms archives a post belongs to or belonged to before being saved. * * @param int $object_id Object ID. * @param array $terms An array of object terms. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. * @param array $old_tt_ids Old array of term taxonomy IDs. */ public function purge_related_elements_on_post_terms_change($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
$post_type = get_post_type($object_id);
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type || 'product_type' === $taxonomy || 'action-group' === $taxonomy) { return; }
/** * Adds a way to exit the purge of terms permalink using the provided parameters. * * @param bool $purge The value filtered, whether or not to purge the related elements * @param int $object_id Object ID. * @param array $terms An array of object terms. * @param array $tt_ids An array of term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether to append new terms to the old terms. * @param array $old_tt_ids Old array of term taxonomy IDs. * @default true * @return boolean */ if (!apply_filters('wpo_cache_purge_related_elements_on_post_terms_change', true, $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids)) return;
// get all affected terms. $affected_terms_ids = array_unique(array_merge($tt_ids, $old_tt_ids));
if (!empty($affected_terms_ids)) { // walk through all changed terms and purge cached pages for them. foreach ($affected_terms_ids as $tt_id) { $term = get_term($tt_id, $taxonomy, ARRAY_A); if (!is_array($term)) continue;
$term_permalink = get_term_link($term['term_id']); if (!is_wp_error($term_permalink)) { $url = parse_url($term_permalink); // Check if the permalink contains a valid path, to avoid deleting the whole cache. if (!isset($url['path']) || '/' === $url['path']) return; WPO_Page_Cache::delete_cache_by_url($term_permalink, true); } } } }
/** * Clears the cache. */ public function purge_cache() { if (!empty($this->config['enable_page_caching'])) { wpo_cache_flush(); } }
/** * Triggered by wpo_cache_config_updated. * * @param array $config */ public function cache_config_updated($config) { // delete front page form cache if defined in the settings if (is_array($config['cache_exception_urls']) && in_array('/', $config['cache_exception_urls'])) { WPO_Page_Cache::delete_cache_by_url(home_url()); } }
/** * Returns an instance of the current class, creates one if it doesn't exist * * @return object */ public static function instance() { if (empty(self::$instance)) { self::$instance = new self(); } return self::$instance; } }
endif;
|