| 1 |
<?php |
| 2 |
/** |
| 3 |
* Related posts widget |
| 4 |
* |
| 5 |
* @since 2.2 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Feature\RelatedPosts; |
| 10 |
|
| 11 |
use ElasticPress\Features; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Related posts widget class |
| 19 |
*/ |
| 20 |
class Widget extends \WP_Widget { |
| 21 |
|
| 22 |
/** |
| 23 |
* Initialize the widget |
| 24 |
* |
| 25 |
* @since 6.4 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$options = array( |
| 29 |
'description' => esc_html__( 'Show related posts using ElasticPress. This widget will only appear on single post, page, and custom type pages.', 'elasticpress' ), |
| 30 |
'show_instance_in_rest' => true, |
| 31 |
); |
| 32 |
|
| 33 |
parent::__construct( 'ep-related-posts', esc_html__( 'ElasticPress - Related Posts', 'elasticpress' ), $options ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Display widget |
| 38 |
* |
| 39 |
* @param array $args Widget arguments |
| 40 |
* @param array $instance Widget instance variables |
| 41 |
* @since 2.2 |
| 42 |
*/ |
| 43 |
public function widget( $args, $instance ) { |
| 44 |
|
| 45 |
if ( ! is_single() ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$related_posts = get_transient( 'ep_related_posts_' . get_the_ID() ); |
| 50 |
|
| 51 |
if ( false === $related_posts ) { |
| 52 |
$related_posts = Features::factory()->get_registered_feature( 'related_posts' )->find_related( get_the_ID(), $instance['num_posts'] ); |
| 53 |
|
| 54 |
if ( empty( $related_posts ) ) { |
| 55 |
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 56 |
set_transient( 'ep_related_posts_' . get_the_ID(), '', HOUR_IN_SECONDS ); // Let's not spam |
| 57 |
} |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
ob_start(); |
| 62 |
|
| 63 |
echo wp_kses_post( $args['before_widget'] ); |
| 64 |
|
| 65 |
if ( ! empty( $instance['title'] ) ) { |
| 66 |
echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] ); |
| 67 |
} |
| 68 |
?> |
| 69 |
|
| 70 |
<ul> |
| 71 |
<?php foreach ( $related_posts as $post ) : ?> |
| 72 |
<li><a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo esc_html( get_the_title( $post->ID ) ); ?></a></li> |
| 73 |
<?php endforeach; ?> |
| 74 |
</ul> |
| 75 |
|
| 76 |
<?php |
| 77 |
wp_reset_postdata(); |
| 78 |
|
| 79 |
echo wp_kses_post( $args['after_widget'] ); |
| 80 |
|
| 81 |
$related_posts = ob_get_clean(); |
| 82 |
|
| 83 |
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 84 |
set_transient( 'ep_related_posts_' . get_the_ID(), $related_posts, HOUR_IN_SECONDS ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
echo wp_kses_post( $related_posts ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Display widget settings form |
| 93 |
* |
| 94 |
* @param array $instance Widget instance variables |
| 95 |
* @since 2.2 |
| 96 |
*/ |
| 97 |
public function form( $instance ) { |
| 98 |
$title = ( isset( $instance['title'] ) ) ? $instance['title'] : ''; |
| 99 |
$num_posts = ( isset( $instance['num_posts'] ) ) ? $instance['num_posts'] : 5; |
| 100 |
?> |
| 101 |
<p> |
| 102 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 103 |
<?php esc_html_e( 'Title:', 'elasticpress' ); ?> |
| 104 |
</label> |
| 105 |
|
| 106 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 107 |
</p> |
| 108 |
|
| 109 |
<p> |
| 110 |
<label for="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>"> |
| 111 |
<?php esc_html_e( 'Number of Posts to Show:', 'elasticpress' ); ?> |
| 112 |
</label> |
| 113 |
|
| 114 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'num_posts' ) ); ?>" type="text" value="<?php echo absint( $num_posts ); ?>" /> |
| 115 |
</p> |
| 116 |
<?php |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Update widget settings |
| 121 |
* |
| 122 |
* @param array $new_instance New instance settings |
| 123 |
* @param array $old_instance Old instance settings |
| 124 |
* @since 2.2 |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
public function update( $new_instance, $old_instance ) { |
| 128 |
|
| 129 |
$instance = []; |
| 130 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 131 |
$instance['num_posts'] = absint( $new_instance['num_posts'] ); |
| 132 |
|
| 133 |
return $instance; |
| 134 |
} |
| 135 |
} |
| 136 |
|