• Resolved Marco

    (@marcoherzog)


    I am using your plugin for a while now and it works really great. I am using the function wpfc_clear_post_cache_by_id( $post_id ) to delete the single cache for various post types, custom archive pages and so on after publishing or altering meta data.

    Today I realized that this function also clears the frontpage cache by calling deleteHomePageCache() within the function singleDeleteCache(). Is there an option or workaround that I might have overseen to avoid this?

    (One of our websites has quite an expensive frontpage on one side and permanent editing of other post types that are not related to the frontpage on the other side.)

    Any help is very welcome.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Emre Vona

    (@emrevona)

    the post may appear on the homepage so the homepage cache is cleared.
    and you cannot avoid it unfortunately.

    Thread Starter Marco

    (@marcoherzog)

    I added a class to be able to do that with na_wpfc_clear_post_cache_by_id($post_id). Actually just copied yours and made some changes. (yet kept author page, cats, tags, …)
    Also limited the deletion of subdirectories to those with integer or page as name.
    I think it might be worth adding a similar feature.

    
    /**
     * Extend class WpFastestCache to not delete frontpage cache on single page cache delete
     */
    if ( class_exists( 'WpFastestCache' ) ) {
    
        class NaWpFastestCache extends WpFastestCache  {
    
            function __construct() {
                //parent::__construct();
                add_action("na_wpfc_clear_post_cache_by_id", array($this, 'naSingleDeleteCache'), 10, 2);
            }
    
            public function na_rm_folder_recursively($dir, $i = 1) {
                if(is_dir($dir)){
                    $files = @scandir($dir);
                    foreach((array)$files as $file) {
                        if($i > 50 && !preg_match("/wp-fastest-cache-premium/i", $dir)){
                            return true;
                        }else{
                            $i++;
                        }
                        //error_log('$file1 ' . $file);
                        if ('.' === $file || '..' === $file) continue; // edited to continue if $file is NOT 'page' or integer
                        if (is_dir("$dir/$file")){
                            if ('page' !== $file && strval($file) !== strval(intval($file)) ) continue; // edited: 'continue' if $file is NOT 'page' or integer
                            $this->na_rm_folder_recursively("$dir/$file", $i);  // edited to not delete complete directory with subfolder content.
                        }else{
                            if(file_exists("$dir/$file")){
                                //error_log('unlink ' . $dir . '/' . $file);
                                @unlink("$dir/$file");
                            }
                        }
                    }
                }
    
                if(is_dir($dir)){
                    $files_tmp = @scandir($dir);
    
                    if(!isset($files_tmp[2]) && false){
                        @rmdir($dir);
                    }
                }
    
                return true;
            }
    
            public function naSingleDeleteCache($comment_id = false, $post_id = false){
                //error_log('naSingleDeleteCache');
                include_once( WP_PLUGIN_DIR . '/wp-fastest-cache/inc/cdn.php');
                CdnWPFC::cloudflare_clear_cache();
    
                $to_clear_parents = true;
                $to_clear_feed = true;
    
                // not to clear cache of homepage/cats/tags after ajax request by other plugins
                if(isset($_POST) && isset($_POST["action"])){
                    // kk Star Rating
                    if($_POST["action"] == "kksr_ajax"){
                        $to_clear_parents = false;
                    }
    
                    // All In One Schema.org Rich Snippets
                    if(preg_match("/bsf_(update|submit)_rating/i", $_POST["action"])){
                        $to_clear_parents = false;
                    }
    
                    // Yet Another Stars Rating
                    if($_POST["action"] == "yasr_send_visitor_rating"){
                        $to_clear_parents = false;
                        $post_id = $_POST["post_id"];
                    }
    
                    // All In One Schema.org Rich Snippets
                    if(preg_match("/bsf_(update|submit)_rating/i", $_POST["action"])){
                        $to_clear_feed = false;
                    }
                }
    
                if($comment_id){
                    $comment_id = intval($comment_id);
    
                    $comment = get_comment($comment_id);
    
                    if($comment && $comment->comment_post_ID){
                        $post_id = $comment->comment_post_ID;
                    }
                }
    
                if($post_id){
                    $post_id = intval($post_id);
    
                    $permalink = get_permalink($post_id);
    
                    $permalink = urldecode(get_permalink($post_id));
    
                    //for trash contents
                    $permalink = rtrim($permalink, "/");
                    $permalink = preg_replace("/__trashed$/", "", $permalink);
                    //for /%postname%/%post_id% : sample-url__trashed/57595
                    $permalink = preg_replace("/__trashed\/(\d+)$/", "/$1", $permalink);
    
                    if(preg_match("/https?:\/\/[^\/]+\/(.+)/", $permalink, $out)){
                        $path = $this->getWpContentDir("/cache/all/").$out[1];
                        //error_log('$path: ' . $path);
                        $mobile_path = $this->getWpContentDir("/cache/wpfc-mobile-cache/").$out[1];
    
                        if($this->isPluginActive("wp-fastest-cache-premium/wpFastestCachePremium.php")){
                            include_once $this->get_premium_path("logs.php");
                            $log = new WpFastestCacheLogs("delete");
                            $log->action();
                        }
    
                        $files = array();
    
                        if(is_dir($path)){
                            array_push($files, $path);
                        }
    
                        if(is_dir($mobile_path)){
                            array_push($files, $mobile_path);
                        }
    
                        if(defined('WPFC_CACHE_QUERYSTRING') && WPFC_CACHE_QUERYSTRING){
                            $files_with_query_string = glob($path."\?*");
                            $mobile_files_with_query_string = glob($mobile_path."\?*");
    
                            if(is_array($files_with_query_string) && (count($files_with_query_string) > 0)){
                                $files = array_merge($files, $files_with_query_string);
                            }
    
                            if(is_array($mobile_files_with_query_string) && (count($mobile_files_with_query_string) > 0)){
                                $files = array_merge($files, $mobile_files_with_query_string);
                            }
                        }
    
                        if($to_clear_feed){
                            // to clear cache of /feed
                            if(preg_match("/https?:\/\/[^\/]+\/(.+)/", get_feed_link(), $feed_out)){
                                array_push($files, $this->getWpContentDir("/cache/all/").$feed_out[1]);
                            }
    
                            // to clear cache of /comments/feed/
                            if(preg_match("/https?:\/\/[^\/]+\/(.+)/", get_feed_link("comments_"), $comment_feed_out)){
                                array_push($files, $this->getWpContentDir("/cache/all/").$comment_feed_out[1]);
                            }
                        }
    
                        foreach((array)$files as $file){
                            //error_log('remove ' . $file);
                            $this->na_rm_folder_recursively($file); // edited to not delete complete directory with subcontent.
                        }
                    } else {
                        // to clear cache of homepage
                        $this->deleteHomePageCache();
                    }
    
                    if($to_clear_parents && false){
                        // to clear cache of homepage
                        //$this->deleteHomePageCache();  // commented to not delete frontpage
    
                        // to clear cache of author page
                        $this->delete_author_page_cache($post_id);
    
                        // to clear sitemap cache
                        $this->delete_sitemap_cache();
    
                        // to clear cache of cats and  tags which contains the post (only first page)
                        global $wpdb;
                        $terms = $wpdb->get_results("SELECT * FROM <code>&quot;.$wpdb->prefix.&quot;term_relationships</code> WHERE <code>object_id</code>=".$post_id, ARRAY_A);
    
                        foreach ($terms as $term_key => $term_val){
                            $this->delete_cache_of_term($term_val["term_taxonomy_id"]);
                        }
                    }
    
                    $this->delete_multiple_domain_mapping_cache();
                }
            }
    
        }
    
        function na_wpfc_clear_post_cache_by_id($post_id = false){
            if($post_id){
                do_action("na_wpfc_clear_post_cache_by_id", false, $post_id);
                //error_log('do_action na_wpfc_clear_post_cache_by_id: ' . $post_id);
            }
        }
    
        new NaWpFastestCache();
    }
    
    
    • This reply was modified 4 years, 8 months ago by Marco.
    • This reply was modified 4 years, 8 months ago by Marco.
    • This reply was modified 4 years, 8 months ago by Marco.
    Plugin Author Emre Vona

    (@emrevona)

    haha that’s nice ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Why deleteHomePageCache() within wpfc_clear_post_cache_by_id?’ is closed to new replies.