<?php
/**
 * Particular Typography function for this theme
 *
 * Some of this functionality may eventually be superseded by essential features.
 *
 * @package Pixel Transport
 */

function pixel_transport_get_all_google_fonts() {
    $pixel_transport_webfonts_json = get_template_directory() . '/inc/typography-settings/google-webfonts.json';
    if ( ! file_exists( $pixel_transport_webfonts_json ) ) {
        return array();
    }

    $pixel_transport_fonts_json_data = file_get_contents( $pixel_transport_webfonts_json );
    if ( false === $pixel_transport_fonts_json_data ) {
        return array();
    }

    $pixel_transport_all_fonts = json_decode( $pixel_transport_fonts_json_data, true );
    if ( json_last_error() !== JSON_ERROR_NONE ) {
        return array();
    }

    $pixel_transport_google_fonts = array();
    foreach ( $pixel_transport_all_fonts as $pixel_transport_font ) {
        $pixel_transport_google_fonts[ $pixel_transport_font['family'] ] = array(
            'family'   => $pixel_transport_font['family'],
            'variants' => $pixel_transport_font['variants'],
        );
    }
    return $pixel_transport_google_fonts;
}


function pixel_transport_get_all_google_font_families() {
    $pixel_transport_google_fonts  = pixel_transport_get_all_google_fonts();
    $pixel_transport_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'pixel-transport' ),
    );

    foreach ( $pixel_transport_google_fonts as $pixel_transport_font ) {
        $pixel_transport_font_families[ $pixel_transport_font['family'] ] = $pixel_transport_font['family'];
    }

    return $pixel_transport_font_families;
}


function pixel_transport_get_fonts_url() {
    $pixel_transport_fonts_url = '';
    $pixel_transport_fonts     = array();

    $pixel_transport_all_fonts = pixel_transport_get_all_google_fonts();
    $pixel_transport_selected_font = get_theme_mod( 'pixel_transport_global_font_setting', '' );

    if ( ! empty( $pixel_transport_selected_font ) && isset( $pixel_transport_all_fonts[ $pixel_transport_selected_font ] ) ) {
        $pixel_transport_variants = $pixel_transport_all_fonts[ $pixel_transport_selected_font ]['variants'];
        $pixel_transport_font_family[] = $pixel_transport_selected_font . ':' . implode( ',', $pixel_transport_variants );

        $pixel_transport_query_args = array(
            'family' => urlencode( implode( '|', $pixel_transport_font_family ) ),
        );

        $pixel_transport_fonts_url = add_query_arg( $pixel_transport_query_args, 'https://fonts.googleapis.com/css' );
    }

    return $pixel_transport_fonts_url;
}

function pixel_transport_sanitize_google_fonts( $pixel_transport_input, $pixel_transport_setting ) {
    $pixel_transport_choices = $pixel_transport_setting->manager->get_control( $pixel_transport_setting->id )->choices;
    return ( array_key_exists( $pixel_transport_input, $pixel_transport_choices ) ? $pixel_transport_input : $pixel_transport_setting->default );
}

function pixel_transport_dynamic_css() {
    $pixel_transport_color       = get_theme_mod( 'pixel_transport_global_color', '#F7AB06' );
    $pixel_transport_font_family = get_theme_mod( 'pixel_transport_global_font_setting', '' );

    // Enqueue Google Fonts if font is selected
    $pixel_transport_fonts_url = pixel_transport_get_fonts_url();
    if ( ! empty( $pixel_transport_fonts_url ) ) {
        wp_enqueue_style( 'pixel-transport-google-fonts', esc_url( $pixel_transport_fonts_url ), array(), null );
    }

    $pixel_transport_custom_css = ':root { --global-color: ' . esc_attr( $pixel_transport_color ) . '; }';

    if ( ! empty( $pixel_transport_font_family ) ) {
        $pixel_transport_custom_css .= ':root { --global-font-family: "' . esc_attr( $pixel_transport_font_family ) . '", sans-serif !important; }';
    }

    wp_add_inline_style( 'pixel-transport-style', $pixel_transport_custom_css );
}
add_action( 'wp_enqueue_scripts', 'pixel_transport_dynamic_css', 99 );