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