PluginProbe
ElasticPress / 4.3.1
ElasticPress v4.3.1
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.3.1, at includes/classes/Feature/RelatedPosts/Widget.php

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