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