1
0
Fork 0
pyramidengarten-berlin/wp-themes/generatepress/inc/typography.php

651 lines
82 KiB
PHP
Raw Normal View History

2019-09-22 07:36:54 +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() {
if ( is_admin() ) {
return;
}
// Grab our options
$generate_settings = wp_parse_args(
get_option( 'generate_settings', array() ),
generate_get_default_fonts()
);
// List our non-Google fonts
$not_google = str_replace( ' ', '+', generate_typography_default_fonts() );
// Grab our font family settings
$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',
);
// Create our Google Fonts array
$google_fonts = array();
if ( ! empty( $font_settings ) ) {
foreach ( $font_settings as $key ) {
// If the key isn't set, move on
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 ] ) );
}
// Replace the spaces in the names with a plus
$value = str_replace( ' ', '+', $generate_settings[ $key ] );
// Grab the variants using the plain name
$variants = generate_get_google_font_variants( $generate_settings[ $key ], $key );
// If we have variants, add them to our value
$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 );
// Separate each different font with a bar
$google_fonts = implode( '|', $google_fonts );
// Apply a filter to the output
$google_fonts = apply_filters( 'generate_typography_google_fonts', $google_fonts );
// Get the subset
$subset = apply_filters( 'generate_fonts_subset','' );
// Set up our arguments
$font_args = array();
$font_args['family'] = $google_fonts;
if ( '' !== $subset ) {
$font_args['subset'] = urlencode( $subset );
}
// Create our URL using the arguments
$fonts_url = add_query_arg( $font_args, '//fonts.googleapis.com/css' );
// Enqueue our fonts
if ( $google_fonts ) {
wp_enqueue_style( 'generate-fonts', $fonts_url, array(), null, 'all' );
}
}
}
if ( ! function_exists( 'generate_default_fonts_customize_register' ) ) {
add_action( 'customize_register', 'generate_default_fonts_customize_register' );
/**
* Build our Typography options
*
* @since 0.1
*/
function generate_default_fonts_customize_register( $wp_customize ) {
if ( function_exists( 'generate_fonts_customize_register' ) ) {
// Bail if GP Premium is active
return;
}
// Load helpers
require_once trailingslashit( get_template_directory() ) . 'inc/customizer/customizer-helpers.php';
$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',
'description' => '',
'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(
'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',
),
),
'priority' => 40,
)
)
);
$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' => '',
),
),
'priority' => 45,
)
)
);
$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',
),
),
'priority' => 47,
)
)
);
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' ),
'description' => __( 'More options are available for this section in our premium version.', 'generatepress' ),
'url' => generate_get_premium_url( 'https://generatepress.com/downloads/generate-typography/' ),
'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
$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
$fonts = array();
foreach ( $content as $item ) {
// Grab what we need from our big list
$atts = array(
'name' => $item->family,
'category' => $item->category,
'variants' => $item->variants,
);
// Create an ID using our font family name
$id = strtolower( str_replace( ' ', '_', $item->family ) );
// Add our attributes to our new array
$fonts[ $id ] = $atts;
}
if ( 'all' !== $amount ) {
$fonts = array_slice( $fonts, 0, $amount );
}
// Alphabetize our fonts
if ( apply_filters( 'generate_alphabetize_google_fonts', true ) ) {
asort( $fonts );
}
// Filter to allow us to modify the fonts array
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() {
// Bail if the nonce doesn't check out
if ( ! isset( $_POST['gp_customize_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['gp_customize_nonce'] ), 'gp_customize_nonce' ) ) {
wp_die();
}
// Do another nonce check
check_ajax_referer( 'gp_customize_nonce', 'gp_customize_nonce' );
// Bail if user can't edit theme options
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_die();
}
// Get all of our fonts
$fonts = generate_get_all_google_fonts();
// Send all of our fonts in JSON format
echo wp_json_encode( $fonts );
// Exit
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' )
* @since 1.3.0
*/
function generate_get_google_font_variants( $font, $key = '' ) {
// Don't need variants if we're using a system font
if ( in_array( $font, generate_typography_default_fonts() ) ) {
return;
}
// Return if we have our variants saved
if ( '' !== $key && get_theme_mod( $key . '_variants' ) ) {
return get_theme_mod( $key . '_variants' );
}
// Get our defaults
$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 ) {
return $defaults[ $key . '_variants' ];
}
// Grab all of our fonts
// It's a big list, so hopefully we're not even still reading
$fonts = generate_get_all_google_fonts();
// Get the ID from our font
$id = strtolower( str_replace( ' ', '_', $font ) );
// If the ID doesn't exist within our fonts, we can bail
if ( ! array_key_exists( $id, $fonts ) ) {
return;
}
// Grab all of the variants associated with our font
$variants = $fonts[ $id ]['variants'];
// Loop through them and put them into an array, then turn them into a comma separated list
$output = array();
if ( $variants ) {
foreach ( $variants as $variant ) {
$output[] = $variant;
}
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
if ( in_array( $font, generate_typography_default_fonts() ) ) {
return;
}
// Return if we have our variants saved
if ( '' !== $key && get_theme_mod( $key . '_category' ) ) {
return ', ' . get_theme_mod( $key . '_category' );
}
// Get our defaults
$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 ) {
return ', ' . $defaults[ $key . '_category' ];
}
// Grab all of our fonts
// It's a big list, so hopefully we're not even still reading
$fonts = generate_get_all_google_fonts();
// Get the ID from our font
$id = strtolower( str_replace( ' ', '_', $font ) );
// 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'] : '';
// Return it to be used by our function
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.
* @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
$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"' ),
);
// Get our font
$font_family = $generate_settings[ $font ];
if ( 'System Stack' == $font_family ) {
$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
if ( strpos( $font_family, ':' ) !== false ) {
$font_family = current( explode( ':', $font_family ) );
}
// Set up our wrapper
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 the CSS
$output = ( 'inherit' == $font_family ) ? '' : $wrapper_start . $font_family . $wrapper_end;
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
*/
function generate_add_to_font_customizer_list( $fonts ) {
// Bail if we don't have our defaults
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
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;
}
}