PluginProbe
ElasticPress / 5.0.1
ElasticPress v5.0.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 / Comments.php

Comments.php in ElasticPress 5.0.1, at includes/classes/Feature/Comments/Comments.php

330 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Comments feature
4 *
5 * @since 3.6.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Feature\Comments;
10
11 use ElasticPress\Feature;
12 use ElasticPress\FeatureRequirementsStatus;
13 use ElasticPress\Features;
14 use ElasticPress\Indexable;
15 use ElasticPress\Indexables;
16 use ElasticPress\Utils;
17 use ElasticPress\REST;
18
19 /**
20 * Comments feature class
21 */
22 class Comments extends Feature {
23 /**
24 * Whether the feature should be always visible in the dashboard
25 *
26 * @since 5.0.0
27 * @var boolean
28 */
29 protected $is_visible = false;
30
31 /**
32 * Initialize feature, setting it's config
33 *
34 * @since 3.6.0
35 */
36 public function __construct() {
37 $this->slug = 'comments';
38
39 $this->title = esc_html__( 'Comments', 'elasticpress' );
40
41 $this->summary = '<p>' . __( 'This feature will empower your website to overcome traditional WordPress comment search and query limitations that can present themselves at scale. This feature is only needed if you are using <code>WP_Comment_Query</code> directly.', 'elasticpress' ) . '</p>';
42
43 $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#comments', 'elasticpress' );
44
45 $this->requires_install_reindex = true;
46
47 Indexables::factory()->register( new Indexable\Comment\Comment(), false );
48
49 parent::__construct();
50 }
51
52 /**
53 * Setup search functionality
54 *
55 * @since 3.6.0
56 */
57 public function setup() {
58 Indexables::factory()->activate( 'comment' );
59
60 add_action( 'init', [ $this, 'register_block' ] );
61 add_action( 'init', [ $this, 'search_setup' ] );
62 add_action( 'widgets_init', [ $this, 'register_widget' ] );
63 add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
64 add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] );
65 add_filter( 'widget_types_to_hide_from_legacy_widget_block', [ $this, 'hide_legacy_widget' ] );
66 }
67
68 /**
69 * Setup search integration
70 *
71 * @since 3.6.0
72 */
73 public function search_setup() {
74 $admin_integration = apply_filters( 'ep_admin_wp_query_integration', false );
75
76 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
77 /**
78 * Filter to integrate with admin ajax queries
79 *
80 * @hook ep_ajax_wp_query_integration
81 * @param {bool} $integrate True to integrate
82 * @return {bool} New value
83 */
84 if ( ! apply_filters( 'ep_ajax_wp_query_integration', false ) ) {
85 return;
86 } else {
87 $admin_integration = true;
88 }
89 }
90
91 if ( is_admin() && ! $admin_integration ) {
92 return;
93 }
94
95 add_filter( 'ep_elasticpress_enabled', [ $this, 'integrate_search_queries' ], 10, 2 );
96 }
97
98 /**
99 * Output feature box long text
100 *
101 * @since 3.6.0
102 */
103 public function output_feature_box_long() {
104 ?>
105 <p><?php esc_html_e( 'This feature will empower your website to overcome traditional WordPress comment search and query limitations that can present themselves at scale.', 'elasticpress' ); ?></p>
106 <?php
107 }
108
109 /**
110 * Enable integration on search queries
111 *
112 * @param bool $enabled Whether EP is enabled
113 * @param \WP_Comment_Query $query Current query object.
114 * @since 3.6.0
115 * @return bool
116 */
117 public function integrate_search_queries( $enabled, $query ) {
118 if ( ! is_a( $query, 'WP_Comment_Query' ) ) {
119 return $enabled;
120 }
121
122 if ( isset( $query->query_vars['ep_integrate'] ) && ! filter_var( $query->query_vars['ep_integrate'], FILTER_VALIDATE_BOOLEAN ) ) {
123 $enabled = false;
124 } elseif ( ! empty( $query->query_vars['search'] ) ) {
125 $enabled = true;
126 }
127
128 return $enabled;
129 }
130
131 /**
132 * Determine feature reqs status
133 *
134 * @since 3.6.0
135 * @return FeatureRequirementsStatus
136 */
137 public function requirements_status() {
138 $status = new FeatureRequirementsStatus( 1 );
139
140 return $status;
141 }
142
143 /**
144 * Register comments widget
145 *
146 * @since 3.6.0
147 */
148 public function register_widget() {
149 register_widget( __NAMESPACE__ . '\Widget' );
150 }
151
152 /**
153 * Registers the API endpoint to search for comments
154 *
155 * @since 3.6.0
156 */
157 public function rest_api_init() {
158 $controller = new REST\Comments();
159 $controller->register_routes();
160 }
161
162 /**
163 * Get a list of searchable post types that support comments.
164 *
165 * @return array Array of post type labels keyed by post type.
166 * @since 4.4.0
167 */
168 public static function get_searchable_post_types() {
169 $searchable_post_types = array();
170
171 $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
172 $post_types = array_filter(
173 $post_types,
174 function( $post_type ) {
175 return post_type_supports( $post_type, 'comments' );
176 }
177 );
178
179 foreach ( $post_types as $post_type ) {
180 $post_type_object = get_post_type_object( $post_type );
181 $post_type_labels = get_post_type_labels( $post_type_object );
182
183 $searchable_post_types[ $post_type ] = $post_type_labels->name;
184 }
185
186 return $searchable_post_types;
187 }
188
189 /**
190 * Enqueue frontend scripts.
191 *
192 * @since 4.4.0
193 */
194 public function frontend_scripts() {
195 wp_register_script(
196 'elasticpress-comments',
197 EP_URL . 'dist/js/comments-script.js',
198 Utils\get_asset_info( 'comments-script', 'dependencies' ),
199 Utils\get_asset_info( 'comments-script', 'version' ),
200 true
201 );
202
203 wp_set_script_translations( 'elasticpress-comments', 'elasticpress' );
204
205 wp_register_style(
206 'elasticpress-comments',
207 EP_URL . 'dist/css/comments-styles.css',
208 Utils\get_asset_info( 'comments-styles', 'dependencies' ),
209 Utils\get_asset_info( 'comments-styles', 'version' )
210 );
211
212 $default_script_data = [
213 'noResultsFoundText' => esc_html__( 'We could not find any results', 'elasticpress' ),
214 'minimumLengthToSearch' => 2,
215 'restApiEndpoint' => get_rest_url( null, 'elasticpress/v1/comments' ),
216 ];
217
218 /**
219 * Filter the l10n data attached to the Widget Search Comments script
220 *
221 * @since 3.6.0
222 * @hook ep_comment_search_widget_l10n_data_script
223 * @param {array} $default_script_data Default data attached to the script
224 * @return {array} New l10n data to be attached
225 */
226 $script_data = apply_filters( 'ep_comment_search_widget_l10n_data_script', $default_script_data );
227
228 wp_localize_script(
229 'elasticpress-comments',
230 'epc',
231 $script_data
232 );
233 }
234
235 /**
236 * Register block.
237 *
238 * @since 4.4.0
239 */
240 public function register_block() {
241 /**
242 * Registering it here so translation works
243 *
244 * @see https://core.trac.wordpress.org/ticket/54797#comment:20
245 */
246 wp_register_script(
247 'elasticpress-comments-editor-script',
248 EP_URL . 'dist/js/comments-block-script.js',
249 Utils\get_asset_info( 'comments-block-script', 'dependencies' ),
250 Utils\get_asset_info( 'comments-block-script', 'version' ),
251 true
252 );
253
254 wp_set_script_translations( 'elasticpress-comments-editor-script', 'elasticpress' );
255
256 wp_localize_script(
257 'elasticpress-comments-editor-script',
258 'epComments',
259 [
260 'searchablePostTypes' => self::get_searchable_post_types(),
261 ]
262 );
263
264 register_block_type_from_metadata(
265 EP_PATH . 'assets/js/blocks/comments',
266 [
267 'render_callback' => [ $this, 'render_block' ],
268 ]
269 );
270 }
271
272 /**
273 * Render block.
274 *
275 * @param array $attributes Block attributes
276 * @since 4.4.0
277 * @return string
278 */
279 public function render_block( $attributes ) {
280 $wrapper_id = 'ep-search-comments-' . uniqid();
281 $wrapper_attributes = get_block_wrapper_attributes(
282 array(
283 'id' => $wrapper_id,
284 'class' => 'ep-widget-search-comments',
285 )
286 );
287
288 $label = ! empty( $attributes['label'] )
289 ? sprintf(
290 '<label for="%1$s-s">%2$s</label>',
291 esc_attr( $wrapper_id ),
292 wp_kses_post( $attributes['label'] )
293 )
294 : '';
295
296 $post_types_input = ! empty( $attributes['postTypes'] )
297 ? sprintf(
298 '<input class="ep-widget-search-comments-post-type" type="hidden" id="%1$s-post-type" value="%2$s">',
299 esc_attr( $wrapper_id ),
300 esc_attr( implode( ',', $attributes['postTypes'] ) )
301 )
302 : '';
303
304 $block_html = sprintf(
305 '<div %1$s>%2$s%3$s</div>',
306 $wrapper_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
307 $label,
308 $post_types_input
309 );
310
311 return $block_html;
312 }
313
314 /**
315 * Hide the legacy widget.
316 *
317 * Hides the legacy widget in favor of the Block when the block editor
318 * is in use and the legacy widget has not been used.
319 *
320 * @since 4.4.0
321 * @param array $widgets An array of excluded widget-type IDs.
322 * @return array array of excluded widget-type IDs to hide.
323 */
324 public function hide_legacy_widget( $widgets ) {
325 $widgets[] = 'ep-comments';
326
327 return $widgets;
328 }
329 }
330