PluginProbe
Gutenberg / 23.5.0
Gutenberg v23.5.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-7.0 / global-styles.php

global-styles.php in Gutenberg 23.5.0, at lib/compat/wordpress-7.0/global-styles.php

163 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress 7.0 compatibility functions for Global Styles.
4 *
5 * Overrides WordPress Core functions to enable full global styles support
6 * (including fonts) for classic themes without theme.json.
7 *
8 * @package gutenberg
9 */
10
11 // Remove WordPress Core's wp_print_font_faces action.
12 remove_action( 'wp_head', 'wp_print_font_faces', 50 );
13
14 /**
15 * Gets font-face styles CSS for fonts from Gutenberg's global styles.
16 *
17 * WordPress Core's wp_print_font_faces() relies on wp_get_global_settings()
18 * which excludes custom origin for classic themes. This function uses
19 * Gutenberg's settings resolver to include user-customized fonts.
20 *
21 * @since Gutenberg 20.0.0
22 * @return string Font-face CSS.
23 */
24 function gutenberg_get_font_face_styles() {
25 // Get settings using Gutenberg's resolver with custom origin.
26 $settings = gutenberg_get_global_settings();
27
28 // Bail out early if there are no font settings.
29 if ( empty( $settings['typography']['fontFamilies'] ) ) {
30 return '';
31 }
32
33 // Parse fonts from settings.
34 $fonts = array();
35 foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
36 foreach ( $font_families as $definition ) {
37 // Skip if "fontFace" is not defined, meaning there are no variations.
38 if ( empty( $definition['fontFace'] ) ) {
39 continue;
40 }
41
42 // Skip if "fontFamily" is not defined.
43 if ( empty( $definition['fontFamily'] ) ) {
44 continue;
45 }
46
47 $font_family_name = $definition['fontFamily'];
48 // Parse font-family name from comma-separated lists.
49 if ( str_contains( $font_family_name, ',' ) ) {
50 $font_family_name = explode( ',', $font_family_name )[0];
51 }
52 $font_family_name = trim( $font_family_name, "\"'" );
53
54 // Skip if no font family is defined.
55 if ( empty( $font_family_name ) ) {
56 continue;
57 }
58
59 // Convert font face properties.
60 $converted_font_faces = array();
61 foreach ( $definition['fontFace'] as $font_face ) {
62 // Add the font-family property to the font-face.
63 $font_face['font-family'] = $font_family_name;
64
65 // Convert relative font URLs to absolute theme URLs.
66 if ( ! empty( $font_face['src'] ) ) {
67 $src = (array) $font_face['src'];
68 $placeholder = 'file:./';
69
70 foreach ( $src as $src_key => $src_url ) {
71 // Skip if the src doesn't start with the placeholder
72 if ( ! str_starts_with( $src_url, $placeholder ) ) {
73 continue;
74 }
75
76 $src_file = str_replace( $placeholder, '', $src_url );
77 $src[ $src_key ] = get_theme_file_uri( $src_file );
78 }
79
80 $font_face['src'] = $src;
81 }
82
83 // Convert camelCase to kebab-case.
84 $converted_face = array();
85 foreach ( $font_face as $key => $value ) {
86 $kebab_case = _wp_to_kebab_case( $key );
87 $converted_face[ $kebab_case ] = $value;
88 }
89
90 $converted_font_faces[] = $converted_face;
91 }
92
93 $fonts[] = $converted_font_faces;
94 }
95 }
96
97 if ( empty( $fonts ) ) {
98 return '';
99 }
100
101 // Use WordPress Core's WP_Font_Face class to generate the CSS.
102 if ( ! class_exists( 'WP_Font_Face' ) ) {
103 return '';
104 }
105
106 // WP_Font_Face only has generate_and_print(), so use output buffering to capture.
107 ob_start();
108 $wp_font_face = new WP_Font_Face();
109 $wp_font_face->generate_and_print( $fonts );
110 $css = ob_get_clean();
111
112 // Remove the wrapping <style> tags if present, we'll add our own.
113 $css = preg_replace( '/<\/?style[^>]*>/', '', $css );
114
115 return trim( $css );
116 }
117
118 /**
119 * Prints font-face styles for fonts on the frontend.
120 *
121 * @since Gutenberg 20.0.0
122 */
123 function gutenberg_print_font_faces() {
124 $font_face_styles = gutenberg_get_font_face_styles();
125 if ( ! empty( $font_face_styles ) ) {
126 printf( "<style id='wp-fonts-local'>\n%s\n</style>\n", $font_face_styles );
127 }
128 }
129 add_action( 'wp_head', 'gutenberg_print_font_faces', 50 );
130
131 /**
132 * Adds font-face styles to block editor settings.
133 *
134 * @since Gutenberg 20.0.0
135 *
136 * @param array $settings Block editor settings.
137 * @return array Modified settings.
138 */
139 function gutenberg_add_font_face_styles_to_editor( $settings ) {
140 $font_face_styles = gutenberg_get_font_face_styles();
141
142 if ( ! empty( $font_face_styles ) ) {
143 // Add font faces as a style entry in the editor settings.
144 if ( ! isset( $settings['styles'] ) ) {
145 $settings['styles'] = array();
146 }
147
148 // Insert font faces at the beginning so they load before other styles.
149 array_unshift(
150 $settings['styles'],
151 array(
152 'css' => $font_face_styles,
153 '__unstableType' => 'font-faces',
154 'isGlobalStyles' => false,
155 )
156 );
157 }
158
159 return $settings;
160 }
161 // Run after gutenberg_get_block_editor_settings() which runs at priority 0.
162 add_filter( 'block_editor_settings_all', 'gutenberg_add_font_face_styles_to_editor', 5 );
163