'widget-vt-blogging-popular widget_posts_thumbnail', 'description' => esc_html__('A widget that displays popular posts by comments with thumbnails.', 'vt-blogging') ); // Create the widget. parent::__construct( 'vt-blogging-popular', // $this->id_base __( '[VT] Popular Posts', 'vt-blogging' ), // $this->name $widget_ops // $this->widget_options ); // Flush the transient. add_action( 'save_post' , array( $this, 'flush_widget_transient' ) ); add_action( 'deleted_post', array( $this, 'flush_widget_transient' ) ); add_action( 'switch_theme', array( $this, 'flush_widget_transient' ) ); } /** * Outputs the widget based on the arguments input through the widget controls. * * @since 1.0.0 */ function widget( $args, $instance ) { extract( $args ); // Output the theme's $before_widget wrapper. echo $before_widget; // If the title not empty, display it. if ( $instance['title'] ) { echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title; } // Display the popular posts. if ( false === ( $popular = get_transient( 'vt_blogging_popular_widget_' . $this->id ) ) ) { // Posts query arguments. $args = array( 'post_type' => 'post', 'posts_per_page' => $instance['limit'], 'orderby' => 'comment_count' ); // The post query $popular = new WP_Query( $args ); // Store the transient. set_transient( 'vt_blogging_popular_widget_' . $this->id, $popular ); } global $post; if ( $popular->have_posts() ) { echo ''; } // Reset the query. wp_reset_postdata(); // Close the theme's widget wrapper. echo $after_widget; } /** * Updates the widget control options for the particular instance of the widget. * * @since 1.0.0 */ function update( $new_instance, $old_instance ) { $instance = $new_instance; $instance['title'] = wp_strip_all_tags ( $new_instance['title'] ); $instance['limit'] = (int) $new_instance['limit']; $instance['show_views'] = isset( $new_instance['show_views'] ) ? (bool) $new_instance['show_views'] : false; // Delete our transient. $this->flush_widget_transient(); return $instance; } /** * Flush the transient. * * @since 1.0.0 */ function flush_widget_transient() { delete_transient( 'vt_blogging_popular_widget_' . $this->id ); } /** * Displays the widget control options in the Widgets admin screen. * * @since 1.0.0 */ function form( $instance ) { // Default value. $defaults = array( 'title' => esc_html__( 'Popular Posts', 'vt-blogging' ), 'limit' => 5, 'show_views' => true ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>

id="get_field_id( 'show_views' ); ?>" name="get_field_name( 'show_views' ); ?>" />