PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / trunk
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript vtrunk
4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 All 169 releases
searchpro / admin / admin-bar.php

admin-bar.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript trunk, at admin/admin-bar.php

317 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH'))
3 exit;
4
5 use BerqWP\BerqWP;
6
7 if (!class_exists('berqAdminBar')) {
8 class berqAdminBar
9 {
10 function __construct() {
11 // Add clear cache link to admin bar
12 add_action('admin_bar_menu', [$this, 'add_admin_bar_menu'], 999);
13
14 // Flush cache
15 add_action('admin_post_clear_cache', [$this, 'handle_clear_cache_action']);
16
17 // Warmup cache
18 add_action('admin_post_warmup_cache', [$this, 'handle_warmup_cache_action']);
19
20 // Purge a page cache
21 add_action('admin_post_berq_purge_page', [$this, 'handle_purge_page_action']);
22
23 // Request page cache
24 add_action('admin_post_berq_request_cache', [$this, 'handle_request_cache_action']);
25
26 // Flush CDN cache
27 add_action('admin_post_berq_flush_cdn', [$this, 'handle_flush_cdn_action']);
28
29 // Flus multisite site cache
30 add_action('admin_post_berq_flush_site', [$this, 'handle_berq_flush_site_action']);
31
32 // Flush critical CSS cache
33 add_action('admin_post_berq_flush_criticalcss', [$this, 'handle_flush_criticalcss_action']);
34 }
35
36 function add_admin_bar_menu()
37 {
38
39 if (!current_user_can('edit_posts')) {
40 return;
41 }
42
43 global $wp_admin_bar;
44 $plugin_name = defined('BERQWP_PLUGIN_NAME') ? BERQWP_PLUGIN_NAME : 'BerqWP';
45
46 $icon = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
47 <path fill-rule="evenodd" clip-rule="evenodd" d="M6.43896 0H17.561C21.1172 0 24 2.88287 24 6.43903V17.561C24 21.1171 21.1172 24 17.561 24H6.43896C2.88281 24 0 21.1171 0 17.561V6.43903C0 2.88287 2.88281 0 6.43896 0ZM15.7888 4.09753L8.59961 12.7534H12.3517L7.02441 20.4878L16.3903 11.0222L12.7814 10.3799L15.7888 4.09753Z" fill="#a7aaad"/>
48 </svg>';
49 $icon_base64 = base64_encode($icon);
50
51 $wp_admin_bar->add_menu(
52 array(
53 'id' => 'berqWP',
54 'title' => '<span class="ab-icon" style="background-image:url(data:image/svg+xml;base64,' . $icon_base64 . ')!important;height:25px;width:18px;background-size:contain;background-repeat:no-repeat;background-position:center;"></span>' . $plugin_name,
55 'href' => get_admin_url() . 'admin.php?page=berqwp',
56 )
57 );
58
59 // Add the sub-menu item
60 $wp_admin_bar->add_menu(
61 array(
62 'parent' => 'berqWP',
63 // ID of the parent menu item
64 'id' => 'flush-cache',
65 'title' => is_multisite() ? 'Flush all sites' : 'Flush cache',
66 'href' => wp_nonce_url(admin_url('admin-post.php?action=clear_cache'), 'clear_cache_action'),
67 'meta' => array(
68 'class' => 'clear-cache-link',
69 'title' => 'Clear BerqWP cache',
70 ),
71 )
72 );
73
74 if (berqwp_can_use_cloud()) {
75 $wp_admin_bar->add_menu(
76 array(
77 'parent' => 'berqWP',
78 'id' => 'warmup-cache',
79 'title' => 'Warmup cache',
80 'href' => wp_nonce_url(admin_url('admin-post.php?action=warmup_cache'), 'warmup_cache_action'),
81 'meta' => array(
82 'class' => 'warmup-cache-link',
83 'title' => 'Warmup BerqWP cache',
84 ),
85 )
86 );
87 }
88
89 if (is_multisite()) {
90 $wp_admin_bar->add_menu(array(
91 'id' => 'berqwp-network-sites',
92 'parent' => 'berqWP',
93 'title' => 'Flush site',
94 'href' => '#',
95 ));
96
97 $sites = get_sites();
98 foreach ($sites as $site) {
99 $wp_admin_bar->add_menu(array(
100 'id' => 'berqwp-site-flush-' . $site->blog_id,
101 'parent' => 'berqwp-network-sites',
102 'title' => 'Flush site ' . get_blog_option($site->blog_id, 'blogname'),
103 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_site&site_id=' . $site->blog_id), 'berq_flush_site_action'),
104 ));
105 }
106 }
107
108 if (berqwp_can_use_cloud()) {
109 $wp_admin_bar->add_menu(
110 array(
111 'parent' => 'berqWP',
112 // ID of the parent menu item
113 'id' => 'flush-cdn',
114 'title' => 'Flush CDN & page cache',
115 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_cdn'), 'berq_flush_cdn_action'),
116 'meta' => array(
117 'class' => 'flush-cdn-cache',
118 'title' => 'Flush CDN & page cache',
119 ),
120 )
121 );
122
123 $wp_admin_bar->add_menu(
124 array(
125 'parent' => 'berqWP',
126 // ID of the parent menu item
127 'id' => 'flush-criticalcss',
128 'title' => 'Flush critical CSS cache',
129 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_criticalcss'), 'berq_flush_criticalcss_action'),
130 'meta' => array(
131 'class' => 'flush-criticalcss',
132 'title' => 'Flush critical CSS cache',
133 ),
134 )
135 );
136 }
137
138 if (!is_admin()) {
139 $page_url = bwp_get_request_url();
140 // Add the sub-menu item
141 $wp_admin_bar->add_menu(
142 array(
143 'parent' => 'berqWP',
144 // ID of the parent menu item
145 'id' => 'purge-page',
146 'title' => 'Purge this page',
147 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_purge_page&uri=' . urlencode($page_url)), 'berq_purge_page_action'),
148 'meta' => array(
149 'class' => 'purge-page-link',
150 'title' => 'Clear this page cache',
151 ),
152 )
153 );
154
155 if (berqwp_can_use_cloud()) {
156 // Add the request cache
157 $wp_admin_bar->add_menu(
158 array(
159 'parent' => 'berqWP',
160 // ID of the parent menu item
161 'id' => 'request-page-cache',
162 'title' => 'Force cache',
163 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_request_cache&uri=' . urlencode($page_url)), 'berq_request_cache_action'),
164 'meta' => array(
165 'class' => 'request-page-cache-link',
166 'title' => 'Force cache for this page',
167 ),
168 )
169 );
170 }
171 }
172 }
173
174 function handle_clear_cache_action()
175 {
176 // Check if the user has the necessary nonce and the action matches
177 if (isset($_GET['action']) && $_GET['action'] === 'clear_cache' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'clear_cache_action')) {
178
179 $cache = berqCache::getInstance();
180 $cache->delete_cache_files(is_multisite()); // If is multisite flush all sites
181
182 if (function_exists('wp_cache_flush')) {
183 wp_cache_flush(); // Clear the entire object cache.
184 }
185
186 set_transient('berq_cache_cleared_notice', 'true', 60);
187
188 $redirect_url = add_query_arg('berq_clear_cache', '', wp_get_referer());
189
190 // Redirect back to the referring page after clearing the cache
191 wp_safe_redirect($redirect_url);
192 exit;
193 }
194 }
195
196 function handle_warmup_cache_action()
197 {
198 // Check if the user has the necessary nonce and the action matches
199 if (isset($_GET['action']) && $_GET['action'] === 'warmup_cache' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'warmup_cache_action')) {
200
201 // trigger cache warmup
202 do_action('berqwp_cache_warmup');
203
204 set_transient('berq_cache_warmup_notice', 'true', 60);
205
206 // Redirect back to the referring page after clearing the cache
207 wp_safe_redirect(wp_get_referer());
208 exit;
209 }
210 }
211
212 function handle_purge_page_action()
213 {
214 // Check if the user has the necessary nonce and the action matches
215 if (isset($_GET['action']) && $_GET['action'] === 'berq_purge_page' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'berq_purge_page_action')) {
216
217 $page_url = esc_url_raw(wp_unslash($_GET['uri'] ?? ''));
218
219 berqCache::purge_page($page_url, true);
220
221 if (is_admin()) {
222 set_transient('berq_purge_page_notice', $page_url, 60);
223 }
224
225 // Redirect back to the referring page after clearing the cache
226 wp_safe_redirect(wp_get_referer());
227 exit;
228 }
229 }
230
231 function handle_request_cache_action()
232 {
233 // Check if the user has the necessary nonce and the action matches
234 if (isset($_GET['action']) && $_GET['action'] === 'berq_request_cache' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'berq_request_cache_action')) {
235
236 $page_url = esc_url_raw(wp_unslash($_GET['uri'] ?? ''));
237 $slug = bwp_url_into_path($page_url);
238
239 if (is_admin()) {
240 set_transient('berq_force_cache_notice', $page_url, 60);
241 }
242
243 // as_enqueue_async_action('warmup_cache_quickly', [$page_url, true]);
244 warmup_cache_by_url($page_url, true);
245
246 // Redirect back to the referring page after clearing the cache
247 wp_safe_redirect(wp_get_referer());
248 exit;
249 }
250 }
251
252 function handle_flush_cdn_action()
253 {
254 // Check if the user has the necessary nonce and the action matches
255 if (isset($_GET['action']) && $_GET['action'] === 'berq_flush_cdn' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'berq_flush_cdn_action')) {
256
257 $cache = berqCache::getInstance();
258 $cache->flush_cdn();
259
260 set_transient('berq_purge_cdn_notice', 'true', 60);
261
262 // Redirect back to the referring page after clearing the cache
263 wp_safe_redirect(wp_get_referer());
264 exit;
265 }
266 }
267
268 function handle_berq_flush_site_action()
269 {
270 // Check if the user has the necessary nonce and the action matches
271 if (isset($_GET['action']) && isset($_GET['site_id']) && $_GET['action'] === 'berq_flush_site' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'berq_flush_site_action')) {
272
273 $site_id = (int) sanitize_text_field($_GET['site_id']);
274 $cache = berqCache::getInstance();
275
276 switch_to_blog($site_id);
277
278 // Flush cache
279 $cache->delete_cache_files();
280
281 restore_current_blog();
282
283 do_action('berqwp_purge_site');
284
285 set_transient('berq_purge_site_notice', 'true', 60);
286
287 // Redirect back to the referring page after clearing the cache
288 wp_safe_redirect(wp_get_referer());
289 exit;
290 }
291 }
292
293 function handle_flush_criticalcss_action()
294 {
295 // Check if the user has the necessary nonce and the action matches
296 if (isset($_GET['action']) && $_GET['action'] === 'berq_flush_criticalcss' && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'berq_flush_criticalcss_action')) {
297
298 $parsed_url = wp_parse_url(home_url());
299 $domain = $parsed_url['host'];
300
301 $berqwp = new BerqWP(berqwp_get_license_key(), null, null);
302 $berqwp->purge_critilclcss($domain);
303
304 do_action('berqwp_purge_criticalcss');
305
306 set_transient('berq_purge_criticalcss_notice', 'true', 60);
307
308 // Redirect back to the referring page after clearing the cache
309 wp_safe_redirect(wp_get_referer());
310 exit;
311 }
312 }
313 }
314
315 new berqAdminBar();
316 }
317