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

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

    $pixel_charity_nonprofit_fonts_json_data = file_get_contents( $pixel_charity_nonprofit_webfonts_json );
    if ( false === $pixel_charity_nonprofit_fonts_json_data ) {
        return array();
    }

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

    $pixel_charity_nonprofit_google_fonts = array();
    foreach ( $pixel_charity_nonprofit_all_fonts as $pixel_charity_nonprofit_font ) {
        $pixel_charity_nonprofit_google_fonts[ $pixel_charity_nonprofit_font['family'] ] = array(
            'family'   => $pixel_charity_nonprofit_font['family'],
            'variants' => $pixel_charity_nonprofit_font['variants'],
        );
    }
    return $pixel_charity_nonprofit_google_fonts;
}


function pixel_charity_nonprofit_get_all_google_font_families() {
    $pixel_charity_nonprofit_google_fonts  = pixel_charity_nonprofit_get_all_google_fonts();
    $pixel_charity_nonprofit_font_families = array(
        '' => esc_html__( 'Default (Theme Font Family)', 'pixel-charity-nonprofit' ),
    );

    foreach ( $pixel_charity_nonprofit_google_fonts as $pixel_charity_nonprofit_font ) {
        $pixel_charity_nonprofit_font_families[ $pixel_charity_nonprofit_font['family'] ] = $pixel_charity_nonprofit_font['family'];
    }

    return $pixel_charity_nonprofit_font_families;
}


function pixel_charity_nonprofit_get_fonts_url() {
    $pixel_charity_nonprofit_fonts_url = '';
    $pixel_charity_nonprofit_fonts     = array();

    $pixel_charity_nonprofit_all_fonts = pixel_charity_nonprofit_get_all_google_fonts();
    $pixel_charity_nonprofit_selected_font = get_theme_mod( 'pixel_charity_nonprofit_global_font_setting', '' );

    if ( ! empty( $pixel_charity_nonprofit_selected_font ) && isset( $pixel_charity_nonprofit_all_fonts[ $pixel_charity_nonprofit_selected_font ] ) ) {
        $pixel_charity_nonprofit_variants = $pixel_charity_nonprofit_all_fonts[ $pixel_charity_nonprofit_selected_font ]['variants'];
        $pixel_charity_nonprofit_font_family[] = $pixel_charity_nonprofit_selected_font . ':' . implode( ',', $pixel_charity_nonprofit_variants );

        $pixel_charity_nonprofit_query_args = array(
            'family' => urlencode( implode( '|', $pixel_charity_nonprofit_font_family ) ),
        );

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

    return $pixel_charity_nonprofit_fonts_url;
}

function pixel_charity_nonprofit_sanitize_google_fonts( $pixel_charity_nonprofit_input, $pixel_charity_nonprofit_setting ) {
    $pixel_charity_nonprofit_choices = $pixel_charity_nonprofit_setting->manager->get_control( $pixel_charity_nonprofit_setting->id )->choices;
    return ( array_key_exists( $pixel_charity_nonprofit_input, $pixel_charity_nonprofit_choices ) ? $pixel_charity_nonprofit_input : $pixel_charity_nonprofit_setting->default );
}

function pixel_charity_nonprofit_dynamic_css() {
    $pixel_charity_nonprofit_color       = get_theme_mod( 'pixel_charity_nonprofit_global_color', '#EE9633' );
    $pixel_charity_nonprofit_colortwo    = get_theme_mod( 'pixel_charity_nonprofit_global_colortwo', '#025548' );
    $pixel_charity_nonprofit_font_family = get_theme_mod( 'pixel_charity_nonprofit_global_font_setting', '' );

    // Enqueue Google Fonts if font is selected
    $pixel_charity_nonprofit_fonts_url = pixel_charity_nonprofit_get_fonts_url();
    if ( ! empty( $pixel_charity_nonprofit_fonts_url ) ) {
        wp_enqueue_style( 'pixel-charity-nonprofit-google-fonts', esc_url( $pixel_charity_nonprofit_fonts_url ), array(), null );
    }

    // Build the CSS
    $pixel_charity_nonprofit_custom_css  = ':root {';
    $pixel_charity_nonprofit_custom_css .= '--global-color: ' . esc_attr( $pixel_charity_nonprofit_color ) . ';';
    $pixel_charity_nonprofit_custom_css .= '--global-colortwo: ' . esc_attr( $pixel_charity_nonprofit_colortwo ) . ';';

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

    $pixel_charity_nonprofit_custom_css .= '}';

    wp_add_inline_style( 'pixel-charity-nonprofit-style', $pixel_charity_nonprofit_custom_css );
}
add_action( 'wp_enqueue_scripts', 'pixel_charity_nonprofit_dynamic_css', 99 );