<?php
/**
 * Custom typography options for this theme
 *
 * @package Revolution Video Vlog
 */

function revolution_video_vlog_output_custom_font_css() {
    $revolution_video_vlog_font_choice = get_theme_mod( 'revolution_video_vlog_font_family', 'default' );

    if ( $revolution_video_vlog_font_choice === 'default' ) {
        return;
    }

    $revolution_video_vlog_font_map = array(
        'bad_script'       => '"Bad Script", cursive',
        'roboto'           => '"Roboto", sans-serif',
        'playfair_display' => '"Playfair Display", serif',
        'open_sans'        => '"Open Sans", sans-serif',
        'lobster'          => '"Lobster", cursive',
        'merriweather'     => '"Merriweather", serif',
        'oswald'           => '"Oswald", sans-serif',
        'raleway'          => '"Raleway", sans-serif',
    );

    $revolution_video_vlog_font_family = isset( $revolution_video_vlog_font_map[ $revolution_video_vlog_font_choice ] ) ? $revolution_video_vlog_font_map[ $revolution_video_vlog_font_choice ] : $revolution_video_vlog_font_map['pt_sans'];

    $revolution_video_vlog_custom_css = "
        body,
        h1, h2, h3, h4, h5, h6,
        p, a, span, div,
        .site, .entry-content, .main-navigation, .widget,
        input, textarea, button, .menu, .site-title, .site-description {
            font-family: {$revolution_video_vlog_font_family} !important;
        }
    ";

    wp_add_inline_style( 'revolution-video-vlog-google-fonts', $revolution_video_vlog_custom_css );
}
add_action( 'wp_enqueue_scripts', 'revolution_video_vlog_output_custom_font_css', 20 );


function revolution_video_vlog_sanitize_font_family( $revolution_video_vlog_input ) {
    $revolution_video_vlog_valid = array(
        'default', 'bad_script', 'roboto',
        'playfair_display', 'open_sans', 'lobster', 'merriweather', 'oswald', 'raleway'
    );
    return in_array( $revolution_video_vlog_input, $revolution_video_vlog_valid ) ? $revolution_video_vlog_input : 'default';
}

function revolution_video_vlog_enqueue_selected_google_font() {
    $revolution_video_vlog_font_choice = get_theme_mod( 'revolution_video_vlog_font_family', 'default' );

    $revolution_video_vlog_font_links = array(
        'bad_script'       => 'https://fonts.googleapis.com/css2?family=Bad+Script&display=swap',
        'roboto'           => 'https://fonts.googleapis.com/css2?family=Roboto&display=swap',
        'playfair_display' => 'https://fonts.googleapis.com/css2?family=Playfair+Display&display=swap',
        'open_sans'        => 'https://fonts.googleapis.com/css2?family=Open+Sans&display=swap',
        'lobster'          => 'https://fonts.googleapis.com/css2?family=Lobster&display=swap',
        'merriweather'     => 'https://fonts.googleapis.com/css2?family=Merriweather&display=swap',
        'oswald'           => 'https://fonts.googleapis.com/css2?family=Oswald&display=swap',
        'raleway'          => 'https://fonts.googleapis.com/css2?family=Raleway&display=swap',
    );

    if ( isset( $revolution_video_vlog_font_links[ $revolution_video_vlog_font_choice ] ) ) {
        wp_enqueue_style( 'revolution-video-vlog-dynamic-font', $revolution_video_vlog_font_links[ $revolution_video_vlog_font_choice ], array(), null );
    }
}
add_action( 'wp_enqueue_scripts', 'revolution_video_vlog_enqueue_selected_google_font' );