pagination_base; if ( strlen( $pagination_base ) > 0 ) { $pagination_dir = $dir . $pagination_base; // clear pagination page(s) cache self::clear_dir( $pagination_dir ); } } // get directory data $dir_objects = self::get_dir_objects( $dir ); // check if directory is now empty or if it needs to be cleared anyways if ( empty( $dir_objects ) || $clear_type === 'dir' ) { self::clear_dir( $dir ); } } /** * clear directory * * * @param string $dir directory */ private static function clear_dir( $dir ) { // remove trailing slash $dir = untrailingslashit( $dir ); // validate directory if ( ! is_dir( $dir ) ) { return; } // get directory data $dir_objects = self::get_dir_objects( $dir ); // check if directory is empty if ( empty( $dir_objects ) ) { // delete empty directory @rmdir( $dir ); // clear file status cache clearstatcache(); // get parent directory $parent_dir = preg_replace( '/\/[^\/]+$/', '', $dir ); // get parent directory data $parent_dir_objects = self::get_dir_objects( $parent_dir ); // check if parent directory is also empty if ( empty( $parent_dir_objects ) ) { self::clear_dir( $parent_dir ); } return; } foreach ( $dir_objects as $dir_object ) { // get full path $dir_object = $dir . '/' . $dir_object; // check if directory if ( is_dir( $dir_object ) ) { self::clear_dir( $dir_object ); // check if file otherwise } elseif ( is_file( $dir_object ) ) { unlink( $dir_object ); } } // delete directory @rmdir( $dir ); // clear file status cache clearstatcache(); } /** * create files for cache * * * @param string $page_contents content of a page from the output buffer */ private static function create_cache_files( $page_contents ) { // get base signature $cache_signature = self::cache_signature(); // // make directory if necessary // if ( ! wp_mkdir_p( self::cache_file_dir_path(),0755 ) ) { // wp_die( 'Unable to create directory.' ); // } // minify HTML $page_contents = self::minify_html( $page_contents ); // create default file self::create_cache_file( self::cache_file_html(), $page_contents . $cache_signature . ' (' . self::cache_file_scheme() . ' html) -->' ); // create pre-compressed file if ( Smack_Cache_Engine::$settings['compress_cache'] ) { $compressed_page_contents = gzencode( $page_contents . $cache_signature . ' (' . self::cache_file_scheme() . ' gzip) -->', 9 ); // validate compression if ( is_string( $compressed_page_contents ) ) { self::create_cache_file( self::cache_file_gzip(), $compressed_page_contents ); } } // create WebP supported files if ( Smack_Cache_Engine::$settings['convert_image_urls_to_webp'] ) { // magic regex rule $image_urls_regex = '#(?:(?:(src|srcset|data-[^=]+)\s*=|(url)\()\s*[\'\"]?\s*)\K(?:[^\?\"\'\s>]+)(?:\.jpe?g|\.png)(?:\s\d+[wx][^\"\'>]*)?(?=\/?[\"\'\s\)>])(?=[^<{]*(?:\)[^<{]*\}|>))#i'; // call the WebP converter callback $converted_page_contents = apply_filters( 'cache_enabler_disk_webp_converted_data', preg_replace_callback( $image_urls_regex, 'self::convert_webp', $page_contents ) ); // create default WebP file self::create_cache_file( self::cache_file_webp_html(), $converted_page_contents . $cache_signature . ' (' . self::cache_file_scheme() . ' webp html) -->' ); // create pre-compressed file if ( Smack_Cache_Engine::$settings['compress_cache'] ) { $compressed_converted_page_contents = gzencode( $converted_page_contents . $cache_signature . ' (' . self::cache_file_scheme() . ' webp gzip) -->', 9 ); // validate compression if ( is_string( $compressed_converted_page_contents ) ) { self::create_cache_file( self::cache_file_webp_gzip(), $compressed_converted_page_contents ); } } } } /** * create file for cache * * * @param string $file_path file path * @param string $page_contents content of a page from the output buffer */ private static function create_cache_file( $file_path, $page_contents ) { // write page contents from output buffer to file if(file_exists($file_path)){ file_put_contents( $file_path, $page_contents ); // clear file status cache clearstatcache(); // set permissions $file_stats = @stat( dirname( $file_path ) ); $permissions = $file_stats['mode'] & 0007777; $permissions = $permissions & 0000666; @chmod( $file_path, $permissions ); // clear file status cache clearstatcache(); } } /** * create settings file * * * @param array $settings settings from database */ public static function create_settings_file( $settings ) { // validate array if ( ! is_array( $settings ) ) { return; } // check settings file requirements if ( ! function_exists( 'home_url' ) ) { return; } // get settings file $settings_file = self::get_settings_file(); // // make directory if necessary // if ( ! wp_mkdir_p( dirname( $settings_file ),0755 ) ) { // wp_die( 'Unable to create directory.' ); // } // create new settings file $new_settings_file_contents = '= filemtime( self::cache_file_html() ) ) ) { header( $server_array['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 ); exit; } // check webp and deliver gzip webp file if support if ( $http_accept && ( strpos( $http_accept, 'webp' ) !== false ) ) { if ( is_readable( self::cache_file_webp_gzip() ) ) { header( 'Content-Encoding: gzip' ); return self::cache_file_webp_gzip(); } elseif ( is_readable( self::cache_file_webp_html() ) ) { return self::cache_file_webp_html(); } } // check encoding and deliver gzip file if support if ( $http_accept_encoding && ( strpos( $http_accept_encoding, 'gzip' ) !== false ) && is_readable( self::cache_file_gzip() ) ) { header( 'Content-Encoding: gzip' ); return self::cache_file_gzip(); } // get default cached file return self::cache_file_html(); } /** * get settings file * * * @param boolean $fallback_for_sub_install return fallback settings file path for subdirectory installations * @param boolean $fallback_for_sub_network return fallback settings file path for subdirectory networks * @return string $settings_file settings file path */ private static function get_settings_file( $fallback_for_sub_install = false, $fallback_for_sub_network = false ) { // single site not in a subdirectory, any site of subdomain network, or main site of subdirectory network (fallback) $blog_path = ''; $server_array = filter_input_array(INPUT_SERVER, FILTER_SANITIZE_STRING); // subdirectory network or subdirectory installation (fallback) if ( $fallback_for_sub_install || is_multisite() && defined( 'SUBDOMAIN_INSTALL' ) && ! SUBDOMAIN_INSTALL && ! $fallback_for_sub_network ) { if ( function_exists( 'home_url' ) ) { $url_path = parse_url( home_url( '/' ), PHP_URL_PATH ); // trailing slash required } else { $url_path = $server_array['REQUEST_URI']; } $url_path_pieces = explode( '/', $url_path, 3 ); $blog_path = $url_path_pieces[1]; if ( ! empty( $blog_path ) ) { $blog_path = '.' . $blog_path; } } // get settings file $settings_file = sprintf( '%s/%s.php', self::$settings_dir, parse_url( ( function_exists( 'home_url' ) ) ? home_url() : 'http://' . strtolower( $server_array['HTTP_HOST'] ), PHP_URL_HOST ) . $blog_path ); return $settings_file; } /** * get settings from settings file * * * @return array $settings current settings from settings file */ public static function get_settings() { // get settings file $settings_file = self::get_settings_file(); $settings = array(); // include existing settings file if ( file_exists( $settings_file ) ) { $settings = include_once $settings_file; // if settings file does not exist try to get fallback settings file } else { if ( is_multisite() && defined( 'SUBDOMAIN_INSTALL' ) && ! SUBDOMAIN_INSTALL ) { $fallback_for_sub_network = true; $fallback_settings_file = self::get_settings_file( $fallback_for_sub_network ); } else { $fallback_for_sub_install = true; $fallback_settings_file = self::get_settings_file( $fallback_for_sub_install ); } // include existing fallback settings file if ( file_exists( $fallback_settings_file ) ) { $settings = include_once $fallback_settings_file; } } return $settings; } /** * get directory file system objects * * * @param string $dir directory path * @return array $dir_objects directory objects */ private static function get_dir_objects( $dir ) { // scan directory $dir_data = @scandir( $dir ); if ( is_array( $dir_data ) ) { $dir_objects = array_diff( $dir_data, array( '..', '.' ) ); return $dir_objects; } } /** * set or unset WP_CACHE constant in wp-config.php * * * @param boolean $set true to set WP_CACHE constant, false to unset */ private static function set_wp_cache_constant( $set = true ) { // get config file if ( file_exists( ABSPATH . 'wp-config.php' ) ) { // config file resides in ABSPATH $wp_config_file = ABSPATH . 'wp-config.php'; } elseif ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) { // config file resides one level above ABSPATH but is not part of another installation $wp_config_file = dirname( ABSPATH ) . '/wp-config.php'; } // check if config file can be written to if ( ! is_writable( $wp_config_file ) ) { return; } // get config file contents $wp_config_file_contents = file_get_contents( $wp_config_file ); // validate config file if ( ! is_string( $wp_config_file_contents ) ) { return; } // search for WP_CACHE constant $found_wp_cache_constant = preg_match( '/define\s*\(\s*[\'\"]WP_CACHE[\'\"]\s*,.+\);/', $wp_config_file_contents ); // if not found set WP_CACHE constant when config file is default (must be before WordPress sets up) if ( $set && ! $found_wp_cache_constant ) { $ce_wp_config_lines = '/** Enables page caching for Smack-Cache. */' . PHP_EOL; $ce_wp_config_lines .= "if ( ! defined( 'WP_CACHE' ) ) {" . PHP_EOL; $ce_wp_config_lines .= "\tdefine( 'WP_CACHE', true );" . PHP_EOL; $ce_wp_config_lines .= '}' . PHP_EOL; $ce_wp_config_lines .= PHP_EOL; $wp_config_file_contents = preg_replace( '/(\/\*\* Sets up WordPress vars and included files\. \*\/)/', $ce_wp_config_lines . '$1', $wp_config_file_contents ); } // unset WP_CACHE constant if set by Cache Enabler if ( ! $set ) { $wp_config_file_contents = preg_replace( '/.+Added by Smack Cache\r\n/', '', $wp_config_file_contents ); // < 1.5.0 $wp_config_file_contents = preg_replace( '/\/\*\* Enables page caching for Smack Cache\. \*\/' . PHP_EOL . '.+' . PHP_EOL . '.+' . PHP_EOL . '\}' . PHP_EOL . PHP_EOL . '/', '', $wp_config_file_contents ); } // update config file file_put_contents( $wp_config_file, $wp_config_file_contents ); } /** * get image path * * * @param string $image_url full or relative URL with or without intrinsic width or density descriptor * @return string $image_path path to image */ private static function image_path( $image_url ) { // in case image has intrinsic width or density descriptor $image_parts = explode( ' ', $image_url ); $image_url = $image_parts[0]; // in case installation is in a subdirectory $image_url_path = ltrim( parse_url( $image_url, PHP_URL_PATH ), '/' ); $installation_dir = preg_replace( '/^[^\/]+\/\K.+/', '', $image_url_path ); $image_path = str_replace( $installation_dir, '', ABSPATH ) . $image_url_path; return $image_path; } /** * convert image URL(s) to WebP * * * @param array $matches pattern matches from parsed page contents * @return string $conversion converted image URL(s) to WebP if applicable, default URL(s) otherwise */ private static function convert_webp( $matches ) { $full_match = $matches[0]; $image_extension_regex = '/(\.jpe?g|\.png)/i'; $image_found = preg_match( $image_extension_regex, $full_match ); if ( $image_found ) { // set image URL(s) $image_urls = explode( ',', $full_match ); foreach ( $image_urls as &$image_url ) { // remove spaces if there are any $image_url = trim( $image_url, ' ' ); // append .webp extension $image_url_webp = preg_replace( $image_extension_regex, '$1.webp', $image_url ); // get WebP image path $image_path_webp = self::image_path( $image_url_webp ); // check if WebP image exists if ( is_file( $image_path_webp ) ) { $image_url = $image_url_webp; } else { // remove default extension $image_url_webp = preg_replace( $image_extension_regex, '', $image_url_webp ); // get WebP image path $image_path_webp = self::image_path( $image_url_webp ); // check if WebP image exists if ( is_file( $image_path_webp ) ) { $image_url = $image_url_webp; } } } $conversion = implode( ', ', $image_urls ); return $conversion; } } /** * minify HTML * * * @param string $page_contents content of a page from the output buffer * @return string $minified_html|$page_contents minified page contents if applicable, unchanged otherwise * * @hook array cache_minify_ignore_tags */ private static function minify_html( $page_contents ) { // check if disabled if ( ! Smack_Cache_Engine::$settings['minify_html'] ) { return $page_contents; } // HTML character limit if ( strlen( $page_contents ) > 700000) { return $page_contents; } // HTML tags to ignore $ignore_tags = (array) apply_filters( 'cache_minify_ignore_tags', array( 'textarea', 'pre', 'code' ) ); // if selected exclude inline CSS and JavaScript if ( ! Smack_Cache_Engine::$settings['minify_inline_css_js'] ) { array_push( $ignore_tags, 'style', 'script' ); } // check if there are ignore tags if ( ! $ignore_tags ) { return $page_contents; } // stringify $ignore_regex = implode( '|', $ignore_tags ); // regex minification $minified_html = preg_replace( array( '//s', '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:' . $ignore_regex . ')\b))*+)(?:<(?>' . $ignore_regex . ')\b|\z))#', ), array( '', ' ', ), $page_contents ); // something went wrong if ( strlen( $minified_html ) <= 1 ) { return $page_contents; } return $minified_html; } /** * delete settings file * */ private static function delete_settings_file() { // get settings file $settings_file = self::get_settings_file(); // delete settings file @unlink( $settings_file ); // delete settings directory if empty @rmdir( self::$settings_dir ); } /** * delete asset (deprecated) * * @deprecated 1.5.0 */ public static function delete_asset( $url ) { if ( empty( $url ) ) { wp_die( 'URL is empty.' ); } self::clear_dir( self::cache_file_dir_path( $url ) ); } }