Frumph / comic-easel

Comic Easel is a plugin for WordPress that let's you have a WebComic with any WordPress theme.
http://comiceasel.com
21 stars 13 forks source link

Conflict with W3 Total Cache in ceo_get_adjacent_chapter() #18

Closed egypturnash closed 8 years ago

egypturnash commented 8 years ago

When calling ceo_get_adjacent_chapter() a second time in my chapter-oriented custom theme, current_chapter->menu_order is empty if W3 Total Cache's object caching is turned on.

This version of ceo_get_adjacent_chapter() fixes this by explicitly caching current_chapter->menu_order, and doesn't seem to have any side effects.

function ceo_get_adjacent_chapter($prev = false) {
    global $post;

    $current_chapter = get_the_terms($post->ID, 'chapters');
    if (is_array($current_chapter)) { $current_chapter = reset($current_chapter); } else { return; }

    // cache the calculation of the desired chapter to work around a weird intermittent bug with w3 total cache's object cache
    $current_order = wp_cache_get( 'ceo_current_order_'.$current_chapter->slug );
    if ( false === $current_order ) {
        $current_order = $current_chapter->menu_order;
        wp_cache_set( 'ceo_current_order_'.$current_chapter->slug, $current_order );
    } 

    $find_order = (bool)$prev ? $current_order - 1 : $current_order + 1;

    if (!$find_order) return false;
    $args = array(
            'orderby' => 'menu_order',
            'order' => 'DESC',
            'hide_empty' => 1,
            'menu_order' => $find_order
            );
    $all_chapters = get_terms( 'chapters', $args );
    if (!is_null($all_chapters)) {
        foreach($all_chapters as $chapter) {
            if ((int)$chapter->menu_order == $find_order) return $chapter;
        }
    }
    return false;
}
Frumph commented 8 years ago

add/replacing with current, https://github.com/Frumph/comic-easel/commit/c49a9ece1f813b7b33b4f7f5051440347e29a848

added egypturnash's github account as collaborator so they can make commits themself