Completed
Push — add/amp-pwa-experiment ( efea12 )
by
unknown
11:53
created

AMP_Customizer_Design_Settings::init_customizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class AMP_Customizer_Design_Settings {
4
	const DEFAULT_HEADER_COLOR = '#fff';
5
	const DEFAULT_HEADER_BACKGROUND_COLOR = '#0a89c0';
6
	const DEFAULT_COLOR_SCHEME = 'light';
7
8
	public static function init() {
9
		add_action( 'amp_customizer_init', array( __CLASS__, 'init_customizer' ) );
10
11
		add_filter( 'amp_customizer_get_settings', array( __CLASS__, 'append_settings' ) );
12
	}
13
14
	public static function init_customizer() {
15
		add_action( 'amp_customizer_register_settings', array( __CLASS__, 'register_customizer_settings' ) );
16
		add_action( 'amp_customizer_register_ui', array( __CLASS__, 'register_customizer_ui' ) );
17
		add_action( 'amp_customizer_enqueue_preview_scripts', array( __CLASS__, 'enqueue_customizer_preview_scripts' ) );
18
	}
19
20
	public static function register_customizer_settings( $wp_customize ) {
21
		// Header text color setting
22
		$wp_customize->add_setting( 'amp_customizer[header_color]', array(
23
			'type'              => 'option',
24
			'default'           => self::DEFAULT_HEADER_COLOR,
25
			'sanitize_callback' => 'sanitize_hex_color',
26
			'transport'         => 'postMessage'
27
		) );
28
29
		// Header background color
30
		$wp_customize->add_setting( 'amp_customizer[header_background_color]', array(
31
			'type'              => 'option',
32
			'default'           => self::DEFAULT_HEADER_BACKGROUND_COLOR,
33
			'sanitize_callback' => 'sanitize_hex_color',
34
			'transport'         => 'postMessage'
35
		) );
36
37
		// Background color scheme
38
		$wp_customize->add_setting( 'amp_customizer[color_scheme]', array(
39
			'type'              => 'option',
40
			'default'           => self::DEFAULT_COLOR_SCHEME,
41
			'sanitize_callback' => array( __CLASS__ , 'sanitize_color_scheme' ),
42
			'transport'         => 'postMessage'
43
		) );
44
	}
45
46
	public static function register_customizer_ui( $wp_customize ) {
47
		$wp_customize->add_section( 'amp_design', array(
48
			'title' => __( 'Design', 'amp' ),
49
			'panel' => AMP_Template_Customizer::PANEL_ID,
50
		) );
51
52
		// Header text color control.
53
		$wp_customize->add_control(
54
			new WP_Customize_Color_Control( $wp_customize, 'amp_header_color', array(
55
				'settings'   => 'amp_customizer[header_color]',
56
				'label'    => __( 'Header Text Color', 'amp' ),
57
				'section'  => 'amp_design',
58
				'priority' => 10
59
			) )
60
		);
61
62
		// Header background color control.
63
		$wp_customize->add_control(
64
			new WP_Customize_Color_Control( $wp_customize, 'amp_header_background_color', array(
65
				'settings'   => 'amp_customizer[header_background_color]',
66
				'label'    => __( 'Header Background & Link Color', 'amp' ),
67
				'section'  => 'amp_design',
68
				'priority' => 20
69
			) )
70
		);
71
72
		// Background color scheme
73
		$wp_customize->add_control( 'amp_color_scheme', array(
74
			'settings'   => 'amp_customizer[color_scheme]',
75
			'label'      => __( 'Color Scheme', 'amp' ),
76
			'section'    => 'amp_design',
77
			'type'       => 'radio',
78
			'priority'   => 30,
79
			'choices'    => self::get_color_scheme_names(),
80
		));
81
	}
82
83
	public static function enqueue_customizer_preview_scripts() {
84
		wp_enqueue_script(
85
			'amp-customizer-design-preview',
86
			amp_get_asset_url( 'js/amp-customizer-design-preview.js' ),
87
			array( 'amp-customizer' ),
88
			false,
89
			true
90
		);
91
		wp_localize_script( 'amp-customizer-design-preview', 'amp_customizer_design', array(
92
			'color_schemes' => self::get_color_schemes(),
93
		) );
94
	}
95
96
	public static function append_settings( $settings ) {
97
		$settings = wp_parse_args( $settings, array(
98
			'header_color' => self::DEFAULT_HEADER_COLOR,
99
			'header_background_color' => self::DEFAULT_HEADER_BACKGROUND_COLOR,
100
			'color_scheme' => self::DEFAULT_COLOR_SCHEME,
101
		) );
102
103
		$theme_colors = self::get_colors_for_color_scheme( $settings['color_scheme'] );
104
105
		return array_merge( $settings, $theme_colors, array(
106
			'link_color' => $settings['header_background_color'],
107
		) );
108
	}
109
110
	protected static function get_color_scheme_names() {
111
		return array(
112
			'light'   => __( 'Light', 'amp'),
113
			'dark'    => __( 'Dark', 'amp' ),
114
		);
115
	}
116
117
	protected static function get_color_schemes() {
118
		return array(
119
			'light' => array(
120
				// Convert colors to greyscale for light theme color; see http://goo.gl/2gDLsp
121
				'theme_color'      => '#fff',
122
				'text_color'       => '#353535',
123
				'muted_text_color' => '#696969',
124
				'border_color'     => '#c2c2c2',
125
			),
126
			'dark' => array(
127
				// Convert and invert colors to greyscale for dark theme color; see http://goo.gl/uVB2cO
128
				'theme_color'      => '#0a0a0a',
129
				'text_color'       => '#dedede',
130
				'muted_text_color' => '#b1b1b1',
131
				'border_color'     => '#707070',
132
			)
133
		);
134
	}
135
136
	protected static function get_colors_for_color_scheme( $scheme ) {
137
		$color_schemes = self::get_color_schemes();
138
139
		if ( isset( $color_schemes[ $scheme ] ) ) {
140
			return $color_schemes[ $scheme ];
141
		}
142
143
		return $color_schemes[ self::DEFAULT_COLOR_SCHEME ];
144
	}
145
146
	public static function sanitize_color_scheme( $value ) {
147
		$schemes = self::get_color_scheme_names();
148
		$scheme_slugs = array_keys( $schemes );
149
150
		if ( ! in_array( $value, $scheme_slugs ) ) {
151
			$value = self::DEFAULT_COLOR_SCHEME;
152
		}
153
154
		return $value;
155
	}
156
}
157