PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.0.3
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.0.3
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 / admin / dashboard.php

dashboard.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 2.0.3, at includes/admin/dashboard.php

756 lines 28.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dashboard Page
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache\Admin\Dashboard;
9
10 use PoweredCache\Async\CachePreloader;
11 use PoweredCache\Async\DatabaseOptimizer;
12 use PoweredCache\Config;
13 use const PoweredCache\Constants\ICON_BASE64;
14 use const PoweredCache\Constants\MENU_SLUG;
15 use const PoweredCache\Constants\PURGE_CACHE_CRON_NAME;
16 use const PoweredCache\Constants\SETTING_OPTION;
17 use function PoweredCache\Utils\can_configure_htaccess;
18 use function PoweredCache\Utils\can_configure_object_cache;
19 use function PoweredCache\Utils\can_control_all_settings;
20 use function PoweredCache\Utils\cdn_zones;
21 use function PoweredCache\Utils\clean_site_cache_dir;
22 use function PoweredCache\Utils\get_available_object_caches;
23 use function PoweredCache\Utils\get_cache_dir;
24 use function PoweredCache\Utils\get_db_cleanup_counts;
25 use function PoweredCache\Utils\get_timeout_with_interval;
26 use function PoweredCache\Utils\is_premium;
27 use function PoweredCache\Utils\js_execution_methods;
28 use function PoweredCache\Utils\powered_cache_flush;
29 use function PoweredCache\Utils\remove_dir;
30
31 // phpcs:disable WordPress.WhiteSpace.PrecisionAlignment.Found
32 // phpcs:disable Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed
33 // phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment
34
35 /**
36 * Default setup routine
37 *
38 * @return void
39 */
40 function setup() {
41 if ( POWERED_CACHE_IS_NETWORK ) {
42 add_action( 'network_admin_menu', __NAMESPACE__ . '\\admin_menu' );
43 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_message' );
44 } else {
45 add_action( 'admin_menu', __NAMESPACE__ . '\\admin_menu' );
46 }
47
48 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_message' );
49
50 add_action( 'admin_init', __NAMESPACE__ . '\\process_form_submit' );
51 add_filter( 'admin_body_class', __NAMESPACE__ . '\\add_sui_admin_body_class' );
52 add_action( 'admin_bar_menu', __NAMESPACE__ . '\\admin_bar_menu', 999 );
53 add_action( 'admin_bar_menu', __NAMESPACE__ . '\\purge_all_admin_bar_menu' );
54 add_action( 'admin_post_powered_cache_purge_all_cache', __NAMESPACE__ . '\\purge_all_cache' );
55 add_action( 'admin_post_powered_cache_download_rewrite_settings', __NAMESPACE__ . '\\download_rewrite_config' );
56 add_action( 'wp_ajax_powered_cache_run_diagnostic', __NAMESPACE__ . '\\run_diagnostic' );
57 add_action( 'admin_post_deactivate_plugin', __NAMESPACE__ . '\\deactivate_plugin' );
58 add_filter( 'plugin_action_links_' . plugin_basename( POWERED_CACHE_PLUGIN_FILE ), __NAMESPACE__ . '\\action_links' );
59 add_filter( 'network_admin_plugin_action_links_' . plugin_basename( POWERED_CACHE_PLUGIN_FILE ), __NAMESPACE__ . '\\action_links' );
60 }
61
62 /**
63 * Add required class for shared UI
64 *
65 * @param string $classes css classes for admin area
66 *
67 * @return string
68 * @see https://wpmudev.github.io/shared-ui/installation/
69 */
70 function add_sui_admin_body_class( $classes ) {
71 $classes .= ' sui-2-11-1 ';
72
73 return $classes;
74 }
75
76 /**
77 * Adds admin menu item
78 *
79 * @since 1.0
80 */
81 function admin_menu() {
82 global $powered_cache_settings_page;
83
84 $capability = 'manage_options';
85
86 if ( POWERED_CACHE_IS_NETWORK ) {
87 $capability = 'manage_network';
88 }
89
90 $powered_cache_settings_page = add_menu_page(
91 esc_html__( 'Powered Cache Settings', 'powered-cache' ),
92 esc_html__( 'Powered Cache', 'powered-cache' ),
93 $capability,
94 MENU_SLUG,
95 __NAMESPACE__ . '\settings_page',
96 ICON_BASE64
97 );
98
99 /**
100 * Different name submenu item, url point same address with parent.
101 */
102 add_submenu_page(
103 MENU_SLUG,
104 esc_html__( 'Powered Cache Settings', 'powered-cache' ),
105 esc_html__( 'Settings', 'powered-cache' ),
106 $capability,
107 MENU_SLUG
108 );
109 }
110
111 /**
112 * Main settings page of the plugin
113 */
114 function settings_page() {
115 include __DIR__ . '/partials/settings-page.php';
116 }
117
118 /**
119 * Process settings form action
120 *
121 * @since 2.0
122 */
123 function process_form_submit() {
124
125 if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) {
126 return;
127 }
128
129 if ( ! current_user_can( 'manage_options' ) ) {
130 return;
131 }
132
133 $nonce = filter_input( INPUT_POST, 'powered_cache_settings_nonce', FILTER_SANITIZE_STRING );
134 if ( wp_verify_nonce( $nonce, 'powered_cache_update_settings' ) ) {
135 $action = $_POST['powered_cache_form_action'] ? $_POST['powered_cache_form_action'] : 'save_settings';
136 $old_options = \PoweredCache\Utils\get_settings();
137 $options = sanitize_options( $_POST );
138
139 switch ( $action ) {
140 case 'reset_settings':
141 if ( POWERED_CACHE_IS_NETWORK ) {
142 delete_site_option( SETTING_OPTION );
143 } else {
144 delete_option( SETTING_OPTION );
145 }
146
147 if ( 'off' !== $old_options['object_cache'] ) {
148 wp_cache_flush();
149 }
150
151 $options = \PoweredCache\Utils\get_settings();
152
153 break;
154 case 'export_settings':
155 $filename = sprintf( 'powered-cache-settings-%s-%s.json', gmdate( 'Y-m-d' ), uniqid() );
156 if ( POWERED_CACHE_IS_NETWORK ) {
157 $options = wp_json_encode( get_site_option( SETTING_OPTION ), JSON_PRETTY_PRINT );
158 } else {
159 $options = wp_json_encode( get_option( SETTING_OPTION ), JSON_PRETTY_PRINT );
160 }
161
162 nocache_headers();
163 // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
164 @header( 'Content-Type: application/json' );
165 @header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
166 @header( 'Content-Transfer-Encoding: binary' );
167 @header( 'Content-Length: ' . strlen( $options ) );
168 @header( 'Connection: close' );
169 // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
170 echo $options; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
171 exit;
172 case 'import_settings':
173 if ( $_FILES['import_file'] && ! empty( $_FILES['import_file']['tmp_name'] ) ) {
174 $import_data = file_get_contents( $_FILES['import_file']['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
175 $import_settings = json_decode( $import_data, true );
176 $options = sanitize_options( $import_settings );
177 }
178 break;
179 case 'save_settings_and_optimize':
180 db_optimize( $options );
181 break;
182 case 'save_settings':
183 default:
184 break;
185 }
186
187 if ( POWERED_CACHE_IS_NETWORK ) {
188 update_site_option( SETTING_OPTION, $options );
189 } else {
190 update_option( SETTING_OPTION, $options );
191 }
192
193 Config::factory()->save_configuration( $options, POWERED_CACHE_IS_NETWORK );
194
195 // drop object cache on backend changes
196 if ( isset( $options['object_cache'] ) && $old_options['object_cache'] !== $options['object_cache'] ) {
197 wp_cache_flush();
198 }
199
200 // maybe cancel preloading process when it turned off
201 if ( $old_options['enable_cache_preload'] && ! $options['enable_cache_preload'] ) {
202 cancel_preloading();
203 }
204
205 // cleanup existing cache on toggling cache option
206 if ( $old_options['enable_page_cache'] && ! $options['enable_page_cache'] ) {
207 clean_site_cache_dir();
208 }
209
210 if ( $old_options['cache_timeout'] !== $options['cache_timeout'] ) {
211 $timestamp = wp_next_scheduled( PURGE_CACHE_CRON_NAME );
212
213 wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME );
214 }
215
216 /**
217 * Fires after saving configurations.
218 *
219 * @hook powered_cache_settings_saved
220 *
221 * @param {array} $old_options Old settings.
222 * @param {array} $options New settings.
223 *
224 * @since 1.0
225 */
226 do_action( 'powered_cache_settings_saved', $old_options, $options );
227
228 $redirect_url = wp_get_referer();
229
230 if ( empty( $redirect_url ) ) {
231 $redirect_url = $_SERVER['REQUEST_URI'];
232 }
233
234 $redirect_url = add_query_arg(
235 [
236 'pc_action' => $action,
237 ],
238 $redirect_url
239 );
240
241 wp_safe_redirect( $redirect_url );
242 exit;
243 }
244 }
245
246 /**
247 * Sanitize options
248 *
249 * @param array $options Raw input, most likely $_POST request
250 *
251 * @return array|mixed Sanitized options
252 */
253 function sanitize_options( $options ) {
254 $sanitized_options = [];
255
256 if ( isset( $options['object_cache'] ) && in_array( $options['object_cache'], get_available_object_caches(), true ) ) {
257 $sanitized_options['object_cache'] = sanitize_text_field( $options['object_cache'] );
258 } else {
259 $sanitized_options['object_cache'] = 'off';
260 }
261
262 $sanitized_options['enable_page_cache'] = ! empty( $options['enable_page_cache'] );
263 $sanitized_options['cache_mobile'] = ! empty( $options['cache_mobile'] );
264 $sanitized_options['cache_mobile_separate_file'] = ! empty( $options['cache_mobile_separate_file'] );
265 $sanitized_options['loggedin_user_cache'] = ! empty( $options['loggedin_user_cache'] );
266 $sanitized_options['gzip_compression'] = ! empty( $options['gzip_compression'] );
267 $sanitized_options['cache_timeout'] = absint( $options['cache_timeout'] );
268 $sanitized_options['auto_configure_htaccess'] = ! empty( $options['auto_configure_htaccess'] );
269 $sanitized_options['rejected_user_agents'] = sanitize_textarea_field( $options['rejected_user_agents'] );
270 $sanitized_options['rejected_cookies'] = sanitize_textarea_field( $options['rejected_cookies'] );
271 $sanitized_options['vary_cookies'] = sanitize_textarea_field( $options['vary_cookies'] );
272 $sanitized_options['rejected_uri'] = sanitize_textarea_field( $options['rejected_uri'] );
273 $sanitized_options['accepted_query_strings'] = sanitize_textarea_field( $options['accepted_query_strings'] );
274 $sanitized_options['purge_additional_pages'] = sanitize_textarea_field( $options['purge_additional_pages'] );
275 $sanitized_options['purge_additional_pages'] = sanitize_textarea_field( $options['purge_additional_pages'] );
276 $sanitized_options['minify_html'] = ! empty( $options['minify_html'] );
277 $sanitized_options['combine_google_fonts'] = ! empty( $options['combine_google_fonts'] );
278 $sanitized_options['minify_css'] = ! empty( $options['minify_css'] );
279 $sanitized_options['combine_css'] = ! empty( $options['combine_css'] );
280 $sanitized_options['excluded_css_files'] = sanitize_textarea_field( $options['excluded_css_files'] );
281 $sanitized_options['minify_js'] = ! empty( $options['minify_js'] );
282 $sanitized_options['combine_js'] = ! empty( $options['combine_js'] );
283 $sanitized_options['excluded_js_files'] = sanitize_textarea_field( $options['excluded_js_files'] );
284 $sanitized_options['excluded_js_files'] = sanitize_textarea_field( $options['excluded_js_files'] );
285 $sanitized_options['js_execution_method'] = sanitize_text_field( $options['js_execution_method'] );
286 $sanitized_options['js_execution_optimized_only'] = ! empty( $options['js_execution_optimized_only'] );
287 $sanitized_options['enable_lazy_load'] = ! empty( $options['enable_lazy_load'] );
288 $sanitized_options['lazy_load_post_content'] = ! empty( $options['lazy_load_post_content'] );
289 $sanitized_options['lazy_load_images'] = ! empty( $options['lazy_load_images'] );
290 $sanitized_options['lazy_load_iframes'] = ! empty( $options['lazy_load_iframes'] );
291 $sanitized_options['lazy_load_widgets'] = ! empty( $options['lazy_load_widgets'] );
292 $sanitized_options['lazy_load_post_thumbnail'] = ! empty( $options['lazy_load_post_thumbnail'] );
293 $sanitized_options['lazy_load_avatars'] = ! empty( $options['lazy_load_avatars'] );
294 $sanitized_options['disable_wp_lazy_load'] = ! empty( $options['disable_wp_lazy_load'] );
295 $sanitized_options['disable_wp_embeds'] = ! empty( $options['disable_wp_embeds'] );
296 $sanitized_options['disable_emoji_scripts'] = ! empty( $options['disable_emoji_scripts'] );
297 $sanitized_options['enable_cdn'] = ! empty( $options['enable_cdn'] );
298
299 // convert TTL in minute
300 if ( $options['cache_timeout'] > 0 && isset( $options['cache_timeout_interval'] ) ) {
301 switch ( $options['cache_timeout_interval'] ) {
302 case 'DAY':
303 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1440;
304 break;
305 case 'HOUR':
306 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 60;
307 break;
308 case 'MINUTE':
309 default:
310 $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1;
311 }
312 }
313
314 $cdn_hostname = [];
315 if ( isset( $options['cdn_hostname'] ) ) {
316 foreach ( (array) $options['cdn_hostname'] as $hostname ) {
317 $cdn_hostname[] = esc_url_raw( $hostname );
318 }
319 }
320
321 $cdn_zone = [];
322
323 if ( isset( $options['cdn_zone'] ) ) {
324 foreach ( (array) $options['cdn_zone'] as $zone ) {
325 $cdn_zone[] = sanitize_text_field( $zone );
326 }
327 }
328
329 $sanitized_options['cdn_hostname'] = $cdn_hostname;
330 $sanitized_options['cdn_zone'] = $cdn_zone;
331 $sanitized_options['cdn_rejected_files'] = sanitize_textarea_field( $options['cdn_rejected_files'] );
332 $sanitized_options['enable_cache_preload'] = ! empty( $options['enable_cache_preload'] );
333 $sanitized_options['preload_homepage'] = ! empty( $options['preload_homepage'] );
334 $sanitized_options['preload_public_posts'] = ! empty( $options['preload_public_posts'] );
335 $sanitized_options['preload_public_tax'] = ! empty( $options['preload_public_tax'] );
336 $sanitized_options['enable_sitemap_preload'] = ! empty( $options['enable_sitemap_preload'] );
337 $sanitized_options['preload_sitemap'] = sanitize_textarea_field( $options['preload_sitemap'] );
338 $sanitized_options['prefetch_dns'] = sanitize_textarea_field( $options['prefetch_dns'] );
339 $sanitized_options['db_cleanup_post_revisions'] = ! empty( $options['db_cleanup_post_revisions'] );
340 $sanitized_options['db_cleanup_auto_drafts'] = ! empty( $options['db_cleanup_auto_drafts'] );
341 $sanitized_options['db_cleanup_trashed_posts'] = ! empty( $options['db_cleanup_trashed_posts'] );
342 $sanitized_options['db_cleanup_spam_comments'] = ! empty( $options['db_cleanup_spam_comments'] );
343 $sanitized_options['db_cleanup_trashed_comments'] = ! empty( $options['db_cleanup_trashed_comments'] );
344 $sanitized_options['db_cleanup_expired_transients'] = ! empty( $options['db_cleanup_expired_transients'] );
345 $sanitized_options['db_cleanup_all_transients'] = ! empty( $options['db_cleanup_all_transients'] );
346 $sanitized_options['db_cleanup_optimize_tables'] = ! empty( $options['db_cleanup_optimize_tables'] );
347 $sanitized_options['enable_scheduled_db_cleanup'] = ! empty( $options['enable_scheduled_db_cleanup'] );
348 $sanitized_options['scheduled_db_cleanup_frequency'] = sanitize_text_field( $options['scheduled_db_cleanup_frequency'] );
349 $sanitized_options['enable_cloudflare'] = ! empty( $options['enable_cloudflare'] );
350 $sanitized_options['cloudflare_email'] = sanitize_email( $options['cloudflare_email'] );
351 $sanitized_options['cloudflare_api_key'] = sanitize_text_field( $options['cloudflare_api_key'] );
352 $sanitized_options['cloudflare_zone'] = sanitize_text_field( $options['cloudflare_zone'] );
353 $sanitized_options['enable_heartbeat'] = ! empty( $options['enable_heartbeat'] );
354 $sanitized_options['heartbeat_dashboard_status'] = sanitize_text_field( $options['heartbeat_dashboard_status'] );
355 $sanitized_options['heartbeat_editor_status'] = sanitize_text_field( $options['heartbeat_editor_status'] );
356 $sanitized_options['heartbeat_frontend_status'] = sanitize_text_field( $options['heartbeat_frontend_status'] );
357 $sanitized_options['heartbeat_dashboard_interval'] = absint( $options['heartbeat_dashboard_interval'] );
358 $sanitized_options['heartbeat_editor_interval'] = absint( $options['heartbeat_editor_interval'] );
359 $sanitized_options['heartbeat_frontend_interval'] = absint( $options['heartbeat_frontend_interval'] );
360 $sanitized_options['enable_varnish'] = ! empty( $options['enable_varnish'] );
361 $sanitized_options['varnish_ip'] = sanitize_text_field( $options['varnish_ip'] );
362 $sanitized_options['cache_footprint'] = ! empty( $options['cache_footprint'] );
363 $sanitized_options['enable_google_tracking'] = ! empty( $options['enable_google_tracking'] );
364 $sanitized_options['enable_fb_tracking'] = ! empty( $options['enable_fb_tracking'] );
365
366 /**
367 * Filters sanitized options.
368 *
369 * @hook powered_cache_sanitized_options
370 *
371 * @param {array} $sanitized_options Sanitized options.
372 * @param {array} $options raw input.
373 *
374 * @return {array} New value.
375 *
376 * @since 2.0
377 */
378 return apply_filters( 'powered_cache_sanitized_options', $sanitized_options, $options );
379 }
380
381 /**
382 * Add base admin bar menu
383 *
384 * @param object $wp_admin_bar Admin bar object
385 *
386 * @since 2.0
387 */
388 function admin_bar_menu( $wp_admin_bar ) {
389 $href = admin_url( 'admin.php?page=powered-cache' );
390
391 if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) {
392 $href = network_admin_url( 'admin.php?page=powered-cache' );
393 }
394
395 if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) {
396 $href = '#';
397 }
398
399 if ( current_user_can( 'manage_options' ) ) {
400 $wp_admin_bar->add_menu(
401 array(
402 'id' => MENU_SLUG,
403 'title' => __( 'Powered Cache', 'powered-cache' ),
404 'href' => $href,
405 )
406 );
407 }
408 }
409
410 /**
411 * Maybe display feedback messages when certain action is taken
412 *
413 * @since 2.0
414 */
415 function maybe_display_message() {
416 // phpcs:disable WordPress.Security.NonceVerification.Recommended
417 if ( ! isset( $_GET['pc_action'] ) ) {
418 return;
419 }
420
421 /**
422 * Dont display multiple message when saving the options while having the query_params
423 */
424 if ( ! empty( $_POST ) ) {
425 return;
426 }
427
428 $screen = get_current_screen();
429
430 $success_messages = [
431 'flush_page_cache_network' => esc_html__( 'Page cache deleted for all websites!', 'powered-cache' ),
432 'flush_page_cache' => esc_html__( 'Page cache deleted successfully!', 'powered-cache' ),
433 'flush_object_cache' => esc_html__( 'Object cache deleted successfully!', 'powered-cache' ),
434 'flush_all_cache' => esc_html__( 'All cached items flushed successfully!', 'powered-cache' ),
435 'start_preload' => esc_html__( 'The cache preloading has been initialized!', 'powered-cache' ),
436 'flush_cf_cache' => esc_html__( 'Cloudflare cache flushed, it can take up to 30 seconds to delete all cache from Cloudflare!', 'powered-cache' ),
437 'reset_settings' => esc_html__( 'Settings have been reset!', 'powered-cache' ),
438 'import_settings' => esc_html__( 'Settings have been imported!', 'powered-cache' ),
439 'save_settings_and_optimize' => esc_html__( 'Settings saved and database being optimized...', 'powered-cache' ),
440 'save_settings' => esc_html__( 'Settings saved.', 'powered-cache' ),
441 ];
442
443 $err_messages = [
444 'flush_page_cache_network_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
445 'flush_page_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
446 'flush_object_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
447 'flush_all_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
448 'start_preload_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ),
449 'flush_cf_cache_failed' => esc_html__( 'Could not flush Cloudflare cache. Please make sure you entered the correct credentials and zone id!', 'powered-cache' ),
450 ];
451
452 if ( isset( $success_messages[ $_GET['pc_action'] ] ) ) {
453 if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
454 add_settings_error( $screen->parent_file, MENU_SLUG, $success_messages[ $_GET['pc_action'] ], 'success' );
455
456 return;
457 }
458
459 printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', $success_messages[ $_GET['pc_action'] ] ); // phpcs:ignore
460 }
461
462 if ( isset( $err_messages[ $_GET['pc_action'] ] ) ) {
463 if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page
464 add_settings_error( $screen->parent_file, MENU_SLUG, $err_messages[ $_GET['pc_action'] ], 'error' );
465
466 return;
467 }
468
469 printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', $err_messages[ $_GET['pc_action'] ] ); // phpcs:ignore
470 }
471 // phpcs:enable WordPress.Security.NonceVerification.Recommended
472 }
473
474
475 /**
476 * Adds `Purge All Cache` menu bar item
477 *
478 * @param object $wp_admin_bar Admin bar object
479 *
480 * @since 1.1
481 */
482 function purge_all_admin_bar_menu( $wp_admin_bar ) {
483 // Only available for the network admins on multisite.
484 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
485 return;
486 }
487
488 $wp_admin_bar->add_menu(
489 array(
490 'id' => 'all-cache-purge',
491 'title' => __( 'Purge All Cache', 'powered-cache' ),
492 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ),
493 'parent' => 'powered-cache',
494 )
495 );
496 }
497
498 /**
499 * Purges all cache related things
500 *
501 * @since 1.1
502 */
503 function purge_all_cache() {
504
505 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_all_cache' ) ) {
506 wp_nonce_ays( '' );
507 }
508
509 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
510 $redirect_url = add_query_arg( 'pc_action', 'flush_all_cache_err_permission', wp_get_referer() );
511
512 wp_safe_redirect( $redirect_url );
513 exit;
514 }
515
516 powered_cache_flush();// cleans object cache + page cache dir
517
518 /**
519 * Fires after purging all cache
520 *
521 * @hook powered_cache_purge_all_cache
522 *
523 * @since 1.1
524 */
525 do_action( 'powered_cache_purge_all_cache' );
526
527 $redirect_url = add_query_arg( 'pc_action', 'flush_all_cache', wp_get_referer() );
528
529 wp_safe_redirect( $redirect_url );
530 exit;
531 }
532
533
534 /**
535 * Downloads proper configuration file
536 *
537 * @since 1.1
538 */
539 function download_rewrite_config() {
540 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_download_rewrite' ) ) {
541 wp_nonce_ays( '' );
542 }
543
544 if ( ! can_control_all_settings() ) {
545 wp_safe_redirect( wp_get_referer() );
546 exit;
547 }
548
549 $server = $_GET['server'];
550 Config::factory()->download_rewrite_rules( $server );
551
552 wp_safe_redirect( wp_get_referer() );
553 die();
554 }
555
556 /**
557 * Run DB optimization
558 *
559 * @param array $options plugin settings
560 */
561 function db_optimize( $options ) {
562 $powered_cache_db_optimizer = DatabaseOptimizer::factory();
563
564 $supported_options = $powered_cache_db_optimizer->get_supported_options();
565
566 if ( POWERED_CACHE_IS_NETWORK ) {
567 $sites = get_sites();
568 foreach ( $sites as $site ) {
569 switch_to_blog( $site->blog_id );
570 foreach ( $supported_options as $optimization_item ) {
571 if ( $options[ $optimization_item ] ) {
572 $powered_cache_db_optimizer->push_to_queue( $optimization_item );
573 }
574 }
575
576 $powered_cache_db_optimizer->save()->dispatch();
577 restore_current_blog();
578 }
579 } else {
580 foreach ( $supported_options as $optimization_item ) {
581 if ( $options[ $optimization_item ] ) {
582 $powered_cache_db_optimizer->push_to_queue( $optimization_item );
583 }
584 }
585
586 $powered_cache_db_optimizer->save()->dispatch();
587 }
588
589 }
590
591 /**
592 * Cancel preloading process on toggling preload option
593 */
594 function cancel_preloading() {
595 \PoweredCache\Utils\log( 'Cancel preload process' );
596 $cache_preloader = CachePreloader::factory();
597 $cache_preloader->cancel_process();
598 }
599
600
601 /**
602 * Perform diagnostic checks
603 */
604 function run_diagnostic() {
605 global $is_apache;
606
607 $nonce = $_POST['nonce'];
608
609 if ( wp_verify_nonce( $nonce, 'powered_cache_run_diagnostic' ) ) {
610 $settings = \PoweredCache\Utils\get_settings();
611 $checks = array();
612
613 // check config file
614 $config_file = Config::factory()->find_wp_config_file();
615 $config_file_status = is_writeable( $config_file );
616
617 if ( $config_file_status ) {
618 $config_file_desc = esc_html__( 'wp-config.php is writable.', 'powered-cache' );
619 } else {
620 $config_file_desc = sprintf( __( 'wp-config.php is not writable. Please make sure the file writable or you can manually define %s constant.', 'powered-cache' ), '<code>WP_CACHE</code>' );
621 }
622
623 $checks[] = array(
624 'check' => 'config',
625 'status' => $config_file_status,
626 'description' => $config_file_desc,
627 );
628
629 // check cache directory
630 $cache_dir = get_cache_dir();
631 $cache_dir_status = false;
632 if ( ! file_exists( $cache_dir ) ) {
633 $cache_dir_desc = sprintf( __( 'Cache directory %s is not exist!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
634 } elseif ( ! is_writeable( $cache_dir ) ) {
635 $cache_dir_desc = sprintf( __( 'Cache directory %s is not writeable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
636 } else {
637 $cache_dir_status = true;
638 $cache_dir_desc = sprintf( __( 'Cache directory %s exist and writable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' );
639 }
640
641 $checks[] = array(
642 'check' => 'cache-dir',
643 'status' => $cache_dir_status,
644 'description' => $cache_dir_desc,
645 );
646
647 // check .htaccess file
648 if ( $is_apache && $settings['auto_configure_htaccess'] ) {
649 $htaccess_file = get_home_path() . '.htaccess';
650 $htaccess_file_status = false;
651 if ( ! file_exists( $htaccess_file ) ) {
652 $htaccess_file_desc = sprintf( __( '.htaccess file %s is not exist!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
653 } elseif ( ! is_writeable( $htaccess_file ) ) {
654 $htaccess_file_desc = sprintf( __( '.htaccess file %s is not writeable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
655 } else {
656 $htaccess_file_status = true;
657 $htaccess_file_desc = sprintf( __( '.htaccess file %s exist and writable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' );
658 }
659
660 $checks[] = array(
661 'check' => 'htaccess',
662 'status' => $htaccess_file_status,
663 'description' => $htaccess_file_desc,
664 );
665 }
666
667 // check page cache
668 if ( $settings['enable_page_cache'] ) {
669 $advanced_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
670 $advanced_cache_file_status = false;
671 if ( ! file_exists( $advanced_cache_file ) ) {
672 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not exist!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
673 } elseif ( ! is_writeable( $advanced_cache_file ) ) {
674 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not writeable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
675 } else {
676 $advanced_cache_file_status = true;
677 $advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s exist and writable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' );
678 }
679
680 $checks[] = array(
681 'check' => 'advanced-cache',
682 'status' => $advanced_cache_file_status,
683 'description' => $advanced_cache_file_desc,
684 );
685 }
686
687 // check object cache
688 if ( 'off' !== $settings['object_cache'] ) {
689 $object_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
690 $object_cache_file_status = false;
691 if ( ! file_exists( $object_cache_file ) ) {
692 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not exist!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
693 } elseif ( ! is_writeable( $object_cache_file ) ) {
694 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not writeable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
695 } else {
696 $object_cache_file_status = true;
697 $object_cache_file_desc = sprintf( __( 'Required file for the object caching %s exist and writable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' );
698 }
699
700 $checks[] = array(
701 'check' => 'object-cache',
702 'status' => $object_cache_file_status,
703 'description' => $object_cache_file_desc,
704 );
705 }
706
707 wp_send_json_success( $checks );
708 }
709
710 wp_send_json_error( [ esc_html__( 'Invalid request', 'powered-cache' ) ] );
711 }
712
713
714 /**
715 * Deactivate incompatible plugins
716 *
717 * @since 1.0
718 */
719 function deactivate_plugin() {
720 if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'deactivate_plugin' ) ) {
721 wp_nonce_ays( '' );
722 }
723
724 if ( ! current_user_can( 'activate_plugins' ) ) {
725 wp_safe_redirect( wp_get_referer() );
726 exit;
727 }
728
729 deactivate_plugins( $_GET['plugin'] );
730
731 wp_safe_redirect( wp_get_referer() );
732 die();
733 }
734
735 /**
736 * Adds settings link to plugin actions
737 *
738 * @param array $actions Plugin actions.
739 *
740 * @return array
741 * @since 1.0
742 */
743 function action_links( $actions ) {
744
745 $settings_url = POWERED_CACHE_IS_NETWORK ? network_admin_url( 'admin.php?page=powered-cache' ) : admin_url( 'admin.php?page=powered-cache' );
746 $powered_cache_url = 'https://poweredcache.com/?utm_source=wp_admin&utm_medium=plugin&utm_campaign=plugin_action_links';
747
748 $actions['powered_settings'] = sprintf( '<a href="%s">%s</a>', esc_url( $settings_url ), esc_html__( 'Settings', 'powered-cache' ) );
749
750 if ( ! is_premium() ) {
751 $actions['get_premium'] = sprintf( '<a href="%s" style="color: red;">%s</a>', esc_url( $powered_cache_url ), esc_html__( 'Get Premium', 'powered-cache' ) );
752 }
753
754 return array_reverse( $actions );
755 }
756