WordPress.org

Plugin Directory

Changeset 750275


Ignore:
Timestamp:
08/02/13 20:57:27 (9 months ago)
Author:
donncha
Message:

Added function "wpsc_post_transition" that deletes the post, tag and category cache when a post changes from draft to publish or publish to draft. Props @Biranit http://wordpress.org/support/topic/wp-super-cache-do-not-delete-category-pages-when-new-post-is-posted?replies=8#post-4467574

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-super-cache/trunk/wp-cache-phase2.php

    r736205 r750275  
    4242        add_filter( 'supercache_filename_str', 'wp_cache_check_mobile' ); 
    4343        add_action( 'wp_cache_gc_watcher', 'wp_cache_gc_watcher' ); 
     44        add_action( 'transition_post_status', 'wpsc_post_transition', 10, 3 ); 
    4445 
    4546        do_cacheaction( 'add_cacheaction' ); 
     
    974975            prune_super_cache( $cache_path . 'blogs/', true ); 
    975976        } 
     977    } 
     978} 
     979 
     980function wpsc_delete_cats_tags( $post ) { 
     981    $post = get_post($post); 
     982    $categories = get_the_category($post->ID); 
     983    if ( $categories ) { 
     984        $category_base = get_option( 'category_base'); 
     985        if ( $category_base == '' ) 
     986            $category_base = '/category/'; 
     987        $category_base = trailingslashit( $category_base ); // paranoid much? 
     988        foreach ($categories as $cat) { 
     989            prune_super_cache ( get_supercache_dir() . $category_base . $cat->slug . '/', true ); 
     990            if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wpsc_post_transition: deleting category: " . get_supercache_dir() . $category_base . $cat->slug . '/' ); 
     991        } 
     992    } 
     993    $posttags = get_the_tags($post->ID); 
     994    if ( $posttags ) { 
     995        $tag_base = get_option( 'tag_base' ); 
     996        if ( $tag_base == '' ) 
     997            $tag_base = '/tag/'; 
     998        $tag_base = trailingslashit( str_replace( '..', '', $tag_base ) ); // maybe! 
     999        foreach ($posttags as $tag) { 
     1000            prune_super_cache( get_supercache_dir() . $tag_base . $tag->slug . '/', true ); 
     1001            if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wpsc_post_transition: deleting tag: " . get_supercache_dir() . $tag_base . $tag->slug . '/' ); 
     1002        } 
     1003    } 
     1004} 
     1005 
     1006function wpsc_post_transition( $new_status, $old_status, $post ) { 
     1007    if ( 
     1008        ($old_status == 'publish' && $new_status != 'publish' ) // post unpublished 
     1009        || 
     1010        ($old_status != 'publish' && $new_status == 'publish') // post published 
     1011    ) { 
     1012        wpsc_delete_cats_tags( $post ); 
     1013        prune_super_cache( get_supercache_dir() . '/' . $post->post_name . '/', true ); 
     1014        if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wpsc_post_transition: deleting post: " . get_supercache_dir() . '/' . $post->post_name . '/' ); 
    9761015    } 
    9771016} 
Note: See TracChangeset for help on using the changeset viewer.