* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer) */ class Cakifo_Widget_Related_Posts extends WP_Widget { /** * Set up the widget's unique name, ID, class, description, and other options. */ function __construct() { /* Set up the widget options. */ $widget_options = array( 'classname' => 'related-posts', 'description' => esc_html__( 'List related posts to the current viewed post, based on taxonomies.', 'cakifo' ) ); /* Create the widget. */ $this->WP_Widget( 'cakifo-related-posts', __( 'Cakifo: Related Posts', 'cakifo' ), $widget_options ); /* Flush the cache when a post is updated or deleted. */ 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. * * @param array $sidebar * @param array $instance */ function widget( $sidebar, $instance ) { $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; /* Open the before widget HTML. */ echo $sidebar['before_widget']; /* Output the widget title. */ if ( $instance['title'] ) { echo $sidebar['before_title'] . apply_filters( 'widget_title', sprintf( $instance['title'], get_the_title( $post_id ) ), $instance, $this->id_base ) . $sidebar['after_title']; } /* Are there any related posts? */ if ( ! empty( $related_posts ) ) : /* Set CSS classes based on thumbnail support. */ if ( (boolean) $instance['show_thumbnail'] && current_theme_supports( 'get-the-image' ) ) { $show_thumbnails = true; } else { $show_thumbnails = false; } $class = ( $show_thumbnails ) ? 'with-thumbnails clearfix' : 'clearfix'; do_atomic( 'before_related_posts_list' ); // cakifo_before_related_posts_list echo '