model = $wtpsw_model;
// Widget settings
$widget_ops = array( 'classname' => 'wtpsw_post_list_widget', 'description' => __( 'Display most popular trending post on your blog.', 'wtpsw' ));
// Create the widget
parent::__construct( 'wtpsw-post-list-widget', __( 'Trending Posts', 'wtpsw' ), $widget_ops );
$this->defaults = array(
'title' => __( 'Trending Posts', 'wtpsw' ),
'limit' => 5,
'post_type' => 'post',
'show_content' => 0,
'show_thumb' => 1,
'show_author' => 1,
'show_date' => 1,
'order' => 'DESC',
'view_by' => 'views',
'content_length' => 20,
'show_comment_count' => 1,
'hide_empty_comment_count' => 0,
);
}
/**
* Updates the widget control options
*
* @since 1.0.0
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Input fields
$instance['post_type'] = $new_instance['post_type'];
$instance['view_by'] = $new_instance['view_by'];
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['limit'] = ( empty($new_instance['limit'] ) || ( $new_instance['limit'] < -1 ) ) ? 5 : $new_instance['limit'];
$instance['order'] = ( isset( $new_instance['order'] ) && $new_instance['order'] == 'ASC' ) ? 'ASC' : 'DESC';
$instance['content_length'] = ! empty( $new_instance['content_length'] ) ? $new_instance['content_length'] : 20;
$instance['show_author'] = ! empty( $new_instance['show_author'] ) ? 1 : 0;
$instance['show_comment_count'] = ! empty( $new_instance['show_comment_count'] ) ? 1 : 0;
$instance['show_content'] = ! empty( $new_instance['show_content'] ) ? 1 : 0;
$instance['show_thumb'] = ! empty( $new_instance['show_thumb'] ) ? 1 : 0;
$instance['show_date'] = ! empty( $new_instance['show_date'] ) ? 1 : 0;
$instance['hide_empty_comment_count'] = ! empty( $new_instance['hide_empty_comment_count'] ) ? 1 : 0;
return $instance;
}
/**
* Displays the widget form in widget area
*
* @since 1.0.0
*/
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
$post_types = wtpsw_get_post_types();
$support_post_types = wtpsw_get_option( 'post_types', array() );
$sel_post_type = ( ! empty( $instance['post_type'] ) && in_array( $instance['post_type'], $support_post_types ) ) ? $instance['post_type'] : '';
?>
".esc_html__('Trending Post List - Widget', 'wtpsw')."
WPOS - Trending Post List";
return;
}
global $wtpsw_options, $wtpsw_view_by;
$instance = wp_parse_args( (array) $instance, $this->defaults );
extract( $args );
$prefix = WTPSW_META_PREFIX;
$support_post_types = wtpsw_get_option('post_types', array());
$title = $instance['title'];
$limit = $instance['limit'];
$order = $instance['order'];
$view_by = $instance['view_by'];
$content_length = $instance['content_length'];
$post_type = ( ! empty( $instance['post_type'] ) && in_array( $instance['post_type'], $support_post_types ) ) ? $instance['post_type'] : '';
$post_type = ( ! empty( $instance['post_type'] ) && in_array( $instance['post_type'], $support_post_types ) ) ? $instance['post_type'] : '';
$show_date = ! empty( $instance['show_date'] ) ? true : false;
$show_author = ! empty( $instance['show_author'] ) ? true : false;
$show_comment_count = ! empty( $instance['show_comment_count'] ) ? true : false;
$show_thumb = ! empty( $instance['show_thumb'] ) ? true : false;
$show_content = ! empty( $instance['show_content'] ) ? true : false;
$hide_empty_comment_count = ! empty( $instance['hide_empty_comment_count'] ) ? true : false;
// If no valid post type is found
if( empty( $post_type ) ) {
return;
}
// Order By
if( $view_by == 'comment' ) {
$orderby = 'comment_count';
} elseif ( $view_by == 'views' ) {
$orderby = 'meta_value_num';
}
$wtpsw_view_by = $orderby; // Assign to global variable for query filter
$post_args = array(
'post_type' => $post_type,
'posts_per_page' => $limit,
'order' => $order,
'orderby' => $orderby
);
if( $view_by == 'views' ) {
$post_args['meta_key'] = $prefix.'views';
}
// Filter to change query where condition
add_filter( 'posts_where', array( $this->model, 'wtpsw_query_where' ));
// Query to get post
$wtpsw_posts = $this->model->wtpsw_get_posts( $post_args );
// Remove Filter for change query where condition
remove_filter( 'posts_where', array( $this->model, 'wtpsw_query_where' ));
echo $before_widget;
if ( $title ) {
echo $before_title . wp_kses_post($title) . $after_title;
}
if( $wtpsw_posts->have_posts() ) : ?>
have_posts() ) : $wtpsw_posts->the_post();
global $post;
$wtpsw_stats = array(); // Need to flush
$comment_text = wtpsw_get_comments_number( $post->ID, $hide_empty_comment_count );
// Design file
include( WTPSW_DIR . '/templates/wtpsw-post-lists.php' );
endwhile; ?>