Completed
Push — add/test-changelog-cli ( c2d6aa...b7621b )
by
unknown
255:12 queued 240:55
created

Jetpack_Search_Customize   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 263
Duplicated Lines 4.56 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 12
loc 263
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B customize_register() 0 225 1
A customize_controls_enqueue_scripts() 12 12 1

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * Jetpack Search Overlay Customization
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
// Exit if file is accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
require_once __DIR__ . '/class.jetpack-search-helpers.php';
14
require_once __DIR__ . '/class-jetpack-search-options.php';
15
16
/**
17
 * Class to customize search on the site.
18
 *
19
 * @since 8.3.0
20
 */
21
class Jetpack_Search_Customize {
22
23
	/**
24
	 * Class initialization.
25
	 *
26
	 * @since 8.3.0
27
	 */
28
	public function __construct() {
29
		add_action( 'customize_register', array( $this, 'customize_register' ) );
30
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
31
	}
32
33
	/**
34
	 * Initialize Customizer controls.
35
	 *
36
	 * @since 8.3.0
37
	 *
38
	 * @param WP_Customize_Manager $wp_customize Customizer instance.
39
	 */
40
	public function customize_register( $wp_customize ) {
41
		require_once dirname( JETPACK__PLUGIN_FILE ) . '/modules/search/customize-controls/class-label-control.php';
42
		require_once dirname( JETPACK__PLUGIN_FILE ) . '/modules/search/customize-controls/class-excluded-post-types-control.php';
43
		$section_id     = 'jetpack_search';
44
		$setting_prefix = Jetpack_Search_Options::OPTION_PREFIX;
45
46
		$wp_customize->add_section(
47
			$section_id,
48
			array(
49
				'title'      => esc_html__( 'Jetpack Search', 'jetpack' ),
50
				'capability' => 'edit_theme_options',
51
				'priority'   => 200,
52
			)
53
		);
54
55
		$id = $setting_prefix . 'color_theme';
56
		$wp_customize->add_setting(
57
			$id,
58
			array(
59
				'default'   => 'light',
60
				'transport' => 'postMessage',
61
				'type'      => 'option',
62
			)
63
		);
64
		$wp_customize->add_control(
65
			$id,
66
			array(
67
				'label'       => __( 'Theme', 'jetpack' ),
68
				'description' => __( 'Select a theme for your search overlay.', 'jetpack' ),
69
				'section'     => $section_id,
70
				'type'        => 'radio',
71
				'choices'     => array(
72
					'light' => __( 'Light', 'jetpack' ),
73
					'dark'  => __( 'Dark', 'jetpack' ),
74
				),
75
			)
76
		);
77
78
		$id = $setting_prefix . 'default_sort';
79
		$wp_customize->add_setting(
80
			$id,
81
			array(
82
				'default' => 'relevance',
83
				'type'    => 'option',
84
			)
85
		);
86
		$wp_customize->add_control(
87
			$id,
88
			array(
89
				'choices'     => array(
90
					'relevance' => __( 'Relevance (recommended)', 'jetpack' ),
91
					'newest'    => __( 'Newest first', 'jetpack' ),
92
					'oldest'    => __( 'Oldest first', 'jetpack' ),
93
				),
94
				'description' => __( 'Pick the initial sort for your search results.', 'jetpack' ),
95
				'label'       => __( 'Default Sort', 'jetpack' ),
96
				'section'     => $section_id,
97
				'type'        => 'select',
98
			)
99
		);
100
101
		$id = $setting_prefix . 'overlay_trigger';
102
		$wp_customize->add_setting(
103
			$id,
104
			array(
105
				'default'   => 'results',
106
				'transport' => 'postMessage',
107
				'type'      => 'option',
108
			)
109
		);
110
		$wp_customize->add_control(
111
			$id,
112
			array(
113
				'label'       => __( 'Search Input Overlay Trigger', 'jetpack' ),
114
				'description' => __( 'Select when your overlay should appear.', 'jetpack' ),
115
				'section'     => $section_id,
116
				'type'        => 'select',
117
				'choices'     => array(
118
					'immediate' => __( 'Open when the user starts typing', 'jetpack' ),
119
					'results'   => __( 'Open when results are available', 'jetpack' ),
120
				),
121
			)
122
		);
123
124
		$id = $setting_prefix . 'excluded_post_types';
125
		$wp_customize->add_setting(
126
			$id,
127
			array(
128
				'default' => '',
129
				'type'    => 'option',
130
			)
131
		);
132
		$wp_customize->add_control(
133
			new Excluded_Post_Types_Control(
134
				$wp_customize,
135
				$id,
136
				array(
137
					'description' => __( 'Choose post types to exclude from search results.', 'jetpack' ),
138
					'label'       => __( 'Excluded Post Types', 'jetpack' ),
139
					'section'     => $section_id,
140
				)
141
			)
142
		);
143
144
		$id = $setting_prefix . 'result_format';
145
		$wp_customize->add_setting(
146
			$id,
147
			array(
148
				'default'   => 'minimal',
149
				'transport' => 'postMessage',
150
				'type'      => 'option',
151
			)
152
		);
153
		$wp_customize->add_control(
154
			$id,
155
			array(
156
				'label'       => __( 'Result Format', 'jetpack' ),
157
				'description' => __( 'Choose how the search results look.', 'jetpack' ),
158
				'section'     => $section_id,
159
				'type'        => 'select',
160
				'choices'     => array(
161
					'minimal'  => __( 'Minimal', 'jetpack' ),
162
					'expanded' => __( 'Expanded (shows images)', 'jetpack' ),
163
					'product'  => __( 'Product (for WooCommerce stores)', 'jetpack' ),
164
				),
165
			)
166
		);
167
168
		$id = $setting_prefix . 'highlight_color';
169
		$wp_customize->add_setting(
170
			$id,
171
			array(
172
				'default'   => '#FFC',
173
				'transport' => 'postMessage',
174
				'type'      => 'option',
175
			)
176
		);
177
		$wp_customize->add_control(
178
			new WP_Customize_Color_Control(
179
				$wp_customize,
180
				$id,
181
				array(
182
					'label'       => __( 'Highlight Search Terms', 'jetpack' ),
183
					'description' => __( 'Choose a color to highlight matching search terms.', 'jetpack' ),
184
					'section'     => $section_id,
185
				)
186
			)
187
		);
188
189
		$id = $setting_prefix . 'additional_settings_placeholder';
190
		$wp_customize->add_setting(
191
			$id,
192
			array( 'type' => 'option' )
193
		);
194
		$wp_customize->add_control(
195
			new Label_Control(
196
				$wp_customize,
197
				$id,
198
				array(
199
					'label'   => __( 'Additional Jetpack Search Settings', 'jetpack' ),
200
					'section' => $section_id,
201
				)
202
			)
203
		);
204
205
		$id = $setting_prefix . 'enable_sort';
206
		$wp_customize->add_setting(
207
			$id,
208
			array(
209
				'default'              => '1',
210
				'sanitize_callback'    => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
211
				'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
212
				'transport'            => 'postMessage',
213
				'type'                 => 'option',
214
			)
215
		);
216
		$wp_customize->add_control(
217
			$id,
218
			array(
219
				'label'   => __( 'Show sort selector', 'jetpack' ),
220
				'section' => $section_id,
221
				'type'    => 'checkbox',
222
			)
223
		);
224
225
		$id = $setting_prefix . 'inf_scroll';
226
		$wp_customize->add_setting(
227
			$id,
228
			array(
229
				'default'              => '1',
230
				'sanitize_callback'    => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
231
				'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
232
				'transport'            => 'postMessage',
233
				'type'                 => 'option',
234
			)
235
		);
236
		$wp_customize->add_control(
237
			$id,
238
			array(
239
				'type'    => 'checkbox',
240
				'section' => $section_id,
241
				'label'   => __( 'Enable infinite scrolling', 'jetpack' ),
242
			)
243
		);
244
245
		$id = $setting_prefix . 'show_powered_by';
246
		$wp_customize->add_setting(
247
			$id,
248
			array(
249
				'default'              => '1',
250
				'sanitize_callback'    => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value' ),
251
				'sanitize_js_callback' => array( 'Jetpack_Search_Helpers', 'sanitize_checkbox_value_for_js' ),
252
				'transport'            => 'postMessage',
253
				'type'                 => 'option',
254
			)
255
		);
256
		$wp_customize->add_control(
257
			$id,
258
			array(
259
				'type'    => 'checkbox',
260
				'section' => $section_id,
261
				'label'   => __( 'Display "Powered by Jetpack"', 'jetpack' ),
262
			)
263
		);
264
	}
265
266
	/**
267
	 * Enqueue assets for Customizer controls.
268
	 *
269
	 * @since 9.6.0
270
	 */
271 View Code Duplication
	public function customize_controls_enqueue_scripts() {
272
		$script_relative_path = 'modules/search/customize-controls/customize-controls.js';
273
		$script_version       = Jetpack_Search_Helpers::get_asset_version( $script_relative_path );
274
		$script_path          = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
275
		wp_enqueue_script(
276
			'jetpack-instant-search-customizer',
277
			$script_path,
278
			array( 'customize-controls' ),
279
			$script_version,
280
			true
281
		);
282
	}
283
}
284