PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.3
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.3
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / cache / class-wpo-cache-rules.php

class-wpo-cache-rules.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at cache/class-wpo-cache-rules.php

365 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 /**
6 * Page caching rules and exceptions
7 */
8
9 if (!class_exists('WPO_Cache_Config')) require_once('class-wpo-cache-config.php');
10
11 require_once dirname(__FILE__) . '/file-based-page-cache-functions.php';
12
13 if (!class_exists('WPO_Cache_Rules')) :
14
15 class WPO_Cache_Rules {
16
17 /**
18 * Cache config object
19 *
20 * @var mixed
21 */
22 public $config;
23
24 /**
25 * Instance of this class
26 *
27 * @var mixed
28 */
29 public static $instance;
30
31 public function __construct() {
32 $this->config = WPO_Cache_Config::instance()->get();
33 $this->setup_hooks();
34 }
35
36 /**
37 * Setup hooks/filters
38 */
39 public function setup_hooks() {
40 add_action('save_post', array($this, 'purge_post_on_update'), 10, 1);
41 add_action('save_post', array($this, 'purge_archive_pages_on_post_update'), 10, 1);
42 add_action('wp_trash_post', array($this, 'purge_post_on_update'), 10, 1);
43 add_action('comment_post', array($this, 'purge_post_on_comment'), 10, 3);
44 add_action('wp_set_comment_status', array($this, 'purge_post_on_comment_status_change'), 10, 1);
45 add_action('edit_terms', array($this, 'purge_related_elements_on_term_updated'), 10, 2);
46 add_action('set_object_terms', array($this, 'purge_related_elements_on_post_terms_change'), 10, 6);
47 add_action('wpo_cache_config_updated', array($this, 'cache_config_updated'), 10, 1);
48 add_action('wp_insert_comment', array($this, 'comment_inserted'), 10, 2);
49
50 add_action('woocommerce_variation_set_stock', array($this, 'purge_product_page'), 10, 1);
51 add_action('woocommerce_product_set_stock', array($this, 'purge_product_page'), 10, 1);
52
53 /**
54 * List of hooks for which when executed, the cache will be purged
55 *
56 * @param array $actions The actions
57 */
58 $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', 'fusion_cache_reset_after'));
59 foreach ($purge_on_action as $action) {
60 if (is_array($action)) {
61 add_action($action[0], array($this, 'purge_cache'), $action[1]);
62 } else {
63 add_action($action, array($this, 'purge_cache'));
64 }
65 }
66
67 add_filter('wpo_cache_cookies', array($this, 'wpo_cache_cookies'), 9);
68 }
69
70 /**
71 * Purge post cache when there is a new approved comment
72 *
73 * @param int $comment_id Comment ID.
74 * @param int|string $approved Comment approved status. can be 0, 1 or 'spam'.
75 * @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/
76 */
77 public function purge_post_on_comment($comment_id, $approved, $commentdata = array()) {
78 if (1 !== $approved) {
79 return;
80 }
81
82 if (!empty($this->config['enable_page_caching']) && !empty($commentdata['comment_post_ID'])) {
83 $post_id = $commentdata['comment_post_ID'];
84
85 WPO_Page_Cache::delete_single_post_cache($post_id);
86 WPO_Page_Cache::delete_comments_feed();
87 }
88 }
89
90 /**
91 * Every time a comment's status changes, purge it's parent posts cache
92 *
93 * @param int $comment_id Comment ID.
94 */
95 public function purge_post_on_comment_status_change($comment_id) {
96 if (!empty($this->config['enable_page_caching'])) {
97 $comment = get_comment($comment_id);
98 if (is_object($comment) && !empty($comment->comment_post_ID)) {
99 WPO_Page_Cache::delete_single_post_cache($comment->comment_post_ID);
100 WPO_Page_Cache::delete_comments_feed();
101 }
102 }
103 }
104
105 /**
106 * Action when a comment is inserted
107 *
108 * @param integer $comment_id - The comment ID
109 * @param boolean|WP_Comment $comment - The comment object (from WP 4.4)
110 * @return void
111 */
112 public function comment_inserted($comment_id, $comment = false) {
113 if ($comment && is_a($comment, 'WP_Comment')) {
114 /**
115 * Filters whether to add a cookie when a comment is posted, in order to exclude the page from caching.
116 * Regular comments have the property comment_type set to '' or 'comment'. So by default, only add the cookie in those cases.
117 *
118 * @param boolean $add_cookie
119 * @param WP_Comment $comment
120 * @return boolean
121 */
122 $add_cookie = apply_filters('wpo_add_commented_post_cookie', '' == $comment->comment_type || 'comment' == $comment->comment_type, $comment);
123 if (!$add_cookie) return;
124
125 $url = get_permalink($comment->comment_post_ID);
126 $url_info = parse_url($url);
127 setcookie('wpo_commented_post', 1, time() + WEEK_IN_SECONDS, isset($url_info['path']) ? $url_info['path'] : '/');
128 }
129 }
130
131 /**
132 * Automatically purge all file based page cache on post changes
133 * We want the whole cache purged here as different parts
134 * of the site could potentially change on post updates
135 *
136 * @param Integer $post_id - WP post id
137 */
138 public function purge_post_on_update($post_id) {
139 $post_type = get_post_type($post_id);
140 $post_type_object = get_post_type_object($post_type);
141
142 if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type || !$post_type_object->public) {
143 return;
144 }
145
146 /**
147 * Purge the whole cache if set to true, only the edited post otherwise. Default is false.
148 *
149 * @param boolean $purge_all_cache The default filter value
150 * @param integer $post_id The saved post ID
151 */
152 if (apply_filters('wpo_purge_all_cache_on_update', false, $post_id)) {
153 $this->purge_cache();
154 return;
155 } else {
156 if (apply_filters('wpo_delete_cached_homepage_on_post_update', true, $post_id)) WPO_Page_Cache::delete_homepage_cache();
157 WPO_Page_Cache::delete_feed_cache();
158 WPO_Page_Cache::delete_single_post_cache($post_id);
159 WPO_Page_Cache::delete_sitemap_cache();
160 WPO_Page_Cache::delete_post_feed_cache($post_id);
161 }
162 }
163
164 /**
165 * Purge archive pages on post update.
166 *
167 * @param integer $post_id
168 */
169 public function purge_archive_pages_on_post_update($post_id) {
170 $post_type = get_post_type($post_id);
171
172 if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type) {
173 return;
174 }
175
176 $post_obj = get_post_type_object($post_type);
177
178 if ('post' == $post_type) {
179 // delete blog page cache
180 $blog_post_id = get_option('page_for_posts');
181 if ($blog_post_id) {
182 WPO_Page_Cache::delete_cache_by_url(get_permalink($blog_post_id), true);
183 }
184
185 // delete next and previus posts cache.
186 $globals_post = isset($GLOBALS['post']) ? $GLOBALS['post'] : false;
187 $GLOBALS['post'] = get_post($post_id);
188 $previous_post = function_exists('get_previous_post') ? get_previous_post() : false;
189 $next_post = function_exists('get_next_post') ? get_next_post() : false;
190 if ($globals_post) $GLOBALS['post'] = $globals_post;
191
192 if ($previous_post) WPO_Page_Cache::delete_cache_by_url(get_permalink($previous_post), true);
193 if ($next_post) WPO_Page_Cache::delete_cache_by_url(get_permalink($next_post), true);
194
195 // delete all archive pages for post.
196 $post_date = get_post_time('Y-m-j', false, $post_id);
197 list($year, $month, $day) = $post_date;
198
199 $archive_links = array(
200 get_year_link($year),
201 get_month_link($year, $month),
202 get_day_link($year, $month, $day),
203 );
204
205 foreach ($archive_links as $link) {
206 WPO_Page_Cache::delete_cache_by_url($link, true);
207 }
208 } elseif ($post_obj->has_archive) {
209 // delete archive page for custom post type.
210 WPO_Page_Cache::delete_cache_by_url(get_post_type_archive_link($post_type), true);
211 }
212
213 }
214
215 /**
216 * We use it with edit_terms action filter to purge cached elements related
217 * to updated term when term updated.
218 *
219 * @param int $term_id Term taxonomy ID.
220 * @param string $taxonomy Taxonomy slug.
221 */
222 public function purge_related_elements_on_term_updated($term_id, $taxonomy) {
223 // purge cached page for term.
224 $term = get_term($term_id, $taxonomy, ARRAY_A);
225 if (is_array($term)) {
226 $term_permalink = get_term_link($term['term_id']);
227 if (!is_wp_error($term_permalink)) {
228 WPO_Page_Cache::delete_cache_by_url($term_permalink, true);
229 }
230 }
231
232 // get posts which belongs to updated term.
233 $posts = get_posts(array(
234 'numberposts' => -1,
235 'post_type' => 'any',
236 'fields' => 'ids',
237 'tax_query' => array(
238 'relation' => 'OR',
239 array(
240 'taxonomy' => $taxonomy,
241 'field' => 'term_id',
242 'terms' => $term_id,
243 )
244 ),
245 ));
246
247 if (!empty($posts)) {
248 foreach ($posts as $post_id) {
249 WPO_Page_Cache::delete_single_post_cache($post_id);
250 }
251 }
252 }
253
254 /**
255 * Triggered by set_object_terms action. Used to clear all the terms archives a post belongs to or belonged to before being saved.
256 *
257 * @param int $object_id Object ID.
258 * @param array $terms An array of object terms.
259 * @param array $tt_ids An array of term taxonomy IDs.
260 * @param string $taxonomy Taxonomy slug.
261 * @param bool $append Whether to append new terms to the old terms.
262 * @param array $old_tt_ids Old array of term taxonomy IDs.
263 */
264 public function purge_related_elements_on_post_terms_change($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {
265
266 $post_type = get_post_type($object_id);
267
268 if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || 'revision' === $post_type || 'product_type' === $taxonomy || 'action-group' === $taxonomy) {
269 return;
270 }
271
272 /**
273 * Adds a way to exit the purge of terms permalink using the provided parameters.
274 *
275 * @param bool $purge The value filtered, whether or not to purge the related elements
276 * @param int $object_id Object ID.
277 * @param array $terms An array of object terms.
278 * @param array $tt_ids An array of term taxonomy IDs.
279 * @param string $taxonomy Taxonomy slug.
280 * @param bool $append Whether to append new terms to the old terms.
281 * @param array $old_tt_ids Old array of term taxonomy IDs.
282 * @default true
283 * @return boolean
284 */
285 if (!apply_filters('wpo_cache_purge_related_elements_on_post_terms_change', true, $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids)) return;
286
287 // get all affected terms.
288 $affected_terms_ids = array_unique(array_merge($tt_ids, $old_tt_ids));
289
290 if (!empty($affected_terms_ids)) {
291 // walk through all changed terms and purge cached pages for them.
292 foreach ($affected_terms_ids as $tt_id) {
293 $term = get_term($tt_id, $taxonomy, ARRAY_A);
294 if (!is_array($term)) continue;
295
296 $term_permalink = get_term_link($term['term_id']);
297 if (!is_wp_error($term_permalink)) {
298 $url = parse_url($term_permalink);
299 // Check if the permalink contains a valid path, to avoid deleting the whole cache.
300 if (!isset($url['path']) || '/' === $url['path']) return;
301 WPO_Page_Cache::delete_cache_by_url($term_permalink, true);
302 }
303 }
304 }
305 }
306
307 /**
308 * Purge product page upon stock update
309 */
310 public function purge_product_page($product_with_stock) {
311 if (!empty($product_with_stock->get_id())) {
312 WPO_Page_Cache::delete_single_post_cache($product_with_stock->get_id());
313 }
314 }
315
316 /**
317 * Clears the cache.
318 */
319 public function purge_cache() {
320 if (!empty($this->config['enable_page_caching'])) {
321 wpo_cache_flush();
322 }
323 }
324
325 /**
326 * Triggered by wpo_cache_config_updated.
327 *
328 * @param array $config
329 */
330 public function cache_config_updated($config) {
331 // delete front page form cache if defined in the settings
332 if (is_array($config['cache_exception_urls']) && in_array('/', $config['cache_exception_urls'])) {
333 WPO_Page_Cache::delete_cache_by_url(home_url());
334 }
335 }
336
337 /**
338 * Add cookie names that are need separate caching
339 */
340 public function wpo_cache_cookies($cookies) {
341 $cookies[] = 'cookie_notice_accepted';
342 $cookies[] = 'cookielawinfo-checkbox-necessary';
343 $cookies[] = 'cookielawinfo-checkbox-functional';
344 $cookies[] = 'cookielawinfo-checkbox-advertisement';
345 $cookies[] = 'cookielawinfo-checkbox-others';
346 $cookies[] = 'cookielawinfo-checkbox-analytics';
347 $cookies[] = 'cookielawinfo-checkbox-performance';
348 return $cookies;
349 }
350
351 /**
352 * Returns an instance of the current class, creates one if it doesn't exist
353 *
354 * @return object
355 */
356 public static function instance() {
357 if (empty(self::$instance)) {
358 self::$instance = new self();
359 }
360 return self::$instance;
361 }
362 }
363
364 endif;
365