Completed
Push — update/calendly-embed-code ( 001c02...b82934 )
by
unknown
08:51
created

Jetpack_Search_Widget::widget_non_instant()   F

Complexity

Conditions 18
Paths 585

Size

Total Lines 98

Duplication

Lines 8
Ratio 8.16 %

Importance

Changes 0
Metric Value
cc 18
nc 585
nop 2
dl 8
loc 98
rs 1.0647
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		if ( Jetpack_Search_Options::is_instant_enabled() ) {
96
			add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_instant_filters' ), 10, 2 );
97
		} else {
98
			add_action( 'jetpack_search_render_filters', array( 'Jetpack_Search_Template_Tags', 'render_available_filters' ), 10, 2 );
99
		}
100
	}
101
102
	/**
103
	 * Check whether search is currently active
104
	 *
105
	 * @since 6.3
106
	 */
107
	public function is_search_active() {
108
		return Jetpack::is_module_active( 'search' );
109
	}
110
111
	/**
112
	 * Activate search
113
	 *
114
	 * @since 6.3
115
	 */
116
	public function activate_search() {
117
		Jetpack::activate_module( 'search', false, false );
118
	}
119
120
121
	/**
122
	 * Enqueues the scripts and styles needed for the customizer.
123
	 *
124
	 * @since 5.7.0
125
	 */
126
	public function widget_admin_setup() {
127
		wp_enqueue_style( 'widget-jetpack-search-filters', plugins_url( 'search/css/search-widget-admin-ui.css', __FILE__ ) );
128
129
		// Required for Tracks
130
		wp_register_script(
131
			'jp-tracks',
132
			'//stats.wp.com/w.js',
133
			array(),
134
			gmdate( 'YW' ),
135
			true
136
		);
137
138
		wp_register_script(
139
			'jp-tracks-functions',
140
			plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
141
			array(),
142
			JETPACK__VERSION,
143
			false
144
		);
145
146
		wp_register_script(
147
			'jetpack-search-widget-admin',
148
			plugins_url( 'search/js/search-widget-admin.js', __FILE__ ),
149
			array( 'jquery', 'jquery-ui-sortable', 'jp-tracks', 'jp-tracks-functions' ),
150
			JETPACK__VERSION
151
		);
152
153
		wp_localize_script(
154
			'jetpack-search-widget-admin', 'jetpack_search_filter_admin', array(
155
				'defaultFilterCount' => self::DEFAULT_FILTER_COUNT,
156
				'tracksUserData'     => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
157
				'tracksEventData'    => array(
158
					'is_customizer' => (int) is_customize_preview(),
159
				),
160
				'i18n'               => array(
161
					'month'        => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', false ),
162
					'year'         => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', false ),
163
					'monthUpdated' => Jetpack_Search_Helpers::get_date_filter_type_name( 'month', true ),
164
					'yearUpdated'  => Jetpack_Search_Helpers::get_date_filter_type_name( 'year', true ),
165
				),
166
			)
167
		);
168
169
		wp_enqueue_script( 'jetpack-search-widget-admin' );
170
	}
171
172
	/**
173
	 * Enqueue scripts and styles for the frontend.
174
	 *
175
	 * @since 5.8.0
176
	 */
177
	public function enqueue_frontend_scripts() {
178
		if ( ! is_active_widget( false, false, $this->id_base, true ) || Jetpack_Search_Options::is_instant_enabled() ) {
179
			return;
180
		}
181
182
		wp_enqueue_script(
183
			'jetpack-search-widget',
184
			plugins_url( 'search/js/search-widget.js', __FILE__ ),
185
			array( 'jquery' ),
186
			JETPACK__VERSION,
187
			true
188
		);
189
190
		wp_enqueue_style( 'jetpack-search-widget', plugins_url( 'search/css/search-widget-frontend.css', __FILE__ ) );
191
	}
192
193
	/**
194
	 * Get the list of valid sort types/orders.
195
	 *
196
	 * @since 5.8.0
197
	 *
198
	 * @return array The sort orders.
199
	 */
200
	private function get_sort_types() {
201
		return array(
202
			'relevance|DESC' => is_admin() ? esc_html__( 'Relevance (recommended)', 'jetpack' ) : esc_html__( 'Relevance', 'jetpack' ),
203
			'date|DESC'      => esc_html__( 'Newest first', 'jetpack' ),
204
			'date|ASC'       => esc_html__( 'Oldest first', 'jetpack' ),
205
		);
206
	}
