• Resolved Flector

    (@flector)


    please add support for cache output.
    now i use code:
    [pop]
    in widget with code in functions.php:

    
    function pop_func( $atts ) {
        $args = array(
        'post_type' => 'post',
        'range' => 'all', 
    );
    
    if ( ! $pop = get_transient( 'cache-popular' ) ) {
    
    ob_start();
    wpp_get_mostpopular( $args );
    $pop = ob_get_clean();
    
    set_transient( 'cache-popular', $pop, 8 * HOUR_IN_SECONDS );
    
    }
    
    return $pop;
    }
    add_shortcode( 'pop', 'pop_func' );
    

    i think it’s wrong way.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @flector,

    Did you know that:

    1. WordPress Popular Posts has a built-in shortcode called [wpp]?
    2. WordPress Popular Posts has its own built-in cache mechanism based on transients which also caches the [wpp] shortcode?

    Why are you trying to build a custom shortcode when the plugin already does everything you seem to be trying to do?

    Thread Starter Flector

    (@flector)

    not working cache for me – i try widget and shortcode.

    Plugin Author Hector Cabrera

    (@hcabrera)

    How do you know it’s not working? Did you enable WPP’s Data Caching feature already?

    Thread Starter Flector

    (@flector)

    i use:

    
    function wph_usage() {
        printf(('%d / %s'), get_num_queries(), timer_stop(0, 3));
        if ( function_exists('memory_get_usage') ) echo ' / '
        . round(memory_get_usage()/1024/1024, 2) . ' mb'; 
    }
    

    and code in footer:

    
    <?php if (current_user_can('administrator')){ ?> <?php wph_usage(); ?>
    

    enabling cache – the same number queries.
    after i use own cache code – minus 30 queries.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Alright. This is the source code of the wpp_get_mostpopular() function:

    /**
     * Template tag - gets popular posts.
     *
     * @since	2.0.3
     * @param	mixed	args
     */
    function wpp_get_mostpopular($args = NULL) {
    
        $shortcode = '[wpp';
    
        if ( is_null( $args ) ) {
            $shortcode .= ']';
        } else {
            if( is_array( $args ) ){
                $atts = '';
                foreach( $args as $key => $arg ){
                    if (
                        is_array( $arg )
                        && ( 'post_type' == $key || 'cat' == $key || 'term_id' == $key || 'pid' == $key || 'author' == $key )
                    ) {
                        $arg = array_filter( $arg, 'is_int' );
                        $arg = join( ',', $arg );
                    }
    
                    $atts .= ' ' . $key . '="' . htmlspecialchars($arg, ENT_QUOTES, $encoding = ini_get("default_charset"), false) . '"';
                }
            } else {
                $atts = trim( str_replace( "&", " ", $args  ) );
            }
    
            $shortcode .= ' ' . $atts . ' php=true]';
        }
    
        echo do_shortcode( $shortcode );
    
    }

    If you notice, it’s just a wrapper that calls the [wpp] shortcode.

    Here’s the source code that registers and processes the [wpp] shortcode:

    /**
     * WPP shortcode handler.
     *
     * @since    1.4.0
     * @param    array    $atts    User defined attributes in shortcode tag
     * @return   string
     */
    public function wpp_shortcode( $atts = null ) {
    
        // Previous lines of code removed for brevity!
    
        $cached = false;
    
        // Return cached results
        if ( $this->admin_options['tools']['cache']['active'] ) {
    
            $key = md5( json_encode($shortcode_ops) );
            $popular_posts = WPP_Cache::get( $key );
    
            if ( false === $popular_posts ) {
    
                $popular_posts = new WPP_Query( $shortcode_ops );
    
                $time_value = $this->admin_options['tools']['cache']['interval']['value']; // eg. 5
                $time_unit = $this->admin_options['tools']['cache']['interval']['time']; // eg. 'minute'
    
                WPP_Cache::set(
                    $key,
                    $popular_posts,
                    $time_value,
                    $time_unit
                );
    
            }
    
            $cached = true;
    
        } // Get popular posts
        else {
            $popular_posts = new WPP_Query( $shortcode_ops );
        }
    
        $output = new WPP_Output( $popular_posts->get_posts(), $shortcode_ops );
    
        $shortcode_content .= $output->get_output();
    
        return $shortcode_content;
    
    }

    If Data Caching is enabled, then the plugin check if there’s a transient set already. If so, that’s what the plugin will use to render the popular posts. If there isn’t a transient set, then WPP does a new query and store its results in a transient (WPP_Cache::set(...)). In case you’re curious, here’s the source code of the WPP_Cache class.

    As you can see, the plugin is doing exactly the same thing you’re trying to do here. Don’t see how your code is any different (in terms of logic) from what WPP already does.

    If you still prefer doing it your way and don’t mind duplicating some code, this is what you need:

    function pop_func( $atts ) {
        $args = array(
            'post_type' => 'post',
            'range' => 'all', 
        );
    
        if ( ! $pop = get_transient( 'cache-popular' ) ) {
            $shortcode = '[wpp';
    
            if ( is_null( $args ) ) {
                $shortcode .= ']';
            } else {
                if( is_array( $args ) ){
                    $atts = '';
                    foreach( $args as $key => $arg ){
                        if (
                            is_array( $arg )
                            && ( 'post_type' == $key || 'cat' == $key || 'term_id' == $key || 'pid' == $key || 'author' == $key )
                        ) {
                            $arg = array_filter( $arg, 'is_int' );
                            $arg = join( ',', $arg );
                        }
    
                        $atts .= ' ' . $key . '="' . htmlspecialchars($arg, ENT_QUOTES, ini_get("default_charset"), false) . '"';
                    }
                } else {
                    $atts = trim( str_replace( "&", " ", $args  ) );
                }
    
                $shortcode .= ' ' . $atts . ' php=true]';
            }
    
            $pop = do_shortcode( $shortcode );
    
            set_transient( 'cache-popular', $pop, 8 * HOUR_IN_SECONDS );
        }
    
        return $pop;
    }
    add_shortcode( 'pop', 'pop_func' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘output cashe’ is closed to new replies.