PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.9.0
Gutenify – Visual Site Builder Blocks & Starter Templates v1.9.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.9.0, at core/inc/helpers/class-typography-helpers.php

153 lines 5.1 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 // The loader is not part of the bootstrap list, so load it before first use.
99 if ( ! function_exists( 'wptt_get_webfont_url' ) ) {
100 $webfont_file = GUTENIFY_BASE_DIR . 'core/' . 'inc/lib/wptt-webfont-loader.php';
101 if ( file_exists( $webfont_file ) ) {
102 require_once $webfont_file;
103 }
104 }
105
106 // Enqueue Google Fonts stylesheet.
107 wp_enqueue_style( 'gutenify-bundle', esc_url( wptt_get_webfont_url( "https://fonts.googleapis.com/css2?{$final_font_settings}&display=swap" ) ), array(), $version );
108 }
109 }
110 }
111
112 /**
113 * Modify theme.json data to clean up font sources.
114 *
115 * @param WP_Theme_JSON $theme_json Theme JSON data object.
116 * @return WP_Theme_JSON Modified theme JSON data.
117 */
118 public static function update_theme_json( $theme_json ) {
119 $constants = Helpers::plugin_constants();
120 $demo_url = $constants['authorDemoWebSite'];
121
122 // Get current theme.json data as array.
123 $data = $theme_json->get_data();
124
125 // Append the custom font to existing fontFamilies.
126 if ( isset( $data['settings']['typography']['fontFamilies']['custom'] ) ) {
127 foreach ( $data['settings']['typography']['fontFamilies']['custom'] as $font_key => $font ) {
128 if ( ! empty( $font['fontFace'] ) ) {
129 foreach ( $font['fontFace'] as $font_face_key => $font_face ) {
130 $src = '';
131 if( ! empty( $font_face['src'] ) && is_string( $font_face['src'] ) ) {
132 $src = $font_face['src'];
133 } else if ( ! empty( $font_face['src'] ) && is_array( $font_face['src'] ) ) {
134 $src = $font_face['src'][0];
135 }
136 if ( ! empty( $src ) && false !== strpos( $src, $demo_url ) ) {
137 unset( $data['settings']['typography']['fontFamilies']['custom'][ $font_key ]['fontFace'][ $font_face_key ]['src'] );
138 }
139 }
140 }
141 }
142 }
143
144 // Update the theme JSON object with modified data.
145 $theme_json->update_with( $data );
146
147 return $theme_json;
148 }
149 }
150
151 // Initialize the Typography_Helpers class.
152 Typography_Helpers::init();
153