Completed
Push — add/jetpack-search-indexing-st... ( 5cba09...3d8dee )
by
unknown
06:35
created

Jetpack_Search_Customize::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Search Overlay Customization
4
 *
5
 * @package jetpack
6
 */
7
8
// Exit if file is accessed directly.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
require_once dirname( __FILE__ ) . '/class-jetpack-search-options.php';
14
15
/**
16
 * Class to customize search on the site.
17
 *
18
 * @since 8.3.0
19
 */
20
class Jetpack_Search_Customize {
21
22
	/**
23
	 * Class initialization.
24
	 *
25
	 * @since 8.3.0
26
	 */
27
	public function __construct() {
28
		add_action( 'customize_register', array( $this, 'customize_register' ) );
29
		add_action( 'customize_controls_print_scripts', array( $this, 'load_assets' ) );
30
	}
31
32
	/**
33
	 * Initialize Customizer controls.
34
	 *
35
	 * @since 8.3.0
36
	 *
37
	 * @param WP_Customize_Manager $wp_customize Customizer instance.
38
	 */
39
	public function customize_register( $wp_customize ) {
40
		$section_id     = 'jetpack_search';
41
		$setting_prefix = Jetpack_Search_Options::OPTION_PREFIX;
42
43
		$wp_customize->add_section(
44
			$section_id,
45
			array(
46
				'title'       => esc_html__( 'Jetpack Search', 'jetpack' ),
47
				'description' => __( 'Use these settings to customize the search overlay.', 'jetpack' ),
48
				'capability'  => 'edit_theme_options',
49
				'priority'    => 200,
50
			)
51
		);
52
53
		$id = $setting_prefix . 'color_theme';
54
		$wp_customize->add_setting(
55
			$id,
56
			array(
57
				'default'   => 'light',
58
				'transport' => 'postMessage',
59
				'type'      => 'option',
60
			)
61
		);
62
		$wp_customize->add_control(
63
			$id,
64
			array(
65
				'label'       => __( 'Theme', 'jetpack' ),
66
				'description' => __( 'A light or dark theme for your search overlay.', 'jetpack' ),
67
				'section'     => $section_id,
68
				'type'        => 'radio',
69
				'choices'     => array(
70
					'light' => __( 'Light', 'jetpack' ),
71
					'dark'  => __( 'Dark', 'jetpack' ),
72
				),
73
			)
74
		);
75
76
		$id = $setting_prefix . 'opacity';
77
		$wp_customize->add_setting(
78
			$id,
79
			array(
80
				'default'   => 97,
81
				'transport' => 'postMessage',
82
				'type'      => 'option',
83
			)
84
		);
85
		$wp_customize->add_control(
86
			$id,
87
			array(
88
				'type'        => 'range',
89
				'section'     => $section_id,
90
				'label'       => __( 'Background Opacity', 'jetpack' ),
91
				'description' => __( 'Select an opacity for the search overlay.', 'jetpack' ),
92
				'input_attrs' => array(
93
					'min'  => 85,
94
					'max'  => 100,
95
					'step' => 0.5,
96
				),
97
			)
98
		);
99
100
		$id = $setting_prefix . 'highlight_color';
101
		$wp_customize->add_setting(
102
			$id,
103
			array(
104
				'default'   => '#FFC',
105
				'transport' => 'postMessage',
106
				'type'      => 'option',
107
			)
108
		);
109
		$wp_customize->add_control(
110
			new WP_Customize_Color_Control(
111
				$wp_customize,
112
				$id,
113
				array(
114
					'label'       => __( 'Highlight Search Terms', 'jetpack' ),
115
					'description' => __( 'Choose a color to highlight matching search terms.', 'jetpack' ),
116
					'section'     => $section_id,
117
				)
118
			)
119
		);
120
121
		$id = $setting_prefix . 'inf_scroll';
122
		$wp_customize->add_setting(
123
			$id,
124
			array(
125
				'default'   => true,
126
				'transport' => 'postMessage',
127
				'type'      => 'option',
128
			)
129
		);
130
		$wp_customize->add_control(
131
			$id,
132
			array(
133
				'type'    => 'checkbox',
134
				'section' => $section_id,
135
				'label'   => __( 'Infinite Scroll Results', 'jetpack' ),
136
			)
137
		);
138
139
		$id = $setting_prefix . 'show_powered_by';
140
		$wp_customize->add_setting(
141
			$id,
142
			array(
143
				'default'   => true,
144
				'transport' => 'postMessage',
145
				'type'      => 'option',
146
			)
147
		);
148
		$wp_customize->add_control(
149
			$id,
150
			array(
151
				'type'    => 'checkbox',
152
				'section' => $section_id,
153
				'label'   => __( 'Display "Powered by Jetpack"', 'jetpack' ),
154
			)
155
		);
156
	}
157
158
	/**
159
	 * Load custom script for customizer
160
	 *
161
	 * @since 8.3.0
162
	 */
163
	public function load_assets() {
164
		$script_relative_path = '_inc/build/instant-search/jp-search-customize.js';
165
		$script_version       = Jetpack_Search_Helpers::get_asset_version( $script_relative_path );
166
		$script_path          = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
167
		wp_enqueue_script( 'jetpack-instant-search-customize', $script_path, array(), $script_version, true );
168
169
		$options = array(
170
			'apiNonce' => wp_create_nonce( 'wp_rest' ),
171
			'apiRoot'  => esc_url_raw( rest_url() ),
172
		);
173
		wp_add_inline_script( 'jetpack-instant-search-customize', 'var JetpackInstantCustomizeOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $options ) ) . '"));', 'before' );
174
	}
175
}
176
177