PluginProbe
ElasticPress / 4.2.0
ElasticPress v4.2.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Feature / RelatedPosts / Widget.php

Widget.php in ElasticPress 4.2.0, at includes/classes/Feature/RelatedPosts/Widget.php

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