|
@@ -217,15 +217,19 @@ class GlobalStylesFontsCustomizer {
|
|
|
}
|
|
|
|
|
|
function customize_preview_js( $wp_customize ) {
|
|
|
- wp_enqueue_script( 'customizer-preview-fonts', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-preview.js', array( 'customize-preview' ) );
|
|
|
- wp_localize_script( 'customizer-preview-fonts', 'googleFonts', $this->fonts );
|
|
|
- wp_localize_script( 'customizer-preview-fonts', 'fontSettings', $this->font_settings );
|
|
|
+ if ( $this->fonts && $this->font_settings ) {
|
|
|
+ wp_enqueue_script( 'customizer-preview-fonts', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-preview.js', array( 'customize-preview' ) );
|
|
|
+ wp_localize_script( 'customizer-preview-fonts', 'googleFonts', $this->fonts );
|
|
|
+ wp_localize_script( 'customizer-preview-fonts', 'fontSettings', $this->font_settings );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function customize_control_js() {
|
|
|
- wp_enqueue_script( 'customizer-font-control', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-control.js', array( 'customize-controls' ), null, true );
|
|
|
- wp_localize_script( 'customizer-font-control', 'fontControlDefaultBody', array( $this->font_control_default_body ) );
|
|
|
- wp_localize_script( 'customizer-font-control', 'fontControlDefaultHeading', array( $this->font_control_default_heading ) );
|
|
|
+ if ( $this->font_control_default_body && $this->font_control_default_heading ) {
|
|
|
+ wp_enqueue_script( 'customizer-font-control', get_template_directory_uri() . '/inc/customizer/wp-customize-fonts-control.js', array( 'customize-controls' ), null, true );
|
|
|
+ wp_localize_script( 'customizer-font-control', 'fontControlDefaultBody', array( $this->font_control_default_body ) );
|
|
|
+ wp_localize_script( 'customizer-font-control', 'fontControlDefaultHeading', array( $this->font_control_default_heading ) );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function enqueue_google_fonts() {
|
|
@@ -233,27 +237,31 @@ class GlobalStylesFontsCustomizer {
|
|
|
}
|
|
|
|
|
|
function create_customization_style_element( $wp_customize ) {
|
|
|
- wp_enqueue_style( 'global-styles-fonts-customizations', ' ', array( 'global-styles' ) ); // This needs to load after global_styles, hence the dependency
|
|
|
- $css = 'body{';
|
|
|
- $css .= 'font-family:' . $this->font_settings['body'] . ';';
|
|
|
- $css .= '}';
|
|
|
- $css .= 'h1,h2,h3,h4,h5,h6,.wp-block-post-title,.wp-block-pullquote{';
|
|
|
- $css .= 'font-family:' . $this->font_settings['heading'] . ';';
|
|
|
- $css .= '}';
|
|
|
- wp_add_inline_style( 'global-styles-fonts-customizations', $css );
|
|
|
+ if ( $this->font_settings ) {
|
|
|
+ wp_enqueue_style( 'global-styles-fonts-customizations', ' ', array( 'global-styles' ) ); // This needs to load after global_styles, hence the dependency
|
|
|
+ $css = 'body {';
|
|
|
+ $css .= '--wp--preset--font-family--body-font:' . $this->font_settings['body'] . ';';
|
|
|
+ $css .= '--wp--preset--font-family--heading-font:' . $this->font_settings['heading'] . ';';
|
|
|
+ $css .= '}';
|
|
|
+ wp_add_inline_style( 'global-styles-fonts-customizations', $css );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function update_font_settings( $wp_customize ) {
|
|
|
- $body_value = $wp_customize->get_setting( $this->section_key . 'body' )->post_value();
|
|
|
- if ( $body_value ) {
|
|
|
- $body_font_setting = $this->fonts[ $body_value ];
|
|
|
- $this->font_settings['body'] = $body_font_setting['fontFamily'];
|
|
|
- }
|
|
|
+ $body_setting = $wp_customize->get_setting( $this->section_key . 'body' );
|
|
|
+ $heading_setting = $wp_customize->get_setting( $this->section_key . 'heading' );
|
|
|
+ if ( $body_setting && $heading_setting ) {
|
|
|
+ $body_value = $body_setting->post_value();
|
|
|
+ if ( $body_value ) {
|
|
|
+ $body_font_setting = $this->fonts[ $body_value ];
|
|
|
+ $this->font_settings['body'] = $body_font_setting['fontFamily'];
|
|
|
+ }
|
|
|
|
|
|
- $heading_value = $wp_customize->get_setting( $this->section_key . 'heading' )->post_value();
|
|
|
- if ( $heading_value ) {
|
|
|
- $heading_font_setting = $this->fonts[ $heading_value ];
|
|
|
- $this->font_settings['heading'] = $heading_font_setting['fontFamily'];
|
|
|
+ $heading_value = $heading_setting->post_value();
|
|
|
+ if ( $heading_value ) {
|
|
|
+ $heading_font_setting = $this->fonts[ $heading_value ];
|
|
|
+ $this->font_settings['heading'] = $heading_font_setting['fontFamily'];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -272,19 +280,6 @@ class GlobalStylesFontsCustomizer {
|
|
|
|
|
|
function initialize( $wp_customize ) {
|
|
|
$theme = wp_get_theme();
|
|
|
- $merged_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
|
|
|
- $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_raw_data();
|
|
|
-
|
|
|
- $body_font_default = $this->get_font_family( array( 'styles', 'typography', 'fontFamily' ), $theme_json );
|
|
|
- $heading_font_default = $this->get_font_family( array( 'styles', 'elements', 'h1', 'typography', 'fontFamily' ), $theme_json );
|
|
|
-
|
|
|
- $body_font_selected = $this->get_font_family( array( 'styles', 'typography', 'fontFamily' ), $merged_json );
|
|
|
- $heading_font_selected = $this->get_font_family( array( 'styles', 'elements', 'h1', 'typography', 'fontFamily' ), $merged_json );
|
|
|
-
|
|
|
- $this->font_settings = array(
|
|
|
- 'body' => $body_font_selected['fontFamily'],
|
|
|
- 'heading' => $heading_font_selected['fontFamily'],
|
|
|
- );
|
|
|
|
|
|
//Add a Section to the Customizer for these bits
|
|
|
$wp_customize->add_section(
|
|
@@ -296,9 +291,71 @@ class GlobalStylesFontsCustomizer {
|
|
|
)
|
|
|
);
|
|
|
|
|
|
+ $merged_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
|
|
|
+ $theme_font_families = $merged_json['settings']['typography']['fontFamilies']['theme'];
|
|
|
+ $body_font_default_array = array_filter( $theme_font_families, function( $font_family ) {
|
|
|
+ return $font_family['slug'] === "body-font";
|
|
|
+ } );
|
|
|
+ $body_font_default = array_shift( $body_font_default_array );
|
|
|
+ $heading_font_default_array = array_filter( $theme_font_families, function( $font_family ) {
|
|
|
+ return $font_family['slug'] === "heading-font";
|
|
|
+ } );
|
|
|
+ $heading_font_default = array_shift( $heading_font_default_array );
|
|
|
+
|
|
|
+ // See if the child theme has been updated. If not then show a notice.
|
|
|
+ if ( ! $body_font_default && ! $heading_font_default ) {
|
|
|
+ $wp_customize->add_control(
|
|
|
+ $this->section_key . '-v1-blockbase-format-notice',
|
|
|
+ array(
|
|
|
+ 'type' => 'hidden',
|
|
|
+ 'description' => '<div class="notice notice-warning">
|
|
|
+ <p>' . __( "Your theme needs to be updated before you can customize fonts", 'blockbase' ) . '</p>
|
|
|
+ </div>',
|
|
|
+ 'settings' => array(),
|
|
|
+ 'section' => $this->section_key,
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( array_key_exists( 'user', $merged_json['settings']['typography']['fontFamilies'] ) ) {
|
|
|
+ $merged_font_families = $merged_json['settings']['typography']['fontFamilies']['user'];
|
|
|
+ $body_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
|
|
|
+ return $font_family['slug'] === "body-font";
|
|
|
+ } );
|
|
|
+ $body_font_selected = array_shift( $body_font_selected_array );
|
|
|
+
|
|
|
+ $heading_font_selected_array = array_filter( $merged_font_families, function( $font_family ) {
|
|
|
+ return $font_family['slug'] === "heading-font";
|
|
|
+ } );
|
|
|
+ $heading_font_selected = array_shift( $heading_font_selected_array );
|
|
|
+ } else {
|
|
|
+ $body_font_selected = $body_font_default;
|
|
|
+ $heading_font_selected = $heading_font_default;
|
|
|
+ }
|
|
|
+
|
|
|
+ // If there's no selected font then the user is probably using the old format for font customization
|
|
|
+ if ( $body_font_selected && $heading_font_selected ) {
|
|
|
+ $body_font_selected_font_family = $body_font_selected['fontFamily'];
|
|
|
+ $body_font_selected_font_slug = $body_font_selected['fontSlug'];
|
|
|
+ $heading_font_selected_font_family = $heading_font_selected['fontFamily'];
|
|
|
+ $heading_font_selected_font_slug = $heading_font_selected['fontSlug'];
|
|
|
+ } else {
|
|
|
+ $body_font_selected_font_family = null;
|
|
|
+ $body_font_selected_font_slug = null;
|
|
|
+ $heading_font_selected_font_family = null;
|
|
|
+ $heading_font_selected_font_slug = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->font_settings = array(
|
|
|
+ 'body' => $body_font_selected_font_family,
|
|
|
+ 'heading' => $heading_font_selected_font_family,
|
|
|
+ );
|
|
|
+
|
|
|
// Add a reset button
|
|
|
- $this->font_control_default_body = $body_font_default['slug'];
|
|
|
- $this->font_control_default_heading = $heading_font_default['slug'];
|
|
|
+ $this->font_control_default_body = $body_font_default['fontSlug'];
|
|
|
+ $this->font_control_default_heading = $heading_font_default['fontSlug'];
|
|
|
$wp_customize->add_control(
|
|
|
$this->section_key . '-reset-button',
|
|
|
array(
|
|
@@ -312,8 +369,8 @@ class GlobalStylesFontsCustomizer {
|
|
|
)
|
|
|
);
|
|
|
|
|
|
- $this->add_setting_and_control( $wp_customize, 'body', __( 'Body font', 'blockbase' ), $body_font_default['slug'], $body_font_selected['slug'], 'sanitize_title' );
|
|
|
- $this->add_setting_and_control( $wp_customize, 'heading', __( 'Heading font', 'blockbase' ), $heading_font_default['slug'], $heading_font_selected['slug'], 'sanitize_title' );
|
|
|
+ $this->add_setting_and_control( $wp_customize, 'body', __( 'Body font', 'blockbase' ), $body_font_default['fontSlug'], $body_font_selected_font_slug, 'sanitize_title' );
|
|
|
+ $this->add_setting_and_control( $wp_customize, 'heading', __( 'Heading font', 'blockbase' ), $heading_font_default['fontSlug'], $heading_font_selected_font_slug, 'sanitize_title' );
|
|
|
}
|
|
|
|
|
|
function get_font_family( $array, $configuration ) {
|
|
@@ -399,8 +456,15 @@ class GlobalStylesFontsCustomizer {
|
|
|
$heading_value = $heading_default;
|
|
|
}
|
|
|
|
|
|
- $body_setting = $this->fonts[ $body_value ];
|
|
|
+ $body_setting = $this->fonts[ $body_value ];
|
|
|
+ $body_setting['name'] = "Body (" . $body_setting['name'] . ")";
|
|
|
+ $body_setting['fontSlug'] = $body_setting['slug'];
|
|
|
+ $body_setting['slug'] = "body-font";
|
|
|
+
|
|
|
$heading_setting = $this->fonts[ $heading_value ];
|
|
|
+ $heading_setting['name'] = "Heading (" . $heading_setting['name'] . ")";
|
|
|
+ $heading_setting['fontSlug'] = $heading_setting['slug'];
|
|
|
+ $heading_setting['slug'] = "heading-font";
|
|
|
|
|
|
// Set up variables for the theme.json.
|
|
|
$font_families = array(
|
|
@@ -427,71 +491,11 @@ class GlobalStylesFontsCustomizer {
|
|
|
$font_families
|
|
|
);
|
|
|
|
|
|
- // Set the body typography settings.
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'typography', 'fontFamily' ),
|
|
|
- $body_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'blocks', 'core/button', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- // Set the heading typography settings.
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h1', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h2', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h3', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h4', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h5', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'elements', 'h6', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'blocks', 'core/post-title', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
- $user_theme_json_post_content = set_settings_array(
|
|
|
- $user_theme_json_post_content,
|
|
|
- array( 'styles', 'blocks', 'core/pullquote', 'typography', 'fontFamily' ),
|
|
|
- $heading_font_family_variable
|
|
|
- );
|
|
|
-
|
|
|
//If the typeface choices === the default then we remove it instead
|
|
|
if ( $body_value === $body_default && $heading_value === $heading_default ) {
|
|
|
unset( $user_theme_json_post_content->settings->typography->fontFamilies );
|
|
|
+
|
|
|
+ // These lines need to stay for backwards compatibility.
|
|
|
unset( $user_theme_json_post_content->styles->typography->fontFamily );
|
|
|
unset( $user_theme_json_post_content->styles->elements->h1->typography->fontFamily );
|
|
|
unset( $user_theme_json_post_content->styles->elements->h2->typography->fontFamily );
|