-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle-variation-switcher.php
263 lines (226 loc) · 10.4 KB
/
style-variation-switcher.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
<?php
/**
* Plugin Name: Style Variation Switcher
* Description: Allows visitors to switch between block theme style variations on the frontend.
* Version: 0.1.0
* Author: Nick Diego
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: style-variation-switcher
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Style_Variation_Switcher {
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'wp_footer', array( $this, 'render_switcher' ) );
add_action( 'wp_head', array( $this, 'preload_variation_fonts' ), 100 );
}
/**
* Preload all fonts from all variations and the default theme
*/
public function preload_variation_fonts(): void {
$variations = WP_Theme_JSON_Resolver::get_style_variations();
$theme_data = WP_Theme_JSON_Resolver::get_theme_data();
$font_faces = array();
// Get fonts from default theme.
$default_fonts = $theme_data->get_data()['settings']['typography']['fontFamilies']['theme'] ?? array();
foreach ( $default_fonts as $font ) {
if ( isset( $font['fontFace'] ) ) {
$generated = $this->generate_font_face( $font );
if ( $generated ) {
$font_faces[] = $generated;
}
}
}
// Get fonts from all variations.
foreach ( $variations as $variation ) {
$variation_fonts = $variation['settings']['typography']['fontFamilies']['theme'] ?? array();
foreach ( $variation_fonts as $font ) {
if ( isset( $font['fontFace'] ) ) {
$generated = $this->generate_font_face( $font );
if ( $generated ) {
$font_faces[] = $generated;
}
}
}
}
$unique_font_faces = array_unique( array_filter( $font_faces ) );
if ( ! empty( $unique_font_faces ) ) {
echo '<style class="wp-fonts-local">' . "\n";
echo implode( "\n", $unique_font_faces );
echo '</style>' . "\n";
}
}
/**
* Generate font-face CSS for a font
*
* @param array $font Font configuration.
* @return string Font-face CSS.
*/
private function generate_font_face( $font ): string {
if ( ! isset( $font['fontFace'] ) ) {
return '';
}
$css = '';
foreach ( $font['fontFace'] as $face ) {
if ( ! isset( $face['src'] ) ) {
continue;
}
$srcs = array_map( function( $src ) {
// Clean up the file path.
$src = str_replace( 'file:./', '', $src );
$src = str_replace( 'file:/', '', $src );
// Get the full URL.
$url = get_theme_file_uri( $src );
return sprintf(
"url('%s') format('%s')",
esc_url( $url ),
$face['format'] ?? 'woff2'
);
}, $face['src'] );
$font_family = preg_replace( '/,\s*(?:serif|sans-serif|monospace)$/i', '', $font['fontFamily'] );
if ( ! empty( $srcs ) ) {
$css .= sprintf(
"@font-face{font-family:%s;font-style:%s;font-weight:%s;font-display:%s;src:%s;}",
$font_family,
$face['fontStyle'] ?? 'normal',
$face['fontWeight'] ?? '400',
'fallback',
implode( ',', $srcs )
);
}
}
return $css;
}
public function enqueue_assets(): void {
wp_enqueue_style(
'style-variation-switcher',
plugin_dir_url( __FILE__ ) . 'assets/css/style-variation-switcher.css',
array(),
'1.0.0'
);
wp_enqueue_script(
'style-variation-switcher',
plugin_dir_url( __FILE__ ) . 'assets/js/style-variation-switcher.js',
array( 'jquery' ),
'1.0.0',
true
);
$variations = array_map( function( $variation ) {
if ( isset( $variation['settings'] ) &&
array_key_exists( 'color', $variation['settings'] ) &&
count( array_diff( array_keys( $variation['settings'] ), array( 'color', 'custom' ) ) ) === 0 ) {
$variation['color_only'] = true;
} else {
$variation['color_only'] = false;
}
return $variation;
}, WP_Theme_JSON_Resolver::get_style_variations() );
// Remove duplicates based on title.
$variations = array_values( array_reduce( $variations, function( $carry, $variation ) {
$carry[ sanitize_title( $variation['title'] ) ] = $variation;
return $carry;
}, array() ) );
$variation_styles = array();
// Get default stylesheet.
$variation_styles['default'] = wp_get_global_stylesheet();
// Get each variation's styles.
foreach ( $variations as $variation ) {
$variation_slug = sanitize_title( $variation['title'] );
$variation_styles[ $variation_slug ] = $this->generate_variation_css( $variation );
}
wp_localize_script( 'style-variation-switcher', 'styleVariationSwitcher', array(
'variations' => $variation_styles,
'variationsData' => $variations,
) );
}
/**
* Generate CSS for a single variation
*
* @param array $variation The variation data.
* @return string Generated CSS.
*/
private function generate_variation_css( $variation ) {
$base_theme_data = WP_Theme_JSON_Resolver::get_theme_data();
// Create a new theme.json instance with the variation data.
$variation_json = new WP_Theme_JSON(
array_merge(
$base_theme_data->get_data(),
$variation
)
);
return $variation_json->get_stylesheet() ?? '';
}
public function render_switcher(): void {
static $variations = null;
if ( is_null( $variations ) ) {
$variations = array_map( function( $variation ) {
if ( isset( $variation['settings'] ) &&
array_key_exists( 'color', $variation['settings'] ) &&
count( array_diff( array_keys( $variation['settings'] ), array( 'color', 'custom' ) ) ) === 0 ) {
$variation['color_only'] = true;
} else {
$variation['color_only'] = false;
}
return $variation;
}, WP_Theme_JSON_Resolver::get_style_variations() );
// Remove duplicates based on title.
$variations = array_values( array_reduce( $variations, function( $carry, $variation ) {
$carry[ sanitize_title( $variation['title'] ) ] = $variation;
return $carry;
}, array() ) );
// Filter to only show color-only variations.
$variations = array_filter( $variations, function( $variation ) {
return isset( $variation['color_only'] ) && $variation['color_only'] === true;
} );
}
if ( empty( $variations ) ) {
return;
}
// Get default theme colors from theme.json.
$theme_data = WP_Theme_JSON_Resolver::get_theme_data();
$default_colors = $theme_data->get_data()['settings']['color']['palette'] ?? array();
$default_palette = array(
'base' => $default_colors[0]['color'] ?? '#000000',
'contrast' => $default_colors[1]['color'] ?? '#ffffff',
'accent1' => $default_colors[2]['color'] ?? '#dddddd',
'accent2' => $default_colors[3]['color'] ?? '#999999',
);
?>
<div class="style-variation-switcher">
<button class="color-swatch default-swatch active" data-value="">
<div class="swatch-grid">
<span class="swatch-color" style="background-color: <?php echo esc_attr( $default_palette['base'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $default_palette['contrast'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $default_palette['accent1'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $default_palette['accent2'] ); ?>"></span>
</div>
<span class="screen-reader-text"><?php esc_html_e( 'Default Style', 'style-variation-switcher' ); ?></span>
</button>
<?php foreach ( $variations as $variation ) : ?>
<?php
$variation_slug = sanitize_title( $variation['title'] );
$colors = array(
'base' => $variation['settings']['color']['palette']['theme'][0]['color'] ?? '#000000',
'contrast' => $variation['settings']['color']['palette']['theme'][1]['color'] ?? '#ffffff',
'accent1' => $variation['settings']['color']['palette']['theme'][2]['color'] ?? '#dddddd',
'accent2' => $variation['settings']['color']['palette']['theme'][3]['color'] ?? '#999999',
);
?>
<button class="color-swatch" data-value="<?php echo esc_attr( $variation_slug ); ?>">
<div class="swatch-grid">
<span class="swatch-color" style="background-color: <?php echo esc_attr( $colors['base'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $colors['contrast'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $colors['accent1'] ); ?>"></span>
<span class="swatch-color" style="background-color: <?php echo esc_attr( $colors['accent2'] ); ?>"></span>
</div>
<span class="screen-reader-text"><?php echo esc_html( $variation['title'] ); ?></span>
</button>
<?php endforeach; ?>
</div>
<?php
}
}
new Style_Variation_Switcher();