Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/google-f.../current
File: load-google-fonts.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Load the google fonts by the new Font Library. See pNEWy-hhx-p2.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit( 0 );
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
if ( ! class_exists( 'Jetpack_Google_Font_Face' ) ) {
[11] Fix | Delete
/**
[12] Fix | Delete
* Load Jetpack Google Font Face
[13] Fix | Delete
*/
[14] Fix | Delete
require_once __DIR__ . '/class-jetpack-google-font-face.php';
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Gets the Google Fonts data
[19] Fix | Delete
*
[20] Fix | Delete
* @return object[] The collection data of the Google Fonts.
[21] Fix | Delete
*/
[22] Fix | Delete
function jetpack_get_google_fonts_data() {
[23] Fix | Delete
/**
[24] Fix | Delete
* Filters the Google Fonts data before the default retrieval process.
[25] Fix | Delete
*
[26] Fix | Delete
* This filter allows short-circuiting the default Google Fonts data retrieval process.
[27] Fix | Delete
* Returning a non-null value from this filter will bypass the default retrieval
[28] Fix | Delete
* and return the filtered value instead.
[29] Fix | Delete
*
[30] Fix | Delete
* @module google-fonts
[31] Fix | Delete
*
[32] Fix | Delete
* @since 13.7
[33] Fix | Delete
*
[34] Fix | Delete
* @param null|array $pre The pre-filtered Google Fonts data, default null.
[35] Fix | Delete
*/
[36] Fix | Delete
$pre = apply_filters( 'pre_jetpack_get_google_fonts_data', null );
[37] Fix | Delete
if ( null !== $pre ) {
[38] Fix | Delete
return $pre;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
$default_google_fonts_api_url = 'https://fonts.gstatic.com';
[42] Fix | Delete
$jetpack_google_fonts_collection_url = 'https://s0.wp.com/i/font-collections/jetpack-google-fonts.json';
[43] Fix | Delete
$cache_key = 'jetpack_google_fonts_' . md5( $jetpack_google_fonts_collection_url );
[44] Fix | Delete
$data = get_transient( $cache_key );
[45] Fix | Delete
if ( $data === false ) {
[46] Fix | Delete
$response = wp_remote_get( $jetpack_google_fonts_collection_url );
[47] Fix | Delete
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
[48] Fix | Delete
return null;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
$data = json_decode( wp_remote_retrieve_body( $response ), true );
[52] Fix | Delete
if ( $data === null ) {
[53] Fix | Delete
return null;
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
set_transient( $cache_key, $data, DAY_IN_SECONDS );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
// Replace the google fonts api url if the custom one is provided.
[60] Fix | Delete
$custom_google_fonts_api_url = \esc_url(
[61] Fix | Delete
/**
[62] Fix | Delete
* Filters the Google Fonts API URL.
[63] Fix | Delete
*
[64] Fix | Delete
* @module google-fonts
[65] Fix | Delete
*
[66] Fix | Delete
* @since 12.8
[67] Fix | Delete
*
[68] Fix | Delete
* @param string $url The Google Fonts API URL.
[69] Fix | Delete
*/
[70] Fix | Delete
apply_filters( 'jetpack_google_fonts_api_url', $default_google_fonts_api_url )
[71] Fix | Delete
);
[72] Fix | Delete
if ( $custom_google_fonts_api_url !== $default_google_fonts_api_url ) {
[73] Fix | Delete
foreach ( $data['fontFamilies'] as &$font_family ) {
[74] Fix | Delete
foreach ( $font_family['fontFace'] as &$font_face ) {
[75] Fix | Delete
$font_face['src'] = str_replace(
[76] Fix | Delete
$default_google_fonts_api_url,
[77] Fix | Delete
$custom_google_fonts_api_url,
[78] Fix | Delete
$font_face['src']
[79] Fix | Delete
);
[80] Fix | Delete
}
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
if ( is_array( $data ) && is_array( $data['fontFamilies'] ) ) {
[85] Fix | Delete
return $data;
[86] Fix | Delete
}
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
/**
[90] Fix | Delete
* Gets the map of the available Google Fonts
[91] Fix | Delete
*
[92] Fix | Delete
* @param object[] $google_fonts_data The collection data of the Google Fonts.
[93] Fix | Delete
* @return object[] The map of the the available Google Fonts.
[94] Fix | Delete
*/
[95] Fix | Delete
function jetpack_get_available_google_fonts_map( $google_fonts_data ) {
[96] Fix | Delete
$jetpack_google_fonts_list = array_map(
[97] Fix | Delete
function ( $font_family ) {
[98] Fix | Delete
return $font_family['name'];
[99] Fix | Delete
},
[100] Fix | Delete
$google_fonts_data['fontFamilies']
[101] Fix | Delete
);
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Curated list of Google Fonts.
[105] Fix | Delete
*
[106] Fix | Delete
* @module google-fonts
[107] Fix | Delete
*
[108] Fix | Delete
* @since 10.8
[109] Fix | Delete
*
[110] Fix | Delete
* @param array $fonts_to_register Array of Google Font names to register.
[111] Fix | Delete
*/
[112] Fix | Delete
$google_font_list = apply_filters( 'jetpack_google_fonts_list', $jetpack_google_fonts_list );
[113] Fix | Delete
$available_google_fonts_map = array();
[114] Fix | Delete
[115] Fix | Delete
foreach ( $google_font_list as $google_font ) {
[116] Fix | Delete
$available_google_fonts_map[ $google_font ] = true;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
return $available_google_fonts_map;
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Register google fonts to the theme json data
[124] Fix | Delete
*
[125] Fix | Delete
* @param WP_Theme_JSON_Data $theme_json The theme json data of core.
[126] Fix | Delete
* @return WP_Theme_JSON_Data The theme json data with registered google fonts.
[127] Fix | Delete
*/
[128] Fix | Delete
function jetpack_register_google_fonts_to_theme_json( $theme_json ) {
[129] Fix | Delete
$google_fonts_data = jetpack_get_google_fonts_data();
[130] Fix | Delete
if ( ! $google_fonts_data ) {
[131] Fix | Delete
return $theme_json;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
$available_google_fonts_map = jetpack_get_available_google_fonts_map( $google_fonts_data );
[135] Fix | Delete
$google_fonts_families = array_values(
[136] Fix | Delete
array_filter(
[137] Fix | Delete
$google_fonts_data['fontFamilies'],
[138] Fix | Delete
function ( $google_fonts_family ) use ( $available_google_fonts_map ) {
[139] Fix | Delete
$name = $google_fonts_family['name'];
[140] Fix | Delete
return $available_google_fonts_map[ $name ] ?? false;
[141] Fix | Delete
}
[142] Fix | Delete
)
[143] Fix | Delete
);
[144] Fix | Delete
[145] Fix | Delete
$raw_data = $theme_json->get_data();
[146] Fix | Delete
$origin = 'default';
[147] Fix | Delete
if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) {
[148] Fix | Delete
$raw_data['settings']['typography']['fontFamilies'][ $origin ] = array();
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
foreach ( $google_fonts_families as $font_family ) {
[152] Fix | Delete
$raw_data['settings']['typography']['fontFamilies'][ $origin ][] = $font_family;
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
$theme_json_class = get_class( $theme_json );
[156] Fix | Delete
return new $theme_json_class( $raw_data, $origin );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
add_filter( 'wp_theme_json_data_default', 'jetpack_register_google_fonts_to_theme_json' );
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Filter out the deprecated font families that are from the jetpack-google-fonts provider.
[163] Fix | Delete
*
[164] Fix | Delete
* @param object[] $font_families The font families.
[165] Fix | Delete
* @return object[] The filtered font families.
[166] Fix | Delete
*/
[167] Fix | Delete
function jetpack_google_fonts_filter_out_deprecated_font_data( $font_families ) {
[168] Fix | Delete
return array_values(
[169] Fix | Delete
array_filter(
[170] Fix | Delete
$font_families,
[171] Fix | Delete
function ( $font_family ) {
[172] Fix | Delete
$has_deprecated_google_fonts_data = false;
[173] Fix | Delete
[174] Fix | Delete
if ( isset( $font_family['fontFace'] ) ) {
[175] Fix | Delete
foreach ( $font_family['fontFace'] as $font_face ) {
[176] Fix | Delete
$provider = $font_face['provider'] ?? '';
[177] Fix | Delete
if ( $provider === 'jetpack-google-fonts' ) {
[178] Fix | Delete
$has_deprecated_google_fonts_data = true;
[179] Fix | Delete
break;
[180] Fix | Delete
}
[181] Fix | Delete
}
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
return ! $has_deprecated_google_fonts_data;
[185] Fix | Delete
}
[186] Fix | Delete
)
[187] Fix | Delete
);
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Unregister the deprecated jetpack-google-fonts provider from theme json data that were stored
[192] Fix | Delete
* before we moved to the Font Library.
[193] Fix | Delete
*
[194] Fix | Delete
* @param WP_Theme_JSON_Data $theme_json The theme json data.
[195] Fix | Delete
* @return WP_Theme_JSON_Data The filtered theme json data.
[196] Fix | Delete
*/
[197] Fix | Delete
function jetpack_unregister_deprecated_google_fonts_from_theme_json_data( $theme_json ) {
[198] Fix | Delete
$raw_data = $theme_json->get_data();
[199] Fix | Delete
$origin = 'theme';
[200] Fix | Delete
if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) {
[201] Fix | Delete
return $theme_json;
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
// Filter out the font definitions that are from the jetpack-google-fonts provider.
[205] Fix | Delete
$raw_data['settings']['typography']['fontFamilies'][ $origin ] = jetpack_google_fonts_filter_out_deprecated_font_data(
[206] Fix | Delete
$raw_data['settings']['typography']['fontFamilies'][ $origin ]
[207] Fix | Delete
);
[208] Fix | Delete
[209] Fix | Delete
$theme_json_class = get_class( $theme_json );
[210] Fix | Delete
return new $theme_json_class( $raw_data, 'custom' );
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
add_filter( 'wp_theme_json_data_theme', 'jetpack_unregister_deprecated_google_fonts_from_theme_json_data' );
[214] Fix | Delete
add_filter( 'wp_theme_json_data_user', 'jetpack_unregister_deprecated_google_fonts_from_theme_json_data' );
[215] Fix | Delete
[216] Fix | Delete
/**
[217] Fix | Delete
* Clean up the Google Fonts data if either google fonts module is disabled or Jetpack is disabled.
[218] Fix | Delete
*/
[219] Fix | Delete
function jetpack_unregister_google_fonts() {
[220] Fix | Delete
$post_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
[221] Fix | Delete
[222] Fix | Delete
// Get user config
[223] Fix | Delete
$user_config = WP_Theme_JSON_Resolver::get_user_data();
[224] Fix | Delete
$user_config_raw_data = $user_config->get_raw_data();
[225] Fix | Delete
$user_config_raw_data['isGlobalStylesUserThemeJSON'] = true;
[226] Fix | Delete
[227] Fix | Delete
// Prepare data for saving
[228] Fix | Delete
if ( ! empty( $user_config_raw_data['settings']['typography']['fontFamilies']['default'] ) ) {
[229] Fix | Delete
$user_config_raw_data['settings']['typography']['fontFamilies']['default'] = array();
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
if ( ! empty( $user_config_raw_data['settings']['typography']['fontFamilies']['theme'] ) ) {
[233] Fix | Delete
$user_config_raw_data['settings']['typography']['fontFamilies']['theme'] = jetpack_google_fonts_filter_out_deprecated_font_data(
[234] Fix | Delete
$user_config_raw_data['settings']['typography']['fontFamilies']['theme'] // @phan-suppress-current-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument
[235] Fix | Delete
);
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
// Prepare changes
[239] Fix | Delete
$changes = new stdClass();
[240] Fix | Delete
$changes->ID = $post_id;
[241] Fix | Delete
$changes->post_content = wp_json_encode( $user_config_raw_data, JSON_UNESCAPED_SLASHES );
[242] Fix | Delete
[243] Fix | Delete
// Update user config
[244] Fix | Delete
wp_update_post( wp_slash( (array) $changes ), true );
[245] Fix | Delete
}
[246] Fix | Delete
add_action( 'jetpack_deactivate_module_google-fonts', 'jetpack_unregister_google_fonts' );
[247] Fix | Delete
[248] Fix | Delete
// Initialize Jetpack Google Font Face to avoid printing **ALL** google fonts provided by this module.
[249] Fix | Delete
// See p1700040028362329-slack-C4GAQ900P and p7DVsv-jib-p2
[250] Fix | Delete
new Jetpack_Google_Font_Face();
[251] Fix | Delete
[252] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function