|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Provides a widget to show available/selected filters on searches |
|
5
|
|
|
*/ |
|
6
|
|
|
class Jetpack_Search_Widget extends WP_Widget { |
|
7
|
|
|
|
|
8
|
|
|
protected $jetpack_search; |
|
9
|
|
|
|
|
10
|
|
|
const DEFAULT_FILTER_COUNT = 5; |
|
11
|
|
|
const DEFAULT_SORT = 'relevance_desc'; |
|
12
|
|
|
|
|
13
|
|
|
function __construct() { |
|
14
|
|
|
if ( ! class_exists( 'Jetpack_Search' ) ) { |
|
15
|
|
|
return; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
parent::__construct( |
|
19
|
|
|
'jetpack-search-filters', |
|
20
|
|
|
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
|
21
|
|
|
apply_filters( 'jetpack_widget_name', esc_html__( 'Search', 'jetpack' ) ), |
|
22
|
|
|
array( |
|
23
|
|
|
'classname' => 'jetpack-filters widget_search', |
|
24
|
|
|
'description' => __( 'Displays Jetpack Search box and filters.', 'jetpack' ), |
|
25
|
|
|
) |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$this->jetpack_search = Jetpack_Search::instance(); |
|
29
|
|
|
|
|
30
|
|
|
if ( is_admin() ) { |
|
31
|
|
|
add_action( 'sidebar_admin_setup', array( $this, 'widget_admin_setup' ) ); |
|
32
|
|
|
} else { |
|
33
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
add_action( 'jetpack_search_render_filters_widget_title', array( 'Jetpack_Search_Template_Tags', 'render_widget_title' ), 10, 3 ); |
|
37
|
|
|
add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_available_filters' ), 10, 2 ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function widget_admin_setup() { |
|
41
|
|
|
wp_enqueue_style( 'widget-jetpack-search-filters', plugins_url( 'css/search-widget-admin-ui.css', __FILE__ ) ); |
|
42
|
|
|
|
|
43
|
|
|
// Required for Tracks |
|
44
|
|
|
wp_register_script( |
|
45
|
|
|
'jp-tracks', |
|
46
|
|
|
'//stats.wp.com/w.js', |
|
47
|
|
|
array(), |
|
48
|
|
|
gmdate( 'YW' ), |
|
49
|
|
|
true |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
wp_register_script( |
|
53
|
|
|
'jp-tracks-functions', |
|
54
|
|
|
plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ), |
|
55
|
|
|
array(), |
|
56
|
|
|
JETPACK__VERSION, |
|
57
|
|
|
false |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
wp_register_script( |
|
61
|
|
|
'jetpack-search-widget-admin', |
|
62
|
|
|
plugins_url( 'js/search-widget-admin.js', __FILE__ ), |
|
63
|
|
|
array( 'jquery', 'jquery-ui-sortable', 'jp-tracks', 'jp-tracks-functions' ), |
|
64
|
|
|
JETPACK__VERSION |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
wp_localize_script( 'jetpack-search-widget-admin', 'jetpack_search_filter_admin', array( |
|
68
|
|
|
'defaultFilterCount' => self::DEFAULT_FILTER_COUNT, |
|
69
|
|
|
'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
|
70
|
|
|
'tracksEventData' => array( |
|
71
|
|
|
'is_customizer' => ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) ? 1 : 0, |
|
72
|
|
|
), |
|
73
|
|
|
'i18n' => array( |
|
74
|
|
|
'month' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', false ), |
|
75
|
|
|
'year' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', false ), |
|
76
|
|
|
'monthUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', true ), |
|
77
|
|
|
'yearUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', true ), |
|
78
|
|
|
), |
|
79
|
|
|
) ); |
|
80
|
|
|
|
|
81
|
|
|
wp_enqueue_script( 'jetpack-search-widget-admin' ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Enqueue scripts and styles for the frontend |
|
86
|
|
|
*/ |
|
87
|
|
|
public function enqueue_frontend_scripts() { |
|
88
|
|
|
if ( ! is_active_widget( false, false, $this->id_base, true ) ) { |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
wp_enqueue_script( |
|
93
|
|
|
'jetpack-search-widget', |
|
94
|
|
|
plugins_url( 'js/search-widget.js', __FILE__ ), |
|
95
|
|
|
array( 'jquery' ), |
|
96
|
|
|
JETPACK__VERSION, |
|
97
|
|
|
true |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
wp_enqueue_style( 'jetpack-search-widget', plugins_url( 'modules/search/css/search-widget-frontend.css', JETPACK__PLUGIN_FILE ) ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function get_sort_types() { |
|
104
|
|
|
return array( |
|
105
|
|
|
'relevance|DESC' => is_admin() ? esc_html__( 'Relevance (recommended)', 'jetpack' ) : esc_html__( 'Relevance', 'jetpack' ), |
|
106
|
|
|
'date|DESC' => esc_html__( 'Newest first', 'jetpack' ), |
|
107
|
|
|
'date|ASC' => esc_html__( 'Oldest first', 'jetpack' ) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
function is_for_current_widget( $item ) { |
|
112
|
|
|
return isset( $item['widget_id'] ) && $this->id == $item['widget_id']; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* This method returns a boolean for whether the widget should show site-wide filters for the site. |
|
117
|
|
|
* |
|
118
|
|
|
* This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually |
|
119
|
|
|
* configured filters via `Jetpack_Search::set_filters()`. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 5.7.0 |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool Whether the widget should display site-wide filters or not |
|
124
|
|
|
*/ |
|
125
|
|
|
function should_display_sitewide_filters() { |
|
126
|
|
|
$filter_widgets = get_option( 'widget_jetpack-search-filters' ); |
|
127
|
|
|
|
|
128
|
|
|
// This shouldn't be empty, but just for sanity |
|
129
|
|
|
if ( empty( $filter_widgets ) ) { |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// If any widget has any filters, return false |
|
134
|
|
|
foreach ( $filter_widgets as $number => $widget ) { |
|
135
|
|
|
$widget_id = sprintf( 'jetpack-search-filters-%d', $number ); |
|
136
|
|
|
if ( ! empty( $widget['filters'] ) && is_active_widget( false, $widget_id, 'jetpack-search-filters' ) ) { |
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Responsible for rendering the widget on the frontend |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $args Widgets args supplied by the theme. |
|
148
|
|
|
* @param array $instance The current widget instance. |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
public function widget( $args, $instance ) { |
|
153
|
|
|
$display_filters = false; |
|
154
|
|
|
|
|
155
|
|
|
if ( is_search() ) { |
|
156
|
|
|
if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview( $instance, $this->id ) ) { |
|
|
|
|
|
|
157
|
|
|
$this->jetpack_search->update_search_results_aggregations(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$filters = $this->jetpack_search->get_filters(); |
|
161
|
|
|
|
|
162
|
|
|
if ( ! $this->jetpack_search->are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) { |
|
163
|
|
|
$filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if ( ! empty( $filters ) ) { |
|
167
|
|
|
$display_filters = true; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$title = isset( $instance['title'] ) ? $instance['title'] : ''; |
|
176
|
|
|
|
|
177
|
|
|
if ( empty( $title ) ) { |
|
178
|
|
|
$title = ''; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** This filter is documented in core/src/wp-includes/default-widgets.php */ |
|
182
|
|
|
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
|
183
|
|
|
|
|
184
|
|
|
echo $args['before_widget']; |
|
185
|
|
|
|
|
186
|
|
|
if ( ! empty( $title ) ) { |
|
187
|
|
|
/** |
|
188
|
|
|
* Responsible for displaying the title of the Jetpack Search filters widget. |
|
189
|
|
|
* |
|
190
|
|
|
* @module search |
|
191
|
|
|
* |
|
192
|
|
|
* @since 5.7.0 |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $title The widget's title |
|
195
|
|
|
* @param string $args['before_title'] The HTML tag to display before the title |
|
196
|
|
|
* @param string $args['after_title'] The HTML tag to display after the title |
|
197
|
|
|
*/ |
|
198
|
|
|
do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$default_sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT; |
|
202
|
|
|
list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort ); |
|
203
|
|
|
$current_sort = "{$orderby}|{$order}"; |
|
204
|
|
|
|
|
205
|
|
|
// we need to dynamically inject the sort field into the search box when the search box is enabled, and display |
|
206
|
|
|
// it separately when it's not. |
|
207
|
|
|
if ( ! empty( $instance['search_box_enabled'] ) ) { |
|
208
|
|
|
Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ): ?> |
|
212
|
|
|
<h4 class="jetpack-search-filters-widget__sub-heading"><?php esc_html_e( 'Sort by', 'jetpack' ); ?></h4> |
|
213
|
|
|
<div class="jetpack-search-sort-wrapper"> |
|
214
|
|
|
<select class="jetpack-search-sort"> |
|
215
|
|
View Code Duplication |
<?php foreach( $this->get_sort_types() as $sort => $label ) { ?> |
|
216
|
|
|
<option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>> |
|
217
|
|
|
<?php echo esc_html( $label ); ?> |
|
218
|
|
|
</option> |
|
219
|
|
|
<?php } ?> |
|
220
|
|
|
</select> |
|
221
|
|
|
</div> |
|
222
|
|
|
<?php endif; |
|
223
|
|
|
|
|
224
|
|
|
if ( $display_filters ) { |
|
225
|
|
|
/** |
|
226
|
|
|
* Responsible for rendering filters to narrow down search results. |
|
227
|
|
|
* |
|
228
|
|
|
* @module search |
|
229
|
|
|
* |
|
230
|
|
|
* @since 5.8.0 |
|
231
|
|
|
* |
|
232
|
|
|
* @param array $filters The possible filters for the current query. |
|
233
|
|
|
* @param array $post_types An array of post types to limit filtering to. |
|
234
|
|
|
*/ |
|
235
|
|
|
do_action( |
|
236
|
|
|
'jetpack_search_render_filters', |
|
237
|
|
|
$filters, |
|
|
|
|
|
|
238
|
|
|
isset( $instance['post_types'] ) ? $instance['post_types'] : null |
|
239
|
|
|
); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$this->maybe_render_sort_javascript( $instance, $order, $orderby ); |
|
243
|
|
|
|
|
244
|
|
|
echo $args['after_widget']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Renders JavaScript for the sorting controls on the frontend. |
|
249
|
|
|
* |
|
250
|
|
|
* This JS is a bit complicated, but here's what it's trying to do: |
|
251
|
|
|
* - find the search form |
|
252
|
|
|
* - find the orderby/order fields and set default values |
|
253
|
|
|
* - detect changes to the sort field, if it exists, and use it to set the order field values |
|
254
|
|
|
* |
|
255
|
|
|
* @param array $instance The current widget instance. |
|
256
|
|
|
* @param string $order The order to initialize the select with. |
|
257
|
|
|
* @param string $orderby The orderby to initialize the select with. |
|
258
|
|
|
* @return void |
|
259
|
|
|
*/ |
|
260
|
|
|
private function maybe_render_sort_javascript( $instance, $order, $orderby ) { |
|
261
|
|
|
if ( ! empty( $instance['user_sort_enabled'] ) ) : |
|
262
|
|
|
?> |
|
263
|
|
|
<script type="text/javascript"> |
|
264
|
|
|
jQuery( document ).ready( function( $ ) { |
|
265
|
|
|
var orderByDefault = <?php echo json_encode( $orderby ); ?>, |
|
266
|
|
|
orderDefault = <?php echo json_encode( $order ); ?>, |
|
267
|
|
|
widgetId = <?php echo json_encode( $this->id ); ?>; |
|
268
|
|
|
|
|
269
|
|
|
var container = $('#' + widgetId); |
|
270
|
|
|
var form = container.find('.jetpack-search-form form'); |
|
271
|
|
|
var orderBy = form.find( 'input[name=orderby]'); |
|
272
|
|
|
var order = form.find( 'input[name=order]'); |
|
273
|
|
|
orderBy.val(orderByDefault); |
|
274
|
|
|
order.val(orderDefault); |
|
275
|
|
|
|
|
276
|
|
|
container.find( '.jetpack-search-sort' ).change( function( event ) { |
|
277
|
|
|
var values = event.target.value.split( '|' ); |
|
278
|
|
|
orderBy.val( values[0] ); |
|
279
|
|
|
order.val( values[1] ); |
|
280
|
|
|
|
|
281
|
|
|
form.submit(); |
|
282
|
|
|
}); |
|
283
|
|
|
} ); |
|
284
|
|
|
</script> |
|
285
|
|
|
<?php |
|
286
|
|
|
endif; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
private function sorting_to_wp_query_param( $sort ) { |
|
290
|
|
|
$parts = explode( '|', $sort ); |
|
291
|
|
|
$orderby = isset( $_GET['orderby'] ) |
|
292
|
|
|
? $_GET['orderby'] |
|
293
|
|
|
: $parts[0]; |
|
294
|
|
|
|
|
295
|
|
|
$order = isset( $_GET['order'] ) |
|
|
|
|
|
|
296
|
|
|
? strtoupper( $_GET['order'] ) |
|
297
|
|
|
: ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' ); |
|
298
|
|
|
|
|
299
|
|
|
return array( $orderby, $order ); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
function update( $new_instance, $old_instance ) { |
|
303
|
|
|
$instance = array(); |
|
304
|
|
|
|
|
305
|
|
|
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
|
306
|
|
|
$instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1'; |
|
307
|
|
|
$instance['user_sort_enabled'] = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1'; |
|
308
|
|
|
$instance['sort'] = $new_instance['sort']; |
|
309
|
|
|
$instance['post_types'] = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] ) |
|
310
|
|
|
? array() |
|
311
|
|
|
: array_map( 'sanitize_key', $new_instance['post_types'] ); |
|
312
|
|
|
|
|
313
|
|
|
$filters = array(); |
|
314
|
|
|
foreach ( (array) $new_instance['filter_type'] as $index => $type ) { |
|
315
|
|
|
$count = intval( $new_instance['num_filters'][ $index ] ); |
|
316
|
|
|
$count = min( 50, $count ); // Set max boundary at 20 |
|
317
|
|
|
$count = max( 1, $count ); // Set min boundary at 1 |
|
318
|
|
|
|
|
319
|
|
|
switch ( $type ) { |
|
320
|
|
|
case 'taxonomy': |
|
321
|
|
|
$filters[] = array( |
|
322
|
|
|
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), |
|
323
|
|
|
'type' => 'taxonomy', |
|
324
|
|
|
'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ), |
|
325
|
|
|
'count' => $count, |
|
326
|
|
|
); |
|
327
|
|
|
break; |
|
328
|
|
|
case 'post_type': |
|
329
|
|
|
$filters[] = array( |
|
330
|
|
|
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), |
|
331
|
|
|
'type' => 'post_type', |
|
332
|
|
|
'count' => $count, |
|
333
|
|
|
); |
|
334
|
|
|
break; |
|
335
|
|
|
case 'date_histogram': |
|
336
|
|
|
$filters[] = array( |
|
337
|
|
|
'name' => sanitize_text_field( $new_instance['filter_name'][ $index ] ), |
|
338
|
|
|
'type' => 'date_histogram', |
|
339
|
|
|
'count' => $count, |
|
340
|
|
|
'field' => sanitize_key( $new_instance['date_histogram_field'][ $index ] ), |
|
341
|
|
|
'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ), |
|
342
|
|
|
); |
|
343
|
|
|
break; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
if ( ! empty( $filters ) ) { |
|
348
|
|
|
$instance['filters'] = $filters; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
return $instance; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
function form( $instance ) { |
|
355
|
|
|
$instance = wp_parse_args( (array) $instance, array( |
|
356
|
|
|
'title' => '', |
|
357
|
|
|
'filters' => array( array() ) |
|
358
|
|
|
) ); |
|
359
|
|
|
|
|
360
|
|
|
$title = strip_tags( $instance['title'] ); |
|
361
|
|
|
|
|
362
|
|
|
$hide_filters = $this->jetpack_search->are_filters_by_widget_disabled(); |
|
363
|
|
|
$search_box_enabled = ! isset( $instance['search_box_enabled'] ) || ! empty( $instance['search_box_enabled'] ); |
|
364
|
|
|
$user_sort_enabled = ! empty( $instance['user_sort_enabled'] ); |
|
365
|
|
|
$sort = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT; |
|
366
|
|
|
$classes = sprintf( |
|
367
|
|
|
'jetpack-search-filters-widget %s %s %s', |
|
368
|
|
|
$hide_filters ? 'hide-filters' : '', |
|
369
|
|
|
$search_box_enabled ? '' : 'hide-post-types', |
|
370
|
|
|
$this->id |
|
371
|
|
|
); |
|
372
|
|
|
?> |
|
373
|
|
|
<div class="<?php echo esc_attr( $classes ); ?>"> |
|
374
|
|
|
<p> |
|
375
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
|
376
|
|
|
<?php esc_html_e( 'Title (optional):', 'jetpack' ); ?> |
|
377
|
|
|
</label> |
|
378
|
|
|
<input |
|
379
|
|
|
class="widefat" |
|
380
|
|
|
id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" |
|
381
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" |
|
382
|
|
|
type="text" |
|
383
|
|
|
value="<?php echo esc_attr( $title ); ?>" |
|
384
|
|
|
/> |
|
385
|
|
|
</p> |
|
386
|
|
|
|
|
387
|
|
|
<p> |
|
388
|
|
|
<label> |
|
389
|
|
|
<input |
|
390
|
|
|
type="checkbox" |
|
391
|
|
|
class="jetpack-search-filters-widget__search-box-enabled" |
|
392
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>" |
|
393
|
|
|
<?php checked( $search_box_enabled ); ?> |
|
394
|
|
|
/> |
|
395
|
|
|
<?php esc_html_e( 'Show search box', 'jetpack' ); ?> |
|
396
|
|
|
</label> |
|
397
|
|
|
</p> |
|
398
|
|
|
<p> |
|
399
|
|
|
<label> |
|
400
|
|
|
<input |
|
401
|
|
|
type="checkbox" |
|
402
|
|
|
class="jetpack-search-filters-widget__sort-controls-enabled" |
|
403
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>" |
|
404
|
|
|
<?php checked( $user_sort_enabled ); ?> |
|
405
|
|
|
/> |
|
406
|
|
|
<?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?> |
|
407
|
|
|
</label> |
|
408
|
|
|
</p> |
|
409
|
|
|
|
|
410
|
|
|
<p class="jetpack-search-filters-widget__post-types-select"> |
|
411
|
|
|
<label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label> |
|
412
|
|
|
<?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?> |
|
413
|
|
|
<label> |
|
414
|
|
|
<input |
|
415
|
|
|
type="checkbox" |
|
416
|
|
|
value="<?php echo esc_attr( $post_type->name ); ?>" |
|
417
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]" |
|
418
|
|
|
<?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'] ) ); ?> |
|
419
|
|
|
/> |
|
420
|
|
|
<?php echo esc_html( $post_type->label ); ?> |
|
421
|
|
|
</label> |
|
422
|
|
|
<?php endforeach; ?> |
|
423
|
|
|
</p> |
|
424
|
|
|
|
|
425
|
|
|
<p> |
|
426
|
|
|
<label> |
|
427
|
|
|
<?php esc_html_e( 'Default sort order:', 'jetpack' ); ?> |
|
428
|
|
|
<select |
|
429
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>" |
|
430
|
|
|
class="widefat jetpack-search-filters-widget__sort-order"> |
|
431
|
|
View Code Duplication |
<?php foreach( $this->get_sort_types() as $sort_type => $label ) { ?> |
|
432
|
|
|
<option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $sort, $sort_type ); ?>> |
|
433
|
|
|
<?php echo esc_html( $label ); ?> |
|
434
|
|
|
</option> |
|
435
|
|
|
<?php } ?> |
|
436
|
|
|
</select> |
|
437
|
|
|
</label> |
|
438
|
|
|
</p> |
|
439
|
|
|
|
|
440
|
|
|
<?php if ( ! $hide_filters ): ?> |
|
441
|
|
|
<script class="jetpack-search-filters-widget__filter-template" type="text/template"> |
|
442
|
|
|
<?php echo $this->render_widget_edit_filter( array(), true ); ?> |
|
443
|
|
|
</script> |
|
444
|
|
|
<div class="jetpack-search-filters-widget__filters"> |
|
445
|
|
|
<?php foreach ( (array) $instance['filters'] as $filter ) : ?> |
|
446
|
|
|
<?php $this->render_widget_edit_filter( $filter ); ?> |
|
447
|
|
|
<?php endforeach; ?> |
|
448
|
|
|
</div> |
|
449
|
|
|
<p class="jetpack-search-filters-widget__add-filter-wrapper"> |
|
450
|
|
|
<a class="button jetpack-search-filters-widget__add-filter" href="#"> |
|
451
|
|
|
<?php esc_html_e( 'Add a filter', 'jetpack' ); ?> |
|
452
|
|
|
</a> |
|
453
|
|
|
</p> |
|
454
|
|
|
<noscript> |
|
455
|
|
|
<p class="jetpack-search-filters-help"> |
|
456
|
|
|
<?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?> |
|
457
|
|
|
</p> |
|
458
|
|
|
</noscript> |
|
459
|
|
|
<?php if ( is_customize_preview() ) : ?> |
|
460
|
|
|
<p class="jetpack-search-filters-help"> |
|
461
|
|
|
<a href="https://jetpack.com/support/search/#filters-not-showing-up" target="_blank"> |
|
462
|
|
|
<?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?> |
|
463
|
|
|
</a> |
|
464
|
|
|
</p> |
|
465
|
|
|
<?php endif; ?> |
|
466
|
|
|
<?php endif; ?> |
|
467
|
|
|
</div> |
|
468
|
|
|
<?php |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* We basically render the values in two formats: |
|
473
|
|
|
* - underscore template |
|
474
|
|
|
* - native PHP |
|
475
|
|
|
* |
|
476
|
|
|
* This is so we can use the same code to render a client-side and server-side template. |
|
477
|
|
|
*/ |
|
478
|
|
|
private function render_widget_attr( $name, $value, $is_template ) { |
|
479
|
|
|
echo $is_template ? "<%= $name %>" : esc_attr( $value ); |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* See above ^^ |
|
484
|
|
|
*/ |
|
485
|
|
|
private function render_widget_option_selected( $name, $value, $compare, $is_template ) { |
|
486
|
|
|
$compare_json = json_encode( $compare ); |
|
487
|
|
|
echo $is_template ? "<%= $compare_json === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare ); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
/** |
|
491
|
|
|
* Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin. |
|
492
|
|
|
* |
|
493
|
|
|
* We use this method for two purposes - rendering the fields server-side, and also rendering a script template for underscore. |
|
494
|
|
|
* |
|
495
|
|
|
* @since 5.7.0 |
|
496
|
|
|
* |
|
497
|
|
|
* @param array $filter |
|
498
|
|
|
*/ |
|
499
|
|
|
function render_widget_edit_filter( $filter, $is_template = false ) { |
|
500
|
|
|
$args = wp_parse_args( $filter, array( |
|
501
|
|
|
'name' => '', |
|
502
|
|
|
'type' => 'taxonomy', |
|
503
|
|
|
'taxonomy' => '', |
|
504
|
|
|
'post_type' => '', |
|
505
|
|
|
'field' => '', |
|
506
|
|
|
'interval' => '', |
|
507
|
|
|
'count' => self::DEFAULT_FILTER_COUNT, |
|
508
|
|
|
) ); |
|
509
|
|
|
|
|
510
|
|
|
$args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args ); |
|
511
|
|
|
|
|
512
|
|
|
?> |
|
513
|
|
|
<div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>"> |
|
514
|
|
|
<p class="jetpack-search-filters-widget__type-select"> |
|
515
|
|
|
<label> |
|
516
|
|
|
<?php esc_html_e( 'Filter Type:', 'jetpack' ); ?> |
|
517
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select"> |
|
518
|
|
|
<option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>> |
|
519
|
|
|
<?php esc_html_e( 'Taxonomy', 'jetpack' ); ?> |
|
520
|
|
|
</option> |
|
521
|
|
|
<option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>> |
|
522
|
|
|
<?php esc_html_e( 'Post Type', 'jetpack' ); ?> |
|
523
|
|
|
</option> |
|
524
|
|
|
<option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>> |
|
525
|
|
|
<?php esc_html_e( 'Date', 'jetpack' ); ?> |
|
526
|
|
|
</option> |
|
527
|
|
|
</select> |
|
528
|
|
|
</label> |
|
529
|
|
|
</p> |
|
530
|
|
|
|
|
531
|
|
|
<p class="jetpack-search-filters-widget__taxonomy-select"> |
|
532
|
|
|
<label> |
|
533
|
|
|
<?php esc_html_e( 'Choose a taxonomy:', 'jetpack' ); $seen_taxonomy_labels = array(); ?> |
|
|
|
|
|
|
534
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select"> |
|
535
|
|
|
<?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?> |
|
536
|
|
|
<option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>> |
|
537
|
|
|
<?php |
|
538
|
|
|
$label = in_array( $taxonomy->label, $seen_taxonomy_labels ) |
|
539
|
|
|
? sprintf( |
|
540
|
|
|
/* 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. */ |
|
541
|
|
|
_x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ), |
|
542
|
|
|
$taxonomy->label, |
|
543
|
|
|
$taxonomy->name |
|
544
|
|
|
) |
|
545
|
|
|
: $taxonomy->label; |
|
546
|
|
|
echo esc_html( $label ); |
|
547
|
|
|
$seen_taxonomy_labels[] = $taxonomy->label; |
|
548
|
|
|
?> |
|
549
|
|
|
</option> |
|
550
|
|
|
<?php endforeach; ?> |
|
551
|
|
|
</select> |
|
552
|
|
|
</label> |
|
553
|
|
|
</p> |
|
554
|
|
|
|
|
555
|
|
|
<p class="jetpack-search-filters-widget__date-histogram-select"> |
|
556
|
|
|
<label> |
|
557
|
|
|
<?php esc_html_e( 'Choose a field:', 'jetpack' ); ?> |
|
558
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select"> |
|
559
|
|
|
<option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>> |
|
560
|
|
|
<?php esc_html_e( 'Date', 'jetpack' ); ?> |
|
561
|
|
|
</option> |
|
562
|
|
|
<option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>> |
|
563
|
|
|
<?php esc_html_e( 'Date GMT', 'jetpack' ); ?> |
|
564
|
|
|
</option> |
|
565
|
|
|
<option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>> |
|
566
|
|
|
<?php esc_html_e( 'Modified', 'jetpack' ); ?> |
|
567
|
|
|
</option> |
|
568
|
|
|
<option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>> |
|
569
|
|
|
<?php esc_html_e( 'Modified GMT', 'jetpack' ); ?> |
|
570
|
|
|
</option> |
|
571
|
|
|
</select> |
|
572
|
|
|
</label> |
|
573
|
|
|
</p> |
|
574
|
|
|
|
|
575
|
|
|
<p class="jetpack-search-filters-widget__date-histogram-select"> |
|
576
|
|
|
<label> |
|
577
|
|
|
<?php esc_html_e( 'Choose an interval:' ); ?> |
|
578
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select"> |
|
579
|
|
|
<option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>> |
|
580
|
|
|
<?php esc_html_e( 'Month', 'jetpack' ); ?> |
|
581
|
|
|
</option> |
|
582
|
|
|
<option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>> |
|
583
|
|
|
<?php esc_html_e( 'Year', 'jetpack' ); ?> |
|
584
|
|
|
</option> |
|
585
|
|
|
</select> |
|
586
|
|
|
</label> |
|
587
|
|
|
</p> |
|
588
|
|
|
|
|
589
|
|
|
<p class="jetpack-search-filters-widget__title"> |
|
590
|
|
|
<label> |
|
591
|
|
|
<?php esc_html_e( 'Title:', 'jetpack' ); ?> |
|
592
|
|
|
<input |
|
593
|
|
|
class="widefat" |
|
594
|
|
|
type="text" |
|
595
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]" |
|
596
|
|
|
value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>" |
|
597
|
|
|
placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>" |
|
598
|
|
|
/> |
|
599
|
|
|
</label> |
|
600
|
|
|
</p> |
|
601
|
|
|
|
|
602
|
|
|
<p> |
|
603
|
|
|
<label> |
|
604
|
|
|
<?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?> |
|
605
|
|
|
<input |
|
606
|
|
|
class="widefat filter-count" |
|
607
|
|
|
name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]" |
|
608
|
|
|
type="number" |
|
609
|
|
|
value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>" |
|
610
|
|
|
min="1" |
|
611
|
|
|
max="50" |
|
612
|
|
|
step="1" |
|
613
|
|
|
required |
|
614
|
|
|
/> |
|
615
|
|
|
</label> |
|
616
|
|
|
</p> |
|
617
|
|
|
|
|
618
|
|
|
<p class="jetpack-search-filters-widget__controls"> |
|
619
|
|
|
<a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a> |
|
620
|
|
|
</p> |
|
621
|
|
|
</div> |
|
622
|
|
|
<?php } |
|
623
|
|
|
} |
|
624
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.