-
Notifications
You must be signed in to change notification settings - Fork 17
/
ctypo.php
302 lines (240 loc) · 10.9 KB
/
ctypo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Plugin Name: Customizer Typography
* Plugin URI: https://github.com/justintadlock/customizer-typography
* Author: Justin Tadlock
* Author URI: http://themehybrid.com
* Description: Proof-of-concept and testing tool for building typography controls in the customizer.
* Version: 1.0.0-dev
* License: GNU General Public License v2.0 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
# Register our customizer panels, sections, settings, and controls.
add_action( 'customize_register', 'ctypo_customize_register', 15 );
# Load scripts and styles.
add_action( 'customize_controls_enqueue_scripts', 'ctypo_customize_controls_register_scripts' );
add_action( 'customize_preview_init', 'ctypo_customize_preview_enqueue_scripts' );
# Add custom styles to `<head>`.
add_action( 'wp_head', 'ctypo_print_styles' );
/**
* Register customizer panels, sections, settings, and controls.
*
* @since 1.0.0
* @access public
* @param object $wp_customize
* @return void
*/
function ctypo_customize_register( $wp_customize ) {
// Load customizer typography control class.
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'customize/control-typography.php' );
// Register typography control JS template.
$wp_customize->register_control_type( 'Customizer_Typo_Control_Typography' );
/* === Testing === */
// Add the typography panel.
$wp_customize->add_panel( 'typography', array( 'priority' => 5, 'title' => esc_html__( 'Typography', 'ctypo' ) ) );
// Add the `<p>` typography section.
$wp_customize->add_section( 'p_typography',
array( 'panel' => 'typography', 'title' => esc_html__( 'Paragraphs', 'ctypo' ) )
);
// Add the headings typography section.
$wp_customize->add_section( 'headings_typography',
array( 'panel' => 'typography', 'title' => esc_html__( 'Headings', 'ctypo' ) )
);
// Add the `<p>` typography settings.
// @todo Better sanitize_callback functions.
$wp_customize->add_setting( 'p_font_family', array( 'default' => 'Arial', 'sanitize_callback' => 'sanitize_text_field', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'p_font_weight', array( 'default' => '400', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'p_font_style', array( 'default' => 'normal', 'sanitize_callback' => 'sanitize_key', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'p_font_size', array( 'default' => '16', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'p_line_height', array( 'default' => '32', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
// Add the `<h1>` typography settings.
// @todo Better sanitize_callback functions.
$wp_customize->add_setting( 'h1_font_family', array( 'default' => 'Georgia', 'sanitize_callback' => 'sanitize_text_field', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'h1_font_weight', array( 'default' => '400', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'h1_font_style', array( 'default' => 'normal', 'sanitize_callback' => 'sanitize_key', 'transport' => 'postMessage' ) );
//$wp_customize->add_setting( 'h1_font_size', array( 'default' => '28', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
//$wp_customize->add_setting( 'h1_line_height', array( 'default' => '42', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
// Add the `<h2>` typography settings.
// @todo Better sanitize_callback functions.
$wp_customize->add_setting( 'h2_font_family', array( 'default' => 'Georgia', 'sanitize_callback' => 'sanitize_text_field', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'h2_font_weight', array( 'default' => '400', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
$wp_customize->add_setting( 'h2_font_style', array( 'default' => 'normal', 'sanitize_callback' => 'sanitize_key', 'transport' => 'postMessage' ) );
//$wp_customize->add_setting( 'h2_font_size', array( 'default' => '24', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
//$wp_customize->add_setting( 'h2_line_height', array( 'default' => '38', 'sanitize_callback' => 'absint', 'transport' => 'postMessage' ) );
// Add the `<p>` typography control.
$wp_customize->add_control(
new Customizer_Typo_Control_Typography(
$wp_customize,
'p_typography',
array(
'label' => esc_html__( 'Paragraph Typography', 'ctypo' ),
'description' => __( 'Select how you want your paragraphs to appear.', 'ctypo' ),
'section' => 'p_typography',
// Tie a setting (defined via `$wp_customize->add_setting()`) to the control.
'settings' => array(
'family' => 'p_font_family',
'weight' => 'p_font_weight',
'style' => 'p_font_style',
'size' => 'p_font_size',
'line_height' => 'p_line_height'
),
// Pass custom labels. Use the setting key (above) for the specific label.
'l10n' => array(),
)
)
);
// Add the `<h1>` typography control.
$wp_customize->add_control(
new Customizer_Typo_Control_Typography(
$wp_customize,
'h1_typography',
array(
'label' => esc_html__( 'Heading 1', 'ctypo' ),
'description' => __( 'Select how heading 1 should appear.', 'ctypo' ),
'section' => 'headings_typography',
// Tie a setting (defined via `$wp_customize->add_setting()`) to the control.
'settings' => array(
'family' => 'h1_font_family',
'weight' => 'h1_font_weight',
'style' => 'h1_font_style',
// 'size' => 'h1_font_size',
// 'line_height' => 'h1_line_height'
),
// Pass custom labels. Use the setting key (above) for the specific label.
'l10n' => array(),
)
)
);
// Add the `<h2>` typography control.
$wp_customize->add_control(
new Customizer_Typo_Control_Typography(
$wp_customize,
'h2_typography',
array(
'label' => esc_html__( 'Heading 2', 'ctypo' ),
'description' => __( 'Select how heading 2 should appear.', 'ctypo' ),
'section' => 'headings_typography',
// Tie a setting (defined via `$wp_customize->add_setting()`) to the control.
'settings' => array(
'family' => 'h2_font_family',
'weight' => 'h2_font_weight',
'style' => 'h2_font_style',
// 'size' => 'h2_font_size',
// 'line_height' => 'h2_line_height'
),
// Pass custom labels. Use the setting key (above) for the specific label.
'l10n' => array(),
)
)
);
/* === End Testing === */
}
/**
* Register control scripts/styles.
*
* @since 1.0.0
* @access public
* @return void
*/
function ctypo_customize_controls_register_scripts() {
$uri = trailingslashit( plugin_dir_url( __FILE__ ) );
wp_register_script( 'ctypo-customize-controls', esc_url( $uri . 'js/customize-controls.js' ), array( 'customize-controls' ) );
wp_register_style( 'ctypo-customize-controls', esc_url( $uri . 'css/customize-controls.css' ) );
}
/**
* Load preview scripts/styles.
*
* @since 1.0.0
* @access public
* @return void
*/
function ctypo_customize_preview_enqueue_scripts() {
$uri = trailingslashit( plugin_dir_url( __FILE__ ) );
wp_enqueue_script( 'ctypo-customize-preview', esc_url( $uri . 'js/customize-preview.js' ), array( 'jquery' ) );
}
/**
* Add custom body class to give some more weight to our styles.
*
* @since 1.0.0
* @access public
* @return void
*/
function ctypo_print_styles() {
$style = $_p_style = $_h1_style = $_h2_style = '';
$allowed_weights = array( 100, 300, 400, 500, 700, 900 );
$allowed_styles = array( 'normal', 'italic', 'oblique' );
/* === <p> === */
$p_family = get_theme_mod( 'p_font_family', '' );
$p_weight = get_theme_mod( 'p_font_weight', '' );
$p_style = get_theme_mod( 'p_font_style', '' );
$p_size = get_theme_mod( 'p_font_size', '' );
$p_line_h = get_theme_mod( 'p_line_height', '' );
if ( $p_family )
$_p_style .= sprintf( "font-family: '%s';", esc_attr( $p_family ) );
if ( $p_weight )
$_p_style .= sprintf( 'font-weight: %s;', in_array( absint( $p_weight ), $allowed_weights ) ? $p_weight : 400 );
if ( $p_style )
$_p_style .= sprintf( 'font-style: %s;', in_array( $p_style, $allowed_styles ) ? $p_style : 'normal' );
if ( $p_size )
$_p_style .= sprintf( 'font-size: %spx;', absint( $p_size ) );
if ( $p_line_h )
$_p_style .= sprintf( 'line-height: %spx;', absint( $p_line_h ) );
if ( $_p_style )
$_p_style = sprintf( 'body.ctypo p { %s }', $_p_style );
/* === <h1> === */
$h1_family = get_theme_mod( 'h1_font_family', '' );
$h1_weight = get_theme_mod( 'h1_font_weight', '' );
$h1_style = get_theme_mod( 'h1_font_style', '' );
$h1_size = get_theme_mod( 'h1_font_size', '' );
$h1_line_h = get_theme_mod( 'h1_line_height', '' );
if ( $h1_family )
$_h1_style .= sprintf( "font-family: '%s';", esc_attr( $h1_family ) );
if ( $h1_weight )
$_h1_style .= sprintf( 'font-weight: %s;', in_array( absint( $h1_weight ), $allowed_weights ) ? $h1_weight : 400 );
if ( $h1_style )
$_h1_style .= sprintf( 'font-style: %s;', in_array( $h1_style, $allowed_styles ) ? $h1_style : 'normal' );
if ( $h1_size )
$_h1_style .= sprintf( 'font-size: %spx;', absint( $h1_size ) );
if ( $h1_line_h )
$_h1_style .= sprintf( 'line-height: %spx;', absint( $h1_line_h ) );
if ( $_h1_style )
$_h1_style = sprintf( 'body.ctypo h1 { %s }', $_h1_style );
/* === <h2> === */
$h2_family = get_theme_mod( 'h2_font_family', '' );
$h2_weight = get_theme_mod( 'h2_font_weight', '' );
$h2_style = get_theme_mod( 'h2_font_style', '' );
$h2_size = get_theme_mod( 'h2_font_size', '' );
$h2_line_h = get_theme_mod( 'h2_line_height', '' );
if ( $h2_family )
$_h2_style .= sprintf( "font-family: '%s';", esc_attr( $h2_family ) );
if ( $h2_weight )
$_h2_style .= sprintf( 'font-weight: %s;', in_array( absint( $h2_weight ), $allowed_weights ) ? $h2_weight : 400 );
if ( $h2_style )
$_h2_style .= sprintf( 'font-style: %s;', in_array( $h2_style, $allowed_styles ) ? $h2_style : 'normal' );
if ( $h2_size )
$_h2_style .= sprintf( 'font-size: %spx;', absint( $h2_size ) );
if ( $h2_line_h )
$_h2_style .= sprintf( 'line-height: %spx;', absint( $h2_line_h ) );
if ( $_h2_style )
$_h2_style = sprintf( 'body.ctypo h2 { %s }', $_h2_style );
/* === Output === */
// Join the styles.
$style = join( '', array( $_p_style, $_h1_style, $_h2_style ) );
// Output the styles.
if ( $style ) {
echo "\n" . '<style type="text/css" id="ctypo-css">' . $style . '</style>' . "\n";
// Body class filter.
add_filter( 'body_class', 'ctypo_body_class' );
}
}
/**
* Add custom body class to give some more weight to our styles.
*
* @since 1.0.0
* @access public
* @param array $classes
* @return array
*/
function ctypo_body_class( $classes ) {
return array_merge( $classes, array( 'ctypo' ) );
}