-
-
Save nfsarmento/4f1f33e57334b15ae767f68d8f0d922e to your computer and use it in GitHub Desktop.
Popular Posts Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Popular Posts Widget | |
* | |
* Displays posts in last X days, sorted by most comments (proxy for popularity) | |
* | |
* @package Core Functionality | |
* @author Nuno Sarmento <hello@nuno-sarmento.com> | |
* | |
*/ | |
class NS_Popular_Widget extends WP_Widget { | |
/** | |
* Constructor | |
* | |
* @return void | |
**/ | |
public function __construct() { | |
/* Widget settings. */ | |
$widget_ops = array( 'classname' => 'widget_popular', 'description' => 'Displays most popular posts by comment count' ); | |
/* Widget control settings. */ | |
$control_ops = array( 'id_base' => 'popular-widget' ); | |
//$this->WP_Widget( 'popular-widget', 'Popular Posts', $widget_ops, $control_ops ); | |
parent::__construct( 'popular-widget', 'Popular Posts', $widget_ops, $control_ops ); | |
} | |
// Limit to last 30 days | |
function filter_where( $where = '' ) { | |
// posts in the last 30 days | |
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-' . $instance['days'] .' days')) . "'"; | |
return $where; | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
// @codingStandardsIgnoreStart | |
echo $before_widget; | |
// @codingStandardsIgnoreEd | |
if( !empty( $instance['title'] ) ) echo esc_attr( $before_title ) . esc_attr( $instance['title'] ) . esc_attr( $after_title ); | |
$loop_args = array( | |
'posts_per_page' => (int) $instance['count'], | |
'orderby' => 'comment_count' | |
); | |
if( 0 == $instance['days'] ) { | |
$loop = new WP_Query( $loop_args ); | |
}else{ | |
add_filter( 'posts_where', array( $this, 'filter_where' ) ); | |
$loop = new WP_Query( $loop_args ); | |
remove_filter( 'posts_where', array( $this, 'filter_where' ) ); | |
} | |
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); global $post; | |
if( 0 == $loop->current_post ) | |
echo '<p class="popular-post first">'; | |
else | |
echo '<p class="popular-post">'; | |
echo '<a class="image" href="' . esc_url( get_permalink() ) . '"></a>'; | |
echo '<a class="title" href="' . esc_url( get_permalink() ) . '">' . esc_attr( get_the_title() ) . '</a>'; | |
echo '<br />'; | |
echo '<span class="date">' . get_the_date() . '</span>'; | |
echo '</p>'; | |
endwhile; endif; wp_reset_query(); | |
// @codingStandardsIgnoreStart | |
echo $after_widget; | |
// @codingStandardsIgnoreEnd | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
/* Strip tags (if needed) and update the widget settings. */ | |
$instance['title'] = $new_instance['title']; | |
$instance['count'] = (int) $new_instance['count']; | |
$instance['days'] = (int) $new_instance['days']; | |
return $instance; | |
} | |
function form( $instance ) { | |
/* Set up some default widget settings. */ | |
$defaults = array( 'title' => '', 'count' => 5, 'days' => 30 ); | |
$instance = wp_parse_args( (array) $instance, $defaults ); | |
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Popular Posts', 'nunosarmento' ); | |
$posts = ! empty( $instance['posts'] ) ? $instance['posts'] : esc_html__( '5', 'nunosarmento' ); | |
$days = ! empty( $instance['days'] ) ? $instance['days'] : esc_html__( '30', 'nunosarmento' ); | |
?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'nunosarmento' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"> | |
</p> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>"><?php esc_attr_e( 'Number of Posts:', 'nunosarmento' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts' ) ); ?>" type="text" value="<?php echo esc_attr( $posts ); ?>"> | |
</p> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'days' ) ); ?>"><?php esc_attr_e( 'Posted in the past X days:', 'nunosarmento' ); ?></label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'days' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'days' ) ); ?>" type="text" value="<?php echo esc_attr( $days ); ?>"> | |
</p> | |
<p class="description">Use 0 for no time limit.</p> | |
<?php | |
} | |
} | |
/** Add our function to the widgets_init hook. **/ | |
add_action( 'widgets_init', 'ns_popular_load_widgets' ); | |
function ns_popular_load_widgets() { | |
register_widget( 'NS_Popular_Widget' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment