PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9.3
Jetpack – WP Security, Backup, Speed, & Growth v7.9.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / search.php
search.php
823 lines 26.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Jetpack Search: Jetpack_Search_Widget class
4 *
5 * @package Jetpack
6 * @subpackage Jetpack Search
7 * @since 5.0.0
8 */
9
10 use Automattic\Jetpack\Constants;
11
12 add_action( 'widgets_init', 'jetpack_search_widget_init' );
13
14 function jetpack_search_widget_init() {
15 if ( ! Jetpack::is_active() || ! Jetpack_Plan::supports( 'search' ) ) {
16 return;
17 }
18
19 require_once JETPACK__PLUGIN_DIR . 'modules/search/class.jetpack-search-helpers.php';
20
21 register_widget( 'Jetpack_Search_Widget' );
22 }
23
24 /**
25 * Provides a widget to show available/selected filters on searches.
26 *
27 * @since 5.0.0
28 *
29 * @see WP_Widget
30 */
31 class Jetpack_Search_Widget extends WP_Widget {
32
33 /**
34 * The Jetpack_Search instance.
35 *
36 * @since 5.7.0
37 * @var Jetpack_Search
38 */
39 protected $jetpack_search;
40
41 /**
42 * Number of aggregations (filters) to show by default.
43 *
44 * @since 5.8.0
45 * @var int
46 */
47 const DEFAULT_FILTER_COUNT = 5;
48
49 /**
50 * Default sort order for search results.
51 *
52 * @since 5.8.0
53 * @var string
54 */
55 const DEFAULT_SORT = 'relevance_desc';
56
57 /**
58 * Jetpack_Search_Widget constructor.
59 *
60 * @since 5.0.0
61 */
62 public function __construct( $name = null ) {
63 if ( empty( $name ) ) {
64 $name = esc_html__( 'Search', 'jetpack' );
65 }
66 parent::__construct(
67 Jetpack_Search_Helpers::FILTER_WIDGET_BASE,
68 /** This filter is documented in modules/widgets/facebook-likebox.php */
69 apply_filters( 'jetpack_widget_name', $name ),
70 array(
71 'classname' => 'jetpack-filters widget_search',
72 'description' => __( 'Replaces the default search with an Elasticsearch-powered search interface and filters.', 'jetpack' ),
73 )
74 );
75
76 if (
77 Jetpack_Search_Helpers::is_active_widget( $this->id ) &&
78 ! $this->is_search_active()
79 ) {
80 $this->activate_search();
81 }
82
83 if ( is_admin() ) {
84 add_action( 'sidebar_admin_setup', array( $this, 'widget_admin_setup' ) );
85 } else {
86 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
87 }
88
89 add_action( 'jetpack_search_render_filters_widget_title', array( 'Jetpack_Search_Template_Tags', 'render_widget_title' ), 10, 3 );
90 add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_available_filters' ), 10, 2 );
91 }
92
93 /**
94 * Check whether search is currently active
95 *
96 * @since 6.3
97 */
98 public function is_search_active() {
99 return Jetpack::is_module_active( 'search' );
100 }
101
102 /**
103 * Activate search
104 *
105 * @since 6.3
106 */
107 public function activate_search() {
108 Jetpack::activate_module( 'search', false, false );
109 }
110
111
112 /**
113 * Enqueues the scripts and styles needed for the customizer.
114 *
115 * @since 5.7.0
116 */
117 public function widget_admin_setup() {
118 wp_enqueue_style( 'widget-jetpack-search-filters', plugins_url( 'search/css/search-widget-admin-ui.css', __FILE__ ) );
119
120 // Required for Tracks
121 wp_register_script(
122 'jp-tracks',
123 '//stats.wp.com/w.js',
124 array(),
125 gmdate( 'YW' ),
126 true
127 );
128
129 wp_register_script(
130 'jp-tracks-functions',
131 plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
132 array(),
133 JETPACK__VERSION,
134 false
135 );
136
137 wp_register_script(
138 'jetpack-search-widget-admin',
139 plugins_url( 'search/js/search-widget-admin.js', __FILE__ ),
140 array( 'jquery', 'jquery-ui-sortable', 'jp-tracks', 'jp-tracks-functions' ),
141 JETPACK__VERSION
142 );
143
144 wp_localize_script(
145 'jetpack-search-widget-admin', 'jetpack_search_filter_admin', array(
146 'defaultFilterCount' => self::DEFAULT_FILTER_COUNT,
147 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
148 'tracksEventData' => array(
149 'is_customizer' => (int) is_customize_preview(),
150 ),
151 'i18n' => array(
152 'month' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', false ),
153 'year' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', false ),
154 'monthUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', true ),
155 'yearUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', true ),
156 ),
157 )
158 );
159
160 wp_enqueue_script( 'jetpack-search-widget-admin' );
161 }
162
163 /**
164 * Enqueue scripts and styles for the frontend.
165 *
166 * @since 5.8.0
167 */
168 public function enqueue_frontend_scripts() {
169 if ( ! is_active_widget( false, false, $this->id_base, true ) || Constants::is_true( 'JETPACK_SEARCH_PROTOTYPE' ) ) {
170 return;
171 }
172
173 wp_enqueue_script(
174 'jetpack-search-widget',
175 plugins_url( 'search/js/search-widget.js', __FILE__ ),
176 array( 'jquery' ),
177 JETPACK__VERSION,
178 true
179 );
180
181 wp_enqueue_style( 'jetpack-search-widget', plugins_url( 'search/css/search-widget-frontend.css', __FILE__ ) );
182 }
183
184 /**
185 * Get the list of valid sort types/orders.
186 *
187 * @since 5.8.0
188 *
189 * @return array The sort orders.
190 */
191 private function get_sort_types() {
192 return array(
193 'relevance|DESC' => is_admin() ? esc_html__( 'Relevance (recommended)', 'jetpack' ) : esc_html__( 'Relevance', 'jetpack' ),
194 'date|DESC' => esc_html__( 'Newest first', 'jetpack' ),
195 'date|ASC' => esc_html__( 'Oldest first', 'jetpack' ),
196 );
197 }
198
199 /**
200 * Callback for an array_filter() call in order to only get filters for the current widget.
201 *
202 * @see Jetpack_Search_Widget::widget()
203 *
204 * @since 5.7.0
205 *
206 * @param array $item Filter item.
207 *
208 * @return bool Whether the current filter item is for the current widget.
209 */
210 function is_for_current_widget( $item ) {
211 return isset( $item['widget_id'] ) && $this->id == $item['widget_id'];
212 }
213
214 /**
215 * This method returns a boolean for whether the widget should show site-wide filters for the site.
216 *
217 * This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually
218 * configured filters via `Jetpack_Search::set_filters()`.
219 *
220 * @since 5.7.0
221 *
222 * @return bool Whether the widget should display site-wide filters or not.
223 */
224 public function should_display_sitewide_filters() {
225 $filter_widgets = get_option( 'widget_jetpack-search-filters' );
226
227 // This shouldn't be empty, but just for sanity
228 if ( empty( $filter_widgets ) ) {
229 return false;
230 }
231
232 // If any widget has any filters, return false
233 foreach ( $filter_widgets as $number => $widget ) {
234 $widget_id = sprintf( '%s-%d', $this->id_base, $number );
235 if ( ! empty( $widget['filters'] ) && is_active_widget( false, $widget_id, $this->id_base ) ) {
236 return false;
237 }
238 }
239
240 return true;
241 }
242
243 public function jetpack_search_populate_defaults( $instance ) {
244 $instance = wp_parse_args(
245 (array) $instance, array(
246 'title' => '',
247 'search_box_enabled' => true,
248 'user_sort_enabled' => true,
249 'sort' => self::DEFAULT_SORT,
250 'filters' => array( array() ),
251 'post_types' => array(),
252 )
253 );
254
255 return $instance;
256 }
257
258 /**
259 * Responsible for rendering the widget on the frontend.
260 *
261 * @since 5.0.0
262 *
263 * @param array $args Widgets args supplied by the theme.
264 * @param array $instance The current widget instance.
265 */
266 public function widget( $args, $instance ) {
267 $instance = $this->jetpack_search_populate_defaults( $instance );
268
269 $display_filters = false;
270
271 if ( Jetpack::is_development_mode() ) {
272 echo $args['before_widget'];
273 ?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper">
274 <div class="jetpack-search-sort-wrapper">
275 <label>
276 <?php esc_html_e( 'Jetpack Search not supported in Development Mode', 'jetpack' ); ?>
277 </label>
278 </div>
279 </div><?php
280 echo $args['after_widget'];
281 return;
282 }
283
284 if ( is_search() ) {
285 if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() ) {
286 Jetpack_Search::instance()->update_search_results_aggregations();
287 }
288
289 $filters = Jetpack_Search::instance()->get_filters();
290
291 if ( ! Jetpack_Search_Helpers::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) {
292 $filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) );
293 }
294
295 if ( ! empty( $filters ) ) {
296 $display_filters = true;
297 }
298 }
299
300 if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) {
301 return;
302 }
303
304 $title = isset( $instance['title'] ) ? $instance['title'] : '';
305
306 if ( empty( $title ) ) {
307 $title = '';
308 }
309
310 /** This filter is documented in core/src/wp-includes/default-widgets.php */
311 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
312
313 echo $args['before_widget'];
314 ?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper" class="<?php
315 echo Constants::is_true( 'JETPACK_SEARCH_PROTOTYPE' ) ? 'jetpack-instant-search-wrapper' : '' ?>">
316 <?php
317
318 if ( ! empty( $title ) ) {
319 /**
320 * Responsible for displaying the title of the Jetpack Search filters widget.
321 *
322 * @module search
323 *
324 * @since 5.7.0
325 *
326 * @param string $title The widget's title
327 * @param string $args['before_title'] The HTML tag to display before the title
328 * @param string $args['after_title'] The HTML tag to display after the title
329 */
330 do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
331 }
332
333 $default_sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT;
334 list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort );
335 $current_sort = "{$orderby}|{$order}";
336
337 // we need to dynamically inject the sort field into the search box when the search box is enabled, and display
338 // it separately when it's not.
339 if ( ! empty( $instance['search_box_enabled'] ) ) {
340 Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
341 }
342
343 if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) :
344 ?>
345 <div class="jetpack-search-sort-wrapper">
346 <label>
347 <?php esc_html_e( 'Sort by', 'jetpack' ); ?>
348 <select class="jetpack-search-sort">
349 <?php foreach ( $this->get_sort_types() as $sort => $label ) { ?>
350 <option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>>
351 <?php echo esc_html( $label ); ?>
352 </option>
353 <?php } ?>
354 </select>
355 </label>
356 </div>
357 <?php
358 endif;
359
360 if ( $display_filters ) {
361 /**
362 * Responsible for rendering filters to narrow down search results.
363 *
364 * @module search
365 *
366 * @since 5.8.0
367 *
368 * @param array $filters The possible filters for the current query.
369 * @param array $post_types An array of post types to limit filtering to.
370 */
371 do_action(
372 'jetpack_search_render_filters',
373 $filters,
374 isset( $instance['post_types'] ) ? $instance['post_types'] : null
375 );
376 }
377
378 $this->maybe_render_sort_javascript( $instance, $order, $orderby );
379
380 echo '</div>';
381 echo $args['after_widget'];
382 }
383
384 /**
385 * Renders JavaScript for the sorting controls on the frontend.
386 *
387 * This JS is a bit complicated, but here's what it's trying to do:
388 * - find the search form
389 * - find the orderby/order fields and set default values
390 * - detect changes to the sort field, if it exists, and use it to set the order field values
391 *
392 * @since 5.8.0
393 *
394 * @param array $instance The current widget instance.
395 * @param string $order The order to initialize the select with.
396 * @param string $orderby The orderby to initialize the select with.
397 */
398 private function maybe_render_sort_javascript( $instance, $order, $orderby ) {
399 if ( Constants::is_true( 'JETPACK_SEARCH_PROTOTYPE' ) ) {
400 return;
401 }
402
403 if ( ! empty( $instance['user_sort_enabled'] ) ) :
404 ?>
405 <script type="text/javascript">
406 jQuery( document ).ready( function( $ ) {
407 var orderByDefault = '<?php echo 'date' === $orderby ? 'date' : 'relevance'; ?>',
408 orderDefault = '<?php echo 'ASC' === $order ? 'ASC' : 'DESC'; ?>',
409 widgetId = decodeURIComponent( '<?php echo rawurlencode( $this->id ); ?>' ),
410 searchQuery = decodeURIComponent( '<?php echo rawurlencode( get_query_var( 's', '' ) ); ?>' ),
411 isSearch = <?php echo (int) is_search(); ?>;
412
413 var container = $( '#' + widgetId + '-wrapper' ),
414 form = container.find('.jetpack-search-form form'),
415 orderBy = form.find( 'input[name=orderby]'),
416 order = form.find( 'input[name=order]'),
417 searchInput = form.find( 'input[name="s"]' );
418
419 orderBy.val( orderByDefault );
420 order.val( orderDefault );
421
422 // Some themes don't set the search query, which results in the query being lost
423 // when doing a sort selection. So, if the query isn't set, let's set it now. This approach
424 // is chosen over running a regex over HTML for every search query performed.
425 if ( isSearch && ! searchInput.val() ) {
426 searchInput.val( searchQuery );
427 }
428
429 searchInput.addClass( 'show-placeholder' );
430
431 container.find( '.jetpack-search-sort' ).change( function( event ) {
432 var values = event.target.value.split( '|' );
433 orderBy.val( values[0] );
434 order.val( values[1] );
435
436 form.submit();
437 });
438 } );
439 </script>
440 <?php
441 endif;
442 }
443
444 /**
445 * Convert a sort string into the separate order by and order parts.
446 *
447 * @since 5.8.0
448 *
449 * @param string $sort A sort string.
450 *
451 * @return array Order by and order.
452 */
453 private function sorting_to_wp_query_param( $sort ) {
454 $parts = explode( '|', $sort );
455 $orderby = isset( $_GET['orderby'] )
456 ? $_GET['orderby']
457 : $parts[0];
458
459 $order = isset( $_GET['order'] )
460 ? strtoupper( $_GET['order'] )
461 : ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' );
462
463 return array( $orderby, $order );
464 }
465
466 /**
467 * Updates a particular instance of the widget. Validates and sanitizes the options.
468 *
469 * @since 5.0.0
470 *
471 * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form().
472 * @param array $old_instance Old settings for this instance.
473 *
474 * @return array Settings to save.
475 */
476 public function update( $new_instance, $old_instance ) {
477 $instance = array();
478
479 $instance['title'] = sanitize_text_field( $new_instance['title'] );
480 $instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1';
481 $instance['user_sort_enabled'] = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1';
482 $instance['sort'] = $new_instance['sort'];
483 $instance['post_types'] = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] )
484 ? array()
485 : array_map( 'sanitize_key', $new_instance['post_types'] );
486
487 $filters = array();
488 if ( isset( $new_instance['filter_type'] ) ) {
489 foreach ( (array) $new_instance['filter_type'] as $index => $type ) {
490 $count = intval( $new_instance['num_filters'][ $index ] );
491 $count = min( 50, $count ); // Set max boundary at 50.
492 $count = max( 1, $count ); // Set min boundary at 1.
493
494 switch ( $type ) {
495 case 'taxonomy':
496 $filters[] = array(
497 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
498 'type' => 'taxonomy',
499 'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ),
500 'count' => $count,
501 );
502 break;
503 case 'post_type':
504 $filters[] = array(
505 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
506 'type' => 'post_type',
507 'count' => $count,
508 );
509 break;
510 case 'date_histogram':
511 $filters[] = array(
512 'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
513 'type' => 'date_histogram',
514 'count' => $count,
515 'field' => sanitize_key( $new_instance['date_histogram_field'][ $index ] ),
516 'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ),
517 );
518 break;
519 }
520 }
521 }
522
523 if ( ! empty( $filters ) ) {
524 $instance['filters'] = $filters;
525 }
526
527 return $instance;
528 }
529
530 /**
531 * Outputs the settings update form.
532 *
533 * @since 5.0.0
534 *
535 * @param array $instance Current settings.
536 */
537 public function form( $instance ) {
538 $instance = $this->jetpack_search_populate_defaults( $instance );
539
540 $title = strip_tags( $instance['title'] );
541
542 $hide_filters = Jetpack_Search_Helpers::are_filters_by_widget_disabled();
543
544 $classes = sprintf(
545 'jetpack-search-filters-widget %s %s %s',
546 $hide_filters ? 'hide-filters' : '',
547 $instance['search_box_enabled'] ? '' : 'hide-post-types',
548 $this->id
549 );
550 ?>
551 <div class="<?php echo esc_attr( $classes ); ?>">
552 <p>
553 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
554 <?php esc_html_e( 'Title (optional):', 'jetpack' ); ?>
555 </label>
556 <input
557 class="widefat"
558 id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
559 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
560 type="text"
561 value="<?php echo esc_attr( $title ); ?>"
562 />
563 </p>
564
565 <p>
566 <label>
567 <input
568 type="checkbox"
569 class="jetpack-search-filters-widget__search-box-enabled"
570 name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>"
571 <?php checked( $instance['search_box_enabled'] ); ?>
572 />
573 <?php esc_html_e( 'Show search box', 'jetpack' ); ?>
574 </label>
575 </p>
576 <p>
577 <label>
578 <input
579 type="checkbox"
580 class="jetpack-search-filters-widget__sort-controls-enabled"
581 name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>"
582 <?php checked( $instance['user_sort_enabled'] ); ?>
583 <?php disabled( ! $instance['search_box_enabled'] ); ?>
584 />
585 <?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?>
586 </label>
587 </p>
588
589 <p class="jetpack-search-filters-widget__post-types-select">
590 <label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label>
591 <?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?>
592 <label>
593 <input
594 type="checkbox"
595 value="<?php echo esc_attr( $post_type->name ); ?>"
596 name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]"
597 <?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'] ) ); ?>
598 />&nbsp;
599 <?php echo esc_html( $post_type->label ); ?>
600 </label>
601 <?php endforeach; ?>
602 </p>
603
604 <p>
605 <label>
606 <?php esc_html_e( 'Default sort order:', 'jetpack' ); ?>
607 <select
608 name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>"
609 class="widefat jetpack-search-filters-widget__sort-order">
610 <?php foreach ( $this->get_sort_types() as $sort_type => $label ) { ?>
611 <option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $instance['sort'], $sort_type ); ?>>
612 <?php echo esc_html( $label ); ?>
613 </option>
614 <?php } ?>
615 </select>
616 </label>
617 </p>
618
619 <?php if ( ! $hide_filters ) : ?>
620 <script class="jetpack-search-filters-widget__filter-template" type="text/template">
621 <?php echo $this->render_widget_edit_filter( array(), true ); ?>
622 </script>
623 <div class="jetpack-search-filters-widget__filters">
624 <?php foreach ( (array) $instance['filters'] as $filter ) : ?>
625 <?php $this->render_widget_edit_filter( $filter ); ?>
626 <?php endforeach; ?>
627 </div>
628 <p class="jetpack-search-filters-widget__add-filter-wrapper">
629 <a class="button jetpack-search-filters-widget__add-filter" href="#">
630 <?php esc_html_e( 'Add a filter', 'jetpack' ); ?>
631 </a>
632 </p>
633 <noscript>
634 <p class="jetpack-search-filters-help">
635 <?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?>
636 </p>
637 </noscript>
638 <?php if ( is_customize_preview() ) : ?>
639 <p class="jetpack-search-filters-help">
640 <a href="https://jetpack.com/support/search/#filters-not-showing-up" target="_blank">
641 <?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?>
642 </a>
643 </p>
644 <?php endif; ?>
645 <?php endif; ?>
646 </div>
647 <?php
648 }
649
650 /**
651 * We need to render HTML in two formats: an Underscore template (client-side)
652 * and native PHP (server-side). This helper function allows for easy rendering
653 * of attributes in both formats.
654 *
655 * @since 5.8.0
656 *
657 * @param string $name Attribute name.
658 * @param string $value Attribute value.
659 * @param bool $is_template Whether this is for an Underscore template or not.
660 */
661 private function render_widget_attr( $name, $value, $is_template ) {
662 echo $is_template ? "<%= $name %>" : esc_attr( $value );
663 }
664
665 /**
666 * We need to render HTML in two formats: an Underscore template (client-size)
667 * and native PHP (server-side). This helper function allows for easy rendering
668 * of the "selected" attribute in both formats.
669 *
670 * @since 5.8.0
671 *
672 * @param string $name Attribute name.
673 * @param string $value Attribute value.
674 * @param string $compare Value to compare to the attribute value to decide if it should be selected.
675 * @param bool $is_template Whether this is for an Underscore template or not.
676 */
677 private function render_widget_option_selected( $name, $value, $compare, $is_template ) {
678 $compare_js = rawurlencode( $compare );
679 echo $is_template ? "<%= decodeURIComponent( '$compare_js' ) === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare );
680 }
681
682 /**
683 * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin.
684 *
685 * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore.
686 *
687 * @since 5.7.0
688 *
689 * @param array $filter The filter to render.
690 * @param bool $is_template Whether this is for an Underscore template or not.
691 */
692 public function render_widget_edit_filter( $filter, $is_template = false ) {
693 $args = wp_parse_args(
694 $filter, array(
695 'name' => '',
696 'type' => 'taxonomy',
697 'taxonomy' => '',
698 'post_type' => '',
699 'field' => '',
700 'interval' => '',
701 'count' => self::DEFAULT_FILTER_COUNT,
702 )
703 );
704
705 $args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args );
706
707 ?>
708 <div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>">
709 <p class="jetpack-search-filters-widget__type-select">
710 <label>
711 <?php esc_html_e( 'Filter Type:', 'jetpack' ); ?>
712 <select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select">
713 <option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>>
714 <?php esc_html_e( 'Taxonomy', 'jetpack' ); ?>
715 </option>
716 <option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>>
717 <?php esc_html_e( 'Post Type', 'jetpack' ); ?>
718 </option>
719 <option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>>
720 <?php esc_html_e( 'Date', 'jetpack' ); ?>
721 </option>
722 </select>
723 </label>
724 </p>
725
726 <p class="jetpack-search-filters-widget__taxonomy-select">
727 <label>
728 <?php
729 esc_html_e( 'Choose a taxonomy:', 'jetpack' );
730 $seen_taxonomy_labels = array();
731 ?>
732 <select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select">
733 <?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?>
734 <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>>
735 <?php
736 $label = in_array( $taxonomy->label, $seen_taxonomy_labels )
737 ? sprintf(
738 /* translators: %1$s is the taxonomy name, %2s is the name of its type to help distinguish between several taxonomies with the same name, e.g. category and tag. */
739 _x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ),
740 $taxonomy->label,
741 $taxonomy->name
742 )
743 : $taxonomy->label;
744 echo esc_html( $label );
745 $seen_taxonomy_labels[] = $taxonomy->label;
746 ?>
747 </option>
748 <?php endforeach; ?>
749 </select>
750 </label>
751 </p>
752
753 <p class="jetpack-search-filters-widget__date-histogram-select">
754 <label>
755 <?php esc_html_e( 'Choose a field:', 'jetpack' ); ?>
756 <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select">
757 <option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>>
758 <?php esc_html_e( 'Date', 'jetpack' ); ?>
759 </option>
760 <option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>>
761 <?php esc_html_e( 'Date GMT', 'jetpack' ); ?>
762 </option>
763 <option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>>
764 <?php esc_html_e( 'Modified', 'jetpack' ); ?>
765 </option>
766 <option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>>
767 <?php esc_html_e( 'Modified GMT', 'jetpack' ); ?>
768 </option>
769 </select>
770 </label>
771 </p>
772
773 <p class="jetpack-search-filters-widget__date-histogram-select">
774 <label>
775 <?php esc_html_e( 'Choose an interval:' ); ?>
776 <select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select">
777 <option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>>
778 <?php esc_html_e( 'Month', 'jetpack' ); ?>
779 </option>
780 <option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>>
781 <?php esc_html_e( 'Year', 'jetpack' ); ?>
782 </option>
783 </select>
784 </label>
785 </p>
786
787 <p class="jetpack-search-filters-widget__title">
788 <label>
789 <?php esc_html_e( 'Title:', 'jetpack' ); ?>
790 <input
791 class="widefat"
792 type="text"
793 name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]"
794 value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>"
795 placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>"
796 />
797 </label>
798 </p>
799
800 <p>
801 <label>
802 <?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?>
803 <input
804 class="widefat filter-count"
805 name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]"
806 type="number"
807 value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>"
808 min="1"
809 max="50"
810 step="1"
811 required
812 />
813 </label>
814 </p>
815
816 <p class="jetpack-search-filters-widget__controls">
817 <a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a>
818 </p>
819 </div>
820 <?php
821 }
822 }
823