* @copyright Copyright (c) 2011-2012, Jesper Johansen * @link http://wpthemes.jayj.dk/cakifo * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer) */ /** * Related Posts widget class. * * @since Cakifo 1.3 */ class Cakifo_Widget_Related_Posts extends WP_Widget { /** * Set up the widget's unique name, ID, class, description, and other options. * * @since Cakifo 1.3 */ function __construct() { /* Set up the widget options. */ $widget_options = array( 'classname' => 'related-posts', 'description' => esc_html__( 'Use this widget to list related posts to the current viewed post based on category and post format.', 'cakifo' ) ); /* Create the widget. */ $this->WP_Widget( 'cakifo-related-posts', // $this->id_base __( 'Cakifo: Related Posts', 'cakifo' ), // $this->name $widget_options // $this->widget_options ); add_action( 'save_post', array( &$this, 'flush_widget_cache' ) ); add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) ); } /** * Outputs the widget based on the arguments input through the widget controls. * * @since Cakifo 1.3 */ function widget( $sidebar, $instance ) { extract( $sidebar ); $post_id = get_the_ID(); /* Get related posts from post meta. */ $related_posts = get_post_meta( $post_id, 'related', false ); /* No related posts found, get them! */ if ( empty( $related_posts ) ) : $this->_get_related_posts( $post_id, $instance ); // Get the post meta again $related_posts = get_post_meta( $post_id, 'related', false ); endif; /* Output the theme's $before_widget wrapper. */ echo $before_widget; /* If a title was input by the user, display it. */ if ( ! empty( $instance['title'] ) ) echo $before_title . apply_filters( 'widget_title', sprintf( $instance['title'], get_the_title( $post_id ) ), $instance, $this->id_base ) . $after_title; /* Are there any related posts? */ if ( ! empty( $related_posts ) ) : $show_thumbnails = (boolean) $instance['show_thumbnail']; // CSS class $class = ( $show_thumbnails ) ? 'with-thumbnails clearfix' : 'clearfix'; do_atomic( 'before_related_posts_list' ); // cakifo_before_related_posts_list echo '