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

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

197 lines 5.3 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( 'description' => esc_html__( 'A search form for comments.', 'elasticpress' ) );
31 parent::__construct( 'ep-comments', esc_html__( 'ElasticPress - Comments', 'elasticpress' ), $options );
32 }
33
34 /**
35 * Display widget
36 *
37 * @param array $args Widget arguments
38 * @param array $instance Widget instance variables
39 * @since 3.6.0
40 */
41 public function widget( $args, $instance ) {
42
43 echo wp_kses_post( $args['before_widget'] );
44
45 ob_start();
46
47 if ( ! empty( $instance['title'] ) ) {
48 echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] );
49 }
50
51 ?>
52 <div class="ep-widget-search-comments">
53 <?php
54 if ( ! empty( $instance['post_type'] ) ) {
55 ?>
56 <input
57 type="hidden"
58 id="ep-widget-search-comments-post-type"
59 value="<?php echo esc_attr( implode( ',', $instance['post_type'] ) ); ?>"
60 />
61 <?php
62 }
63 ?>
64 </div>
65
66 <?php
67 $search_comments_html = ob_get_clean();
68
69 // Enqueue Script & Styles
70 wp_enqueue_script(
71 'elasticpress-comments',
72 EP_URL . 'dist/js/comments-script.min.js',
73 Utils\get_asset_info( 'comments-script', 'dependencies' ),
74 Utils\get_asset_info( 'comments-script', 'version' ),
75 true
76 );
77
78 wp_enqueue_style(
79 'elasticpress-comments',
80 EP_URL . 'dist/css/comments-styles.min.css',
81 Utils\get_asset_info( 'comments-styles', 'dependencies' ),
82 Utils\get_asset_info( 'comments-styles', 'version' )
83 );
84
85 $default_script_data = [
86 'noResultsFoundText' => esc_html__( 'We could not find any results', 'elasticpress' ),
87 'minimumLengthToSearch' => 2,
88 'restApiEndpoint' => get_rest_url( null, 'elasticpress/v1/comments' ),
89 ];
90
91 /**
92 * Filter the l10n data attached to the Widget Search Comments script
93 *
94 * @since 3.6.0
95 * @hook ep_comment_search_widget_l10n_data_script
96 * @param {array} $default_script_data Default data attached to the script
97 * @return {array} New l10n data to be attached
98 */
99 $script_data = apply_filters( 'ep_comment_search_widget_l10n_data_script', $default_script_data );
100
101 wp_localize_script(
102 'elasticpress-comments',
103 'epc',
104 $script_data
105 );
106
107 // phpcs:disable
108 /**
109 * Filter comment search widget HTML
110 *
111 * @hook ep_widget_search_comments
112 * @since 3.6.0
113 * @param {string} $search_comments_html Widget HTML
114 * @param {string} $title Widget title
115 * @return {string} New HTML
116 */
117 echo apply_filters( 'ep_widget_search_comments', $search_comments_html, $instance['title'] );
118 // phpcs:enable
119
120 echo wp_kses_post( $args['after_widget'] );
121 }
122
123 /**
124 * Display widget settings form
125 *
126 * @param array $instance Widget instance variables
127 * @since 3.6.0
128 */
129 public function form( $instance ) {
130 $title = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
131 $post_type = ( isset( $instance['post_type'] ) ) ? $instance['post_type'] : [];
132
133 $post_types_options = array_map(
134 'get_post_type_object',
135 array_filter(
136 Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types(),
137 function ( $post_type ) {
138 return post_type_supports( $post_type, 'comments' );
139 }
140 )
141 );
142
143 ?>
144 <p>
145 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
146 <?php esc_html_e( 'Title:', 'elasticpress' ); ?>
147 </label>
148
149 <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 ); ?>" />
150 </p>
151 <p>
152 <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>">
153 <?php esc_html_e( 'Search for comments on:', 'elasticpress' ); ?>
154 </label>
155
156 <?php foreach ( $post_types_options as $indexable_post_type ) : ?>
157 <p>
158 <input
159 class="checkbox"
160 type="checkbox"
161 id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type->name ); ?>"
162 name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]"
163 value="<?php echo esc_attr( $indexable_post_type->name ); ?>"
164 <?php checked( in_array( $indexable_post_type->name, $post_type, true ) ); ?>
165 />
166 <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) . '-' . $indexable_post_type->name ); ?>">
167 <?php echo esc_html( $indexable_post_type->label ); ?>
168 </label>
169 </p>
170 <?php endforeach; ?>
171 </p>
172 <?php
173 }
174
175 /**
176 * Update widget settings
177 *
178 * @param array $new_instance New instance settings
179 * @param array $old_instance Old instance settings
180 * @since 3.6.0
181 * @return array
182 */
183 public function update( $new_instance, $old_instance ) {
184 $instance = [];
185 $instance['title'] = sanitize_text_field( $new_instance['title'] );
186
187 if ( is_array( $new_instance['post_type'] ) ) {
188 $instance['post_type'] = array_map(
189 'sanitize_text_field',
190 $new_instance['post_type']
191 );
192 }
193
194 return $instance;
195 }
196 }
197