207
208
	/**
209
	 * Callback for an array_filter() call in order to only get filters for the current widget.
210
	 *
211
	 * @see   Jetpack_Search_Widget::widget()
212
	 *
213
	 * @since 5.7.0
214
	 *
215
	 * @param array $item Filter item.
216
	 *
217
	 * @return bool Whether the current filter item is for the current widget.
218
	 */
219
	function is_for_current_widget( $item ) {
220
		return isset( $item['widget_id'] ) && $this->id == $item['widget_id'];
221
	}
222
223
	/**
224
	 * This method returns a boolean for whether the widget should show site-wide filters for the site.
225
	 *
226
	 * This is meant to provide backwards-compatibility for VIP, and other professional plan users, that manually
227
	 * configured filters via `Jetpack_Search::set_filters()`.
228
	 *
229
	 * @since 5.7.0
230
	 *
231
	 * @return bool Whether the widget should display site-wide filters or not.
232
	 */
233
	public function should_display_sitewide_filters() {
234
		$filter_widgets = get_option( 'widget_jetpack-search-filters' );
235
236
		// This shouldn't be empty, but just for sanity
237
		if ( empty( $filter_widgets ) ) {
238
			return false;
239
		}
240
241
		// If any widget has any filters, return false
242
		foreach ( $filter_widgets as $number => $widget ) {
243
			$widget_id = sprintf( '%s-%d', $this->id_base, $number );
244
			if ( ! empty( $widget['filters'] ) && is_active_widget( false, $widget_id, $this->id_base ) ) {
245
				return false;
246
			}
247
		}
248
249
		return true;
250
	}
251
252
	public function jetpack_search_populate_defaults( $instance ) {
253
		$instance = wp_parse_args(
254
			(array) $instance, array(
255
				'title'              => '',
256
				'search_box_enabled' => true,
257
				'user_sort_enabled'  => true,
258
				'sort'               => self::DEFAULT_SORT,
259
				'filters'            => array( array() ),
260
				'post_types'         => array(),
261
			)
262
		);
263
264
		return $instance;
265
	}
266
267
	/**
268
	 * Responsible for rendering the widget on the frontend.
269
	 *
270
	 * @since 5.0.0
271
	 *
272
	 * @param array $args     Widgets args supplied by the theme.
273
	 * @param array $instance The current widget instance.
274
	 */
275
	public function widget( $args, $instance ) {
276
		$instance = $this->jetpack_search_populate_defaults( $instance );
277
278
		if ( ( new Status() )->is_development_mode() ) {
279
			echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
280
			?><div id="<?php echo esc_attr( $this->id ); ?>-wrapper">
281
				<div class="jetpack-search-sort-wrapper">
282
					<label>
283
						<?php esc_html_e( 'Jetpack Search not supported in Development Mode', 'jetpack' ); ?>
284
					</label>
285
				</div>
286
			</div><?php
287
			echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
288
			return;
289
		}
290
291
		if ( Jetpack_Search_Options::is_instant_enabled() ) {
292
			if ( 'jetpack-instant-search-sidebar' === $args['id'] ) {
293
				$this->widget_empty_instant( $args, $instance );
294
			} else {
295
				$this->widget_instant( $args, $instance );
296
			}
297
		} else {
298
			$this->widget_non_instant( $args, $instance );
299
		}
300
	}
301
302
	/**
303
	 * Render the non-instant frontend widget.
304
	 *
305
	 * @since 8.3.0
306
	 *
307
	 * @param array $args     Widgets args supplied by the theme.
308
	 * @param array $instance The current widget instance.
309
	 */
310
	public function widget_non_instant( $args, $instance ) {
311
		$display_filters = false;
312
313
		if ( is_search() ) {
314
			if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() ) {
315
				Jetpack_Search::instance()->update_search_results_aggregations();
316
			}
317
318
			$filters = Jetpack_Search::instance()->get_filters();
319
320 View Code Duplication
			if ( ! Jetpack_Search_Helpers::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) {
321
				$filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) );
322
			}
323
324
			if ( ! empty( $filters ) ) {
325
				$display_filters = true;
326
			}
327
		}
328
329
		if ( ! $display_filters && empty( $instance['search_box_enabled'] ) && empty( $instance['user_sort_enabled'] ) ) {
330
			return;
331
		}
