'mnmlwp_recent_posts',
'description' => 'A list of of your recent posts.',
);
parent::__construct(
'mnmlwp_recent_posts',
esc_html__('mnmlWP Recent Posts', 'mnmlwp'),
$widget_options
);
$this->widget_folder = get_template_directory_uri() . '/widgets/mnmlwp-recent-posts/';
$this->enqueue_scripts_and_styles();
}
private function enqueue_scripts_and_styles()
{
wp_enqueue_style( 'mnmlwp-recent-posts', $this->widget_folder . 'mnmlwp-recent-posts-style.css' );
}
public function widget( $args, $instance )
{
$instance_title = isset( $instance['title'] ) ? $instance['title'] : '';
$instance_show_thumbs = isset( $instance['show_thumbs'] ) ? $instance['show_thumbs'] : false;
$instance_title_length = isset( $instance['title_length'] ) ? $instance['title_length'] : 44;
$title = apply_filters( 'widget_title', $instance_title, $instance, $this->id_base );
$show_thumbs = filter_var( $instance_show_thumbs, FILTER_VALIDATE_BOOLEAN );
$title_length = (int)$instance_title_length;
if( $title_length === -1 || $title_length > 1024 )
$title_length = 1024;
$num_posts = (int)$instance['num_posts'];
if( $num_posts > 9999999 )
$num_posts = 9999999;
echo $args['before_widget'];
if( ! empty( $title ) ) {
echo $args['before_title'];
echo $title;
echo $args['after_title'];
}
$sticky = get_option( 'sticky_posts' );
$posts = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $num_posts,
'post__not_in' => $sticky,
'orderby' => 'date',
'order' => 'DESC',
) );
$sticky_posts = get_posts( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $num_posts,
'post__in' => $sticky,
'orderby' => 'date',
'order' => 'DESC',
) );
$posts = array_merge( $sticky_posts, $posts );
if( empty( $posts ) ) {
echo esc_html__('No posts found', 'mnmlwp');
echo $args['after_widget'];
return;
}
foreach( $posts as $post )
{
$url = has_post_thumbnail( $post->ID ) ? get_the_post_thumbnail_url( $post->ID, 'thumbnail' ) : '';
echo '';
$tack = in_array( $post->ID, $sticky ) ? '' : '';
$more = strlen( $post->post_title ) >= $title_length ? '…' : '';
if( $show_thumbs ) {
echo '' . $tack . substr(esc_attr( $post->post_title ), 0 , $title_length) . $more . '';
echo '';
} else {
echo '' . $tack . substr(esc_attr( $post->post_title ), 0 , $title_length) . $more . '';
}
echo '';
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance )
{
$title = ! empty( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) : '';
$num_posts = ! empty( $instance['num_posts'] ) ? (int)$instance['num_posts'] : 5;
$show_thumbs = ! empty( $instance['show_thumbs'] ) ? (int)$instance['show_thumbs'] : 0;
$title_length = ! empty( $instance['title_length'] ) ? (int)$instance['title_length'] : 44;
?>
>
1024 ) $instance['title_length'] = 1024; $instance['num_posts'] = ( ! empty( $new_instance['num_posts'] ) ) ? (int)$new_instance['num_posts'] : 5; if( $instance['num_posts'] > 9999999 ) $instance['num_posts'] = 9999999; if( $instance['num_posts'] <= -1 ) $instance['num_posts'] = -1; $instance['show_thumbs'] = ( ! empty( $new_instance['show_thumbs'] ) ) ? absint( $new_instance['show_thumbs'] ) : ''; return $instance; } } add_action( 'widgets_init', function() { return register_widget('mnmlWP_Widget_Recent_Posts'); });