PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.21
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.21
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 4.0.21, at admin/admin-bar.php

320 lines 12.8 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 if (empty(berqwp_get_license_key())) {
39 return;
40 }
41
42 if (!current_user_can('edit_posts')) {
43 return;
44 }
45
46 global $wp_admin_bar;
47 $plugin_name = defined('BERQWP_PLUGIN_NAME') ? BERQWP_PLUGIN_NAME : 'BerqWP';
48
49 $icon = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
50 <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"/>
51 </svg>';
52 $icon_base64 = base64_encode($icon);
53
54 $wp_admin_bar->add_menu(
55 array(
56 'id' => 'berqWP',
57 '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,
58 'href' => get_admin_url() . 'admin.php?page=berqwp',
59 )
60 );
61
62 // Add the sub-menu item
63 $wp_admin_bar->add_menu(
64 array(
65 'parent' => 'berqWP',
66 // ID of the parent menu item
67 'id' => 'flush-cache',
68 'title' => is_multisite() ? 'Flush all sites' : 'Flush cache',
69 'href' => wp_nonce_url(admin_url('admin-post.php?action=clear_cache'), 'clear_cache_action'),
70 'meta' => array(
71 'class' => 'clear-cache-link',
72 'title' => 'Clear BerqWP cache',
73 ),
74 )
75 );
76
77 if (berqwp_can_use_cloud()) {
78 $wp_admin_bar->add_menu(
79 array(
80 'parent' => 'berqWP',
81 'id' => 'warmup-cache',
82 'title' => 'Warmup cache',
83 'href' => wp_nonce_url(admin_url('admin-post.php?action=warmup_cache'), 'warmup_cache_action'),
84 'meta' => array(
85 'class' => 'warmup-cache-link',
86 'title' => 'Warmup BerqWP cache',
87 ),
88 )
89 );
90 }
91
92 if (is_multisite()) {
93 $wp_admin_bar->add_menu(array(
94 'id' => 'berqwp-network-sites',
95 'parent' => 'berqWP',
96 'title' => 'Flush site',
97 'href' => '#',
98 ));
99
100 $sites = get_sites();
101 foreach ($sites as $site) {
102 $wp_admin_bar->add_menu(array(
103 'id' => 'berqwp-site-flush-' . $site->blog_id,
104 'parent' => 'berqwp-network-sites',
105 'title' => 'Flush site ' . get_blog_option($site->blog_id, 'blogname'),
106 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_site&site_id=' . $site->blog_id), 'berq_flush_site_action'),
107 ));
108 }
109 }
110
111 if (berqwp_can_use_cloud()) {
112 $wp_admin_bar->add_menu(
113 array(
114 'parent' => 'berqWP',
115 // ID of the parent menu item
116 'id' => 'flush-cdn',
117 'title' => 'Flush CDN & page cache',
118 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_cdn'), 'berq_flush_cdn_action'),
119 'meta' => array(
120 'class' => 'flush-cdn-cache',
121 'title' => 'Flush CDN & page cache',
122 ),
123 )
124 );
125
126 $wp_admin_bar->add_menu(
127 array(
128 'parent' => 'berqWP',
129 // ID of the parent menu item
130 'id' => 'flush-criticalcss',
131 'title' => 'Flush critical CSS cache',
132 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_flush_criticalcss'), 'berq_flush_criticalcss_action'),
133 'meta' => array(
134 'class' => 'flush-criticalcss',
135 'title' => 'Flush critical CSS cache',
136 ),
137 )
138 );
139 }
140
141 if (!is_admin()) {
142 $page_url = bwp_get_request_url();
143 // Add the sub-menu item
144 $wp_admin_bar->add_menu(
145 array(
146 'parent' => 'berqWP',
147 // ID of the parent menu item
148 'id' => 'purge-page',
149 'title' => 'Purge this page',
150 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_purge_page&uri=' . urlencode($page_url)), 'berq_purge_page_action'),
151 'meta' => array(
152 'class' => 'purge-page-link',
153 'title' => 'Clear this page cache',
154 ),
155 )
156 );
157
158 if (berqwp_can_use_cloud()) {
159 // Add the request cache
160 $wp_admin_bar->add_menu(
161 array(
162 'parent' => 'berqWP',
163 // ID of the parent menu item
164 'id' => 'request-page-cache',
165 'title' => 'Force cache',
166 'href' => wp_nonce_url(admin_url('admin-post.php?action=berq_request_cache&uri=' . urlencode($page_url)), 'berq_request_cache_action'),
167 'meta' => array(
168 'class' => 'request-page-cache-link',
169 'title' => 'Force cache for this page',
170 ),
171 )
172 );
173 }
174 }
175 }
176
177 function handle_clear_cache_action()
178 {
179 // Check if the user has the necessary nonce and the action matches
180 if (isset($_GET['action']) && $_GET['action'] === 'clear_cache' && wp_verify_nonce($_GET['_wpnonce'], 'clear_cache_action')) {
181
182 $cache = berqCache::getInstance();
183 $cache->delete_cache_files(is_multisite()); // If is multisite flush all sites
184
185 if (function_exists('wp_cache_flush')) {
186 wp_cache_flush(); // Clear the entire object cache.
187 }
188
189 set_transient('berq_cache_cleared_notice', 'true', 60);
190
191 $redirect_url = add_query_arg('berq_clear_cache', '', wp_get_referer());
192
193 // Redirect back to the referring page after clearing the cache
194 wp_safe_redirect($redirect_url);
195 exit;
196 }
197 }
198
199 function handle_warmup_cache_action()
200 {
201 // Check if the user has the necessary nonce and the action matches
202 if (isset($_GET['action']) && $_GET['action'] === 'warmup_cache' && wp_verify_nonce($_GET['_wpnonce'], 'warmup_cache_action')) {
203
204 // trigger cache warmup
205 do_action('berqwp_cache_warmup');
206
207 set_transient('berq_cache_warmup_notice', 'true', 60);
208
209 // Redirect back to the referring page after clearing the cache
210 wp_safe_redirect(wp_get_referer());
211 exit;
212 }
213 }
214
215 function handle_purge_page_action()
216 {
217 // Check if the user has the necessary nonce and the action matches
218 if (isset($_GET['action']) && $_GET['action'] === 'berq_purge_page' && wp_verify_nonce($_GET['_wpnonce'], 'berq_purge_page_action')) {
219
220 $page_url = $_GET['uri'];
221
222 berqCache::purge_page($page_url, true);
223
224 if (is_admin()) {
225 set_transient('berq_purge_page_notice', $page_url, 60);
226 }
227
228 // Redirect back to the referring page after clearing the cache
229 wp_safe_redirect(wp_get_referer());
230 exit;
231 }
232 }
233
234 function handle_request_cache_action()
235 {
236 // Check if the user has the necessary nonce and the action matches
237 if (isset($_GET['action']) && $_GET['action'] === 'berq_request_cache' && wp_verify_nonce($_GET['_wpnonce'], 'berq_request_cache_action')) {
238
239 $page_url = $_GET['uri'];
240 $slug = bwp_url_into_path($page_url);
241
242 if (is_admin()) {
243 set_transient('berq_force_cache_notice', $page_url, 60);
244 }
245
246 // as_enqueue_async_action('warmup_cache_quickly', [$page_url, true]);
247 warmup_cache_by_url($page_url, true);
248
249 // Redirect back to the referring page after clearing the cache
250 wp_safe_redirect(wp_get_referer());
251 exit;
252 }
253 }
254
255 function handle_flush_cdn_action()
256 {
257 // Check if the user has the necessary nonce and the action matches
258 if (isset($_GET['action']) && $_GET['action'] === 'berq_flush_cdn' && wp_verify_nonce($_GET['_wpnonce'], 'berq_flush_cdn_action')) {
259
260 $cache = berqCache::getInstance();
261 $cache->flush_cdn();
262
263 set_transient('berq_purge_cdn_notice', 'true', 60);
264
265 // Redirect back to the referring page after clearing the cache
266 wp_safe_redirect(wp_get_referer());
267 exit;
268 }
269 }
270
271 function handle_berq_flush_site_action()
272 {
273 // Check if the user has the necessary nonce and the action matches
274 if (isset($_GET['action']) && isset($_GET['site_id']) && $_GET['action'] === 'berq_flush_site' && wp_verify_nonce($_GET['_wpnonce'], 'berq_flush_site_action')) {
275
276 $site_id = (int) sanitize_text_field($_GET['site_id']);
277 $cache = berqCache::getInstance();
278
279 switch_to_blog($site_id);
280
281 // Flush cache
282 $cache->delete_cache_files();
283
284 restore_current_blog();
285
286 do_action('berqwp_purge_site');
287
288 set_transient('berq_purge_site_notice', 'true', 60);
289
290 // Redirect back to the referring page after clearing the cache
291 wp_safe_redirect(wp_get_referer());
292 exit;
293 }
294 }
295
296 function handle_flush_criticalcss_action()
297 {
298 // Check if the user has the necessary nonce and the action matches
299 if (isset($_GET['action']) && $_GET['action'] === 'berq_flush_criticalcss' && wp_verify_nonce($_GET['_wpnonce'], 'berq_flush_criticalcss_action')) {
300
301 $parsed_url = wp_parse_url(home_url());
302 $domain = $parsed_url['host'];
303
304 $berqwp = new BerqWP(berqwp_get_license_key(), null, null);
305 $berqwp->purge_critilclcss($domain);
306
307 do_action('berqwp_purge_criticalcss');
308
309 set_transient('berq_purge_criticalcss_notice', 'true', 60);
310
311 // Redirect back to the referring page after clearing the cache
312 wp_safe_redirect(wp_get_referer());
313 exit;
314 }
315 }
316 }
317
318 new berqAdminBar();
319 }
320