PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.8.0
Gutenify – Visual Site Builder Blocks & Starter Templates v1.8.0
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / core / inc / helpers / class-typography-helpers.php

class-typography-helpers.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.8.0, at core/inc/helpers/class-typography-helpers.php

145 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Typography Helpers class.
4 *
5 * Provides utility functions to enqueue Fonts based on theme.json
6 * typography settings and to modify theme.json font family data accordingly.
7 *
8 * @package gutenify
9 * @subpackage Typography
10 * @since 1.0.0
11 */
12
13 namespace gutenify;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Helper class to manage typography-related functionalities
19 * such as enqueueing Google Fonts and modifying theme.json data.
20 */
21 class Typography_Helpers {
22
23 /**
24 * Initialize hooks.
25 */
26 public static function init() {
27 // Enqueue fonts for blocks.
28 add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_fonts' ) );
29
30 // Filter the theme JSON data for user modifications.
31 add_filter( 'wp_theme_json_data_user', array( __CLASS__, 'update_theme_json' ), 1 );
32 }
33
34 /**
35 * Generate Google Fonts URL parts from font configuration.
36 *
37 * @param array $font_config Font configuration array.
38 * @return string Google Fonts URL query part.
39 */
40 public static function generate_google_fonts_url_parts( $font_config ) {
41 if ( empty( $font_config['fontFace'] ) || empty( $font_config['name'] ) ) {
42 return '';
43 }
44
45 // Replace spaces in font name with '+' for URL encoding.
46 $font_family = str_replace( ' ', '+', $font_config['name'] );
47 $styles = array();
48
49 foreach ( $font_config['fontFace'] as $face ) {
50 $ital = ( strtolower( $face['fontStyle'] ) === 'italic' ) ? 1 : 0;
51 $weight = $face['fontWeight'];
52 $styles[] = "{$ital},{$weight}";
53 }
54
55 // Remove duplicate styles and sort them.
56 $styles = array_unique( $styles );
57 sort( $styles );
58
59 // Build style query string for Google Fonts URL.
60 $style_query = implode( ';', $styles );
61
62 return "family={$font_family}:ital,wght@{$style_query}";
63 }
64
65 /**
66 * Enqueue Google Fonts styles based on theme.json fontFamilies.
67 */
68 public static function enqueue_fonts() {
69 $constants = Helpers::plugin_constants();
70 $version = $constants['plugin_main_version'];
71 $demo_url = $constants['authorDemoWebSite'];
72 $theme_json_data = \WP_Theme_JSON_Resolver::get_user_data();
73 $data = $theme_json_data->get_data();
74 $font_settings = array();
75 if ( isset( $data['settings']['typography']['fontFamilies'] ) ) {
76 foreach ( $data['settings']['typography']['fontFamilies'] as $font ) {
77 if ( ! empty( $font['fontFace'] ) ) {
78 foreach ( $font['fontFace'] as $font_face ) {
79 $src = '';
80 if( ! empty( $font_face['src'] ) && is_string( $font_face['src'] ) ) {
81 $src = $font_face['src'];
82 } else if ( ! empty( $font_face['src'] ) && is_array( $font_face['src'] ) ) {
83 $src = $font_face['src'][0];
84 }
85 // Include fonts hosted on demo url or those without a 'src' attribute.
86 if ( ! empty( $src ) && false !== strpos( $src, $demo_url ) ) {
87 $font_settings[ $font['slug'] ] = self::generate_google_fonts_url_parts( $font );
88 } elseif ( empty( $src ) ) {
89 $font_settings[ $font['slug'] ] = self::generate_google_fonts_url_parts( $font );
90 }
91 }
92 }
93 }
94 if ( ! empty( $font_settings ) ) {
95 // Combine all font queries separated by '&'.
96 $final_font_settings = implode( '&', $font_settings );
97
98 // Enqueue Google Fonts stylesheet.
99 wp_enqueue_style( 'gutenify-bundle', esc_url( wptt_get_webfont_url( "https://fonts.googleapis.com/css2?{$final_font_settings}&display=swap" ) ), array(), $version );
100 }
101 }
102 }
103
104 /**
105 * Modify theme.json data to clean up font sources.
106 *
107 * @param WP_Theme_JSON $theme_json Theme JSON data object.
108 * @return WP_Theme_JSON Modified theme JSON data.
109 */
110 public static function update_theme_json( $theme_json ) {
111 $constants = Helpers::plugin_constants();
112 $demo_url = $constants['authorDemoWebSite'];
113
114 // Get current theme.json data as array.
115 $data = $theme_json->get_data();
116
117 // Append the custom font to existing fontFamilies.
118 if ( isset( $data['settings']['typography']['fontFamilies']['custom'] ) ) {
119 foreach ( $data['settings']['typography']['fontFamilies']['custom'] as $font_key => $font ) {
120 if ( ! empty( $font['fontFace'] ) ) {
121 foreach ( $font['fontFace'] as $font_face_key => $font_face ) {
122 $src = '';
123 if( ! empty( $font_face['src'] ) && is_string( $font_face['src'] ) ) {
124 $src = $font_face['src'];
125 } else if ( ! empty( $font_face['src'] ) && is_array( $font_face['src'] ) ) {
126 $src = $font_face['src'][0];
127 }
128 if ( ! empty( $src ) && false !== strpos( $src, $demo_url ) ) {
129 unset( $data['settings']['typography']['fontFamilies']['custom'][ $font_key ]['fontFace'][ $font_face_key ]['src'] );
130 }
131 }
132 }
133 }
134 }
135
136 // Update the theme JSON object with modified data.
137 $theme_json->update_with( $data );
138
139 return $theme_json;
140 }
141 }
142
143 // Initialize the Typography_Helpers class.
144 Typography_Helpers::init();
145