' ); } } /** * upgrade hook * * * @param WP_Upgrader $obj upgrade instance * @param array $data update data */ public static function on_upgrade( $obj, $data ) { // if setting enabled clear complete cache on any plugin update if ( Smack_Cache_Engine::$settings['clear_complete_cache_on_changed_plugin'] ) { self::clear_complete_cache(); } // check updated plugins if ( $data['action'] === 'update' && $data['type'] === 'plugin' && array_key_exists( 'plugins', $data ) ) { foreach ( (array) $data['plugins'] as $plugin_file ) { // check if Cache Enabler has been updated if ( $plugin_file === SMACK_BASE ) { self::on_ce_update(); } } } } /** * Cache Enabler update actions * */ public static function on_ce_update() { $network_wide = is_multisite(); $plugin_update = true; // clean system files self::each_site( $network_wide, 'Smack_Cache_Disk::clean' ); // configure system files Smack_Cache_Disk::setup(); // update backend requirements, triggering new settings file(s) to be created self::each_site( $network_wide, 'self::update_backend', array( $plugin_update ) ); // clear complete cache self::clear_complete_cache(); } /** * deactivation hook * * * @param boolean $network_wide network deactivated */ public static function on_deactivation( $network_wide ) { // Remove mu-plugin if ( file_exists( WPMU_PLUGIN_DIR . '/smack-profile.php' ) ) { if ( is_writable( WPMU_PLUGIN_DIR . '/smack-profile.php' ) ) { // Some servers give write permission, but not delete permission. Empty the file out, first, then try to delete it. file_put_contents( WPMU_PLUGIN_DIR . '/smack-profile.php', '' ); unlink( WPMU_PLUGIN_DIR . '/smack-profile.php' ); } } // clean system files self::each_site( $network_wide, 'Smack_Cache_Disk::clean' ); // clear site cache of deactivated site if ( is_multisite() && ! $network_wide ) { self::clear_site_cache_by_blog_id( get_current_blog_id() ); // clear complete cache otherwise } else { self::clear_complete_cache(); } } /** * uninstall hook * */ public static function on_uninstall() { // uninstall backend requirements self::each_site( is_multisite(), 'self::uninstall_backend' ); // clear complete cache self::clear_complete_cache(); } /** * install on new site in multisite network * * * @param WP_Site $new_site new site instance */ public static function install_later( $new_site ) { // check if network activated if ( ! is_plugin_active_for_network( SMACK_BASE ) ) { return; } // switch to blog switch_to_blog( (int) $new_site->blog_id ); // install backend requirements, triggering the settings file to be created self::install_backend(); // restore blog restore_current_blog(); } /** * install backend requirements * */ private static function install_backend() { // if old or current database option exists update backend requirements if ( get_option( 'smack-cache-enhancer' ) || get_option( 'smack_cache_enhancer' ) ) { self::update_backend(); // add default database option otherwise } else { $default_settings = self::get_default_settings(); add_option( 'smack_cache_enhancer', $default_settings ); // create settings file if action was not fired, like when in activation hook if ( ! has_action( 'add_option_cache_enabler' ) ) { self::on_update_backend( 'cache_enabler', $default_settings ); } } } /** * update backend requirements * * * @param $plugin_update whether or not an update is in progress * @return $new_option_value new or current database option value */ public static function update_backend( $plugin_update = false ) { // check if backend should be updated if ( $plugin_update && ! is_plugin_active( SMACK_BASE ) ) { return; } // delete user(s) meta key from deleted publishing action (1.5.0) delete_metadata( 'user', 0, '_clear_post_cache_on_update', '', true ); // rename database option (1.5.0) $old_option_value = get_option( 'cache-enabler' ); if ( $old_option_value !== false ) { delete_option( 'cache-enabler' ); add_option( 'smack_cache_enhancer', $old_option_value ); } // get defined settings, fall back to empty array if not found $old_option_value = get_option( 'smack_cache_enhancer', array() ); // maybe convert old settings to new settings $old_option_value = self::convert_settings( $old_option_value ); // update default system settings $old_option_value = wp_parse_args( self::get_default_settings( 'system' ), $old_option_value ); // merge defined settings into default settings $new_option_value = wp_parse_args( $old_option_value, self::get_default_settings() ); // update database option update_option( 'smack_cache_enhancer', $new_option_value ); // create settings file if action was not fired, like when in upgrade hook or if a database update was unnecessary if ( ! has_action( 'update_option_cache_enabler' ) ) { self::on_update_backend( $old_option_value, $new_option_value ); } return $new_option_value; } /** * add or update database option hook * * * @param mixed $option old database option value or name of the option to add * @param mixed $new_option_value new database option value */ public static function on_update_backend( $option, $new_option_value ) { Smack_Cache_Disk::create_settings_file( $new_option_value ); } /** * uninstall on deleted site in multisite network * * * @param WP_Site $old_site old site instance */ public static function uninstall_later( $old_site ) { $delete_cache_size_transient = false; // clean system files Smack_Cache_Disk::clean(); // clear site cache of deleted site self::clear_site_cache_by_blog_id( (int) $old_site->blog_id, $delete_cache_size_transient ); } /** * uninstall backend requirements * */ private static function uninstall_backend() { // delete database option delete_option( 'smack_cache_enhancer' ); } /** * enter each site * * * @param boolean $network whether or not each site in network * @param string $callback callback function * @param array $callback_params callback function parameters * @return array $callback_return returned value(s) from callback function */ private static function each_site( $network, $callback, $callback_params = array() ) { $callback_return = array(); if ( $network ) { $blog_ids = self::get_blog_ids(); // switch to each site in network foreach ( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); $callback_return[] = (int) call_user_func_array( $callback, $callback_params ); restore_current_blog(); } } else { $callback_return[] = (int) call_user_func_array( $callback, $callback_params ); } return $callback_return; } /** * plugin activation and deactivation hooks * */ public static function on_plugin_activation_deactivation() { // if setting enabled clear complete cache on any plugin activation or deactivation if ( Smack_Cache_Engine::$settings['clear_complete_cache_on_changed_plugin'] ) { self::clear_complete_cache(); } } /** * get settings from database * * * @return array $settings current settings from database */ public static function get_settings() { // get database option value $settings = get_option( 'smack_cache_enhancer' ); // if database option does not exist or settings are outdated if ( $settings === false ) { $settings = self::update_backend(); } return $settings; } /** * get blog IDs * * * @return array blog IDs array */ private static function get_blog_ids() { global $wpdb; return $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); } /** * get blog paths * * * @return array blog paths array */ private static function get_blog_paths() { global $wpdb; return $wpdb->get_col( "SELECT path FROM $wpdb->blogs" ); } /** * get permalink structure * * * @return string permalink structure */ private static function get_permalink_structure() { // get permalink structure $permalink_structure = get_option( 'permalink_structure' ); // permalink structure is custom and has a trailing slash if ( $permalink_structure && preg_match( '/\/$/', $permalink_structure ) ) { return 'has_trailing_slash'; } // permalink structure is custom and does not have a trailing slash if ( $permalink_structure && ! preg_match( '/\/$/', $permalink_structure ) ) { return 'no_trailing_slash'; } // permalink structure is not custom if ( empty( $permalink_structure ) ) { return 'plain'; } } /** * get cache size * * * @return integer $size cache size (bytes) */ public static function get_cache_size() { $cache_size = get_transient( self::get_cache_size_transient_name() ); if ( ! $cache_size ) { $cache_size = Smack_Cache_Disk::cache_size(); set_transient( self::get_cache_size_transient_name(), $cache_size, MINUTE_IN_SECONDS * 15 ); } return $cache_size; } /** * get the cache size transient name * * * @param integer $blog_id blog ID * @return string $transient_name transient name */ private static function get_cache_size_transient_name( $blog_id = null ) { // set blog ID if provided, get current blog ID otherwise $blog_id = ( $blog_id ) ? $blog_id : get_current_blog_id(); $transient_name = 'cache_enabler_cache_size_' . $blog_id; return $transient_name; } /** * get the cache cleared transient name used for the clear notice * * * @return string $transient_name transient name */ private static function get_cache_cleared_transient_name() { $transient_name = 'smack_cache_enhancer_cache_cleared_' . get_current_user_id(); return $transient_name; } /** * get default settings * * * @param string $settings_type default `system` settings * @return array $system_default_settings|$default_settings system or all default settings */ private static function get_default_settings( $settings_type = null ) { $system_default_settings = array( 'permalink_structure' => (string) self::get_permalink_structure(), ); if ( $settings_type === 'system' ) { return $system_default_settings; } $user_default_settings = array( 'cache_expires' => 0, 'cache_expiry_time' => 0, 'clear_complete_cache_on_saved_post' => 0, 'clear_complete_cache_on_new_comment' => 0, 'clear_complete_cache_on_changed_plugin' => 0, 'compress_cache' => 0, 'convert_image_urls_to_webp' => 0, 'excluded_post_ids' => '', 'excluded_page_paths' => '', 'excluded_query_strings' => '', 'excluded_cookies' => '', 'minify_html' => 0, 'minify_inline_css_js' => 0, ); // merge default settings $default_settings = wp_parse_args( $user_default_settings, $system_default_settings ); return $default_settings; } /** * convert settings to new structure * * * @param array $settings settings * @return array $settings converted settings if applicable, unchanged otherwise */ private static function convert_settings( $settings ) { // check if there are any settings to convert if ( empty( $settings ) ) { return $settings; } // updated settings if ( isset( $settings['expires'] ) && $settings['expires'] > 0 ) { $settings['cache_expires'] = 1; } if ( isset( $settings['minify_html'] ) && $settings['minify_html'] === 2 ) { $settings['minify_html'] = 1; $settings['minify_inline_css_js'] = 1; } // renamed or removed settings $settings_names = array( 'expires' => 'cache_expiry_time', 'new_post' => 'clear_complete_cache_on_saved_post', 'update_product_stock' => '', // deprecated in 1.5.0 'new_comment' => 'clear_complete_cache_on_new_comment', 'clear_on_upgrade' => 'clear_complete_cache_on_changed_plugin', 'compress' => 'compress_cache', 'webp' => 'convert_image_urls_to_webp', 'excl_ids' => 'excluded_post_ids', 'excl_regexp' => 'excluded_page_paths', // < 1.4.0 'excl_paths' => 'excluded_page_paths', 'excl_cookies' => 'excluded_cookies', 'incl_parameters' => '', // deprecated in 1.5.0 ); foreach ( $settings_names as $old_name => $new_name ) { if ( array_key_exists( $old_name, $settings ) ) { if ( ! empty( $new_name ) ) { $settings[ $new_name ] = $settings[ $old_name ]; } unset( $settings[ $old_name ] ); } } return $settings; } /** * add plugin action links in the plugins list table * * * @param array $action_links action links * @return array $action_links updated action links if applicable, unchanged otherwise */ public static function add_plugin_action_links( $action_links ) { // check user role if ( ! current_user_can( 'manage_options' ) ) { return $action_links; } return $action_links; } /** * add dashboard cache size count * * @since 1.0.0 * @change 1.5.0 * * @param array $items initial array with dashboard items * @return array $items merged array with dashboard items */ public static function add_dashboard_cache_size( $items = array() ) { // check user role if ( ! current_user_can( 'manage_options' ) ) { return $items; } // get cache size $size = self::get_cache_size(); // display items $items = array( sprintf( '%s %s', admin_url( 'options-general.php?page=ultimate-performance-enhancer' ), esc_html__( 'Refreshes every 15 minutes', 'smack-cache' ), ( empty( $size ) ) ? esc_html__( 'Empty', 'smack-cache' ) : size_format( $size ), esc_html__( 'Cache Size', 'smack-cache' ) ) ); return $items; } /** * add admin links * * * @param object menu properties * * @hook mixed user_can_clear_cache */ public static function add_admin_links( $wp_admin_bar ) { // check user role if ( ! is_admin_bar_showing() || ! apply_filters( 'user_can_clear_cache', current_user_can( 'manage_options' ) ) ) { return; } // get clear complete cache button title $title = ( is_multisite() && is_network_admin() ) ? esc_html__( 'Clear Network Cache', 'smack-cache' ) : esc_html__( 'Clear Cache', 'smack-cache' ); // add "Clear Network Cache" or "Clear Cache" button in admin bar // $wp_admin_bar->add_menu( // array( // 'id' => 'clear-cache', // 'href' => wp_nonce_url( add_query_arg( array( // '_cache' => 'smack-cache', // '_action' => 'clear', // ) ), 'cache_enabler_clear_cache_nonce' ), // 'parent' => 'top-secondary', // 'title' => '' . $title . '', // 'meta' => array( 'title' => $title ), // ) // ); // add "Clear URL Cache" button in admin bar // if ( ! is_admin() ) { // $wp_admin_bar->add_menu( // array( // 'id' => 'clear-url-cache', // 'href' => wp_nonce_url( add_query_arg( array( // '_cache' => 'smack-cache', // '_action' => 'clearurl', // ) ), 'cache_enabler_clear_cache_nonce' ), // 'parent' => 'top-secondary', // 'title' => '' . esc_html__( 'Clear URL Cache', 'smack-cache' ) . '', // 'meta' => array( 'title' => esc_html__( 'Clear URL Cache', 'smack-cache' ) ), // ) // ); // } } /** * process clear cache request * */ public static function process_clear_cache_request() { $gets = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); // check if clear cache request if ( empty( $gets['_cache'] ) || empty( $gets['_action'] ) || $gets['_cache'] !== 'smack-cache' && ( $gets['_action'] !== 'clear' || $gets['_action'] !== 'clearurl' ) ) { return; } // validate nonce if ( empty( $gets['_wpnonce'] ) || ! wp_verify_nonce( $gets['_wpnonce'], 'cache_enabler_clear_cache_nonce' ) ) { return; } // check user role if ( ! is_admin_bar_showing() || ! apply_filters( 'user_can_clear_cache', current_user_can( 'manage_options' ) ) ) { return; } // set clear URL without query string and check if installation is in a subdirectory $installation_dir = parse_url( home_url(), PHP_URL_PATH ); $server_array = filter_input_array(INPUT_SERVER, FILTER_SANITIZE_STRING); $clear_url = str_replace( $installation_dir, '', home_url() ) . preg_replace( '/\?.*/', '', $server_array['REQUEST_URI'] ); // network activated if ( is_multisite() ) { // network admin if ( is_network_admin() && $gets['_action'] === 'clear' ) { // clear complete cache self::clear_complete_cache(); // site admin } else { if ( $gets['_action'] === 'clearurl' ) { // clear specific site URL cache self::clear_page_cache_by_url( $clear_url ); } elseif ( $gets['_action'] === 'clear' ) { // clear specific site complete cache self::clear_site_cache_by_blog_id( get_current_blog_id() ); } } // site activated } else { if ( $gets['_action'] === 'clearurl' ) { // clear URL cache self::clear_page_cache_by_url( $clear_url ); } elseif ( $gets['_action'] === 'clear' ) { // clear complete cache self::clear_complete_cache(); } } // redirect to same page wp_safe_redirect( wp_get_referer() ); // set transient for clear notice if ( is_admin() ) { set_transient( self::get_cache_cleared_transient_name(), 1 ); } // clear cache request completed exit; } /** * admin notice after cache has been cleared * * * @hook mixed user_can_clear_cache */ public static function cache_cleared_notice() { // check user role if ( ! is_admin_bar_showing() || ! apply_filters( 'user_can_clear_cache', current_user_can( 'manage_options' ) ) ) { return; } if ( get_transient( self::get_cache_cleared_transient_name() ) ) { // echo sprintf( // '
%s
%s
%s
%s
755',
'wp-content/cache',
sprintf(
'%s',
'https://wordpress.org/support/article/changing-file-permissions/',
esc_html__( 'file permissions', 'smack-cache' )
)
)
);
}
}
/**
* register textdomain
*
*/
public static function register_textdomain() {
// load translated strings
load_plugin_textdomain( 'smack-cache', false, 'smack-cache/lang' );
}
/**
* register settings
*
*/
public static function register_settings() {
register_setting( 'smack_cache', 'smack_cache', array( __CLASS__, 'validate_settings' ) );
}
/**
* validate regex
*
*
* @param string $regex string containing regex
* @return string $validated_regex string containing regex or empty string if input is invalid
*/
public static function validate_regex( $regex ) {
if ( ! empty( $regex ) ) {
if ( ! preg_match( '/^\/.*\/$/', $regex ) ) {
$regex = '/' . $regex . '/';
}
if ( @preg_match( $regex, null ) === false ) {
return '';
}
$validated_regex = sanitize_text_field( $regex );
return $validated_regex;
}
return '';
}
/**
* validate settings
*
*
* @param array $settings user defined settings
* @return array $validated_settings validated settings
*/
public static function validate_settings( $settings ) {
// validate array
if ( ! is_array( $settings ) ) {
return;
}
$validated_settings = array(
'cache_expires' => (int) ( ! empty( $settings['cache_expires'] ) ),
'cache_expiry_time' => (int) @$settings['cache_expiry_time'],
'clear_complete_cache_on_saved_post' => (int) ( ! empty( $settings['clear_complete_cache_on_saved_post'] ) ),
'clear_complete_cache_on_new_comment' => (int) ( ! empty( $settings['clear_complete_cache_on_new_comment'] ) ),
'clear_complete_cache_on_changed_plugin' => (int) ( ! empty( $settings['clear_complete_cache_on_changed_plugin'] ) ),
'compress_cache' => (int) ( ! empty( $settings['compress_cache'] ) ),
'convert_image_urls_to_webp' => (int) ( ! empty( $settings['convert_image_urls_to_webp'] ) ),
'excluded_post_ids' => (string) sanitize_text_field( @$settings['excluded_post_ids'] ),
'excluded_page_paths' => (string) self::validate_regex( @$settings['excluded_page_paths'] ),
'excluded_query_strings' => (string) self::validate_regex( @$settings['excluded_query_strings'] ),
'excluded_cookies' => (string) self::validate_regex( @$settings['excluded_cookies'] ),
'minify_html' => (int) ( ! empty( $settings['minify_html'] ) ),
'minify_inline_css_js' => (int) ( ! empty( $settings['minify_inline_css_js'] ) ),
);
// add default system settings
$validated_settings = wp_parse_args( $validated_settings, self::get_default_settings( 'system' ) );
// check if cache should be cleared
if ( ! empty( $settings['clear_complete_cache_on_saved_settings'] ) ) {
self::clear_complete_cache();
set_transient( self::get_cache_cleared_transient_name(), 1 );
}
return $validated_settings;
}
}