laipower/wp-content/themes/generatepress/inc/typography.php

1122 lines
94 KiB
PHP
Raw Normal View History

2020-04-07 13:03:04 +00:00
<?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() {
$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.
2020-04-07 13:03:04 +00:00
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.
2020-04-07 13:03:04 +00:00
if ( ! in_array( $value, $google_fonts ) ) {
$google_fonts[] = $value;
}
}
}
// Ignore any non-Google fonts.
2020-04-07 13:03:04 +00:00
$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', '' );
2020-04-07 13:03:04 +00:00
$font_args = array();
$font_args['family'] = $google_fonts;
2020-04-07 13:03:04 +00:00
if ( '' !== $subset ) {
$font_args['subset'] = rawurlencode( $subset );
2020-04-07 13:03:04 +00:00
}
$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
2020-04-07 13:03:04 +00:00
}
}
}
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.
2020-04-07 13:03:04 +00:00
*/
function generate_default_fonts_customize_register( $wp_customize ) {
if ( function_exists( 'generate_fonts_customize_register' ) ) {
// Bail if GP Premium is active.
2020-04-07 13:03:04 +00:00
return;
}
require_once trailingslashit( get_template_directory() ) . 'inc/customizer/customizer-helpers.php'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
2020-04-07 13:03:04 +00:00
$defaults = generate_get_default_fonts();
if ( method_exists( $wp_customize, 'register_control_type' ) ) {
2020-04-07 13:03:04 +00:00
$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,
)
);
$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',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_1_category',
array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_1_variants',
array(
'default' => '',
'sanitize_callback' => 'generate_sanitize_variants',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_1_weight]',
array(
'default' => $defaults['heading_1_weight'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_1_transform]',
array(
'default' => $defaults['heading_1_transform'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Typography_Customize_Control(
$wp_customize,
'font_heading_1_control',
array(
'label' => __( 'Heading 1 (H1)', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'family' => 'generate_settings[font_heading_1]',
'variant' => 'font_heading_1_variants',
'category' => 'font_heading_1_category',
'weight' => 'generate_settings[heading_1_weight]',
'transform' => 'generate_settings[heading_1_transform]',
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_1_font_size]',
array(
'default' => $defaults['heading_1_font_size'],
'type' => 'option',
'sanitize_callback' => 'absint',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[mobile_heading_1_font_size]',
array(
'default' => $defaults['mobile_heading_1_font_size'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_empty_absint',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'h1_font_sizes',
array(
'description' => __( 'Font size', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_1_font_size]',
'mobile' => 'generate_settings[mobile_heading_1_font_size]',
),
'choices' => array(
'desktop' => array(
'min' => 15,
'max' => 100,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
'mobile' => array(
'min' => 15,
'max' => 100,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_1_line_height]',
array(
'default' => $defaults['heading_1_line_height'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_decimal_integer',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[heading_1_line_height]',
array(
'description' => __( 'Line height', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_1_line_height]',
),
'choices' => array(
'desktop' => array(
'min' => 0,
'max' => 5,
'step' => .1,
'edit' => true,
'unit' => 'em',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[font_heading_2]',
array(
'default' => $defaults['font_heading_2'],
'type' => 'option',
'sanitize_callback' => 'sanitize_text_field',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_2_category',
array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_2_variants',
array(
'default' => '',
'sanitize_callback' => 'generate_sanitize_variants',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_2_weight]',
array(
'default' => $defaults['heading_2_weight'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_2_transform]',
array(
'default' => $defaults['heading_2_transform'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Typography_Customize_Control(
$wp_customize,
'font_heading_2_control',
array(
'label' => __( 'Heading 2 (H2)', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'family' => 'generate_settings[font_heading_2]',
'variant' => 'font_heading_2_variants',
'category' => 'font_heading_2_category',
'weight' => 'generate_settings[heading_2_weight]',
'transform' => 'generate_settings[heading_2_transform]',
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_2_font_size]',
array(
'default' => $defaults['heading_2_font_size'],
'type' => 'option',
'sanitize_callback' => 'absint',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[mobile_heading_2_font_size]',
array(
'default' => $defaults['mobile_heading_2_font_size'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_empty_absint',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'h2_font_sizes',
array(
'description' => __( 'Font size', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_2_font_size]',
'mobile' => 'generate_settings[mobile_heading_2_font_size]',
),
'choices' => array(
'desktop' => array(
'min' => 10,
'max' => 80,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
'mobile' => array(
'min' => 10,
'max' => 80,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_2_line_height]',
array(
'default' => $defaults['heading_2_line_height'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_decimal_integer',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[heading_2_line_height]',
array(
'description' => __( 'Line height', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_2_line_height]',
),
'choices' => array(
'desktop' => array(
'min' => 0,
'max' => 5,
'step' => .1,
'edit' => true,
'unit' => 'em',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[font_heading_3]',
array(
'default' => $defaults['font_heading_3'],
'type' => 'option',
'sanitize_callback' => 'sanitize_text_field',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_3_category',
array(
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'font_heading_3_variants',
array(
'default' => '',
'sanitize_callback' => 'generate_sanitize_variants',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_3_weight]',
array(
'default' => $defaults['heading_3_weight'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_setting(
'generate_settings[heading_3_transform]',
array(
'default' => $defaults['heading_3_transform'],
'type' => 'option',
'sanitize_callback' => 'sanitize_key',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Typography_Customize_Control(
$wp_customize,
'font_heading_3_control',
array(
'label' => __( 'Heading 3 (H3)', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'family' => 'generate_settings[font_heading_3]',
'variant' => 'font_heading_3_variants',
'category' => 'font_heading_3_category',
'weight' => 'generate_settings[heading_3_weight]',
'transform' => 'generate_settings[heading_3_transform]',
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_3_font_size]',
array(
'default' => $defaults['heading_3_font_size'],
'type' => 'option',
'sanitize_callback' => 'absint',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'h3_font_sizes',
array(
'description' => __( 'Font size', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_3_font_size]',
2020-04-07 13:03:04 +00:00
),
'choices' => array(
'desktop' => array(
'min' => 10,
'max' => 80,
'step' => 1,
'edit' => true,
'unit' => 'px',
),
),
)
)
);
$wp_customize->add_setting(
'generate_settings[heading_3_line_height]',
array(
'default' => $defaults['heading_3_line_height'],
'type' => 'option',
'sanitize_callback' => 'generate_sanitize_decimal_integer',
'transport' => 'postMessage',
2020-04-07 13:03:04 +00:00
)
);
$wp_customize->add_control(
new Generate_Range_Slider_Control(
$wp_customize,
'generate_settings[heading_3_line_height]',
array(
'description' => __( 'Line height', 'generatepress' ),
'section' => 'font_section',
'settings' => array(
'desktop' => 'generate_settings[heading_3_line_height]',
),
'choices' => array(
'desktop' => array(
'min' => 0,
'max' => 5,
'step' => .1,
'edit' => true,
'unit' => 'em',
),
),
)
)
);
if ( ! function_exists( 'generate_fonts_customize_register' ) && ! defined( 'GP_PREMIUM_VERSION' ) ) {
$wp_customize->add_control(
new Generate_Customize_Misc_Control(
$wp_customize,
'typography_get_addon_desc',
array(
'section' => 'font_section',
'type' => 'addon',
'label' => __( 'Learn more', 'generatepress' ),
2020-04-07 13:03:04 +00:00
'description' => __( 'More options are available for this section in our premium version.', 'generatepress' ),
'url' => generate_get_premium_url( 'https://generatepress.com/premium/#typography', false ),
'priority' => 50,
'settings' => ( isset( $wp_customize->selective_refresh ) ) ? array() : 'blogname',
)
)
);
}
}
}
if ( ! function_exists( 'generate_get_all_google_fonts' ) ) {
/**
* Return an array of all of our Google Fonts.
*
* @since 1.3.0
* @param string $amount How many fonts to return.
* @return array The list of Google Fonts.
*/
function generate_get_all_google_fonts( $amount = 'all' ) {
// Our big list Google Fonts.
// We use json_decode to reduce PHP memory usage.
// Adding them as a PHP array seems to use quite a bit more memory.
2020-04-07 13:03:04 +00:00
$content = json_decode( '[{"family":"Roboto","category":"sans-serif","variants":["100","100italic","300","300italic","regular","italic","500","500italic","700","700italic","900","900italic"]},{"family":"Open Sans","category":"sans-serif","variants":["300","300italic","regular","italic","600","600italic","700","700italic","800","800italic"]},{"family":"Lato","category":"sans-serif","variants":["100","100italic","300","300italic","regular","italic","700","700italic","900","900italic"]},{"family":"Slabo 27px","category":"serif","variants":["regular"]},{"family":"Oswald","category":"sans-serif","variants":["200","300","regular","500","600","700"]},{"family":"Roboto Condensed","category":"sans-serif","variants":["300","300italic","regular","italic","700","700italic"]},{"family":"Source Sans Pro","category":"sans-serif","variants":["200","200italic","300","300italic","regular","italic","600","600italic","700","700italic","900","900italic"]},{"family":"Montserrat","category":"sans-serif","variants":["100","100italic","200","200italic","300","300italic","regular","italic","500","500italic","600","600italic","700","700italic","800","800italic","900","900italic"]},{"family":"Raleway","category":"sans-serif","variants":["100","100italic","200","200italic","300","300italic","regular","italic","500","500italic","600","600italic","700","700italic","800","800italic","900","900italic"]},{"family":"PT Sans","category":"sans-serif","variants":["regular","italic","700","700italic"]},{"family":"Roboto Slab","category":"serif","variants":["100","300","regular","700"]},{"family":"Merriweather","category":"serif","variants":["300","300italic","regular","italic","700","700italic","900","900italic"]},{"family":"Open Sans Condensed","category":"sans-serif","variants":["300","300italic","700"]},{"family":"Droid Sans","category":"sans-serif","variants":["regular","700"]},{"family":"Lora","category":"serif","variants":["regular","italic","700","700italic"]},{"family":"Ubuntu","category":"sans-serif","variants":["300","300italic","regular","italic","500","500italic","700","700italic"]},{"family":"Droid Serif","category":"serif","variants":["regular","italic","700","700italic"]},{"family":"Playfair Display","category":"serif","variants":["regular","italic","700","700italic","900","900italic"]},{"family":"Arimo","category":"sans-serif","variants":["regular","italic","700","700italic"]},{"family":"Noto Sans","category":"sans-serif","variants":["regular","italic","700","700italic"]},{"family":"PT Serif","category":"serif","variants":["regular","italic","700","700italic"]},{"family":"Titillium Web","category":"sans-serif","variants":["200","200italic","300","300italic","regular","italic","600","600italic","700","700italic","900"]},{"family":"PT Sans Narrow","category":"sans-serif","variants":["regular","700"]},{"family":"Muli","category":"sans-serif","variants":["200","200italic","300","300italic","regular","italic","600","600italic","700","700italic","800","800italic","900","900italic"]},{"family":"Indie Flower","category":"handwriting","variants":["regular"]},{"family":"Bitter","category":"serif","variants":["regular","italic","700"]},{"family":"Poppins","category":"sans-serif","variants":["300","regular","500","600","700"]},{"family":"Inconsolata","category":"monospace","variants":["regular","700"]},{"family":"Dosis","category":"sans-serif","variants":["200","300","regular","500","600","700","800"]},{"family":"Fjalla One","category":"sans-serif","variants":["regular"]},{"family":"Oxygen","category":"sans-serif","variants":["300","regular","700"]},{"family":"Hind","category":"sans-serif","variants":["300","regular","500","600","700"]},{"family":"Cabin","category":"sans-serif","variants":["regular","italic","500","500italic","600","600italic","700","700italic"]},{"family":"Anton","category":"sans-serif","variants":["regular"]},{"family":"Arvo","category":"serif","variants":["regular","italic","700","700italic"]},{"family":"Noto Serif","category":"serif","variants":["regular","italic","700","700italic"]},{"family":"Crimson Text","category":"serif","var
// Loop through them and put what we need into our fonts array.
2020-04-07 13:03:04 +00:00
$fonts = array();
foreach ( $content as $item ) {
// Grab what we need from our big list.
2020-04-07 13:03:04 +00:00
$atts = array(
'name' => $item->family,
'category' => $item->category,
'variants' => $item->variants,
);
// Create an ID using our font family name.
2020-04-07 13:03:04 +00:00
$id = strtolower( str_replace( ' ', '_', $item->family ) );
// Add our attributes to our new array.
2020-04-07 13:03:04 +00:00
$fonts[ $id ] = $atts;
}
if ( 'all' !== $amount ) {
$fonts = array_slice( $fonts, 0, $amount );
}
if ( apply_filters( 'generate_alphabetize_google_fonts', true ) ) {
asort( $fonts );
}
return apply_filters( 'generate_google_fonts_array', $fonts );
}
}
if ( ! function_exists( 'generate_get_all_google_fonts_ajax' ) ) {
add_action( 'wp_ajax_generate_get_all_google_fonts_ajax', 'generate_get_all_google_fonts_ajax' );
/**
* Return an array of all of our Google Fonts.
*
* @since 1.3.0
*/
function generate_get_all_google_fonts_ajax() {
if ( ! isset( $_POST['gp_customize_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['gp_customize_nonce'] ), 'gp_customize_nonce' ) ) {
wp_die();
}
check_ajax_referer( 'gp_customize_nonce', 'gp_customize_nonce' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_die();
}
$fonts = generate_get_all_google_fonts();
echo wp_json_encode( $fonts );
die();
}
}
if ( ! function_exists( 'generate_get_google_font_variants' ) ) {
/**
* Wrapper function to find variants for chosen Google Fonts
* Example: generate_get_google_font_variation( 'Open Sans' )
*
2020-04-07 13:03:04 +00:00
* @since 1.3.0
*
* @param string $font The font to look up.
* @param string $key The option to look up.
2020-04-07 13:03:04 +00:00
*/
function generate_get_google_font_variants( $font, $key = '' ) {
// Don't need variants if we're using a system font.
2020-04-07 13:03:04 +00:00
if ( in_array( $font, generate_typography_default_fonts() ) ) {
return;
}
// Return if we have our variants saved.
2020-04-07 13:03:04 +00:00
if ( '' !== $key && get_theme_mod( $key . '_variants' ) ) {
return get_theme_mod( $key . '_variants' );
}
$defaults = generate_get_default_fonts();
if ( $defaults[ $key ] === $font ) {
2020-04-07 13:03:04 +00:00
return $defaults[ $key . '_variants' ];
}
// Grab all of our fonts.
// It's a big list, so hopefully we're not even still reading.
2020-04-07 13:03:04 +00:00
$fonts = generate_get_all_google_fonts();
// Get the ID from our font.
2020-04-07 13:03:04 +00:00
$id = strtolower( str_replace( ' ', '_', $font ) );
// If the ID doesn't exist within our fonts, we can bail.
2020-04-07 13:03:04 +00:00
if ( ! array_key_exists( $id, $fonts ) ) {
return;
}
// Grab all of the variants associated with our font.
2020-04-07 13:03:04 +00:00
$variants = $fonts[ $id ]['variants'];
// Loop through them and put them into an array, then turn them into a comma separated list.
2020-04-07 13:03:04 +00:00
$output = array();
if ( $variants ) {
foreach ( $variants as $variant ) {
$output[] = $variant;
}
2020-04-07 13:03:04 +00:00
return implode( ',', apply_filters( 'generate_typography_variants', $output ) );
}
}
}
if ( ! function_exists( 'generate_get_google_font_category' ) ) {
/**
* Wrapper function to find the category for chosen Google Font
* Example: generate_get_google_font_category( 'Open Sans' )
*
* @since 1.3.0
*
* @param string $font The name of our font.
* @param string $key The ID of the font setting.
* @return string The category of our font.
*/
function generate_get_google_font_category( $font, $key = '' ) {
// Don't need a category if we're using a system font.
2020-04-07 13:03:04 +00:00
if ( in_array( $font, generate_typography_default_fonts() ) ) {
return;
}
// Return if we have our variants saved.
2020-04-07 13:03:04 +00:00
if ( '' !== $key && get_theme_mod( $key . '_category' ) ) {
return ', ' . get_theme_mod( $key . '_category' );
}
$defaults = generate_get_default_fonts();
// If our default font is selected and the category isn't saved, we already know the category.
if ( $defaults[ $key ] === $font ) {
2020-04-07 13:03:04 +00:00
return ', ' . $defaults[ $key . '_category' ];
}
// Grab all of our fonts.
// It's a big list, so hopefully we're not even still reading.
2020-04-07 13:03:04 +00:00
$fonts = generate_get_all_google_fonts();
// Get the ID from our font.
2020-04-07 13:03:04 +00:00
$id = strtolower( str_replace( ' ', '_', $font ) );
// If the ID doesn't exist within our fonts, we can bail.
2020-04-07 13:03:04 +00:00
if ( ! array_key_exists( $id, $fonts ) ) {
return;
}
// Let's grab our category to go with our font.
2020-04-07 13:03:04 +00:00
$category = ! empty( $fonts[ $id ]['category'] ) ? ', ' . $fonts[ $id ]['category'] : '';
return $category;
}
}
if ( ! function_exists( 'generate_get_font_family_css' ) ) {
/**
* Wrapper function to create font-family value for CSS.
*
* @since 1.3.0
*
* @param string $font The name of our font.
* @param string $settings The ID of the settings we're looking up.
* @param array $default The defaults for our $settings.
2020-04-07 13:03:04 +00:00
* @return string The CSS value for our font family.
*/
function generate_get_font_family_css( $font, $settings, $default ) {
$generate_settings = wp_parse_args(
get_option( $settings, array() ),
$default
);
// We don't want to wrap quotes around these values.
2020-04-07 13:03:04 +00:00
$no_quotes = array(
'inherit',
'Arial, Helvetica, sans-serif',
'Georgia, Times New Roman, Times, serif',
'Helvetica',
'Impact',
'Segoe UI, Helvetica Neue, Helvetica, sans-serif',
'Tahoma, Geneva, sans-serif',
'Trebuchet MS, Helvetica, sans-serif',
'Verdana, Geneva, sans-serif',
apply_filters( 'generate_typography_system_stack', '-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"' ),
);
$font_family = $generate_settings[ $font ];
if ( 'System Stack' === $font_family ) {
2020-04-07 13:03:04 +00:00
$font_family = apply_filters( 'generate_typography_system_stack', '-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"' );
}
// If our value is still using the old format, fix it.
2020-04-07 13:03:04 +00:00
if ( strpos( $font_family, ':' ) !== false ) {
$font_family = current( explode( ':', $font_family ) );
}
// Set up our wrapper.
2020-04-07 13:03:04 +00:00
if ( in_array( $font_family, $no_quotes ) ) {
$wrapper_start = null;
$wrapper_end = null;
} else {
$wrapper_start = '"';
$wrapper_end = '"' . generate_get_google_font_category( $font_family, $font );
}
$output = ( 'inherit' === $font_family ) ? '' : $wrapper_start . $font_family . $wrapper_end;
2020-04-07 13:03:04 +00:00
return $output;
}
}
if ( ! function_exists( 'generate_add_to_font_customizer_list' ) ) {
add_filter( 'generate_typography_customize_list', 'generate_add_to_font_customizer_list' );
/**
* This function makes sure your selected typography option exists in the Customizer list
* Why wouldn't it? Originally, all 800+ fonts were in each list. This has been reduced to 200.
* This functions makes sure that if you were using a font that is now not included in the 200, you won't lose it.
*
* @since 1.3.40
*
* @param array $fonts The existing fonts.
2020-04-07 13:03:04 +00:00
*/
function generate_add_to_font_customizer_list( $fonts ) {
// Bail if we don't have our defaults.
2020-04-07 13:03:04 +00:00
if ( ! function_exists( 'generate_get_default_fonts' ) ) {
return;
}
$generate_settings = wp_parse_args(
get_option( 'generate_settings', array() ),
generate_get_default_fonts()
);
$font_settings = array(
'font_body',
'font_top_bar',
'font_site_title',
'font_site_tagline',
'font_navigation',
'font_widget_title',
'font_heading_1',
'font_heading_2',
'font_heading_3',
);
$all_fonts = generate_get_all_google_fonts();
$select_fonts = generate_get_all_google_fonts( apply_filters( 'generate_number_of_fonts', 200 ) );
foreach ( $font_settings as $setting ) {
// If we don't have a setting, keep going.
2020-04-07 13:03:04 +00:00
if ( ! isset( $generate_settings[ $setting ] ) ) {
continue;
}
$id = strtolower( str_replace( ' ', '_', $generate_settings[ $setting ] ) );
if ( array_key_exists( $id, $select_fonts ) || in_array( $generate_settings[ $setting ], generate_typography_default_fonts() ) ) {
continue;
}
$fonts[ strtolower( str_replace( ' ', '_', $generate_settings[ $setting ] ) ) ] = array(
'name' => $generate_settings[ $setting ],
'variants' => array_key_exists( $id, $all_fonts ) ? $all_fonts[ $id ]['variants'] : array(),
'category' => array_key_exists( $id, $all_fonts ) ? $all_fonts[ $id ]['category'] : 'sans-serif',
);
}
if ( function_exists( 'generate_secondary_nav_get_defaults' ) ) {
$secondary_nav_settings = wp_parse_args(
get_option( 'generate_secondary_nav_settings', array() ),
generate_secondary_nav_get_defaults()
);
$secondary_nav_id = strtolower( str_replace( ' ', '_', $secondary_nav_settings['font_secondary_navigation'] ) );
if ( ! array_key_exists( $secondary_nav_id, $select_fonts ) && ! in_array( $secondary_nav_settings['font_secondary_navigation'], generate_typography_default_fonts() ) ) {
$fonts[ strtolower( str_replace( ' ', '_', $secondary_nav_settings['font_secondary_navigation'] ) ) ] = array(
'name' => $secondary_nav_settings['font_secondary_navigation'],
'variants' => array_key_exists( $secondary_nav_id, $all_fonts ) ? $all_fonts[ $secondary_nav_id ]['variants'] : array(),
'category' => array_key_exists( $secondary_nav_id, $all_fonts ) ? $all_fonts[ $secondary_nav_id ]['category'] : 'sans-serif',
);
}
}
return $fonts;
}
}
if ( ! function_exists( 'generate_typography_set_font_data' ) ) {
add_action( 'admin_init', 'generate_typography_set_font_data' );
/**
* This function will check to see if your category and variants are saved
* If not, it will set them for you
* Generally, set_theme_mod isn't best practice, but this is here for migration purposes for a set amount of time only
* Any time a user saves a font in the Customizer from now on, the category and variants are saved as theme_mods, so this function won't be necessary.
*
* @since 1.3.40
*/
function generate_typography_set_font_data() {
// Get our defaults.
$defaults = generate_get_default_fonts();
// Get our settings.
$generate_settings = wp_parse_args(
get_option( 'generate_settings', array() ),
$defaults
);
// We don't need to do this if we're using the default font, as these values have defaults already.
if ( $defaults['font_body'] == $generate_settings['font_body'] ) { // phpcs:ignore
return;
}
// Don't need to continue if we're using a system font or our default font.
if ( in_array( $generate_settings['font_body'], generate_typography_default_fonts() ) ) {
return;
}
// Don't continue if our category and variants are already set.
if ( get_theme_mod( 'font_body_category' ) && get_theme_mod( 'font_body_variants' ) ) {
return;
}
// Get all of our fonts.
$fonts = generate_get_all_google_fonts();
// Get the ID from our font.
$id = strtolower( str_replace( ' ', '_', $generate_settings['font_body'] ) );
// If the ID doesn't exist within our fonts, we can bail.
if ( ! array_key_exists( $id, $fonts ) ) {
return;
}
// Let's grab our category to go with our font.
$category = ! empty( $fonts[ $id ]['category'] ) ? $fonts[ $id ]['category'] : '';
// Grab all of the variants associated with our font.
$variants = $fonts[ $id ]['variants'];
// Loop through our variants and put them into an array, then turn them into a comma separated list.
$output = array();
if ( $variants ) {
foreach ( $variants as $variant ) {
$output[] = $variant;
}
$variants = implode( ',', $output );
}
// Set our theme mods with our new settings.
if ( '' !== $category ) {
set_theme_mod( 'font_body_category', $category );
}
if ( '' !== $variants ) {
set_theme_mod( 'font_body_variants', $variants );
}
}
}