save_configuration( $options, POWERED_CACHE_IS_NETWORK ); // drop object cache on backend changes if ( isset( $options['object_cache'] ) && $old_options['object_cache'] !== $options['object_cache'] ) { wp_cache_flush(); } // maybe cancel preloading process when it turned off if ( $old_options['enable_cache_preload'] && ! $options['enable_cache_preload'] ) { cancel_preloading(); } // cleanup existing cache on toggling cache option if ( $old_options['enable_page_cache'] && ! $options['enable_page_cache'] ) { clean_site_cache_dir(); } if ( $old_options['cache_timeout'] !== $options['cache_timeout'] ) { $timestamp = wp_next_scheduled( PURGE_CACHE_CRON_NAME ); wp_unschedule_event( $timestamp, PURGE_CACHE_CRON_NAME ); } /** * Fires after saving configurations. * * @hook powered_cache_settings_saved * * @param {array} $old_options Old settings. * @param {array} $options New settings. * * @since 1.0 */ do_action( 'powered_cache_settings_saved', $old_options, $options ); $redirect_url = wp_get_referer(); if ( empty( $redirect_url ) ) { $redirect_url = $_SERVER['REQUEST_URI']; } $redirect_url = add_query_arg( [ 'pc_action' => $action, ], $redirect_url ); wp_safe_redirect( $redirect_url ); exit; } } /** * Sanitize options * * @param array $options Raw input, most likely $_POST request * * @return array|mixed Sanitized options */ function sanitize_options( $options ) { $sanitized_options = []; if ( isset( $options['object_cache'] ) && in_array( $options['object_cache'], get_available_object_caches(), true ) ) { $sanitized_options['object_cache'] = sanitize_text_field( $options['object_cache'] ); } else { $sanitized_options['object_cache'] = 'off'; } $sanitized_options['enable_page_cache'] = ! empty( $options['enable_page_cache'] ); $sanitized_options['cache_mobile'] = ! empty( $options['cache_mobile'] ); $sanitized_options['cache_mobile_separate_file'] = ! empty( $options['cache_mobile_separate_file'] ); $sanitized_options['loggedin_user_cache'] = ! empty( $options['loggedin_user_cache'] ); $sanitized_options['gzip_compression'] = ! empty( $options['gzip_compression'] ); $sanitized_options['cache_timeout'] = absint( $options['cache_timeout'] ); $sanitized_options['auto_configure_htaccess'] = ! empty( $options['auto_configure_htaccess'] ); $sanitized_options['rejected_user_agents'] = sanitize_textarea_field( $options['rejected_user_agents'] ); $sanitized_options['rejected_cookies'] = sanitize_textarea_field( $options['rejected_cookies'] ); $sanitized_options['vary_cookies'] = sanitize_textarea_field( $options['vary_cookies'] ); $sanitized_options['rejected_uri'] = sanitize_textarea_field( $options['rejected_uri'] ); $sanitized_options['accepted_query_strings'] = sanitize_textarea_field( $options['accepted_query_strings'] ); $sanitized_options['purge_additional_pages'] = sanitize_textarea_field( $options['purge_additional_pages'] ); $sanitized_options['purge_additional_pages'] = sanitize_textarea_field( $options['purge_additional_pages'] ); $sanitized_options['minify_html'] = ! empty( $options['minify_html'] ); $sanitized_options['combine_google_fonts'] = ! empty( $options['combine_google_fonts'] ); $sanitized_options['minify_css'] = ! empty( $options['minify_css'] ); $sanitized_options['combine_css'] = ! empty( $options['combine_css'] ); $sanitized_options['excluded_css_files'] = sanitize_textarea_field( $options['excluded_css_files'] ); $sanitized_options['minify_js'] = ! empty( $options['minify_js'] ); $sanitized_options['combine_js'] = ! empty( $options['combine_js'] ); $sanitized_options['excluded_js_files'] = sanitize_textarea_field( $options['excluded_js_files'] ); $sanitized_options['excluded_js_files'] = sanitize_textarea_field( $options['excluded_js_files'] ); $sanitized_options['js_execution_method'] = sanitize_text_field( $options['js_execution_method'] ); $sanitized_options['js_execution_optimized_only'] = ! empty( $options['js_execution_optimized_only'] ); $sanitized_options['enable_lazy_load'] = ! empty( $options['enable_lazy_load'] ); $sanitized_options['lazy_load_post_content'] = ! empty( $options['lazy_load_post_content'] ); $sanitized_options['lazy_load_images'] = ! empty( $options['lazy_load_images'] ); $sanitized_options['lazy_load_iframes'] = ! empty( $options['lazy_load_iframes'] ); $sanitized_options['lazy_load_widgets'] = ! empty( $options['lazy_load_widgets'] ); $sanitized_options['lazy_load_post_thumbnail'] = ! empty( $options['lazy_load_post_thumbnail'] ); $sanitized_options['lazy_load_avatars'] = ! empty( $options['lazy_load_avatars'] ); $sanitized_options['disable_wp_lazy_load'] = ! empty( $options['disable_wp_lazy_load'] ); $sanitized_options['disable_wp_embeds'] = ! empty( $options['disable_wp_embeds'] ); $sanitized_options['disable_emoji_scripts'] = ! empty( $options['disable_emoji_scripts'] ); $sanitized_options['enable_cdn'] = ! empty( $options['enable_cdn'] ); // convert TTL in minute if ( $options['cache_timeout'] > 0 && isset( $options['cache_timeout_interval'] ) ) { switch ( $options['cache_timeout_interval'] ) { case 'DAY': $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1440; break; case 'HOUR': $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 60; break; case 'MINUTE': default: $sanitized_options['cache_timeout'] = $options['cache_timeout'] * 1; } } $cdn_hostname = []; if ( isset( $options['cdn_hostname'] ) ) { foreach ( (array) $options['cdn_hostname'] as $hostname ) { $cdn_hostname[] = esc_url_raw( $hostname ); } } $cdn_zone = []; if ( isset( $options['cdn_zone'] ) ) { foreach ( (array) $options['cdn_zone'] as $zone ) { $cdn_zone[] = sanitize_text_field( $zone ); } } $sanitized_options['cdn_hostname'] = $cdn_hostname; $sanitized_options['cdn_zone'] = $cdn_zone; $sanitized_options['cdn_rejected_files'] = sanitize_textarea_field( $options['cdn_rejected_files'] ); $sanitized_options['enable_cache_preload'] = ! empty( $options['enable_cache_preload'] ); $sanitized_options['preload_homepage'] = ! empty( $options['preload_homepage'] ); $sanitized_options['preload_public_posts'] = ! empty( $options['preload_public_posts'] ); $sanitized_options['preload_public_tax'] = ! empty( $options['preload_public_tax'] ); $sanitized_options['enable_sitemap_preload'] = ! empty( $options['enable_sitemap_preload'] ); $sanitized_options['preload_sitemap'] = sanitize_textarea_field( $options['preload_sitemap'] ); $sanitized_options['prefetch_dns'] = sanitize_textarea_field( $options['prefetch_dns'] ); $sanitized_options['db_cleanup_post_revisions'] = ! empty( $options['db_cleanup_post_revisions'] ); $sanitized_options['db_cleanup_auto_drafts'] = ! empty( $options['db_cleanup_auto_drafts'] ); $sanitized_options['db_cleanup_trashed_posts'] = ! empty( $options['db_cleanup_trashed_posts'] ); $sanitized_options['db_cleanup_spam_comments'] = ! empty( $options['db_cleanup_spam_comments'] ); $sanitized_options['db_cleanup_trashed_comments'] = ! empty( $options['db_cleanup_trashed_comments'] ); $sanitized_options['db_cleanup_expired_transients'] = ! empty( $options['db_cleanup_expired_transients'] ); $sanitized_options['db_cleanup_all_transients'] = ! empty( $options['db_cleanup_all_transients'] ); $sanitized_options['db_cleanup_optimize_tables'] = ! empty( $options['db_cleanup_optimize_tables'] ); $sanitized_options['enable_scheduled_db_cleanup'] = ! empty( $options['enable_scheduled_db_cleanup'] ); $sanitized_options['scheduled_db_cleanup_frequency'] = sanitize_text_field( $options['scheduled_db_cleanup_frequency'] ); $sanitized_options['enable_cloudflare'] = ! empty( $options['enable_cloudflare'] ); $sanitized_options['cloudflare_email'] = sanitize_email( $options['cloudflare_email'] ); $sanitized_options['cloudflare_api_key'] = sanitize_text_field( $options['cloudflare_api_key'] ); $sanitized_options['cloudflare_zone'] = sanitize_text_field( $options['cloudflare_zone'] ); $sanitized_options['enable_heartbeat'] = ! empty( $options['enable_heartbeat'] ); $sanitized_options['heartbeat_dashboard_status'] = sanitize_text_field( $options['heartbeat_dashboard_status'] ); $sanitized_options['heartbeat_editor_status'] = sanitize_text_field( $options['heartbeat_editor_status'] ); $sanitized_options['heartbeat_frontend_status'] = sanitize_text_field( $options['heartbeat_frontend_status'] ); $sanitized_options['heartbeat_dashboard_interval'] = absint( $options['heartbeat_dashboard_interval'] ); $sanitized_options['heartbeat_editor_interval'] = absint( $options['heartbeat_editor_interval'] ); $sanitized_options['heartbeat_frontend_interval'] = absint( $options['heartbeat_frontend_interval'] ); $sanitized_options['enable_varnish'] = ! empty( $options['enable_varnish'] ); $sanitized_options['varnish_ip'] = sanitize_text_field( $options['varnish_ip'] ); $sanitized_options['cache_footprint'] = ! empty( $options['cache_footprint'] ); $sanitized_options['enable_google_tracking'] = ! empty( $options['enable_google_tracking'] ); $sanitized_options['enable_fb_tracking'] = ! empty( $options['enable_fb_tracking'] ); /** * Filters sanitized options. * * @hook powered_cache_sanitized_options * * @param {array} $sanitized_options Sanitized options. * @param {array} $options raw input. * * @return {array} New value. * * @since 2.0 */ return apply_filters( 'powered_cache_sanitized_options', $sanitized_options, $options ); } /** * Add base admin bar menu * * @param object $wp_admin_bar Admin bar object * * @since 2.0 */ function admin_bar_menu( $wp_admin_bar ) { $href = admin_url( 'admin.php?page=powered-cache' ); if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) { $href = network_admin_url( 'admin.php?page=powered-cache' ); } if ( POWERED_CACHE_IS_NETWORK && ! current_user_can( 'manage_network' ) ) { $href = '#'; } if ( current_user_can( 'manage_options' ) ) { $wp_admin_bar->add_menu( array( 'id' => MENU_SLUG, 'title' => __( 'Powered Cache', 'powered-cache' ), 'href' => $href, ) ); } } /** * Maybe display feedback messages when certain action is taken * * @since 2.0 */ function maybe_display_message() { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['pc_action'] ) ) { return; } /** * Dont display multiple message when saving the options while having the query_params */ if ( ! empty( $_POST ) ) { return; } $screen = get_current_screen(); $success_messages = [ 'flush_page_cache_network' => esc_html__( 'Page cache deleted for all websites!', 'powered-cache' ), 'flush_page_cache' => esc_html__( 'Page cache deleted successfully!', 'powered-cache' ), 'flush_object_cache' => esc_html__( 'Object cache deleted successfully!', 'powered-cache' ), 'flush_all_cache' => esc_html__( 'All cached items flushed successfully!', 'powered-cache' ), 'start_preload' => esc_html__( 'The cache preloading has been initialized!', 'powered-cache' ), 'flush_cf_cache' => esc_html__( 'Cloudflare cache flushed, it can take up to 30 seconds to delete all cache from Cloudflare!', 'powered-cache' ), 'reset_settings' => esc_html__( 'Settings have been reset!', 'powered-cache' ), 'import_settings' => esc_html__( 'Settings have been imported!', 'powered-cache' ), 'save_settings_and_optimize' => esc_html__( 'Settings saved and database being optimized...', 'powered-cache' ), 'save_settings' => esc_html__( 'Settings saved.', 'powered-cache' ), ]; $err_messages = [ 'flush_page_cache_network_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ), 'flush_page_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ), 'flush_object_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ), 'flush_all_cache_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ), 'start_preload_err_permission' => esc_html__( 'You don\'t have permission to perform this action!', 'powered-cache' ), 'flush_cf_cache_failed' => esc_html__( 'Could not flush Cloudflare cache. Please make sure you entered the correct credentials and zone id!', 'powered-cache' ), ]; if ( isset( $success_messages[ $_GET['pc_action'] ] ) ) { if ( MENU_SLUG === $screen->parent_base ) { // display with shared-ui on plugin page add_settings_error( $screen->parent_file, MENU_SLUG, $success_messages[ $_GET['pc_action'] ], 'success' ); return; } printf( '
%s
%s
WP_CACHE' );
}
$checks[] = array(
'check' => 'config',
'status' => $config_file_status,
'description' => $config_file_desc,
);
// check cache directory
$cache_dir = get_cache_dir();
$cache_dir_status = false;
if ( ! file_exists( $cache_dir ) ) {
$cache_dir_desc = sprintf( __( 'Cache directory %s is not exist!', 'powered-cache' ), '' . $cache_dir . '' );
} elseif ( ! is_writeable( $cache_dir ) ) {
$cache_dir_desc = sprintf( __( 'Cache directory %s is not writeable!', 'powered-cache' ), '' . $cache_dir . '' );
} else {
$cache_dir_status = true;
$cache_dir_desc = sprintf( __( 'Cache directory %s exist and writable!', 'powered-cache' ), '' . $cache_dir . '' );
}
$checks[] = array(
'check' => 'cache-dir',
'status' => $cache_dir_status,
'description' => $cache_dir_desc,
);
// check .htaccess file
if ( $is_apache && $settings['auto_configure_htaccess'] ) {
$htaccess_file = get_home_path() . '.htaccess';
$htaccess_file_status = false;
if ( ! file_exists( $htaccess_file ) ) {
$htaccess_file_desc = sprintf( __( '.htaccess file %s is not exist!', 'powered-cache' ), '' . $htaccess_file . '' );
} elseif ( ! is_writeable( $htaccess_file ) ) {
$htaccess_file_desc = sprintf( __( '.htaccess file %s is not writeable!', 'powered-cache' ), '' . $htaccess_file . '' );
} else {
$htaccess_file_status = true;
$htaccess_file_desc = sprintf( __( '.htaccess file %s exist and writable!', 'powered-cache' ), '' . $htaccess_file . '' );
}
$checks[] = array(
'check' => 'htaccess',
'status' => $htaccess_file_status,
'description' => $htaccess_file_desc,
);
}
// check page cache
if ( $settings['enable_page_cache'] ) {
$advanced_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php';
$advanced_cache_file_status = false;
if ( ! file_exists( $advanced_cache_file ) ) {
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not exist!', 'powered-cache' ), '' . $advanced_cache_file . '' );
} elseif ( ! is_writeable( $advanced_cache_file ) ) {
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not writeable!', 'powered-cache' ), '' . $advanced_cache_file . '' );
} else {
$advanced_cache_file_status = true;
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s exist and writable!', 'powered-cache' ), '' . $advanced_cache_file . '' );
}
$checks[] = array(
'check' => 'advanced-cache',
'status' => $advanced_cache_file_status,
'description' => $advanced_cache_file_desc,
);
}
// check object cache
if ( 'off' !== $settings['object_cache'] ) {
$object_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
$object_cache_file_status = false;
if ( ! file_exists( $object_cache_file ) ) {
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not exist!', 'powered-cache' ), '' . $object_cache_file . '' );
} elseif ( ! is_writeable( $object_cache_file ) ) {
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not writeable!', 'powered-cache' ), '' . $object_cache_file . '' );
} else {
$object_cache_file_status = true;
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s exist and writable!', 'powered-cache' ), '' . $object_cache_file . '' );
}
$checks[] = array(
'check' => 'object-cache',
'status' => $object_cache_file_status,
'description' => $object_cache_file_desc,
);
}
wp_send_json_success( $checks );
}
wp_send_json_error( [ esc_html__( 'Invalid request', 'powered-cache' ) ] );
}
/**
* Deactivate incompatible plugins
*
* @since 1.0
*/
function deactivate_plugin() {
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'deactivate_plugin' ) ) {
wp_nonce_ays( '' );
}
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_safe_redirect( wp_get_referer() );
exit;
}
deactivate_plugins( $_GET['plugin'] );
wp_safe_redirect( wp_get_referer() );
die();
}
/**
* Adds settings link to plugin actions
*
* @param array $actions Plugin actions.
*
* @return array
* @since 1.0
*/
function action_links( $actions ) {
$settings_url = POWERED_CACHE_IS_NETWORK ? network_admin_url( 'admin.php?page=powered-cache' ) : admin_url( 'admin.php?page=powered-cache' );
$powered_cache_url = 'https://poweredcache.com/?utm_source=wp_admin&utm_medium=plugin&utm_campaign=plugin_action_links';
$actions['powered_settings'] = sprintf( '%s', esc_url( $settings_url ), esc_html__( 'Settings', 'powered-cache' ) );
if ( ! is_premium() ) {
$actions['get_premium'] = sprintf( '%s', esc_url( $powered_cache_url ), esc_html__( 'Get Premium', 'powered-cache' ) );
}
return array_reverse( $actions );
}