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