Skip to content

Instantly share code, notes, and snippets.

@abouolia
Created March 9, 2016 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abouolia/7b7235c71aefbd26ef74 to your computer and use it in GitHub Desktop.
Save abouolia/7b7235c71aefbd26ef74 to your computer and use it in GitHub Desktop.
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
/**
* Sort Recent Posts Widget.
*
* @since PostVision 0.1.0
* @package PostVision
* @subpackage Sort Recent Posts Widget / PostVision Widgets
* @author Ahmed Bouhuolia <a.bouhuolia@gmail.com>
* @copyright Copyright (c) 2015, Customattic.
* @link http://groundwork.customattic.io
*/
class Customatic_Widget_Sort_Recent_Posts extends WP_Widget{
/**
* Unique identifier for the widgets.
* @since PostVision 0.1.0
* @var string
*/
protected $widget_slug = 'widget-cu-sort-recent-posts';
/**
* Specifies the classname and description, instantiates the widget,
* loads localization files.
*
* @since PostVision 0.1.0
* @return void
*/
public function __construct() {
parent::__construct(
$this->get_widget_slug(),
_x('Customatic - Sort Recent Posts', 'widget_sort_posts', 'PostVision'),
array(
'classname' => $this->widget_slug,
'description' => __('Short description of the widget goes here.', 'PostVision')
)
);
// Refreshing the widget's cached output with each new post
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' ) );
} // End constructor
/**
* Return the widget slug.
*
* @return Plugin slug variable.
*/
public function get_widget_slug() {
return $this->widget_slug;
}
public function flush_widget_cache() {
wp_cache_delete( $this->get_widget_slug(), 'widget' );
}
/**
* Retrieve the most recent posts with spacific count of posts.
*
* @since PostVision 0.1.0
* @param int $num_posts Number of posts allowed to display
* @param bool $show_thumb Allows to display thumbnail of post or no.
* @access public
* @return void
*/
function widget( $args, $instance ) {
extract( $args );
// Our variables from the widget settings.
$title = apply_filters('widget_title', $instance['title'] );
$sort_by = isset( $instance['sort_by'] ) ? $instance['sort_by'] : false;
$number_posts = isset( $instance['number_posts']) ? intval($instance['number_posts']) : false;
$categories = isset( $instance['categories'] ) ? $instance['categories'] : false;
$tags = isset( $instance['tags'] ) ? $instance['tags'] : false;
$show_meta = $instance['show_meta'];
/**
* Filter the arguments for the Recent Posts widget.
*
* @since 3.4.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
*/
$recent_posts = new WP_Query( apply_filters( 'customatic_widget_sort_recent_posts_args', array(
'showposts' => $number_posts,
'post_status' => 'publish',
'post_type' => 'post',
'cat' => $category_ID,
'tag__in' => $tag_ID,
'orderby' => $orderby ) ) );
if ( $recent_posts->have_posts() ) : ?>
<?php echo $before_widget; ?>
<?php if ( $title ){ echo $before_title . $title . $after_title; } ?>
<ul class="post-list-widget">
<?php while( $recent_posts->have_posts($args) ) : $recent_posts->the_post(); ?>
<li id="post-<?php the_ID(); ?>" class="item-list">
<div class="content">
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php if( $show_meta ) : ?>
<div class="entry-meta">
<?php postvision_post_format(array('before' => '<span class="entry-format">', 'after' => '</span>')); ?>
<?php postvision_post_date(array('before' => '<span class="entry-date">', 'after' => '</span>')); ?>
</div>
<?php endif; ?>
</div>
</li><!-- /#post-## -->
<?php endwhile; ?>
<?php // Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
?>
</ul>
<?php echo $after_widget; ?>
<?php endif; ?>
<?php
}
/**
* Processes the widget's options to be saved.
*
* @param array new_instance The new instance of values to be generated via the update.
* @param array old_instance The previous instance of values before the update.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['categories'] = strip_tags($new_instance['categories']);
$instance['tags'] = strip_tags($new_instance['tags']);
$instance['number_posts'] = $new_instance['number_posts'];
$instance['show_meta'] = $new_instance['show_meta'];
$instance['order_by'] = $new_instance['order_by'];
$instance['order'] = $new_instance['order'];
$instance['ignore_sticky'] = $new_instance['ignore_sticky'];
return $instance;
}
/**
* Generates the administration form for the widget.
*
* @param array instance The/ array of keys and values for the widget.
* @return void
*/
function form( $instance ) {
// Set up some default widget settings.
$defaults = array(
'title' => _x('Sort Recent Posts', 'widget_sort_posts', 'PostVision'),
'order_by' => 'publish-data',
'order' => 'desc',
'number_posts' => 5,
'show_meta' => 'on',
'tags' => '',
'categories' => '' );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _ex('Title', 'widget_sort_posts', 'PostVision'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for"<?php echo $this->get_field_id('order_by'); ?>"><?php _ex('Sort Posts By') ?></label>
<select id="<?php echo $this->get_field_id('order_by'); ?>" class="widefat" name="<?php echo $this->get_field_name('order_by'); ?>">
<option <?php selected($instance['order_by'], 'publish-data'); ?> value="publish-data"><?php _e('Puplish Data'); ?></option>
<option <?php selected($instance['order_by'], 'most-popular'); ?> value="most-popular"><?php _ex('Most Popular'); ?></option>
<option <?php selected($instance['order_by'], 'comment-count'); ?> value="comment-count"><?php _ex('Comment Count', 'widget-sort-posts', 'PostVision'); ?></option>
<option <?php selected($instance['order_by'], 'random'); ?> value="random"><?php _ex('Random', 'widget-sort-posts', 'PostVision'); ?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php _ex('Order', 'widget-sort-posts'); ?></label>
<select id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>" class="widefat">
<option <?php selected($instance['order'], 'desc'); ?> value="desc"><?php _ex('Descending', 'widget-sort-posts', 'PostVision'); ?></option>
<option <?php selected($instance['order'], 'asc'); ?> value="asc"><?php _ex('Ascending', 'widget-sort-posts', 'PostVision') ?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'categories' ); ?>"><?php _ex('Categories', 'widget_sort_posts', 'PostVision'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'categories' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'categories' ); ?>" value="<?php echo $instance['categories']; ?>" />
<br />
<small><?php _ex('Categorie IDs, separated by commas.', 'widget_sort_posts', 'PostVision'); ?></small>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'tags' ); ?>"><?php _ex('Tags', 'widget_sort_posts', 'PostVision'); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'tags' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'tags' ); ?>" value="<?php echo $instance['tags']; ?>" style="width:100%;" />
<span><?php _ex('Tag IDs, separated by commas.', 'widget_sort_posts', 'PostVision'); ?></span>
</p>
<p>
<label for="<?php echo $this->get_field_id('number_posts'); ?>"><?php _ex('Number of posts to show', 'widget_sort_posts', 'PostVision'); ?>:</label>
<input class="widefat" type="number" id="<?php echo $this->get_field_id('number_posts'); ?>" name="<?php echo $this->get_field_name('number_posts'); ?>" value="<?php echo $instance['number_posts']; ?>" />
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['show_meta'], "on" ); ?> id="<?php echo $this->get_field_id( 'show_meta' ); ?>" name="<?php echo $this->get_field_name( 'show_meta' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_meta' ); ?>"><?php _ex('Display Meta post?', 'widget_sort_posts', 'PostVision'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['ignore_sticky'], "on" ); ?> id="<?php echo esc_attr($this->get_field_id( 'ignore_sticky' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'ignore_sticky' )); ?>" />
<label for="<?php echo esc_attr($this->get_field_id( 'ignore_sticky' )); ?>"><?php _ex('Ignore sticky posts?', 'widget-sort-posts', 'PostVision'); ?></label>
</p>
<?php
} // End form
}
add_action( 'widgets_init', create_function( '', 'register_widget("Customatic_Widget_Sort_Recent_Posts");') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment