Completed
Push — fix/gutenberg-7.2.0-classnames ( 3b3138...aca56b )
by Jeremy
63:47 queued 56:55
created

Jetpack_Search_Widget   F

Complexity

Total Complexity 96

Size/Duplication

Total Lines 943
Duplicated Lines 1.7 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 16
loc 943
rs 1.657
c 0
b 0
f 0
wmc 96
lcom 1
cbo 7

20 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 6
A is_search_active() 0 3 1
A activate_search() 0 3 1
A widget_admin_setup() 0 45 1
A enqueue_frontend_scripts() 0 15 3
A get_sort_types() 0 7 2
A is_for_current_widget() 0 3 2
A should_display_sitewide_filters() 0 18 5
A jetpack_search_populate_defaults() 0 14 1
C widget_instant() 3 72 11
A widget() 0 26 4
F widget_non_instant() 8 98 18
A widget_empty_instant() 0 33 4
B maybe_render_sort_javascript() 0 52 5
A sorting_to_wp_query_param() 0 12 5
C update() 0 53 11
C form() 5 112 9
A render_widget_attr() 0 3 2
A render_widget_option_selected() 0 4 2
B render_widget_edit_filter() 0 130 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_Search_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Search_Widget, and based on these observations, apply Extract Interface, too.

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(),
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
			var jetpackSearchModuleSorting = 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 = document.getElementById( widgetId + '-wrapper' ),
563
					form = container.querySelector( '.jetpack-search-form form' ),
564
					orderBy = form.querySelector( 'input[name=orderby]' ),
565
					order = form.querySelector( 'input[name=order]' ),
566
					searchInput = form.querySelector( 'input[name="s"]' ),
567
					sortSelectInput = container.querySelector( '.jetpack-search-sort' );
568
569
				orderBy.value = orderByDefault;
570
				order.value = orderDefault;
571
572
				// Some themes don't set the search query, which results in the query being lost
573
				// when doing a sort selection. So, if the query isn't set, let's set it now. This approach
574
				// is chosen over running a regex over HTML for every search query performed.
575
				if ( isSearch && ! searchInput.value ) {
576
					searchInput.value = searchQuery;
577
				}
578
579
				searchInput.classList.add( 'show-placeholder' );
580
581
				sortSelectInput.addEventListener( 'change', function( event ) {
582
					var values  = event.target.value.split( '|' );
583
					orderBy.value = values[0];
584
					order.value = values[1];
585
586
					form.submit();
587
				} );
588
			}
589
590
			if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
591
				jetpackSearchModuleSorting();
592
			} else {
593
				document.addEventListener( 'DOMContentLoaded', jetpackSearchModuleSorting );
594
			}
595
			</script>
596
		<?php
597
		endif;
598
	}
599
600
	/**
601
	 * Convert a sort string into the separate order by and order parts.
602
	 *
603
	 * @since 5.8.0
604
	 *
605
	 * @param string $sort A sort string.
606
	 *
607
	 * @return array Order by and order.
608
	 */
609
	private function sorting_to_wp_query_param( $sort ) {
610
		$parts   = explode( '|', $sort );
611
		$orderby = isset( $_GET['orderby'] )
612
			? $_GET['orderby']
613
			: $parts[0];
614
615
		$order = isset( $_GET['order'] )
616
			? strtoupper( $_GET['order'] )
617
			: ( ( isset( $parts[1] ) && 'ASC' === strtoupper( $parts[1] ) ) ? 'ASC' : 'DESC' );
618
619
		return array( $orderby, $order );
620
	}
621
622
	/**
623
	 * Updates a particular instance of the widget. Validates and sanitizes the options.
624
	 *
625
	 * @since 5.0.0
626
	 *
627
	 * @param array $new_instance New settings for this instance as input by the user via Jetpack_Search_Widget::form().
628
	 * @param array $old_instance Old settings for this instance.
629
	 *
630
	 * @return array Settings to save.
631
	 */
632
	public function update( $new_instance, $old_instance ) {
633
		$instance = array();
634
635
		$instance['title']              = sanitize_text_field( $new_instance['title'] );
636
		$instance['search_box_enabled'] = empty( $new_instance['search_box_enabled'] ) ? '0' : '1';
637
		$instance['user_sort_enabled']  = empty( $new_instance['user_sort_enabled'] ) ? '0' : '1';
638
		$instance['sort']               = $new_instance['sort'];
639
		$instance['post_types']         = empty( $new_instance['post_types'] ) || empty( $instance['search_box_enabled'] )
640
			? array()
641
			: array_map( 'sanitize_key', $new_instance['post_types'] );
642
643
		$filters = array();
644
		if ( isset( $new_instance['filter_type'] ) ) {
645
			foreach ( (array) $new_instance['filter_type'] as $index => $type ) {
646
				$count = intval( $new_instance['num_filters'][ $index ] );
647
				$count = min( 50, $count ); // Set max boundary at 50.
648
				$count = max( 1, $count );  // Set min boundary at 1.
649
650
				switch ( $type ) {
651
					case 'taxonomy':
652
						$filters[] = array(
653
							'name'     => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
654
							'type'     => 'taxonomy',
655
							'taxonomy' => sanitize_key( $new_instance['taxonomy_type'][ $index ] ),
656
							'count'    => $count,
657
						);
658
						break;
659
					case 'post_type':
660
						$filters[] = array(
661
							'name'  => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
662
							'type'  => 'post_type',
663
							'count' => $count,
664
						);
665
						break;
666
					case 'date_histogram':
667
						$filters[] = array(
668
							'name'     => sanitize_text_field( $new_instance['filter_name'][ $index ] ),
669
							'type'     => 'date_histogram',
670
							'count'    => $count,
671
							'field'    => sanitize_key( $new_instance['date_histogram_field'][ $index ] ),
672
							'interval' => sanitize_key( $new_instance['date_histogram_interval'][ $index ] ),
673
						);
674
						break;
675
				}
676
			}
677
		}
678
679
		if ( ! empty( $filters ) ) {
680
			$instance['filters'] = $filters;
681
		}
682
683
		return $instance;
684
	}
685
686
	/**
687
	 * Outputs the settings update form.
688
	 *
689
	 * @since 5.0.0
690
	 *
691
	 * @param array $instance Current settings.
692
	 */
693
	public function form( $instance ) {
694
		$instance = $this->jetpack_search_populate_defaults( $instance );
695
696
		$title = strip_tags( $instance['title'] );
697
698
		$hide_filters = Jetpack_Search_Helpers::are_filters_by_widget_disabled();
699
700
		$classes = sprintf(
701
			'jetpack-search-filters-widget %s %s %s',
702
			$hide_filters ? 'hide-filters' : '',
703
			$instance['search_box_enabled'] ? '' : 'hide-post-types',
704
			$this->id
705
		);
706
		?>
707
		<div class="<?php echo esc_attr( $classes ); ?>">
708
			<p>
709
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
710
					<?php esc_html_e( 'Title (optional):', 'jetpack' ); ?>
711
				</label>
712
				<input
713
					class="widefat"
714
					id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
715
					name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
716
					type="text"
717
					value="<?php echo esc_attr( $title ); ?>"
718
				/>
719
			</p>
720
721
			<p>
722
				<label>
723
					<input
724
						type="checkbox"
725
						class="jetpack-search-filters-widget__search-box-enabled"
726
						name="<?php echo esc_attr( $this->get_field_name( 'search_box_enabled' ) ); ?>"
727
						<?php checked( $instance['search_box_enabled'] ); ?>
728
					/>
729
					<?php esc_html_e( 'Show search box', 'jetpack' ); ?>
730
				</label>
731
			</p>
732
			<p>
733
				<label>
734
					<input
735
						type="checkbox"
736
						class="jetpack-search-filters-widget__sort-controls-enabled"
737
						name="<?php echo esc_attr( $this->get_field_name( 'user_sort_enabled' ) ); ?>"
738
						<?php checked( $instance['user_sort_enabled'] ); ?>
739
						<?php disabled( ! $instance['search_box_enabled'] ); ?>
740
					/>
741
					<?php esc_html_e( 'Show sort selection dropdown', 'jetpack' ); ?>
742
				</label>
743
			</p>
744
745
			<p class="jetpack-search-filters-widget__post-types-select">
746
				<label><?php esc_html_e( 'Post types to search (minimum of 1):', 'jetpack' ); ?></label>
747
				<?php foreach ( get_post_types( array( 'exclude_from_search' => false ), 'objects' ) as $post_type ) : ?>
748
					<label>
749
						<input
750
							type="checkbox"
751
							value="<?php echo esc_attr( $post_type->name ); ?>"
752
							name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]"
753
							<?php checked( empty( $instance['post_types'] ) || in_array( $post_type->name, $instance['post_types'] ) ); ?>
754
						/>&nbsp;
755
						<?php echo esc_html( $post_type->label ); ?>
756
					</label>
757
				<?php endforeach; ?>
758
			</p>
759
760
			<p>
761
				<label>
762
					<?php esc_html_e( 'Default sort order:', 'jetpack' ); ?>
763
					<select
764
						name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>"
765
						class="widefat jetpack-search-filters-widget__sort-order">
766 View Code Duplication
						<?php foreach ( $this->get_sort_types() as $sort_type => $label ) { ?>
767
							<option value="<?php echo esc_attr( $sort_type ); ?>" <?php selected( $instance['sort'], $sort_type ); ?>>
768
								<?php echo esc_html( $label ); ?>
769
							</option>
770
						<?php } ?>
771
					</select>
772
				</label>
773
			</p>
774
775
			<?php if ( ! $hide_filters ) : ?>
776
				<script class="jetpack-search-filters-widget__filter-template" type="text/template">
777
					<?php echo $this->render_widget_edit_filter( array(), true ); ?>
778
				</script>
779
				<div class="jetpack-search-filters-widget__filters">
780
					<?php foreach ( (array) $instance['filters'] as $filter ) : ?>
781
						<?php $this->render_widget_edit_filter( $filter ); ?>
782
					<?php endforeach; ?>
783
				</div>
784
				<p class="jetpack-search-filters-widget__add-filter-wrapper">
785
					<a class="button jetpack-search-filters-widget__add-filter" href="#">
786
						<?php esc_html_e( 'Add a filter', 'jetpack' ); ?>
787
					</a>
788
				</p>
789
				<noscript>
790
					<p class="jetpack-search-filters-help">
791
						<?php echo esc_html_e( 'Adding filters requires JavaScript!', 'jetpack' ); ?>
792
					</p>
793
				</noscript>
794
				<?php if ( is_customize_preview() ) : ?>
795
					<p class="jetpack-search-filters-help">
796
						<a href="https://jetpack.com/support/search/#filters-not-showing-up" target="_blank">
797
							<?php esc_html_e( "Why aren't my filters appearing?", 'jetpack' ); ?>
798
						</a>
799
					</p>
800
				<?php endif; ?>
801
			<?php endif; ?>
802
		</div>
803
		<?php
804
	}
805
806
	/**
807
	 * We need to render HTML in two formats: an Underscore template (client-side)
808
	 * and native PHP (server-side). This helper function allows for easy rendering
809
	 * of attributes in both formats.
810
	 *
811
	 * @since 5.8.0
812
	 *
813
	 * @param string $name        Attribute name.
814
	 * @param string $value       Attribute value.
815
	 * @param bool   $is_template Whether this is for an Underscore template or not.
816
	 */
817
	private function render_widget_attr( $name, $value, $is_template ) {
818
		echo $is_template ? "<%= $name %>" : esc_attr( $value );
819
	}
820
821
	/**
822
	 * We need to render HTML in two formats: an Underscore template (client-size)
823
	 * and native PHP (server-side). This helper function allows for easy rendering
824
	 * of the "selected" attribute in both formats.
825
	 *
826
	 * @since 5.8.0
827
	 *
828
	 * @param string $name        Attribute name.
829
	 * @param string $value       Attribute value.
830
	 * @param string $compare     Value to compare to the attribute value to decide if it should be selected.
831
	 * @param bool   $is_template Whether this is for an Underscore template or not.
832
	 */
833
	private function render_widget_option_selected( $name, $value, $compare, $is_template ) {
834
		$compare_js = rawurlencode( $compare );
835
		echo $is_template ? "<%= decodeURIComponent( '$compare_js' ) === $name ? 'selected=\"selected\"' : '' %>" : selected( $value, $compare );
836
	}
837
838
	/**
839
	 * Responsible for rendering a single filter in the customizer or the widget administration screen in wp-admin.
840
	 *
841
	 * We use this method for two purposes - rendering the fields server-side, and also rendering a script template for Underscore.
842
	 *
843
	 * @since 5.7.0
844
	 *
845
	 * @param array $filter      The filter to render.
846
	 * @param bool  $is_template Whether this is for an Underscore template or not.
847
	 */
848
	public function render_widget_edit_filter( $filter, $is_template = false ) {
849
		$args = wp_parse_args(
850
			$filter, array(
851
				'name'      => '',
852
				'type'      => 'taxonomy',
853
				'taxonomy'  => '',
854
				'post_type' => '',
855
				'field'     => '',
856
				'interval'  => '',
857
				'count'     => self::DEFAULT_FILTER_COUNT,
858
			)
859
		);
860
861
		$args['name_placeholder'] = Jetpack_Search_Helpers::generate_widget_filter_name( $args );
862
863
		?>
864
		<div class="jetpack-search-filters-widget__filter is-<?php $this->render_widget_attr( 'type', $args['type'], $is_template ); ?>">
865
			<p class="jetpack-search-filters-widget__type-select">
866
				<label>
867
					<?php esc_html_e( 'Filter Type:', 'jetpack' ); ?>
868
					<select name="<?php echo esc_attr( $this->get_field_name( 'filter_type' ) ); ?>[]" class="widefat filter-select">
869
						<option value="taxonomy" <?php $this->render_widget_option_selected( 'type', $args['type'], 'taxonomy', $is_template ); ?>>
870
							<?php esc_html_e( 'Taxonomy', 'jetpack' ); ?>
871
						</option>
872
						<option value="post_type" <?php $this->render_widget_option_selected( 'type', $args['type'], 'post_type', $is_template ); ?>>
873
							<?php esc_html_e( 'Post Type', 'jetpack' ); ?>
874
						</option>
875
						<option value="date_histogram" <?php $this->render_widget_option_selected( 'type', $args['type'], 'date_histogram', $is_template ); ?>>
876
							<?php esc_html_e( 'Date', 'jetpack' ); ?>
877
						</option>
878
					</select>
879
				</label>
880
			</p>
881
882
			<p class="jetpack-search-filters-widget__taxonomy-select">
883
				<label>
884
					<?php
885
						esc_html_e( 'Choose a taxonomy:', 'jetpack' );
886
						$seen_taxonomy_labels = array();
887
					?>
888
					<select name="<?php echo esc_attr( $this->get_field_name( 'taxonomy_type' ) ); ?>[]" class="widefat taxonomy-select">
889
						<?php foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) : ?>
890
							<option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php $this->render_widget_option_selected( 'taxonomy', $args['taxonomy'], $taxonomy->name, $is_template ); ?>>
