get_data(); $origin = 'default'; if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) { $raw_data['settings']['typography']['fontFamilies'][ $origin ] = array(); } foreach ( $google_fonts_families as $font_family ) { $raw_data['settings']['typography']['fontFamilies'][ $origin ][] = $font_family; } $theme_json_class = get_class( $theme_json ); return new $theme_json_class( $raw_data, $origin ); } add_filter( 'wp_theme_json_data_default', 'jetpack_register_google_fonts_to_theme_json' ); /** * Filter out the deprecated font families that are from the jetpack-google-fonts provider. * * @param object[] $font_families The font families. * @return object[] The filtered font families. */ function jetpack_google_fonts_filter_out_deprecated_font_data( $font_families ) { return array_values( array_filter( $font_families, function ( $font_family ) { $has_deprecated_google_fonts_data = false; if ( isset( $font_family['fontFace'] ) ) { foreach ( $font_family['fontFace'] as $font_face ) { $provider = $font_face['provider'] ?? ''; if ( $provider === 'jetpack-google-fonts' ) { $has_deprecated_google_fonts_data = true; break; } } } return ! $has_deprecated_google_fonts_data; } ) ); } /** * Unregister the deprecated jetpack-google-fonts provider from theme json data that were stored * before we moved to the Font Library. * * @param WP_Theme_JSON_Data $theme_json The theme json data. * @return WP_Theme_JSON_Data The filtered theme json data. */ function jetpack_unregister_deprecated_google_fonts_from_theme_json_data( $theme_json ) { $raw_data = $theme_json->get_data(); $origin = 'theme'; if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) { return $theme_json; } // Filter out the font definitions that are from the jetpack-google-fonts provider. $raw_data['settings']['typography']['fontFamilies'][ $origin ] = jetpack_google_fonts_filter_out_deprecated_font_data( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ); $theme_json_class = get_class( $theme_json ); return new $theme_json_class( $raw_data, 'custom' ); } add_filter( 'wp_theme_json_data_theme', 'jetpack_unregister_deprecated_google_fonts_from_theme_json_data' ); add_filter( 'wp_theme_json_data_user', 'jetpack_unregister_deprecated_google_fonts_from_theme_json_data' ); /** * Clean up the Google Fonts data if either google fonts module is disabled or Jetpack is disabled. */ function jetpack_unregister_google_fonts() { $post_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); // Get user config $user_config = WP_Theme_JSON_Resolver::get_user_data(); $user_config_raw_data = $user_config->get_raw_data(); $user_config_raw_data['isGlobalStylesUserThemeJSON'] = true; // Prepare data for saving if ( ! empty( $user_config_raw_data['settings']['typography']['fontFamilies']['default'] ) ) { $user_config_raw_data['settings']['typography']['fontFamilies']['default'] = array(); } if ( ! empty( $user_config_raw_data['settings']['typography']['fontFamilies']['theme'] ) ) { $user_config_raw_data['settings']['typography']['fontFamilies']['theme'] = jetpack_google_fonts_filter_out_deprecated_font_data( $user_config_raw_data['settings']['typography']['fontFamilies']['theme'] // @phan-suppress-current-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument ); } // Prepare changes $changes = new stdClass(); $changes->ID = $post_id; $changes->post_content = wp_json_encode( $user_config_raw_data ); // Update user config wp_update_post( wp_slash( (array) $changes ), true ); } add_action( 'jetpack_deactivate_module_google-fonts', 'jetpack_unregister_google_fonts' ); // Initialize Jetpack Google Font Face to avoid printing **ALL** google fonts provided by this module. // See p1700040028362329-slack-C4GAQ900P and p7DVsv-jib-p2 new Jetpack_Google_Font_Face();