PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.6
Jetpack – WP Security, Backup, Speed, & Growth v6.6
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 in Jetpack – WP Security, Backup, Speed, & Growth 6.6, at modules/widgets/search.php

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