<?php
/**
* Typography related functions.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! function_exists( 'generate_enqueue_google_fonts' ) ) {
add_action( 'wp_enqueue_scripts', 'generate_enqueue_google_fonts', 0 );
/**
* Add Google Fonts to wp_head if needed.
*
* @since 0.1
*/
function generate_enqueue_google_fonts() {
if ( generate_is_using_dynamic_typography() ) {
return;
}
$generate_settings = wp_parse_args(
get_option( 'generate_settings', array() ),
generate_get_default_fonts()
);
$not_google = str_replace( ' ', '+', generate_typography_default_fonts() );
$font_settings = array(
'font_body',
'font_top_bar',
'font_site_title',
'font_site_tagline',
'font_navigation',
'font_widget_title',
'font_buttons',
'font_heading_1',
'font_heading_2',
'font_heading_3',
'font_heading_4',
'font_heading_5',
'font_heading_6',
'font_footer',
);
$google_fonts = array();
if ( ! empty( $font_settings ) ) {
foreach ( $font_settings as $key ) {
if ( ! isset( $generate_settings[ $key ] ) ) {
continue;
}
// If our value is still using the old format, fix it.
if ( strpos( $generate_settings[ $key ], ':' ) !== false ) {
$generate_settings[ $key ] = current( explode( ':', $generate_settings[ $key ] ) );
}
$value = str_replace( ' ', '+', $generate_settings[ $key ] );
$variants = generate_get_google_font_variants( $generate_settings[ $key ], $key );
$value = ! empty( $variants ) ? $value . ':' . $variants : $value;
// Make sure we don't add the same font twice.
if ( ! in_array( $value, $google_fonts ) ) {
$google_fonts[] = $value;
}
}
}
// Ignore any non-Google fonts.
$google_fonts = array_diff( $google_fonts, $not_google );
$google_fonts = implode( '|', $google_fonts );
$google_fonts = apply_filters( 'generate_typography_google_fonts', $google_fonts );
$subset = apply_filters( 'generate_fonts_subset', '' );
$font_args = array();
$font_args['family'] = $google_fonts;
if ( '' !== $subset ) {
$font_args['subset'] = rawurlencode( $subset );
}
$display = apply_filters( 'generate_google_font_display', '' );
if ( $display ) {
$font_args['display'] = $display;
}
$fonts_url = add_query_arg( $font_args, '//fonts.googleapis.com/css' );
if ( $google_fonts ) {
wp_enqueue_style( 'generate-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore
}
}
}
if ( ! function_exists( 'generate_default_fonts_customize_register' ) ) {
add_action( 'customize_register', 'generate_default_fonts_customize_register' );
/**
* Build our Typography options
*
* @since 0.1
*
* @param std_Class $wp_customize The Customize class.
*/
function generate_default_fonts_customize_register( $wp_customize ) {
if ( generate_is_using_dynamic_typography() ) {
return;
}
if ( function_exists( 'generate_fonts_customize_register' ) ) {
// Bail if GP Premium is active.
return;
}
require_once trailingslashit( get_template_directory() ) . 'inc/customizer/customizer-helpers.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
$defaults = generate_get_default_fonts();
if ( method_exists( $wp_customize, 'register_control_type' ) ) {
$wp_customize->register_control_type( 'Generate_Typography_Customize_Control' );
$wp_customize->register_control_type( 'Generate_Range_Slider_Control' );
}
$wp_customize->add_section(
'font_section',
array(
'title' => __( 'Typography', 'generatepress' ),
'capability' => 'edit_theme_options',
'priority' => 30,
'active_callback' => function() {
if ( generate_is_using_dynamic_typography() ) {
return false;
}
return true;
},
)
);
$wp_customize->add_setting(
'generate_settings[font_body]',
array(
'default' => $defaults['font_body'],
'type' => 'option',
'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_setting(
'font_body_category',
array(
'default' => $defaults['font_body_category'],
'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_setting(
'font_body_variants',
array(
'default' => $defaults['font_body_variants'],
'sanitize_callback' => 'generate_sanitize_variants',
)
);
$wp_customize->add_setting(
'generate_settings[body_font_weight]',
array(
'default' => $defaults['body_font_weight'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
)
);
$wp_customize->add_setting(
'generate_settings[body_font_transform]',
array(
'default' => $defaults['body_font_transform'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
)
);
$wp_customize->add_control(
new Generate_Typography_Customize_Control(
$wp_customize,
'body_typography',
array(
'label' => __( 'Body', 'generatepress' ),
'section' => 'font_section',
'priority' => 1,
'settings' => array(
'family' => 'generate_settings[font_body]',
'variant' => 'font_body_variants',
'category' => 'font_body_category',
'weight' => 'generate_settings[body_font_weight]',
'transform' => 'generate_settings[body_font_transform]',
),
)
)
);
$wp_customize->add_setting(
'generate_settings[body_font_size]',
array(
'default' => $defaults['body_font_size'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_integer',
'transport' => 'postMessage',
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[body_font_size]',
array(
'type' => 'generatepress-range-slider',
'description' => __( 'Font size', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[body_font_size]',
),
'choices' => array(
'desktop' => array(
'min' => 6,
'max' => 25,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[body_line_height]',
array(
'default' => $defaults['body_line_height'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_decimal_integer',
'transport' => 'postMessage',
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[body_line_height]',
array(
'type' => 'generatepress-range-slider',
'description' => __( 'Line height', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[body_line_height]',
),
'choices' => array(
'desktop' => array(
'min' => 1,
'max' => 3,
'step' => .1,
'edit' => true,
'unit' => '',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[paragraph_margin]',
array(
'default' => $defaults['paragraph_margin'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_decimal_integer',
'transport' => 'postMessage',
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[paragraph_margin]',
array(
'type' => 'generatepress-range-slider',
'description' => __( 'Paragraph margin', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[paragraph_margin]',
),
'choices' => array(
'desktop' => array(
'min' => 0,
'max' => 5,
'step' => .1,
'edit' => true,
'unit' => 'em',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[font_heading_1]',
array(
'default' => $defaults['font_heading_1'],
'type' => 'option',
'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_setting(
'font_heading_1_category',
array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_setting(
'font_heading_1_variants',
array(
'default' => '',
'sanitize_callback' => 'generate_sanitize_variants',
)
);
$wp_customize->add_setting(
'generate_settings[heading_1_weight]',
array(
'default' => $defaults['heading_1_weight'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
Loading ...