/var/www/html/wp-content/plugins/elementor/core/files/fonts/google-font.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
262
263
264
265
266
<?php
namespace Elementor\Core\Files\Fonts;

use 
Elementor\Plugin;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

class 
Google_Font {

    const 
FOLDER_BASE 'elementor/google-fonts';

    const 
FOLDER_CSS 'css';

    const 
FOLDER_FONTS 'fonts';

    const 
AVAILABLE_FOLDERS = [
        
self::FOLDER_CSS,
        
self::FOLDER_FONTS,
    ];

    const 
UA_STRING 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36';

    const 
TYPE_DEFAULT 'default';

    const 
TYPE_EARLYACCESS 'earlyaccess';

    private static array 
$folders = [];

    public static function 
enqueuestring $font_namestring $font_type self::TYPE_DEFAULT ): bool {
        if ( static::
enqueue_style$font_name ) ) {
            return 
true;
        }

        if ( ! static::
fetch_font_data$font_name$font_type ) ) {
            static::
enqueue_from_cdn$font_name$font_type );
            return 
false;
        }

        return static::
enqueue_style$font_name );
    }

    private static function 
sanitize_font_namestring $font_name ): string {
        return 
sanitize_key$font_name );

    }

    private static function 
enqueue_stylestring $font_name ): bool {
        
$sanitize_font_name = static::sanitize_font_name$font_name );

        
$font_data = static::get_font_data$sanitize_font_name );

        if ( empty( 
$font_data['url'] ) ) {
            return 
false;
        }

        
wp_enqueue_style(
            
'elementor-gf-local-' $sanitize_font_name,
            
$font_data['url'],
            [],
            
$font_data['version']
        );

        return 
true;
    }

    private static function 
get_font_datastring $font_name ) {
        
$local_google_fonts = static::get_local_google_fonts();

        return 
$local_google_fonts$font_name ] ?? null;
    }

    private static function 
get_local_google_fonts(): array {
        return (array) 
get_option'_elementor_local_google_fonts', [] );
    }

    private static function 
set_local_google_fontsstring $font_name, array $font_data ): void {
        
$local_google_fonts = static::get_local_google_fonts();

        
$local_google_fonts$font_name ] = $font_data;

        
update_option'_elementor_local_google_fonts'$local_google_fonts );
    }

    private static function 
fetch_font_datastring $font_namestring $font_type ): bool {
        
$sanitize_font_name = static::sanitize_font_name$font_name );

        
$fonts_folder = static::get_folder( static::FOLDER_FONTS );
        
$css_folder = static::get_folder( static::FOLDER_CSS );

        if ( empty( 
$fonts_folder ) || empty( $css_folder ) ) {
            return 
false;
        }

        
$css_content = static::get_css_content$font_name$font_type );

        if ( empty( 
$css_content ) ) {
            return 
false;
        }

        
$font_data = [
            
'url' => $css_folder['url'] . $sanitize_font_name '.css',
            
'version' => time(),
        ];

        
$css_folder_path $css_folder['path'] . $sanitize_font_name '.css';

        
$is_font_file_saved file_put_contents$css_folder_path$css_content );

        if ( ! 
$is_font_file_saved ) {
            return 
false;
        }

        static::
set_local_google_fonts$sanitize_font_name$font_data );

        return 
true;
    }

    private static function 
get_folderstring $folder ): array {
        
$folders = static::get_folders();

        return 
$folders$folder ] ?? [];
    }

    private static function 
get_folders(): array {
        static::
init_folders();

        return static::
$folders;
    }

    private static function 
init_folders(): void {
        if ( ! empty( static::
$folders ) ) {
            return;
        }

        static::
$folders = [];

        
$upload_dir wp_upload_dir();

        foreach ( static::
AVAILABLE_FOLDERS as $folder ) {
            
$folder_path $upload_dir['basedir'] . '/' . static::FOLDER_BASE '/' $folder;
            
$folder_url $upload_dir['baseurl'] . '/' . static::FOLDER_BASE '/' $folder;

            if ( ! 
file_exists$folder_path ) ) {
                
wp_mkdir_p$folder_path );
            }

            static::
