1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Add theme options to customizer |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use Flynt\Utils\Asset; |
8
|
|
|
|
9
|
|
|
$colorsDefault = [ |
10
|
|
|
'accent-default' => [ |
11
|
|
|
'label' => 'Accent', |
12
|
|
|
'default' => '#2b44df', |
13
|
|
|
'description' => '' |
14
|
|
|
], |
15
|
|
|
'headline-default' => [ |
16
|
|
|
'label' => 'Headline', |
17
|
|
|
'default' => '#252525', |
18
|
|
|
'description' => '' |
19
|
|
|
], |
20
|
|
|
'text-default' => [ |
21
|
|
|
'label' => 'Text', |
22
|
|
|
'default' => '#252525', |
23
|
|
|
'description' => '' |
24
|
|
|
], |
25
|
|
|
'border-default' => [ |
26
|
|
|
'label' => 'Border', |
27
|
|
|
'default' => '#8791BA', |
28
|
|
|
'description' => '' |
29
|
|
|
], |
30
|
|
|
'background-default' => [ |
31
|
|
|
'label' => 'Background', |
32
|
|
|
'default' => '#ffffff', |
33
|
|
|
'description' => '' |
34
|
|
|
], |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
$colorsLight = [ |
38
|
|
|
'accent-light' => [ |
39
|
|
|
'label' => 'Accent', |
40
|
|
|
'default' => '#2b44df', |
41
|
|
|
'description' => '' |
42
|
|
|
], |
43
|
|
|
'headline-light' => [ |
44
|
|
|
'label' => 'Headline', |
45
|
|
|
'default' => '#252525', |
46
|
|
|
'description' => '' |
47
|
|
|
], |
48
|
|
|
'text-light' => [ |
49
|
|
|
'label' => 'Text', |
50
|
|
|
'default' => '#252525', |
51
|
|
|
'description' => '' |
52
|
|
|
], |
53
|
|
|
'border-light' => [ |
54
|
|
|
'label' => 'Border', |
55
|
|
|
'default' => '#8791BA', |
56
|
|
|
'description' => '' |
57
|
|
|
], |
58
|
|
|
'background-light' => [ |
59
|
|
|
'label' => 'Background', |
60
|
|
|
'default' => '#F8F9FD', |
61
|
|
|
'description' => '' |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$colorsDark = [ |
66
|
|
|
'accent-dark' => [ |
67
|
|
|
'label' => 'Accent', |
68
|
|
|
'default' => '#ffffff', |
69
|
|
|
'description' => '', |
70
|
|
|
], |
71
|
|
|
'headline-dark' => [ |
72
|
|
|
'label' => 'Headline', |
73
|
|
|
'default' => '#FBFBFB', |
74
|
|
|
'description' => '', |
75
|
|
|
], |
76
|
|
|
'text-dark' => [ |
77
|
|
|
'label' => 'Text', |
78
|
|
|
'default' => '#E9E9EC', |
79
|
|
|
'description' => '', |
80
|
|
|
], |
81
|
|
|
'border-dark' => [ |
82
|
|
|
'label' => 'Border', |
83
|
|
|
'default' => '#C3C4F7', |
84
|
|
|
'description' => '', |
85
|
|
|
], |
86
|
|
|
'background-dark' => [ |
87
|
|
|
'label' => 'Background', |
88
|
|
|
'default' => '#10205A', |
89
|
|
|
'description' => '', |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$colorsHero = [ |
94
|
|
|
'accent-hero' => [ |
95
|
|
|
'label' => 'Accent', |
96
|
|
|
'default' => '#ffffff', |
97
|
|
|
'description' => '', |
98
|
|
|
], |
99
|
|
|
'headline-hero' => [ |
100
|
|
|
'label' => 'Headline', |
101
|
|
|
'default' => '#FBFBFB', |
102
|
|
|
'description' => '', |
103
|
|
|
], |
104
|
|
|
'text-hero' => [ |
105
|
|
|
'label' => 'Text', |
106
|
|
|
'default' => '#E9E9EC', |
107
|
|
|
'description' => '', |
108
|
|
|
], |
109
|
|
|
'border-hero' => [ |
110
|
|
|
'label' => 'Border', |
111
|
|
|
'default' => '#CDE2FD', |
112
|
|
|
'description' => '', |
113
|
|
|
], |
114
|
|
|
'background-hero' => [ |
115
|
|
|
'label' => 'Background', |
116
|
|
|
'default' => '#2B44DF', |
117
|
|
|
'description' => '', |
118
|
|
|
], |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
add_action('customize_register', function ($wp_customize) use ($colorsDefault, $colorsLight, $colorsDark, $colorsHero) { |
122
|
|
|
$wp_customize->add_panel( |
123
|
|
|
'theme_colors_panel', |
124
|
|
|
[ |
125
|
|
|
'title' => __('Colors', 'flynt'), |
126
|
|
|
'description' => 'description', // Include html tags such as <p>. |
127
|
|
|
'priority' => 160, // Mixed with top-level-section hierarchy. |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$wp_customize->add_section( |
132
|
|
|
'theme_colors_default', |
133
|
|
|
[ |
134
|
|
|
'title' => __('Default', 'flynt'), |
135
|
|
|
'priority' => 20, |
136
|
|
|
'panel' => 'theme_colors_panel' |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$wp_customize->add_section( |
141
|
|
|
'theme_colors_light', |
142
|
|
|
[ |
143
|
|
|
'title' => __('Theme Light', 'flynt'), |
144
|
|
|
'priority' => 20, |
145
|
|
|
'panel' => 'theme_colors_panel' |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$wp_customize->add_section( |
150
|
|
|
'theme_colors_dark', |
151
|
|
|
[ |
152
|
|
|
'title' => __('Theme Dark', 'flynt'), |
153
|
|
|
'priority' => 20, |
154
|
|
|
'panel' => 'theme_colors_panel' |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$wp_customize->add_section( |
159
|
|
|
'theme_colors_hero', |
160
|
|
|
[ |
161
|
|
|
'title' => __('Theme Hero', 'flynt'), |
162
|
|
|
'priority' => 20, |
163
|
|
|
'panel' => 'theme_colors_panel' |
164
|
|
|
] |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
foreach ($colorsDefault as $name => $color) { |
168
|
|
|
// Settings |
169
|
|
|
$wp_customize->add_setting( |
170
|
|
|
'theme_colors_' . $name, |
171
|
|
|
[ |
172
|
|
|
'default' => $color['default'], |
173
|
|
|
'transport' => 'postMessage', |
174
|
|
|
] |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
// Controls |
178
|
|
|
$wp_customize->add_control( |
179
|
|
|
new WP_Customize_Color_Control( |
180
|
|
|
$wp_customize, |
181
|
|
|
'theme_colors_' . $name, |
182
|
|
|
[ |
183
|
|
|
'section' => 'theme_colors_default', |
184
|
|
|
'label' => __($color['label']), |
185
|
|
|
'description' => __($color['description']), |
186
|
|
|
] |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
foreach ($colorsLight as $name => $color) { |
192
|
|
|
// Settings |
193
|
|
|
$wp_customize->add_setting( |
194
|
|
|
'theme_colors_' . $name, |
195
|
|
|
[ |
196
|
|
|
'default' => $color['default'], |
197
|
|
|
'transport' => 'postMessage', |
198
|
|
|
] |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
// Controls |
202
|
|
|
$wp_customize->add_control( |
203
|
|
|
new WP_Customize_Color_Control( |
204
|
|
|
$wp_customize, |
205
|
|
|
'theme_colors_' . $name, |
206
|
|
|
[ |
207
|
|
|
'section' => 'theme_colors_light', |
208
|
|
|
'label' => __($color['label']), |
209
|
|
|
'description' => __($color['description']), |
210
|
|
|
] |
211
|
|
|
) |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
foreach ($colorsDark as $name => $color) { |
216
|
|
|
// Settings |
217
|
|
|
$wp_customize->add_setting( |
218
|
|
|
'theme_colors_' . $name, |
219
|
|
|
[ |
220
|
|
|
'default' => $color['default'], |
221
|
|
|
'transport' => 'postMessage', |
222
|
|
|
] |
223
|
|
|
); |
224
|
|
|
|
225
|
|
|
// Controls |
226
|
|
|
$wp_customize->add_control( |
227
|
|
|
new WP_Customize_Color_Control( |
228
|
|
|
$wp_customize, |
229
|
|
|
'theme_colors_' . $name, |
230
|
|
|
[ |
231
|
|
|
'section' => 'theme_colors_dark', |
232
|
|
|
'label' => __($color['label']), |
233
|
|
|
'description' => __($color['description']), |
234
|
|
|
] |
235
|
|
|
) |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
foreach ($colorsHero as $name => $color) { |
240
|
|
|
// Settings |
241
|
|
|
$wp_customize->add_setting( |
242
|
|
|
'theme_colors_' . $name, |
243
|
|
|
[ |
244
|
|
|
'default' => $color['default'], |
245
|
|
|
'transport' => 'postMessage', |
246
|
|
|
] |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
// Controls |
250
|
|
|
$wp_customize->add_control( |
251
|
|
|
new WP_Customize_Color_Control( |
252
|
|
|
$wp_customize, |
253
|
|
|
'theme_colors_' . $name, |
254
|
|
|
[ |
255
|
|
|
'section' => 'theme_colors_hero', |
256
|
|
|
'label' => __($color['label']), |
257
|
|
|
'description' => __($color['description']), |
258
|
|
|
] |
259
|
|
|
) |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
}); |
263
|
|
|
|
264
|
|
|
add_action('customize_preview_init', function () { |
265
|
|
|
wp_enqueue_script( |
266
|
|
|
'customizer-colors', |
267
|
|
|
Asset::requireUrl('assets/customizer-colors.js'), |
268
|
|
|
array('jquery','customize-preview'), |
269
|
|
|
'', |
270
|
|
|
true |
271
|
|
|
); |
272
|
|
|
}); |
273
|
|
|
|
274
|
|
|
function hex2rgba($color, $opacity = false) |
275
|
|
|
{ |
276
|
|
|
$default = 'rgb(0,0,0)'; |
277
|
|
|
|
278
|
|
|
// Return default if no color provided |
279
|
|
|
if (empty($color)) { |
280
|
|
|
return $default; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// Sanitize $color if "#" is provided |
284
|
|
|
if ($color[0] == '#') { |
285
|
|
|
$color = substr($color, 1); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Check if color has 6 or 3 characters and get values |
289
|
|
|
if (strlen($color) == 6) { |
290
|
|
|
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); |
291
|
|
|
} elseif (strlen($color) == 3) { |
292
|
|
|
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); |
293
|
|
|
} else { |
294
|
|
|
return $default; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// Convert hexadec to rgb |
298
|
|
|
$rgb = array_map('hexdec', $hex); |
299
|
|
|
|
300
|
|
|
// Check if opacity is set(rgba or rgb) |
301
|
|
|
if ($opacity) { |
302
|
|
|
if (abs($opacity) > 1) { |
303
|
|
|
$opacity = 1.0; |
304
|
|
|
} |
305
|
|
|
$output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')'; |
306
|
|
|
} else { |
307
|
|
|
$output = 'rgb(' . implode(",", $rgb) . ')'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
// Return rgb(a) color string |
311
|
|
|
return $output; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
function colorBrightness($hex, $percent) |
315
|
|
|
{ |
316
|
|
|
// Work out if hash given |
317
|
|
|
$hash = ''; |
318
|
|
|
if (stristr($hex, '#')) { |
319
|
|
|
$hex = str_replace('#', '', $hex); |
320
|
|
|
$hash = '#'; |
321
|
|
|
} |
322
|
|
|
// HEX TO RGB |
323
|
|
|
$rgb = [hexdec(substr($hex, 0, 2)), hexdec(substr($hex, 2, 2)), hexdec(substr($hex, 4, 2))]; |
324
|
|
|
// CALCULATE |
325
|
|
|
for ($i = 0; $i < 3; $i++) { |
326
|
|
|
// See if brighter or darker |
327
|
|
|
if ($percent > 0) { |
328
|
|
|
// Lighter |
329
|
|
|
$rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1 - $percent)); |
330
|
|
|
} else { |
331
|
|
|
// Darker |
332
|
|
|
$positivePercent = $percent - ($percent * 2); |
333
|
|
|
$rgb[$i] = round($rgb[$i] * (1 - $positivePercent)); // round($rgb[$i] * (1-$positivePercent)); |
334
|
|
|
} |
335
|
|
|
// In case rounding up causes us to go to 256 |
336
|
|
|
if ($rgb[$i] > 255) { |
337
|
|
|
$rgb[$i] = 255; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
// RBG to Hex |
341
|
|
|
$hex = ''; |
342
|
|
|
for ($i = 0; $i < 3; $i++) { |
343
|
|
|
// Convert the decimal digit to hex |
344
|
|
|
$hexDigit = dechex($rgb[$i]); |
|
|
|
|
345
|
|
|
// Add a leading zero if necessary |
346
|
|
|
if (strlen($hexDigit) == 1) { |
347
|
|
|
$hexDigit = "0" . $hexDigit; |
348
|
|
|
} |
349
|
|
|
// Append to the hex string |
350
|
|
|
$hex .= $hexDigit; |
351
|
|
|
} |
352
|
|
|
return $hash . $hex; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
add_action('wp_head', function () use ($colorsDefault, $colorsLight, $colorsDark, $colorsHero) { |
356
|
|
|
$hasChangedDefault = array_filter($colorsDefault, function ($color, $name) { |
357
|
|
|
$value = get_theme_mod('theme_colors_' . $name, $color['default']); |
358
|
|
|
return $value !== $color['default']; |
359
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
360
|
|
|
|
361
|
|
|
$hasChangedLight = array_filter($colorsLight, function ($color, $name) { |
362
|
|
|
$value = get_theme_mod('theme_colors_' . $name, $color['default']); |
363
|
|
|
return $value !== $color['default']; |
364
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
365
|
|
|
|
366
|
|
|
$hasChangedDark = array_filter($colorsDark, function ($color, $name) { |
367
|
|
|
$value = get_theme_mod('theme_colors_' . $name, $color['default']); |
368
|
|
|
return $value !== $color['default']; |
369
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
370
|
|
|
|
371
|
|
|
$hasChangedHero = array_filter($colorsHero, function ($color, $name) { |
372
|
|
|
$value = get_theme_mod('theme_colors_' . $name, $color['default']); |
373
|
|
|
return $value !== $color['default']; |
374
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
375
|
|
|
|
376
|
|
|
$alphaColorAmount = 0.4; |
377
|
|
|
$darkenColorAmount = -0.1; // -0.1 darken value approximately equals scss darken 5% |
378
|
|
|
|
379
|
|
|
if ($hasChangedDefault or $hasChangedLight or $hasChangedDark or $hasChangedHero) { ?> |
380
|
|
|
<style type="text/css"> |
381
|
|
|
:root.html { |
382
|
|
|
<?php if ($hasChangedDefault) { |
|
|
|
|
383
|
|
|
foreach ($colorsDefault as $name => $color) { |
384
|
|
|
echo (get_theme_mod('theme_colors_' . $name)) ? '--theme-color-' . $name . ': ' . get_theme_mod('theme_colors_' . $name) . ';' : null; |
|
|
|
|
385
|
|
|
}; |
386
|
|
|
echo (get_theme_mod('theme_colors_accent-default')) ? '--theme-color-accent-alpha-default: ' . hex2rgba(get_theme_mod('theme_colors_accent-default'), $alphaColorAmount) . ';' : null; |
387
|
|
|
echo (get_theme_mod('theme_colors_accent-default')) ? '--theme-color-accent-hover-default: ' . colorBrightness(get_theme_mod('theme_colors_accent-default'), $darkenColorAmount) . ';' : null; |
388
|
|
|
} ?> |
389
|
|
|
<?php if ($hasChangedLight) { |
|
|
|
|
390
|
|
|
foreach ($colorsLight as $name => $color) { |
391
|
|
|
echo (get_theme_mod('theme_colors_' . $name)) ? '--theme-color-' . $name . ': ' . get_theme_mod('theme_colors_' . $name) . ';' : null; |
392
|
|
|
}; |
393
|
|
|
echo (get_theme_mod('theme_colors_accent-light')) ? '--theme-color-accent-alpha-light: ' . hex2rgba(get_theme_mod('theme_colors_accent-light'), $alphaColorAmount) . ';' : null; |
394
|
|
|
echo (get_theme_mod('theme_colors_accent-light')) ? '--theme-color-accent-hover-light: ' . colorBrightness(get_theme_mod('theme_colors_accent-light'), $darkenColorAmount) . ';' : null; |
395
|
|
|
} ?> |
396
|
|
|
<?php if ($hasChangedDark) { |
|
|
|
|
397
|
|
|
foreach ($colorsDark as $name => $color) { |
398
|
|
|
echo (get_theme_mod('theme_colors_' . $name)) ? '--theme-color-' . $name . ': ' . get_theme_mod('theme_colors_' . $name) . ';' : null; |
399
|
|
|
}; |
400
|
|
|
echo (get_theme_mod('theme_colors_accent-dark')) ? '--theme-color-accent-alpha-dark: ' . hex2rgba(get_theme_mod('theme_colors_accent-dark'), $alphaColorAmount) . ';' : null; |
401
|
|
|
echo (get_theme_mod('theme_colors_accent-dark')) ? '--theme-color-accent-hover-dark: ' . colorBrightness(get_theme_mod('theme_colors_accent-dark'), $darkenColorAmount) . ';' : null; |
402
|
|
|
} ?> |
403
|
|
|
<?php if ($hasChangedHero) { |
|
|
|
|
404
|
|
|
foreach ($colorsHero as $name => $color) { |
405
|
|
|
echo (get_theme_mod('theme_colors_' . $name)) ? '--theme-color-' . $name . ': ' . get_theme_mod('theme_colors_' . $name) . ';' : null; |
406
|
|
|
}; |
407
|
|
|
echo (get_theme_mod('theme_colors_accent-hero')) ? '--theme-color-accent-alpha-hero: ' . hex2rgba(get_theme_mod('theme_colors_accent-hero'), $alphaColorAmount) . ';' : null; |
408
|
|
|
echo (get_theme_mod('theme_colors_accent-hero')) ? '--theme-color-accent-hover-hero: ' . colorBrightness(get_theme_mod('theme_colors_accent-hero'), $darkenColorAmount) . ';' : null; |
409
|
|
|
} ?> |
410
|
|
|
} |
411
|
|
|
</style> |
412
|
|
|
<?php } |
413
|
|
|
}); |
414
|
|
|
|