'widget_recently_viewed_products', 'description' => __( 'A list of your customers most recently viewed products', 'jigoshop' ) ); // Create the widget parent::__construct( 'recently_viewed_products', __( 'Jigoshop: Recently Viewed', 'jigoshop' ), $options ); // Flush cache after every save add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) ); // Attach the tracker to the product view action add_action( 'jigoshop_before_single_product', array( &$this, 'jigoshop_product_view_tracker' ), 10, 2 ); } /** * Widget * * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget( $args, $instance ) { // Get the most recently viewed products from the cache $cache = wp_cache_get( 'widget_recently_viewed_products', 'widget' ); // If no entry exists use array if ( ! is_array( $cache ) ) { $cache = array(); } // If cached get from the cache if ( isset( $cache[$args['widget_id']] ) ) { echo $cache[$args['widget_id']]; return false; } // Check if session contains recently viewed products if ( empty( jigoshop_session::instance()->recently_viewed_products ) ) return false; // Start buffering the output ob_start(); extract($args); // Set the widget title $title = apply_filters( 'widget_title', ($instance['title']) ? $instance['title'] : __('Recently Viewed Products', 'jigoshop'), $instance, $this->id_base ); // Set number of products to fetch if( ! $number = absint( $instance['number'] ) ) { $number = 5; } // Set up query $query_args = array( 'posts_per_page' => $number, 'post_type' => 'product', 'post_status' => 'publish', 'nopaging' => true, 'post__in' => jigoshop_session::instance()->recently_viewed_products, 'orderby' => 'date', // TODO: Not ideal as it doesn't order latest first 'meta_query' => array( array( 'key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN', ), ) ); // Run the query $q = new WP_Query( $query_args ); if( $q->have_posts() ) { // Print the widget wrapper & title echo $before_widget; echo $before_title . $title . $after_title; // Open the list echo '
'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } // Flush output buffer and save to cache $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set( 'widget_recent_products', $cache, 'widget' ); } public function update( $new_instance, $old_instance ) { $instance = $old_instance; // Save the new values $instance['title'] = strip_tags( $new_instance['title'] ); $instance['number'] = absint( $new_instance['number'] ); // Flush the cache $this->flush_widget_cache(); // Unset the session array unset( jigoshop_session::instance()->recently_viewed_products ); // Remove the cache entry from the options array $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset( $alloptions['widget_recently_viewed_products'] ) ) { delete_option( 'widget_recently_viewed_products' ); } return $instance; } /** * Flush Widget Cache * * Flushes the cached output */ public function flush_widget_cache() { wp_cache_delete( 'widget_recently_viewed_products', 'widget' ); } /** * Logs viewed products into the session * * @return void **/ public function jigoshop_product_view_tracker( $post, $_product ) { $instance = get_option('widget_recently_viewed_products'); $number = 0; if ( ! empty( $instance )) foreach ( $instance as $index => $entry ) { if ( is_array( $entry )) foreach ( $entry as $key => $value ) { if ( $key == 'number' ) { $number = $value; break; } } } if ( ! $number ) return false; // stop the show! // Check if we already have some data if ( ! is_array( jigoshop_session::instance()->recently_viewed_products ) ) { $viewed = array(); jigoshop_session::instance()->recently_viewed_products = $viewed; } // If the product isn't in the list, add it if ( ! in_array( $post->ID, jigoshop_session::instance()->recently_viewed_products) ) { $viewed = jigoshop_session::instance()->recently_viewed_products; $viewed[] = $post->ID; jigoshop_session::instance()->recently_viewed_products = $viewed; } if ( sizeof( jigoshop_session::instance()->recently_viewed_products) > $number ) { $viewed = jigoshop_session::instance()->recently_viewed_products; array_shift( $viewed ); jigoshop_session::instance()->recently_viewed_products = $viewed; } } /** * Form * * Displays the form for the wordpress admin * * @param array instance */ public function form( $instance ) { // Get instance data $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; // Widget Title echo ""; // Number of posts to fetch echo "
"; } }