cache_dir = $path.'/'; // Stop caching for admin, logged-in users, or POST requests //if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) { //return; //} // Load WP_Filesystem require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; add_action( 'init', array( $this, 'aabcore_cache_ifex_cache' ), 0 ); add_action( 'template_redirect', array( $this, 'aabcore_start_buffer' ) ); add_action( 'shutdown', array( $this, 'aabcore_cache_on_shutdown' ) ); add_action( 'update_option_permalink_structure', array( $this, 'aabcore_cache_dkpl_cache' ), 10, 2 ); add_action( 'switch_theme', array( $this, 'aabcore_cache_dkth_cache' ), 10, 2 ); add_action( 'wp_ajax_aabcore_up_cache_op_cache', [$this,'aabcore_up_cache_op_cache'] ); //add_action( 'wp_enqueue_scripts', [ $this, 'aabcore_cache_enq_cache_assets' ] ); } public function aabcore_cache_enq_cache_assets() { wp_enqueue_style( 'aabcore-global', wp_upload_dir()['baseurl'].'/aabcore-cdn-cache/'.$this->url_path.'.css', array(), filemtime( wp_upload_dir()['basedir'] . '/aabcore-cdn-cache/'.$this->url_path.'.css' ) ); } /** * AJAX handler. */ function aabcore_up_cache_op_cache() { check_ajax_referer( 'aabcore_tmss2_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Permission denied' ); } $option = sanitize_text_field( wp_unslash( $_POST['option_name'] ?? '' )); $state = sanitize_text_field( wp_unslash( $_POST['state'] ?? '' )); if ( empty( $option ) ) { wp_send_json_error( 'Missing option name' ); } update_option( $option, $state ); wp_send_json_success( [ 'state' => $state ] ); } function aabcore_cache_pa_cdn_cache() { $Aabcore_top = new Aabcore_top(); $Aabcore_top->aabcore_cache_top(); $af_cache_a_cache = new Aabcore_Af_cache_cdn_cache(); $af_cache_a_cache->aabcore_ar_cache_cdn_cache(); ?> cache_dir . '*.html' ); $total_size = 0; $cot_cache = count( $files ); return $cot_cache; } public function aabcore_cache_sie_cache() { $files = glob( $this->cache_dir . '*.html' ); $total_size = 0; $count = count( $files ); foreach ( $files as $file ) { $total_size += filesize( $file ); } $sie_kb = round( $total_size / 1024, 2 ); return $sie_kb; } private function aabcore_cache_file_cache() { $h_url= isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : ''; $url_path = trim( wp_parse_url( $h_url, PHP_URL_PATH ), '/' ); if ( $url_path === '' ) $url_path = 'index'; $this->url_path=$url_path; $file_name = sanitize_title_with_dashes( $url_path ) . '.html'; return trailingslashit( $this->cache_dir ) . $file_name; } public function aabcore_d_cache_d($url) { $submitted_url = esc_url_raw( $url ); $parsed = wp_parse_url( $submitted_url ); $path = $parsed['path'] ?? ''; $query = isset($parsed['query']) ? '?' . $parsed['query'] : ''; $url_path= $path . $query; if ($url_path === '/' ) $url_path = 'index'; $file_name = sanitize_title_with_dashes( $url_path ) . '.html'; $rJ_cache_cdn= trailingslashit( $this->cache_dir ) . $file_name; wp_delete_file($rJ_cache_cdn); return $url_path; } public function aabcore_cache_c_cache() { $cache_c_cache= new Aabcore_cache_on_cache(); if ($cache_c_cache->Aabcore_cache_ona_cache() <> "") { $d_cache = json_decode($cache_c_cache->Aabcore_cache_ona_cache(), true); } return $d_cache[3]['t_cache_cdn']; } public function aabcore_cache_dkpl_cache() { $filet = glob( $this->cache_dir . '*.*' ); foreach ( $filet as $filed ) { wp_delete_file( $filed ); } } public function aabcore_cache_dkth_cache() { $filet = glob( $this->cache_dir . '*.*' ); foreach ( $filet as $filed ) { wp_delete_file( $filed ); } } public function aabcore_cache_de_cache() { $filet = glob( $this->cache_dir . '*.*' ); foreach ( $filet as $filed ) { wp_delete_file( $filed ); } } private $start_time; private $end_time; public function aabcore_cache_ifex_cache() { WP_Filesystem(); global $wp_filesystem; $this->cache_file = $this->aabcore_cache_file_cache(); $cache_lifetime = get_option('cache_ti_cdn_cache'); // 1 hour if ( $wp_filesystem->exists( $this->cache_file ) ) { $filemtime = filemtime( $this->cache_file ); // This is okay for metadata if ( ( time() - $filemtime ) < $cache_lifetime ) { $file=$this->cache_file; $Maxage =$cache_lifetime; $fa_cache=new Aabcore_cache_on_cache(); $fa_cache->aabcore_cache_he_cache( $file, $Maxage, $filemtime ); if(get_option('aabcore_cache_eig_cache_cdn_cache')===$this->ob_cache_eig_cache){ } $html = $wp_filesystem->get_contents( $this->cache_file ); if ( $html ) { $aabcore_cache_my_cache_p_cache_lug_cache= get_option('aabcore_cache_my_cache_p_cache_lug', 'off'); // default 'off' echo wp_kses($html, $this->aabcore_cache_allowed_cache_htmltag_cache()); exit; } } } } public function aabcore_start_buffer() { ob_start( array( $this, 'aabcore_capture_output' ) ); } public function aabcore_capture_output( $html ) { $this->buffer = $html; if ( ! headers_sent() ) { //header( 'X-Cache: MISS' ); } return $html; } public function aabcore_cache_allowed_cache_htmltag_cache() { $aabcore_cache_allow_cache_html_cache= wp_kses_allowed_html('post'); $aabcore_cache_allow_cache_html_cache['script'] = [ 'src' => true, 'type' => true, 'async' => true, 'defer' => true, 'id' => true, ]; $aabcore_cache_allow_cache_html_cache['link'] = [ 'rel' => true, 'href' => true, 'type' => true, ]; $aabcore_cache_allow_cache_html_cache['meta'] = [ 'name' => true, 'content' => true, 'charset' => true, ]; $aabcore_cache_allow_cache_html_cache['style'] = [ 'type' => true, 'media' => true, 'id' => true, 'css' => true, ]; // Allow $aabcore_cache_allow_cache_html_cache['html'] = array( 'lang' => array(), 'dir' => array()); $aabcore_cache_allow_cache_html_cache['head'] = array(); $aabcore_cache_allow_cache_html_cache['body'] = array( 'class' => array(), 'style' => true, 'id' => true, ); $tags= array('circle','image','div','span','p','a','ul','li','hr','br','strong','em','img','nav','ol','header','center','footer','label','button','svg','path','iframe','main','h1','h2','h3','h4','h5','h6','time','symbol','table','td','tr','a','abbr','acronym','address','applet','area','article','aside','audio','b','base','basefont','bdi','bdo','big','blockquote','body','br','button','canvas','caption','center','cite','code','col','colgroup','data','datalist','dd','del','details','dfn','dialog','dir','div','dl','dt','em','embed','fieldset','figcaption','figure','font','footer','form','frame','frameset','h1','h6','head','header','hgroup','hr','html','i','iframe','img','input','ins','kbd','label','legend','li','link','main','map','mark','menu','meta','meter','nav','noframes','noscript','object','ol','optgroup','option','output','p','param','picture','pre','progress','q','rp','rt','ruby','s','samp','script','search','section','select','small','source','span','strike','strong','style','sub','summary','sup','svg','table','tbody','td','template','textarea','tfoot','th','thead','time','title','tr','track','tt','u','ul','var','video','wbr'); //$tags= array('div','a','p','img','button','h1','h2','h3','ul','ol','li','svg','nav','hr','br','main','header','path','label','form','footer','img','strong','em','iframe','input','textarea','table','tr','td','i'); foreach ($tags as $tag) { if (!isset($aabcore_cache_allow_cache_html_cache[$tag])) { $aabcore_cache_allow_cache_html_cache[$tag] = array(); } $aabcore_cache_allow_cache_html_cache[$tag] = array( 'href' => true, 'title' => true, 'class' => true, 'target' => true, 'rel' => true, 'role' => true, 'aria' => true, 'aria-*' => true, 'id' => true, 'style' => true, 'for' => true, 'src' => true, 'data' => true, 'data-*' => true, 'width' => true, 'height' => true, 'd' => true, 'xmlns' => true, 'viewBox' => true, 'tabindex' => true, 'focusable' => true, 'datetime' => true, 'current' => true, 'tabindex' => true, 'value' => true, 'type' => true, 'method' => true, 'action' => true, 'target' => true, 'hidden' => true, 'name' => true, 'current' => true, 'stroke' => true, 'stroke-*' => true, 'media' => true, '@media' => true, 'label' => true, 'decoding' => true, 'loading' => true, 'srcset' => true, 'xmlns' => true, 'cx' => true, 'cy' => true, 'r' => true, 'alt' => true, 'values' => true, 'display' => true, 'sizes' => true, 'alt' => true, 'picture' => true, 'image' => true, 'img' => true, );} return $aabcore_cache_allow_cache_html_cache; } public function aabcore_cache_on_shutdown() { if ( ob_get_level() > 0 ) { ob_end_flush(); } if ( empty( $this->buffer ) ) return; WP_Filesystem(); global $wp_filesystem; $this->cache_file = $this->aabcore_cache_file_cache(); $existing_files = glob( $this->cache_dir . '*.html' ); $to_cache_to_cache_cdn = count( $existing_files ); if ( $to_cache_to_cache_cdn >= $this->aabcore_cache_c_cache() ) { return; } if(get_option('aabcore_cache_one_cache_cdn_cache')!==$this->fi_cache_one_cache){ return; } if ( is_404() ) { return; } $upload_dir = wp_upload_dir(); $base_url = $upload_dir['baseurl']; $cache_min = new Aabcore_cache_on_cache(); $lu_cache_lu_cache = $cache_min->aabcore_min_cache_html_cache($this->buffer) . "\n" . ''; //------------------------ //$lu_cache_lu_cache=str_replace($this->aabcorecss,'',$lu_cache_lu_cache_a); $wp_filesystem->put_contents($this->cache_file, $lu_cache_lu_cache, FS_CHMOD_FILE ); //$wp_filesystem->put_contents(str_replace('.html','.css',$this->cache_file), $this->aabcorecss, FS_CHMOD_FILE ); } }new Aabcore_CACHE_Fa_Cache();