'areview_top_rated_posts_widget', 'description' => __( 'Display your top rated posts/products', 'areview') ); parent::WP_Widget(false, $name = __('aReview: Top rated', 'areview'), $widget_ops); $this->alt_option_name = 'areview_top_rated_posts_widget'; 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') ); } // widget form creation function form($instance) { // Check values $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; ?>
flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['areview_top_rated_posts']) ) delete_option('areview_top_rated_posts'); return $instance; } function flush_widget_cache() { wp_cache_delete('areview_top_rated_posts', 'widget'); } // display widget function widget($args, $instance) { $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'areview_top_rated_posts', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); extract($args); $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Top rated', 'areview' ); /** This filter is documented in wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; if ( ! $number ) $number = 3; ?> get_results("SELECT v.overall_rating, v.post_id FROM " . YASR_VOTES_TABLE . " AS v, $wpdb->posts AS p WHERE v.post_id = p.ID AND p.post_status = 'publish' ORDER BY v.overall_rating DESC, v.id ASC LIMIT $number"); if ($query_result) { echo "