PluginProbe
ElasticPress / 4.4.1
ElasticPress v4.4.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 / Comments / Widget.php

Widget.php in ElasticPress 4.4.1, at includes/classes/Feature/Comments/Widget.php

160 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Search comments widget
4 *
5 * @since 3.6.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Feature\Comments;
10
11 use \WP_Widget as WP_Widget;
12 use ElasticPress\Features as Features;
13 use ElasticPress\Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Search comment widget class
21 */
22 class Widget extends WP_Widget {
23
24 /**
25 * Initialize the widget
26 *
27 * @since 3.6.0
28 */
29 public function __construct() {
30 $options = array(
31 'description' => esc_html__( 'A search form for comments.', 'elasticpress' ),
32 'show_instance_in_rest' => true,
33 );
34
35 parent::__construct( 'ep-comments', esc_html__( 'ElasticPress - Comments', 'elasticpress' ), $options );
36 }
37
38 /**
39 * Display widget
40 *
41 * @param array $args Widget arguments
42 * @param array $instance Widget instance variables
43 * @since 3.6.0
44 */
45 public function widget( $args, $instance ) {
46
47 echo wp_kses_post( $args['before_widget'] );
48
49 ob_start();
50
51 if ( ! empty( $instance['title'] ) ) {
52 echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] );
53 }
54
55 $wrapper_id = 'ep-search-comments-' . uniqid();
56 ?>
57 <div id="<?php echo esc_attr( $wrapper_id ); ?>" class="ep-widget-search-comments">
58 <?php
59 if ( ! empty( $instance['post_type'] ) ) {
60 ?>
61 <input
62 class="ep-widget-search-comments-post-type"
63 type="hidden"
64 id="<?php echo esc_attr( $wrapper_id ); ?>-post-type"
65 value="<?php echo esc_attr( implode( ',', $instance['post_type'] ) ); ?>"
66 />
67 <?php
68 }
69 ?>
70 </div>
71
72 <?php
73 $search_comments_html = ob_get_clean();
74
75 // Enqueue Script & Styles
76 wp_enqueue_script( 'elasticpress-comments' );
77 wp_enqueue_style( 'elasticpress-comments' );
78
79 // phpcs:disable
80 /**
81 * Filter comment search widget HTML
82 *
83 * @hook ep_widget_search_comments
84 * @since 3.6.0
85 * @param {string} $search_comments_html Widget HTML
86 * @param {string} $title Widget title
87 * @return {string} New HTML
88 */
89 echo apply_filters( 'ep_widget_search_comments', $search_comments_html, $instance['title'] );
90 // phpcs:enable
91
92 echo wp_kses_post( $args['after_widget'] );
93 }
94
95 /**
96 * Display widget settings form
97 *
98 * @param array $instance Widget instance variables
99 * @since 3.6.0
100 */
101 public function form( $instance ) {
102 $title = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
103 $post_type = ( isset( $instance['post_type'] ) ) ? $instance['post_type'] : [];
104
105 $post_types_options = Comments::get_searchable_post_types();
106 ?>
107 <p>
108 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
109 <?php esc_html_e( 'Title:', 'elasticpress' ); ?>
110 </label>
111
112 <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 ); ?>" />
113 </p>
114 <p>
115 <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>">
116 <?php esc_html_e( 'Search for comments on:', 'elasticpress' ); ?>
117 </label>
118
119 <?php foreach ( $post_types_options as $indexable_post_type => $label ) : ?>
120 <p>
121 <input
122 class="checkbox"
123 type="checkbox"
124 id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type ); ?>"
125 name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]"
126 value="<?php echo esc_attr( $indexable_post_type ); ?>"
127 <?php checked( in_array( $indexable_post_type, $post_type, true ) ); ?>
128 />
129 <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type ); ?>">
130 <?php echo esc_html( $label ); ?>
131 </label>
132 </p>
133 <?php endforeach; ?>
134 </p>
135 <?php
136 }
137
138 /**
139 * Update widget settings
140 *
141 * @param array $new_instance New instance settings
142 * @param array $old_instance Old instance settings
143 * @since 3.6.0
144 * @return array
145 */
146 public function update( $new_instance, $old_instance ) {
147 $instance = [];
148 $instance['title'] = sanitize_text_field( $new_instance['title'] );
149
150 if ( is_array( $new_instance['post_type'] ) ) {
151 $instance['post_type'] = array_map(
152 'sanitize_text_field',
153 $new_instance['post_type']
154 );
155 }
156
157 return $instance;
158 }
159 }
160