PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.2.5
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.2.5
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / class-powered-cache-advanced-cache.php

class-powered-cache-advanced-cache.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.2.5, at includes/class-powered-cache-advanced-cache.php

161 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class Powered_Cache_Advanced_Cache {
8
9 /**
10 * Return an instance of the current class
11 *
12 * @since 1.0
13 * @return Powered_Cache_Advanced_Cache
14 */
15 public static function factory() {
16
17 static $instance;
18
19 if ( ! $instance ) {
20 $instance = new self();
21 $instance->setup();
22 }
23
24 return $instance;
25 }
26
27 /**
28 * Setup hooks
29 *
30 * @since 1.0
31 */
32 public function setup() {
33 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
34 add_action( 'admin_post_powered_cache_purge_page_cache', array( $this, 'purge_page_cache' ) );
35
36 add_action( 'pre_post_update', array( $this, 'purge_on_post_update' ), 10, 1 );
37 add_action( 'save_post', array( $this, 'purge_on_post_update' ), 10, 1 );
38 add_action( 'wp_trash_post', array( $this, 'purge_on_post_update' ), 10, 1 );
39 add_action( 'wp_set_comment_status', array( $this, 'purge_post_on_comment_status_change' ), 10, 2 );
40 add_action( 'set_comment_cookies', array( $this, 'set_comment_cookie' ), 10, 2 );
41 add_filter( 'powered_cache_post_related_urls', array( $this, 'powered_cache_post_related_urls' ) );
42 }
43
44 /**
45 * Add purge button on admin bar
46 * @since 1.0
47 * @param $wp_admin_bar
48 */
49 public function admin_bar_menu( $wp_admin_bar ) {
50 $wp_admin_bar->add_menu( array(
51 'id' => 'advanced-cache-purge',
52 'title' => __( 'Purge Page Cache', 'powered-cache' ),
53 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache' ), 'powered_cache_purge_page_cache' ),
54 'parent' => 'powered-cache',
55 ) );
56 }
57
58
59 /**
60 * Purge page cache directory
61 *
62 * @since 1.0
63 * @since 1.1 clean site dir instead of root page caching dir
64 */
65 public function purge_page_cache() {
66 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_page_cache' ) ) {
67 wp_nonce_ays( '' );
68 }
69
70 powered_cache_clean_site_cache_dir();
71 Powered_Cache_Admin_Helper::set_flash_message( __( 'Page cache deleted successfully!', 'powered-cache' ) );
72
73 wp_safe_redirect( wp_get_referer() );
74 die();
75 }
76
77
78 /**
79 * Purge post related page cache
80 *
81 * @since 1.0
82 *
83 * @param $post_id
84 */
85 public function purge_on_post_update( $post_id ) {
86 $current_post_status = get_post_status( $post_id );
87
88 $urls = array();
89
90 if ( in_array( $current_post_status, array( "publish", "trash" ) ) ) {
91 $urls = powered_cache_get_post_related_urls( $post_id );
92
93 $urls = apply_filters( 'powered_cache_advanced_cache_purge_urls', $urls, $post_id );
94
95 foreach ( $urls as $url ) {
96 powered_cache_delete_page_cache( $url );
97 }
98 }
99
100 do_action( 'powered_cache_advanced_cache_purge_post', $post_id, $urls );
101 }
102
103 /**
104 * Delete page cache when post update
105 *
106 * @since 1.0
107 *
108 * @param $comment_ID
109 * @param $comment_status
110 */
111 public function purge_post_on_comment_status_change( $comment_ID, $comment_status ) {
112 $comment = get_comment( $comment_ID );
113 $post_id = $comment->comment_post_ID;
114 powered_cache_delete_page_cache( get_permalink( $post_id ) );
115 }
116
117 /**
118 * leave a cookie when comment a post as usual and don't show cached page.
119 *
120 * @since 1.0
121 *
122 * @param $comment
123 * @param $user
124 */
125 public function set_comment_cookie( $comment, $user ) {
126 $post_id = $comment->comment_post_ID;
127 setcookie( 'powered_cache_commented_posts[' . $post_id . ']', parse_url( get_permalink( $post_id ), PHP_URL_PATH ), ( time() + HOUR_IN_SECONDS * 24 * 30 ) );
128 }
129
130
131 /**
132 * Adds custom pages to post related urls
133 *
134 * @since 1.1
135 *
136 * @param array $urls
137 *
138 * @return array urls
139 */
140 public function powered_cache_post_related_urls( $urls ) {
141 $additional_pages = powered_cache_get_option( 'purge_additional_pages' );
142
143 if ( empty( $additional_pages ) ) {
144 return $urls;
145 }
146
147 // we only need relative path
148 $additional_pages = str_replace( site_url(), '', $additional_pages );
149 $additional_page_paths = explode( PHP_EOL, $additional_pages );
150 $additional_urls = array();
151
152 foreach ( $additional_page_paths as $page ) {
153 $additional_urls[] = site_url( $page );
154 }
155
156 $urls = array_merge( $urls, $additional_urls );
157
158 return $urls;
159 }
160
161 }