891
								<?php
892
									$label = in_array( $taxonomy->label, $seen_taxonomy_labels )
893
										? sprintf(
894
											/* 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. */
895
											_x( '%1$s (%2$s)', 'A label for a taxonomy selector option', 'jetpack' ),
896
											$taxonomy->label,
897
											$taxonomy->name
898
										)
899
										: $taxonomy->label;
900
									echo esc_html( $label );
901
									$seen_taxonomy_labels[] = $taxonomy->label;
902
								?>
903
							</option>
904
						<?php endforeach; ?>
905
					</select>
906
				</label>
907
			</p>
908
909
			<p class="jetpack-search-filters-widget__date-histogram-select">
910
				<label>
911
					<?php esc_html_e( 'Choose a field:', 'jetpack' ); ?>
912
					<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_field' ) ); ?>[]" class="widefat date-field-select">
913
						<option value="post_date" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date', $is_template ); ?>>
914
							<?php esc_html_e( 'Date', 'jetpack' ); ?>
915
						</option>
916
						<option value="post_date_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_date_gmt', $is_template ); ?>>
917
							<?php esc_html_e( 'Date GMT', 'jetpack' ); ?>
918
						</option>
919
						<option value="post_modified" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified', $is_template ); ?>>
920
							<?php esc_html_e( 'Modified', 'jetpack' ); ?>
921
						</option>
922
						<option value="post_modified_gmt" <?php $this->render_widget_option_selected( 'field', $args['field'], 'post_modified_gmt', $is_template ); ?>>
923
							<?php esc_html_e( 'Modified GMT', 'jetpack' ); ?>
924
						</option>
925
					</select>
926
				</label>
927
			</p>
928
929
			<p class="jetpack-search-filters-widget__date-histogram-select">
930
				<label>
931
					<?php esc_html_e( 'Choose an interval:' ); ?>
932
					<select name="<?php echo esc_attr( $this->get_field_name( 'date_histogram_interval' ) ); ?>[]" class="widefat date-interval-select">
933
						<option value="month" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'month', $is_template ); ?>>
934
							<?php esc_html_e( 'Month', 'jetpack' ); ?>
935
						</option>
936
						<option value="year" <?php $this->render_widget_option_selected( 'interval', $args['interval'], 'year', $is_template ); ?>>
937
							<?php esc_html_e( 'Year', 'jetpack' ); ?>
938
						</option>
939
					</select>
940
				</label>
941
			</p>
942
943
			<p class="jetpack-search-filters-widget__title">
944
				<label>
945
					<?php esc_html_e( 'Title:', 'jetpack' ); ?>
946
					<input
947
						class="widefat"
948
						type="text"
949
						name="<?php echo esc_attr( $this->get_field_name( 'filter_name' ) ); ?>[]"
950
						value="<?php $this->render_widget_attr( 'name', $args['name'], $is_template ); ?>"
951
						placeholder="<?php $this->render_widget_attr( 'name_placeholder', $args['name_placeholder'], $is_template ); ?>"
952
					/>
953
				</label>
954
			</p>
955
956
			<p>
957
				<label>
958
					<?php esc_html_e( 'Maximum number of filters (1-50):', 'jetpack' ); ?>
959
					<input
960
						class="widefat filter-count"
961
						name="<?php echo esc_attr( $this->get_field_name( 'num_filters' ) ); ?>[]"
962
						type="number"
963
						value="<?php $this->render_widget_attr( 'count', $args['count'], $is_template ); ?>"
964
						min="1"
965
						max="50"
966
						step="1"
967
						required
968
					/>
969
				</label>
970
			</p>
971
972
			<p class="jetpack-search-filters-widget__controls">
973
				<a href="#" class="delete"><?php esc_html_e( 'Remove', 'jetpack' ); ?></a>
974
			</p>
975
		</div>
976
	<?php
977
	}
978
}
979