Skip to content

Commit

Permalink
WIP migrating Blockbase Customizer font settings
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed May 9, 2022
1 parent d7c6553 commit 8a0935f
Showing 1 changed file with 54 additions and 34 deletions.
88 changes: 54 additions & 34 deletions blockbase/inc/customizer/wp-customize-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,45 +409,63 @@ function( $font_family ) {
)
);

$body_font_selected_font_variable = $merged_json['styles']['typography']['fontFamily'];
preg_match( '/font-family\|(?P<slug>.+)$/', $body_font_selected_font_variable, $matches );
$body_font_selected_font_slug = $matches['slug'] ?? '';

$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 ) {
$variable = get_settings_array( $array, $configuration );
$slug = preg_replace( '/var\(--wp--preset--font-family--(.*)\)/', '$1', $variable );
if ( ! isset( $this->fonts[ $slug ] ) ) {
$this->fonts[ $slug ] = $this->build_font_from_theme_data( $slug, $configuration );
}
return $this->fonts[ $slug ];
}

function build_font_from_theme_data( $slug, $configuration ) {
$new_font = array();
$font_families = $configuration['settings']['typography']['fontFamilies']['theme'];
foreach ( $font_families as $font_family ) {
if ( $font_family['slug'] === $slug ) {
$new_font['fontFamily'] = $font_family['fontFamily'];
$new_font['name'] = $font_family['name'];
if ( ! empty( $font_family['google'] ) ) {
$new_font['google'] = $font_family['google'];
}
}
}
$new_font['slug'] = $slug;
return $new_font;
}
// Not used, yo!!!
// function get_font_family( $array, $configuration ) {
// $variable = get_settings_array( $array, $configuration );
// $slug = preg_replace( '/var\(--wp--preset--font-family--(.*)\)/', '$1', $variable );
// if ( ! isset( $this->fonts[ $slug ] ) ) {
// $this->fonts[ $slug ] = $this->build_font_from_theme_data( $slug, $configuration );
// }
// return $this->fonts[ $slug ];
// }

// function build_font_from_theme_data( $slug, $configuration ) {
// $new_font = array();
// $font_families = $configuration['settings']['typography']['fontFamilies']['theme'];
// foreach ( $font_families as $font_family ) {
// if ( $font_family['slug'] === $slug ) {
// $new_font['fontFamily'] = $font_family['fontFamily'];
// $new_font['name'] = $font_family['name'];
// if ( ! empty( $font_family['google'] ) ) {
// $new_font['google'] = $font_family['google'];
// }
// }
// }
// $new_font['slug'] = $slug;
// return $new_font;
// }

function add_setting_and_control( $wp_customize, $name, $label, $default, $user_value, $sanitize_callback ) {
$setting_name = $this->section_key . $name;
$global_styles_setting = new WP_Customize_Global_Styles_Setting(
$wp_customize,
$setting_name,
array(
'default' => $default,
'user_value' => $user_value,
)
);

if ( $name === 'body' ) {
$global_styles_setting = new WP_Customize_Global_Styles_Setting(
$wp_customize,
$setting_name,
array(
'default' => $default,
'user_value' => $user_value,
)
);
} else {
$global_styles_setting = new WP_Customize_Setting(
$wp_customize,
$setting_name,
array(
'default' => $default,
'user_value' => $user_value,
)
);
}

$wp_customize->add_setting(
$global_styles_setting,
array(
Expand Down Expand Up @@ -513,7 +531,8 @@ function handle_customize_save_after( $wp_customize ) {
$heading_setting,
);

$body_font_family_variable = 'var(--wp--preset--font-family--' . $body_setting['slug'] . ')';
// $body_font_family_variable = 'var(--wp--preset--font-family--' . $body_setting['slug'] . ')';
$body_font_family_variable = 'var:preset|font-family|' . $body_setting['fontSlug'];
$heading_font_family_variable = 'var(--wp--preset--font-family--' . $heading_setting['slug'] . ')';

// Get the user's global styles CPT id
Expand All @@ -532,7 +551,8 @@ function handle_customize_save_after( $wp_customize ) {

// Set new typography settings
if ( $font_families ) {
$new_settings['typography']['fontFamilies']['custom'] = $font_families;
// $new_settings['typography']['fontFamilies']['custom'] = $font_families;
$new_styles['typography']['fontFamily'] = $body_font_family_variable;
}

// Add the updated global styles to the update request
Expand Down

0 comments on commit 8a0935f

Please sign in to comment.