332
333
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
334
335
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
336
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
337
338
		echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
339
		?>
340
			<div id="<?php echo esc_attr( $this->id ); ?>-wrapper" >
341
		<?php
342
343
		if ( ! empty( $title ) ) {
344
			/**
345
			 * Responsible for displaying the title of the Jetpack Search filters widget.
346
			 *
347
			 * @module search
348
			 *
349
			 * @since  5.7.0
350
			 *
351
			 * @param string $title                The widget's title
352
			 * @param string $args['before_title'] The HTML tag to display before the title
353
			 * @param string $args['after_title']  The HTML tag to display after the title
354
			 */
355
			do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
356
		}
357
358
		$default_sort            = isset( $instance['sort'] ) ? $instance['sort'] : self::DEFAULT_SORT;
359
		list( $orderby, $order ) = $this->sorting_to_wp_query_param( $default_sort );
360
		$current_sort            = "{$orderby}|{$order}";
361
362
		// we need to dynamically inject the sort field into the search box when the search box is enabled, and display
363
		// it separately when it's not.
364
		if ( ! empty( $instance['search_box_enabled'] ) ) {
365
			Jetpack_Search_Template_Tags::render_widget_search_form( $instance['post_types'], $orderby, $order );
366
		}
367
368
		if ( ! empty( $instance['search_box_enabled'] ) && ! empty( $instance['user_sort_enabled'] ) ) :
369
				?>
370
					<div class="jetpack-search-sort-wrapper">
371
				<label>
372
					<?php esc_html_e( 'Sort by', 'jetpack' ); ?>
373
					<select class="jetpack-search-sort">
374 View Code Duplication
						<?php foreach ( $this->get_sort_types() as $sort => $label ) { ?>
375
							<option value="<?php echo esc_attr( $sort ); ?>" <?php selected( $current_sort, $sort ); ?>>
376
								<?php echo esc_html( $label ); ?>
377
							</option>
378
						<?php } ?>
379
					</select>
380
				</label>
381
			</div>
382
		<?php
383
		endif;
384
385
		if ( $display_filters ) {
386
			/**
387
			 * Responsible for rendering filters to narrow down search results.
388
			 *
389
			 * @module search
390
			 *
391
			 * @since  5.8.0
392
			 *
393
			 * @param array $filters    The possible filters for the current query.
394
			 * @param array $post_types An array of post types to limit filtering to.
395
			 */
396
			do_action(
397
				'jetpack_search_render_filters',
398
				$filters,
0 ignored issues
show
Bug introduced by
The variable $filters does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
399
				isset( $instance['post_types'] ) ? $instance['post_types'] : null
400
			);
401
		}
402
403
		$this->maybe_render_sort_javascript( $instance, $order, $orderby );
404
405
		echo '</div>';
406
		echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
407
	}
408
409
	/**
410
	 * Render the instant frontend widget.
411
	 *
412
	 * @since 8.3.0
413
	 *
414
	 * @param array $args     Widgets args supplied by the theme.
415
	 * @param array $instance The current widget instance.
416
	 */
417
	public function widget_instant( $args, $instance ) {
418
		if ( Jetpack_Search_Helpers::should_rerun_search_in_customizer_preview() ) {
419
			Jetpack_Search::instance()->update_search_results_aggregations();
420
		}
421
422
		$filters = Jetpack_Search::instance()->get_filters();
423
424 View Code Duplication
		if ( ! Jetpack_Search_Helpers::are_filters_by_widget_disabled() && ! $this->should_display_sitewide_filters() ) {
425
			$filters = array_filter( $filters, array( $this, 'is_for_current_widget' ) );
426
		}
427
428
		$display_filters = ! empty( $filters );
429
430
		if ( ! $display_filters && empty( $instance['search_box_enabled'] ) ) {
431
			return;
432
		}
433
434
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
435
436
		if ( empty( $title ) ) {
437
			$title = '';
438
		}
439
440
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
441
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
442
443
		echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
444
		?>
445
			<div id="<?php echo esc_attr( $this->id ); ?>-wrapper" class="jetpack-instant-search-wrapper">
446
		<?php
447
448
		if ( ! empty( $title ) ) {
449
			/**
450
			 * Responsible for displaying the title of the Jetpack Search filters widget.
451
			 *
452
			 * @module search
453
			 *
454
			 * @since  5.7.0
455
			 *
456
			 * @param string $title                The widget's title
457
			 * @param string $args['before_title'] The HTML tag to display before the title
458
			 * @param string $args['after_title']  The HTML tag to display after the title
459
			 */
460
			do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
461
		}
462
463
		// TODO: create new search box?
464
		if ( ! empty( $instance['search_box_enabled'] ) ) {
465
			Jetpack_Search_Template_Tags::render_widget_search_form( array(), '', '' );
466
		}
467
468
		if ( $display_filters ) {
469
			/**
470
			 * Responsible for rendering filters to narrow down search results.
471
			 *
472
			 * @module search
473
			 *
474
			 * @since  5.8.0
475
			 *
476
			 * @param array $filters    The possible filters for the current query.
477
			 * @param array $post_types An array of post types to limit filtering to.
478
			 */
479
			do_action(
480
				'jetpack_search_render_filters',
481
				$filters,
482
				null
483
			);
484
		}
485
486
		echo '</div>';
487
		echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
488
	}
489
490
	/**
491
	 * Render the instant widget for the overlay.
492
	 *
493
	 * @since 8.3.0
494
	 *
495
	 * @param array $args     Widgets args supplied by the theme.
496
	 * @param array $instance The current widget instance.
497
	 */
498
	public function widget_empty_instant( $args, $instance ) {
499
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
500
501
		if ( empty( $title ) ) {
502
			$title = '';
503
		}
504
505
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
506
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
507
508
		echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
509
		?>
510
			<div id="<?php echo esc_attr( $this->id ); ?>-wrapper" class="jetpack-instant-search-wrapper">
511
		<?php
512
513
		if ( ! empty( $title ) ) {
514
			/**
515
			 * Responsible for displaying the title of the Jetpack Search filters widget.
516
			 *
517
			 * @module search
518
			 *
519
			 * @since  5.7.0
520
			 *
521
			 * @param string $title                The widget's title
522
			 * @param string $args['before_title'] The HTML tag to display before the title
523
			 * @param string $args['after_title']  The HTML tag to display after the title
524
			 */
525
			do_action( 'jetpack_search_render_filters_widget_title', $title, $args['before_title'], $args['after_title'] );
526
		}
527
528
		echo '</div>';
529
		echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
530
	}
531
532
533
	/**
534
	 * Renders JavaScript for the sorting controls on the frontend.
535
	 *
536
	 * This JS is a bit complicated, but here's what it's trying to do:
537
	 * - find the search form
538
	 * - find the orderby/order fields and set default values
539
	 * - detect changes to the sort field, if it exists, and use it to set the order field values
540
	 *
541
	 * @since 5.8.0
542
	 *
543
	 * @param array  $instance The current widget instance.
544
	 * @param string $order    The order to initialize the select with.
545
	 * @param string $orderby  The orderby to initialize the select with.
546
	 */
547
	private function maybe_render_sort_javascript( $instance, $order, $orderby ) {
548
		if ( Jetpack_Search_Options::is_instant_enabled() ) {
549
			return;
550
		}
551
552
		if ( ! empty( $instance['user_sort_enabled'] ) ) :
553
		?>
554
		<script type="text/javascript">
555
				jQuery( document ).ready( function( $ ) {
556
					var orderByDefault = '<?php echo 'date' === $orderby ? 'date' : 'relevance'; ?>',
557
						orderDefault   = '<?php echo 'ASC' === $order ? 'ASC' : 'DESC'; ?>',
558
						widgetId       = decodeURIComponent( '<?php echo rawurlencode( $this->id ); ?>' ),
559
						searchQuery    = decodeURIComponent( '<?php echo rawurlencode( get_query_var( 's', '' ) ); ?>' ),
560
						isSearch       = <?php echo (int) is_search(); ?>;
561
562
					var container = $( '#' + widgetId + '-wrapper' ),
563
						form = container.find('.jetpack-search-form form'),
564
						orderBy = form.find( 'input[name=orderby]'),
565
						order = form.find( 'input[name=order]'),
566
						searchInput = form.find( 'input[name="s"]' );
567
568
					orderBy.val( orderByDefault );
569
					order.val( orderDefault );
570
571
					// Some themes don't set the search query, which results in the query being lost
572
					// when doing a sort selection. So, if the query isn't set, let's set it now. This approach
573
					// is chosen over running a regex over HTML for every search query performed.
574
					if ( isSearch && ! searchInput.val() ) {
575
						searchInput.val( searchQuery );
576
					}
577
578
					searchInput.addClass( 'show-placeholder' );
579
580
					container.find( '.jetpack-search-sort' ).change( function( event ) {
581
						var values  = event.target.value.split( '|' );
582
						orderBy.val( values[0] );
583
						order.val( values[1] );
584
585
						form.submit();
586
					});
587
				} );
588
			</script>
589
		<?php
590
		endif;
591
	}
592
593
	/**
594
	 * Convert a sort string into the separate order by and order parts.
595
	 *
596
	 * @since 5.8.0
597
	 *
598
	 * @param string $sort A sort string.
599
	 *
600
	 * @return array Order by and order.
601
	 */
602
	private function sorting_to_wp_query_param( $sort ) {
603
		$parts   = explode( '|', $sort );
604
		$orderby = isset( $_GET['orderby'] )
605
			? $_GET['orderby']
606
			: $parts[0];
607
608
		$order = isset( $_GET['order'] )
609
			? strtoupper( $_GET['order'] )
610
			: ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' );
611
612
		return array( $orderby, $order );
613
	}
614
615
	/**
616
	 * Updates a particular instance of the widget. Validates and sanitizes the options.
617
	 *
618
	 * @since 5.0.0
619
	 *
620
	 * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form().
621
	 * @param array $old_instance Old settings for this instance.
622
	 *
623
	 * @return array Settings to save.
624
	 */
625
	public function update( $new_instance, $old_instance ) {
626
		$instance = array();
627
628
		$instance['title']              = sanitize_text_field( $new_instance['title'] );
629
		$instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1';
630
		$instance['user_sort_enabled']  = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1';
631
		$instance['sort']               = $new_instance['sort'];
632
		$instance['post_types']         = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] )
633
			? array()
634
			: array_map( 'sanitize_key', $new_instance['post_types'] );
635
636
		$filters = array();
637
		if ( isset( $new_instance['filter_type'] ) ) {
638
			foreach ( (array) $new_instance['filter_type'] as $index => $type ) {
639
				$count = intval( $new_instance['num_filters'][ $index ] );
640
				$count = min( 50, $count ); // Set max boundary at 50.
641
				$count = max( 1, $count );  // Set min boundary at 1.
642
643
				switch ( $type ) {
644
					case 'taxonomy':
645
						$filters[] = array(
646
							'name'     => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
647
							'type'     => 'taxonomy',
648
							'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ),
649
							'count'    => $count,
650
						);
651
						break;
652
					case 'post_type':
653
						$filters[] = array(
654
							'name'  => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
655
							'type'  => 'post_type',
656
							'count' => $count,
657
						);
658
						break;
659
					case 'date_histogram':
660
						$filters[] = array(
661
							'name'     => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
662
							'type'     => 'date_histogram',
663
							'count'    => $count,
664
							'field'    => sanitize_key( $new_instance['date_histogram_field'][ $index ] ),
665
							'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ),
666
						);
667
						break;
668
				}
669
			}
670
		}
671
672
		if ( ! empty( $filters ) ) {
673
			$instance['filters'] = $filters;
674
		}
675
676
		return $instance;
677
	}
678
679
	/**
680
	 * Outputs the settings update form.
681
	 *
682
	 * @since 5.0.0
683
	 *
684
	 * @param array $instance Current settings.
685
	 */
686
	public function form( $instance ) {
687
		$instance = $this->jetpack_search_populate_defaults( $instance );
688
689
		$title = strip_tags( $instance['title'] );
690
691
		$hide_filters = Jetpack_Search_Helpers::are_filters_by_widget_disabled();
692
693
		$classes = sprintf(
694
			'jetpack-search-filters-widget %s %s %s',
695
			$hide_filters ? 'hide-filters' : '',
696
			$instance['search_box_enabled'] ? '' : 'hide-post-types',
697
			$this->id
698
		);
699
		?>
700
		<div class="<?php echo esc_attr( $classes ); ?>">
701
			<p>
702
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
703
					<?php esc_html_e( 'Title (optional):', 'jetpack' ); ?>
704
				</label>
705
				<input
706
					class="widefat"
707
					id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
708
					name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
709
					type="text"
710
					value="<?php echo esc_attr( $title ); ?>"
711
				/>
712
			</p>
713
714
			<p>
715
				<label>
716
					<input
717
						type="checkbox"
718
						class="jetpack-search-filters-widget__search-box-enabled"
719
						name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>"
720
						<?php checked( $instance['search_box_enabled'] ); ?>
721
					/>
722
					<?php esc_html_e( 'Show search box', 'jetpack' ); ?>
723
				</label>
724
			</p>
725
			<p>
726
				<label>
727
					<input
728
						type="checkbox"
729
						class="jetpack-search-filters-widget__sort-controls-enabled"
730
						name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>"
731
						<?php checked( $instance['user_sort_enabled'] ); ?>
732
						<?php disabled( ! $instance['search_box_enabled'] ); ?>
733
					/>
734
					<?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?>
735
				</label>
736
			</p>
737
738
			<p class="jetpack-search-filters-widget__post-types-select">
739
				<label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label>
740
				<?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?>
741
					<label>
742
						<input
743
							type="checkbox"
744
							value="<?php echo esc_attr( $post_type->name ); ?>"
745
							name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]"
746
							<?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'] ) ); ?>
747
						/>&nbsp;
748
						<?php echo esc_html( $post_type->label ); ?>
749
					</label>
750
				<?php endforeach; ?>
751
			</p>
752
753
			<p>
754
				<label>
755
					<?php esc_html_e( 'Default sort order:', 'jetpack' ); ?>
756
					<select
757
						name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>"
758
						class="widefat jetpack-search-filters-widget__sort-order">
759 View Code Duplication
						<?php foreach ( $this->get_sort_types() as $sort_type => $label ) { ?>
760
							<option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $instance['sort'], $sort_type ); ?>>
761
								<?php echo esc_html( $label ); ?>
762
							</option>
763
						<?php } ?>
764
					</select>
765
				</label>
766
			</p>
767
768
			<?php if ( ! $hide_filters ) : ?>
769
				<script class="jetpack-search-filters-widget__filter-template" type="text/template">
770
					<?php echo $this->render_widget_edit_filter( array(), true ); ?>
771
				</script>
772
				<div class="jetpack-search-filters-widget__filters">
773
					<?php foreach ( (array) $instance['filters'] as $filter ) : ?>
774
						<?php $this->render_widget_edit_filter( $filter ); ?>
775
					<?php endforeach; ?>
776
				</div>
777
				<p class="jetpack-search-filters-widget__add-filter-wrapper">
778
					<a class="button jetpack-search-filters-widget__add-filter" href="#">
779
						<?php esc_html_e( 'Add a filter', 'jetpack' ); ?>
780
					</a>
781
				</p>
782
				<noscript>
783
					<p class="jetpack-search-filters-help">
784
						<?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?>
785
					</p>
786
				</noscript>
787
				<?php if ( is_customize_preview() ) : ?>
788
					<p class="jetpack-search-filters-help">
789
						<a href="https://jetpack.com/support/search/#filters-not-showing-up" target="_blank">
790
							<?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?>
791
						</a>
792
					</p>
793
				<?php endif; ?>
794
			<?php endif; ?>
795
		</div>
796
		<?php
797
	}
798
799
	/**
800
	 * We need to render HTML in two formats: an Underscore template (client-side)
801
	 * and native PHP (server-side). This helper function allows for easy rendering
802
	 * of attributes in both formats.
803
	 *
804
	 * @since 5.8.0
805
	 *
806
	 * @param string $name        Attribute name.
807
	 * @param string $value       Attribute value.
808
	 * @param bool   $is_template Whether this is for an Underscore template or not.
809
	 */
810
	private function render_widget_attr( $name, $value, $is_template ) {
811
		echo $is_template ? "<%= $name %>" : esc_attr( $value );
812
	}
813
814
	/**
815
	 * We need to render HTML in two formats: an Underscore template (client-size)
816
	 * and native PHP (server-side). This helper function allows for easy rendering
817
	 * of the "selected" attribute in both formats.
818
	 *
819
	 * @since 5.8.0
820
	 *
821
	 * @param string $name        Attribute name.
822
	 * @param string $value       Attribute value.
823
	 * @param string $compare     Value to compare to the attribute value to decide if it should be selected.
824
	 * @param bool   $is_template Whether this is for an Underscore template or not.
825
	 */
826
	private function render_widget_option_selected( $name, $value, $compare, $is_template ) {
827
		$compare_js = rawurlencode( $compare );
828
		echo $is_template ? "<%= decodeURIComponent( '$compare_js' ) === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare );
829
	}
830
831
	/**
832
	 * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin.
833
	 *
834
	 * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore.
835
	 *
836
	 * @since 5.7.0
837
	 *
838
	 * @param array $filter      The filter to render.
839
	 * @param bool  $is_template Whether this is for an Underscore template or not.
840
	 */
841
	public function render_widget_edit_filter( $filter, $is_template = false ) {
842
		$args = wp_parse_args(
843
			$filter, array(
844
				'name'      => '',
845
				'type'      => 'taxonomy',
846
				'taxonomy'  => '',
847
				'post_type' => '',
848
				'field'     => '',
849
				'interval'  => '',
850
				'count'     => self::DEFAULT_FILTER_COUNT,
851
			)
852
		);
853
854
		$args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args );
855
856
		?>
857
		<div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>">
858
			<p class="jetpack-search-filters-widget__type-select">
859
				<label>
860
					<?php esc_html_e( 'Filter Type:', 'jetpack' ); ?>
861
					<select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select">
862
						<option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>>
863
							<?php esc_html_e( 'Taxonomy', 'jetpack' ); ?>
864
						</option>
865
						<option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>>
866
							<?php esc_html_e( 'Post Type', 'jetpack' ); ?>
867
						</option>
868
						<option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>>
869
							<?php esc_html_e( 'Date', 'jetpack' ); ?>
870
						</option>
871
					</select>
872
				</label>
873
			</p>
874
875
			<p class="jetpack-search-filters-widget__taxonomy-select">
876
				<label>
877
					<?php
878
						esc_html_e( 'Choose a taxonomy:', 'jetpack' );
879
						$seen_taxonomy_labels = array();
880
					?>
881
					<select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select">
882
						<?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?>
883
							<option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>>
884
								<?php
885
									$label = in_array( $taxonomy->label, $seen_taxonomy_labels )
886
										? sprintf(
887
											/* 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. */
888
											_x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ),
889
											$taxonomy->label,
890
											$taxonomy->name
891
										)
892
										: $taxonomy->label;
893
									echo esc_html( $label );
894
									$seen_taxonomy_labels[] = $taxonomy->label;
895
								?>
896
							</option>
897
						<?php endforeach; ?>
898
					</select>
899
				</label>
900
			</p>
901
902
			<p class="jetpack-search-filters-widget__date-histogram-select">
903
				<label>
904
					<?php esc_html_e( 'Choose a field:', 'jetpack' ); ?>
905
					<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select">
906
						<option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>>
907
							<?php esc_html_e( 'Date', 'jetpack' ); ?>
908
						</option>
909
						<option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>>
910
							<?php esc_html_e( 'Date GMT', 'jetpack' ); ?>
911
						</option>
912
						<option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>>
913
							<?php esc_html_e( 'Modified', 'jetpack' ); ?>
914
						</option>
915
						<option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>>
916
							<?php esc_html_e( 'Modified GMT', 'jetpack' ); ?>
917
						</option>
918
					</select>
919
				</label>
920
			</p>
921
922
			<p class="jetpack-search-filters-widget__date-histogram-select">
923
				<label>
924
					<?php esc_html_e( 'Choose an interval:' ); ?>
925
					<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select">
926
						<option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>>
927
							<?php esc_html_e( 'Month', 'jetpack' ); ?>
928
						</option>
929
						<option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>>
930
							<?php esc_html_e( 'Year', 'jetpack' ); ?>
931
						</option>
932
					</select>
933
				</label>
934
			</p>
935
936
			<p class="jetpack-search-filters-widget__title">
937
				<label>
938
					<?php esc_html_e( 'Title:', 'jetpack' ); ?>
939
					<input
940
						class="widefat"
941
						type="text"
942
						name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]"
943
						value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>"
944
						placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>"
945
					/>
946
				</label>
947
			</p>
948
949
			<p>
950
				<label>
951
					<?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?>
952
					<input
953
						class="widefat filter-count"
954
						name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]"
955
						type="number"
956
						value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>"
957
						min="1"
958
						max="50"
959
						step="1"
960
						required
961
					/>
962
				</label>
963
			</p>
964
965
			<p class="jetpack-search-filters-widget__controls">
966
				<a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a>
967
			</p>
968
		</div>
969
	<?php
970
	}
971
}
972