Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Related Posts widget v1.2 - changes in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jayj committed Sep 23, 2013
1 parent 541e4e9 commit 5107062
Showing 1 changed file with 98 additions and 88 deletions.
186 changes: 98 additions & 88 deletions functions/widget-related-posts.php
@@ -1,143 +1,159 @@
<?php
/**
* The related posts widget is created to give users the ability to show related posts after each posts.
* Related Posts widget
*
* Gives users the ability to show related posts after each posts,
* based on taxonomies.
*
* @package Cakifo
* @subpackage Classes
* @since Cakifo 1.3.0
* @version 1.1.0
* @version 1.2.0
* @author Jesper Johansen <kontakt@jayj.dk>
* @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.
*
* @since Cakifo 1.3.0
*/
function __construct() {

// Set up the widget options.
/* 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 taxonomies.', 'cakifo' )
'description' => esc_html__( 'List related posts to the current viewed post, based on taxonomies.', 'cakifo' )
);

// Create the widget.
/* 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.
/* 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.
*
* @since Cakifo 1.3.0
* @param array $sidebar
* @param array $instance
*/
function widget( $sidebar, $instance ) {

extract( $sidebar );

$post_id = get_the_ID();

// Get related posts from post meta.
/* Get related posts from post meta. */
$related_posts = get_post_meta( $post_id, 'related', false );

// No related posts found, get them.
/* No related posts found, get them! */
if ( empty( $related_posts ) ) :
$this->_get_related_posts( $post_id, $instance );

// Get the post meta again
// Get the post meta again.
$related_posts = get_post_meta( $post_id, 'related', false );
endif;

echo $before_widget;
/* Open the before widget HTML. */
echo $sidebar['before_widget'];

// Display title
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;
/* 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?
/* Are there any related posts? */
if ( ! empty( $related_posts ) ) :

$show_thumbnails = (boolean) $instance['show_thumbnail'];
/* 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;
}

// CSS class
$class = ( $show_thumbnails ) ? 'with-thumbnails clearfix' : 'clearfix';

do_atomic( 'before_related_posts_list' ); // cakifo_before_related_posts_list

echo '<ul class="' . esc_attr( $class ) . '">';

$i = 0;

// Run through each post
foreach( $related_posts as $post ) : ?>

<?php $title = $post['title']; ?>

<?php do_atomic( 'before_related_posts_list_item' ); // cakifo_before_related_posts_list_item ?>
// Count the number of posts.
$count = 0;

/* Run through each post. */
foreach( $related_posts as $post ) :

/* Post data. */
$id = $post['post_id'];
$title = get_the_title( $id );

/**
* Get the post thumbnail
* @var string
*/
$thumbnail = get_the_image( array(
'post_id' => $id,
'size' => 'small',
'link_to_post' => false,
'meta_key' => null,
'echo' => false,
) );
?>

<li class="related-post">
<?php do_atomic( 'open_related_posts_list_item' ); // cakifo_open_related_posts_list_item ?>

<a href="<?php echo get_permalink( $post['post_id'] ); ?>" title="<?php printf( esc_attr__( 'Read %s', 'cakifo' ), esc_attr( $title ) ); ?>">
<a href="<?php echo get_permalink( $id ); ?>" title="<?php the_title_attribute( array( 'post' => $id ) ); ?>">
<?php
// Show post thumbnail
if ( $show_thumbnails && ! empty( $post['thumbnail'] ) ) {
echo wp_get_attachment_image( $post['thumbnail'], 'small', false, array( 'title' => esc_attr( $title ) ) );
// Show the post thumbnail, if supported
if ( $thumbnail && $show_thumbnails ) {

echo $thumbnail;

// Show post title
// Else, show the post title
} else {
echo ( empty( $title ) ) ? '<span>' . _x( 'Untitled', 'untitled post', 'cakifo' ) . '</span>' : '<span>' . $title . '</span>';
echo ( empty( $title ) ) ? '<span>Untitled</span>' : '<span>' . $title . '</span>';
}
?>
</a>

<?php do_atomic( 'close_related_posts_list_item' ); // cakifo_close_related_posts_list_item ?>
</li> <!-- .related-post -->

<?php do_atomic( 'after_related_posts_list_item' ); // cakifo_after_related_posts_list_item ?>
<?php
// Stop at the limit.
// This is used when there's more than one Related Posts widget at a page.
$count++;

if ( $count == $instance['limit'] ) {
break;
}

<?php
// Stop at the limit. This is used when there's more than one Related Posts widget at a page.
$i++;
if ( $i == $instance['limit'] ) break;
?>
<?php
endforeach;

echo '</ul>';

do_atomic( 'after_related_posts_list' ); // cakifo_after_related_posts_list

// Nope, no related posts.
/* Nope, no related posts. */
else :

_e( 'No related posts.', 'cakifo' );

endif;

echo $after_widget;
/* Close the after widget HTML. */
echo $sidebar['after_widget'];
}

/**
* Get the related posts from the choosen taxonomies
* and add them to the post meta.
*
* @access private
* @since Cakifo 1.3.0
* @param int $post_id
* @param array $args
*/
* Gets the related posts based on the choosen taxonomies
* and puts them in the post meta.
*
* @access private
* @param int $post_id
* @param array $args
*/
private function _get_related_posts( $post_id, $args ) {

// Set up the query.
/* Set up the query. */
$related_query = array(
'posts_per_page' => $args['limit'],
'orderby' => $args['orderby'],
Expand All @@ -146,10 +162,12 @@ private function _get_related_posts( $post_id, $args ) {
'tax_query' => array( 'relation' => 'OR' )
);

// Make sure the taxonomies are set.
/* Make sure the taxonomies are set. They won't be if an user updates the theme and doesn't resave the widget settings. */
$taxonomies = ( isset( $args['taxonomies'] ) ) ? $args['taxonomies'] : array();

// Loop through each selected taxonomy.
/**
* Loop through each selected taxonomy.
*/
foreach ( $taxonomies as $taxonomy ) :

// Skip post formats.
Expand All @@ -175,7 +193,9 @@ private function _get_related_posts( $post_id, $args ) {

endforeach;

// Post formats query.
/**
* Post formats query.
*/
if ( in_array( 'post_format', $taxonomies ) ) :
$format = ( get_post_format() ) ? 'post-format-' . get_post_format() : '';

Expand All @@ -186,20 +206,15 @@ private function _get_related_posts( $post_id, $args ) {
);
endif;

// Fire up the query.
/* Fire up the query. */
$related = new WP_Query( $related_query );

// Add the posts in a post meta field called 'related'.
/**
* Find all related posts and put them in a post meta field called 'related' containing the post ID.
*/
if ( $related->have_posts() ) while ( $related->have_posts() ) : $related->the_post();

add_post_meta( $post_id, 'related',
array(
'title' => get_the_title(),
'post_id' => get_the_ID(),
'thumbnail' => get_post_thumbnail_id()
),
false
);
add_post_meta( $post_id, 'related', array( 'post_id' => get_the_ID() ), false );

endwhile;

Expand All @@ -209,19 +224,16 @@ private function _get_related_posts( $post_id, $args ) {
/**
* Updates the widget control options for the particular instance of the widget.
*
* @since Cakifo 1.3.0
* @param array $new_instance The new setting
* @param array $instance The new setting
* @param array $old_instance The old setting
*/
function update( $new_instance, $old_instance ) {

$instance = $new_instance;
function update( $instance, $old_instance ) {

$instance['title'] = strip_tags( $new_instance['title'] );
$instance['limit'] = intval( $new_instance['limit'] );
$instance['taxonomies'] = (array) $new_instance['taxonomies'];
$instance['orderby'] = strip_tags( $new_instance['orderby'] );
$instance['show_thumbnail'] = ( isset( $new_instance['show_thumbnail'] ) ? 1 : 0 );
$instance['title'] = strip_tags( $instance['title'] );
$instance['limit'] = intval( $instance['limit'] );
$instance['taxonomies'] = (array) $instance['taxonomies'];
$instance['orderby'] = strip_tags( $instance['orderby'] );
$instance['show_thumbnail'] = ( isset( $instance['show_thumbnail'] ) ? 1 : 0 );

// Update the post meta cache.
$this->flush_widget_cache();
Expand All @@ -233,17 +245,16 @@ function update( $new_instance, $old_instance ) {
* Flush the related posts meta when a post is updated, deleted,
* or the settings has been changed.
*
* @since Cakifo 1.3.0
* @param int|null $post_ID
*/
function flush_widget_cache( $post_ID = null ) {

// A post is being updated or deleted.
/* A post is being updated or deleted. */
if ( isset( $post_ID ) ) :

$related_meta = get_post_meta( $post_ID, 'related', false );

// Delete the related post meta for all the related posts.
// Delete the related post meta for the updated post.
if ( isset( $related_meta ) ) :

$related_posts = array( $post_ID );
Expand All @@ -269,12 +280,11 @@ function flush_widget_cache( $post_ID = null ) {
/**
* Displays the widget control options in the Widgets admin screen.
*
* @since Cakifo 1.3.0
* @param array $instance The widget settings in the database
* @param array $instance The widget settings in the database.
*/
function form( $instance ) {

// Set up the default form values.
/* Set up the default form values. */
$defaults = array(
'title' => esc_attr__( 'Related Posts', 'cakifo' ),
'limit' => 5,
Expand All @@ -283,10 +293,10 @@ function form( $instance ) {
'show_thumbnail' => true
);

// Merge the user-selected arguments with the defaults.
/* Merge the user-selected arguments with the defaults. */
$instance = wp_parse_args( (array) $instance, $defaults );

// Create an array of orderby types.
/* Create an array of orderby types. */
$orderby = array(
'rand' => _x( 'Random', 'order by', 'cakifo' ),
'date' => _x( 'Date', 'order by', 'cakifo' ),
Expand Down

1 comment on commit 5107062

@jayj
Copy link
Owner Author

@jayj jayj commented on 5107062 Sep 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Only store the post ID in the meta field
  • Inline docs update
  • Don't use extract on the sidebar settings
  • Use Get The Image to grab the thumbnail
  • Remove the xxx_related_posts_list_item hooks (the list hooks are still there)

Please sign in to comment.