Automattic\WooCommerce\Internal\ComingSoon
ComingSoonRequestHandler::experimental_filter_theme_json_theme
Filters the theme.json data to add Coming Soon fonts. This runs after child theme merging to ensure parent theme fonts are included.
Метод класса: ComingSoonRequestHandler{}
Хуков нет.
Возвращает
WP_Theme_JSON_Data. The filtered theme json data.
Использование
$ComingSoonRequestHandler = new ComingSoonRequestHandler(); $ComingSoonRequestHandler->experimental_filter_theme_json_theme( $theme_json );
- $theme_json(WP_Theme_JSON_Data) (обязательный)
- The theme json data object.
Код ComingSoonRequestHandler::experimental_filter_theme_json_theme() ComingSoonRequestHandler::experimental filter theme json theme WC 10.3.6
public function experimental_filter_theme_json_theme( $theme_json ) {
if ( ! Features::is_enabled( 'launch-your-store' ) ) {
return $theme_json;
}
$theme_data = $theme_json->get_data();
$font_data = $theme_data['settings']['typography']['fontFamilies']['theme'] ?? array();
// Check if the current theme is a child theme. And if so, merge the parent theme fonts with the existing fonts.
if ( wp_get_theme()->parent() ) {
$parent_theme = wp_get_theme()->parent();
$parent_theme_json_file = $parent_theme->get_file_path( 'theme.json' );
if ( is_readable( $parent_theme_json_file ) ) {
$parent_theme_json_data = json_decode( file_get_contents( $parent_theme_json_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( isset( $parent_theme_json_data['settings']['typography']['fontFamilies'] ) ) {
$parent_fonts = $parent_theme_json_data['settings']['typography']['fontFamilies'];
// Merge parent theme fonts with existing fonts.
foreach ( $parent_fonts as $parent_font ) {
$found = false;
foreach ( $font_data as $existing_font ) {
if ( isset( $parent_font['name'] ) && isset( $existing_font['name'] ) &&
$parent_font['name'] === $existing_font['name'] ) {
$found = true;
break;
}
}
if ( ! $found ) {
$font_data[] = $parent_font;
}
}
}
}
}
$fonts_to_add = array(
array(
'fontFamily' => '"Inter", sans-serif',
'name' => 'Inter',
'slug' => 'inter',
'fontFace' => array(
array(
'fontFamily' => 'Inter',
'fontStretch' => 'normal',
'fontStyle' => 'normal',
'fontWeight' => '300 900',
'src' => array( WC()->plugin_url() . '/assets/fonts/Inter-VariableFont_slnt,wght.woff2' ),
),
),
),
array(
'fontFamily' => 'Cardo',
'name' => 'Cardo',
'slug' => 'cardo',
'fontFace' => array(
array(
'fontFamily' => 'Cardo',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => array( WC()->plugin_url() . '/assets/fonts/cardo_normal_400.woff2' ),
),
),
),
);
// Add WooCommerce fonts if they don't already exist.
foreach ( $fonts_to_add as $font_to_add ) {
$found = false;
foreach ( $font_data as $font ) {
if ( isset( $font['name'] ) && $font['name'] === $font_to_add['name'] ) {
$found = true;
break;
}
}
if ( ! $found ) {
$font_data[] = $font_to_add;
}
}
$new_data = array(
'version' => 1,
'settings' => array(
'typography' => array(
'fontFamilies' => array(
'theme' => $font_data,
),
),
),
);
$theme_json->update_with( $new_data );
return $theme_json;
}