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