$folders$folder ] = [
                
'path' => trailingslashit$folder_path ),
                
'url' => trailingslashit$folder_url ),
            ];
        }
    }

    private static function 
get_css_contentstring $font_name$font_type ): string {
        
$css_content = static::get_raw_css_content$font_name$font_type );

        if ( empty( 
$css_content ) ) {
            return 
'';
        }

        return static::
download_fonts$font_name$css_content );
    }

    private static function 
get_raw_css_contentstring $font_namestring $font_type ): string {
        
$font_url = static::get_google_fonts_remote_url$font_name$font_type );

        
$css_content_response wp_remote_get$font_url, [
            
'headers' => [
                
'User-Agent' => static::UA_STRING,
            ],
        ] );

        if ( 
is_wp_error$css_content_response ) || 200 !== (int) wp_remote_retrieve_response_code$css_content_response ) ) {
            return 
'';
        }

        return 
wp_remote_retrieve_body$css_content_response );
    }

    private static function 
get_google_fonts_remote_urlstring $fontstring $font_type ): string {
        if ( 
self::TYPE_EARLYACCESS === $font_type ) {
            return static::
get_earlyaccess_google_fonts_url$font );
        }

        return 
Plugin::$instance->frontend->get_stable_google_fonts_url( [ $font ] );
    }

    private static function 
get_earlyaccess_google_fonts_urlstring $font ): string {
        return 
sprintf'https://fonts.googleapis.com/earlyaccess/%s.css'strtolowerstr_replace' '''$font ) ) );
    }

    private static function 
download_fontsstring $font_namestring $css_content ): string {
        
preg_match_all'/url\(([^)]+)\)/'$css_content$font_urls );

        if ( ! 
function_exists'download_url' ) ) {
            require_once 
ABSPATH 'wp-admin/includes/file.php';
        }

        if ( ! empty( 
$font_urls[1] ) ) {
            
$font_urls $font_urls[1];

            
$fonts_folder = static::get_folder( static::FOLDER_FONTS );
            
$sanitize_font_name = static::sanitize_font_name$font_name );

            foreach ( 
$font_urls as $current_font_url ) {
                
$original_font_url trim$current_font_url'\'"' );

                
$cleaned_url set_url_scheme$original_font_url'https' );
                
$cleaned_url strtok$cleaned_url'?#' );

                
$tmp_font_file download_url$cleaned_url );
                if ( 
is_wp_error$tmp_font_file ) ) {
                    return 
'';
                }

                
$current_font_basename $sanitize_font_name '-' strtolowersanitize_file_namebasename$cleaned_url ) ) );

                
$dest_file $fonts_folder['path'] . $current_font_basename;
                
$dest_file_url $fonts_folder['url'] . $current_font_basename;

                
// Use copy and unlink because rename breaks streams.
                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
                
$is_font_file_saved = @copy$tmp_font_file$dest_file );
                @
unlink$tmp_font_file );

                if ( ! 
$is_font_file_saved ) {
                    return 
'';
                }

                
$css_content str_replace$original_font_url$dest_file_url$css_content );
            }
        }

        return 
$css_content;
    }

    private static function 
enqueue_from_cdnstring $font_namestring $font_type ): void {
        
$font_url = static::get_google_fonts_remote_url$font_name$font_type );

        
$sanitize_font_name = static::sanitize_font_name$font_name );

        
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
        
wp_enqueue_style(
            
'elementor-gf-' $sanitize_font_name,
            
$font_url,
            [],
            
null
        
);
    }

    public static function 
clear_cache() {
        
$folders = static::get_folders();

        foreach ( 
$folders as $folder ) {
            
$path $folder['path'] . '*';

            foreach ( 
glob$path ) as $file_path ) {
                
unlink$file_path );
            }
        }

        
delete_option'_elementor_local_google_fonts' );
    }
}