<?php


// font families
function dw_fonts_families() {

    $dw_font_families = array(
        'default'                                => 'default',
        'Tahoma'                                 => 'Tahoma',
        'IRANSansWeb'                            => 'IRANSansWeb',
        'IRANSansWeb_Black'                      => 'IRANSansWeb_Black',
        'IRANSansWeb_Bold'                       => 'IRANSansWeb_Bold',
        'IRANSansWeb_Light'                      => 'IRANSansWeb_Light',
        'IRANSansWeb_Medium'                     => 'IRANSansWeb_Medium',
        'IRANSansWeb_UltraLight'                 => 'IRANSansWeb_UltraLight',
        'IRANSansWebFaNum'                     => 'IRANSansWebFaNum',
        'IRANSansWebFaNum_Black'               => 'IRANSansWebFaNum_Black',
        'IRANSansWebFaNum_Bold'                => 'IRANSansWebFaNum_Bold',
        'IRANSansWebFaNum_Light'               => 'IRANSansWebFaNum_Light',
        'IRANSansWebFaNum_Medium'              => 'IRANSansWebFaNum_Medium',
        'IRANSansWebFaNum_UltraLight'          => 'IRANSansWebFaNum_UltraLight',
        'dw-Yekan'                               => 'dw-Yekan',
        'dw-Abasan'                              => 'dw-Abasan',
        'dw-Afsoon'                              => 'dw-Afsoon',
        'dw-Arshia'                              => 'dw-Arshia',
        'dw-Aref'                                => 'dw-Aref',
        'dw-BBaran'                              => 'dw-BBaran',
        'dw-BBadr'                               => 'dw-BBadr',
        'dw-BBardiya'                            => 'dw-BBardiya',
        'dw-BBCNassim'                           => 'dw-BBCNassim',
        'dw-BCompset'                            => 'dw-BCompset',
        'dw-BDavat'                              => 'dw-BDavat',
        'dw-Behdad-Regular'                      => 'dw-Behdad-Regular',
        'dw-BeirutLtX3'                          => 'dw-BeirutLtX3',
        'dw-BElham'                              => 'dw-BElham',
        'dw-BEsfehanBold'                        => 'dw-BEsfehanBold',
        'dw-BFantezy'                            => 'dw-BFantezy',
        'dw-BFarnaz'                             => 'dw-BFarnaz',
        'dw-BFerdosi'                            => 'dw-BFerdosi',
        'dw-BHamid'                              => 'dw-BHamid',
        'dw-BHelal'                              => 'dw-BHelal',
        'dw-BHoma'                               => 'dw-BHoma',
        'dw-BJadidBold'                          => 'dw-BJadidBold',
        'dw-BJalal'                              => 'dw-BJalal',
        'dw-BKoodakBold'                         => 'dw-BKoodakBold',
        'dw-BKourosh'                            => 'dw-BKourosh',
        'dw-BLotus'                              => 'dw-BLotus',
        'dw-BMahsa'                              => 'dw-BMahsa',
        'dw-BMehrBold'                           => 'dw-BMehrBold',
        'dw-BMitra'                              => 'dw-BMitra',
        'dw-BMitraBold'                          => 'dw-BMitraBold',
        'dw-BMorvarid'                           => 'dw-BMorvarid',
        'dw-BNarm'                               => 'dw-BNarm',
        'dw-BNasim'                              => 'dw-BNasim',
        'dw-BNazanin'                            => 'dw-BNazanin',
        'dw-BRoya'                               => 'dw-BRoya',
        'dw-BSetarehBold'                        => 'dw-BSetarehBold',
        'dw-BShiraz'                             => 'dw-BShiraz',
        'dw-BSinaBold'                           => 'dw-BSinaBold',
        'dw-BTabassom'                           => 'dw-BTabassom',
        'dw-BTehran'                             => 'dw-BTehran',
        'dw-BTitr'                               => 'dw-BTitr',
        'dw-BTitrTGE'                            => 'dw-BTitrTGE',
        'dw-BTraffic'                            => 'dw-BTraffic',
        'dw-BVahidBold'                          => 'dw-BVahidBold',
        'dw-BYagut'                              => 'dw-BYagut',
        'dw-BYas'                                => 'dw-BYas',
        'dw-BZar'                                => 'dw-BZar',
        'dw-BZiba'                               => 'dw-BZiba',
        'dw-Casablanca'                          => 'dw-Casablanca',
        'dw-DastNevis'                           => 'dw-DastNevis',
        'dw-Dirooz'                              => 'dw-Dirooz',
        'dw-Dirooz-FarsiDigit'                   => 'dw-Dirooz-FarsiDigit',
        'dw-Ekhlass'                             => 'dw-Ekhlass',
        'dw-FarsiSimple'                         => 'dw-FarsiSimple',
        'dw-Flow'                                => 'dw-Flow',
        'dw-FlowBold'                            => 'dw-FlowBold',
        'dw-frutiger'                            => 'dw-frutiger',
        'dw-Gandom'                              => 'dw-Gandom',
        'dw-HeritageTwo'                         => 'dw-HeritageTwo',
        'dw-IranNastaliq'                        => 'dw-IranNastaliq',
        'dw-Kufi'                                => 'dw-Kufi',
        'dw-LinerScreen'                         => 'dw-LinerScreen',
        'dw-Naskh'                               => 'dw-Naskh',
        'dw-Parastoo'                            => 'dw-Parastoo',
        'dw-Parastoo-Bold'                       => 'dw-Parastoo-Bold',
        'dw-Parastoo-Bold-FarsiDigit'            => 'dw-Parastoo-Bold-FarsiDigit',
        'dw-Parastoo-FarsiDigit'                 => 'dw-Parastoo-FarsiDigit',
        'dw-Rezvan'                              => 'dw-Rezvan',
        'dw-Sahel'                               => 'dw-Sahel',
        'dw-Sahel-Black'                         => 'dw-Sahel-Black',
        'dw-Sahel-Black-FarsiDigit'              => 'dw-Sahel-Black-FarsiDigit',
        'dw-Sahel-Bold'                          => 'dw-Sahel-Bold',
        'dw-Sahel-Bold-FarsiDigit'               => 'dw-Sahel-Bold-FarsiDigit',
        'dw-Sahel-FarsiDigit'                    => 'dw-Sahel-FarsiDigit',
        'dw-Samim'                               => 'dw-Samim',
        'dw-Samim-Bold'                          => 'dw-Samim-Bold',
        'dw-Samim-Bold-FarsiDigit'               => 'dw-Samim-Bold-FarsiDigit',
        'dw-Samim-FarsiDigit'                    => 'dw-Samim-FarsiDigit',
        'dw-Shabnam'                             => 'dw-Shabnam',
        'dw-Shabnam-Bold'                        => 'dw-Shabnam-Bold',
        'dw-Shabnam-Bold-FarsiDigit'             => 'dw-Shabnam-Bold-FarsiDigit',
        'dw-Shabnam-FarsiDigit'                  => 'dw-Shabnam-FarsiDigit',
        'dw-Shabnam-Light'                       => 'dw-Shabnam-Light',
        'dw-Shabnam-Light-FarsiDigit'            => 'dw-Shabnam-Light-FarsiDigit',
        'dw-Shams'                               => 'dw-Shams',
        'dw-Silicon'                             => 'dw-Silicon',
        'dw-Talat'                               => 'dw-Talat',
        'dw-Tanha'                               => 'dw-Tanha',
        'dw-Tanha-FarsiDigit'                    => 'dw-Tanha-FarsiDigit',
        'dw-ThameenDemi'                         => 'dw-ThameenDemi',
        'dw-TitrDF'                              => 'dw-TitrDF',
        'dw-TunisiaBold'                         => 'dw-TunisiaBold',
        'dw-TVBold'                              => 'dw-TVBold',
        'dw-TwoBold'                             => 'dw-TwoBold',
        'dw-TwoLight'                            => 'dw-TwoLight',
        'dw-TwoMedium'                           => 'dw-TwoMedium',
        'dw-Vazir'                               => 'dw-Vazir',
        'dw-Vazir-Bold'                          => 'dw-Vazir-Bold',
        'dw-Vazir-Bold-FarsiDigit'               => 'dw-Vazir-Bold-FarsiDigit',
        'dw-Vazir-Code'                          => 'dw-Vazir-Code',
        'dw-Vazir-FarsiDigit'                    => 'dw-Vazir-FarsiDigit',
        'dw-Vazir-Light'                         => 'dw-Vazir-Light',
        'dw-Vazir-Light-FarsiDigit'              => 'dw-Vazir-Light-FarsiDigit',
        'dw-Vazir-Medium'                        => 'dw-Vazir-Medium',
        'dw-Vazir-Medium-FarsiDigit'             => 'dw-Vazir-Medium-FarsiDigit',
        'dw-Vazir-Thin'                          => 'dw-Vazir-Thin',
        'dw-Vazir-Thin-FarsiDigit'               => 'dw-Vazir-Thin-FarsiDigit',
    );

    $new_font_family = array();
	$new_font1 = get_theme_mod( 'dw_new_font_family1_name' );

    if (!empty( $new_font1 ))
        $new_font_family[$new_font1] = $new_font1;

	$new_font2 = get_theme_mod( 'dw_new_font_family2_name' );
	
    if (!empty( $new_font2 ))
        $new_font_family[$new_font2] = $new_font2;

    return array_merge($new_font_family, $dw_font_families);

}

// font size
function dw_fonts_size () {

    $font_size = array(
            'default'   => 'default',
            '10'        =>  '10px',
            '11'        =>  '11px',
            '12'        =>  '12px',
            '13'        =>  '13px',
            '14'        =>  '14px',
            '15'        =>  '15px',
            '16'        =>  '16px',
            '17'        =>  '17px',
            '18'        =>  '18px',
            '19'        =>  '19px',
            '20'        =>  '20px',
            '21'        =>  '21px',
            '22'        =>  '22px',
            '23'        =>  '23px',
            '24'        =>  '24px',
            '25'        =>  '25px',
            '26'        =>  '26px',
            '27'        =>  '27px',
            '28'        =>  '28px',
            '29'        =>  '29px',
            '30'        =>  '30px',
            '31'        =>  '31px',
            '32'        =>  '32px',
            '33'        =>  '33px',
            '34'        =>  '34px',
            '35'        =>  '35px',
            '36'        =>  '36px',
            '37'        =>  '37px',
            '38'        =>  '38px',
            '39'        =>  '39px',
            '40'        =>  '40px',
            '41'        =>  '41px',
            '42'        =>  '42px',
            '43'        =>  '43px',
            '44'        =>  '44px',
            '45'        =>  '45px',
            '46'        =>  '46px',
            '47'        =>  '47px',
            '48'        =>  '48px',
            '49'        =>  '49px',
            '50'        =>  '50px',
    );

    return $font_size;
}


/**
 * Output the font CSS to wp_head.
 */
function dw_output_css() {
    $is_enable_font_family = get_theme_mod( 'dw_enable_persian_fonts' );
    $is_enable_font_size = get_theme_mod( 'dw_enable_font_size' );
    if ( '1' !== $is_enable_font_family && '1' !== $is_enable_font_size ) {
        return;
    }

    ?>
    <!-- Dehkadeh-wp.ir Persian Fonts -->
    <style>
        <?php if ( '1' === $is_enable_font_family ): ?>
        <?php dw_custom_font_face(); ?>
        <?php dw_generate_family_css( 'body', 'dw_body_font' ); ?>
        <?php dw_generate_family_css( '.site-title, h1, h2, h3, h4, h5, h6', 'dw_headings_font' ); ?>
        <?php dw_generate_family_css( 'button, input, select, textarea', 'dw_inputs_font' ); ?>
        <?php dw_generate_family_css( '.site-title', 'dw_site_title_font' ); ?>
        <?php dw_generate_family_css( '.site-description', 'dw_site_description_font' ); ?>
        <?php dw_generate_family_css( '.menu, .page_item, .menu-item, .menu-item li, .menu-item a', 'dw_navigation_font' ); ?>
        <?php dw_generate_family_css( 'article h1, article h2, article h3, article h4, article h5, article h6', 'dw_post_page_headings_font' ); ?>
        <?php dw_generate_family_css( 'article', 'dw_post_page_content_font' ); ?>
        <?php dw_generate_family_css( '.widget h1, .widget h2, .widget h3, .widget h4, .widget h5, .widget h6', 'dw_sidebar_headings_font' ); ?>
        <?php dw_generate_family_css( '.widget', 'dw_sidebar_content_font' ); ?>
        <?php dw_generate_family_css( 'footer h1, footer h2, footer h3, footer h4, .widgets-area h5, footer h6', 'dw_footer_headings_font' ); ?>
        <?php dw_generate_family_css( 'footer', 'dw_footer_content_font' ); ?>
        <?php dw_generate_family_css( get_theme_mod( 'dw_custom_elements1' ), 'dw_custom_elements1_font' ); ?>
        <?php dw_generate_family_css( get_theme_mod( 'dw_custom_elements2' ), 'dw_custom_elements2_font' ); ?>
        <?php endif; ?>
        <?php if ( '1' === $is_enable_font_size ): ?>
        <?php dw_generate_font_size_css( 'body', 'dw_body_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h1', 'dw_h1_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h2', 'dw_h2_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h3', 'dw_h3_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h4', 'dw_h4_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h5', 'dw_h5_font_size' ); ?>
        <?php dw_generate_font_size_css( 'h6', 'dw_h6_font_size' ); ?>
        <?php dw_generate_font_size_css( '.site-title', 'dw_site_title_font_size' ); ?>
        <?php dw_generate_font_size_css( '.site-description', 'dw_site_description_font_size' ); ?>
        <?php dw_generate_font_size_css( '.menu, .page_item, .menu-item, .menu-item li, .menu-item a', 'dw_navigation_font_size' ); ?>
        <?php dw_generate_font_size_css( 'article', 'dw_post_content_font_size' ); ?>
        <?php dw_generate_font_size_css( '.widget', 'dw_sidebar_content_font_size' ); ?>
        <?php dw_generate_font_size_css( 'footer', 'dw_footer_content_font_size' ); ?>
        <?php endif; ?>
    </style>
    <!-- Dehkadeh-wp.ir Persian Fonts -->
    <?php
}

// Output custom CSS to live site.
add_action( 'wp_head', 'dw_output_css' );


/**
 * build the font family CSS styles
 * @param string $selector The CSS selector to apply the styles to.
 * @param string $option_name The option name to pull from the database.
 */
function dw_generate_family_css( $selector, $option_name ) {
    $output = '';
	$stack = get_theme_mod( $option_name );
	
    if ( false !== $stack && false != $selector ) {

        if ( ! empty( $stack ) && 'default' !== $stack ) {
            $output = sprintf('%s { font-family: \'%s\'%s; }' . PHP_EOL,
                $selector,
                $stack ,
                dw_forced_font_family()
            );
        }
        echo wp_kses_post( $output );
    }
}

/**
 * Check if the styles for font family should be forced.
 */
function dw_forced_font_family() {

    if ( '1' === get_theme_mod( 'dw_force_styles' ) ) {
        return ' !important';
    }
}


/**
 * build the font size CSS styles
 * @param string $selector The CSS selector to apply the styles to.
 * @param string $option_name The option name to pull from the database.
 */
function dw_generate_font_size_css( $selector, $option_name ) {
    $output = '';
	$stack = get_theme_mod( $option_name );
	
    if ( false !== $stack && false != $selector ) {

        if ( ! empty( $stack ) && 'default' !== $stack ) {
            $output = sprintf('%s { font-size: %spx%s; }' . PHP_EOL,
                $selector,
                $stack ,
                dw_is_forced_font_size()
            );
        }
        echo wp_kses_post( $output );

    }
}

/**
 * Check if the styles for font family should be forced.
 */
function dw_is_forced_font_size() {

    if ( '1' === get_theme_mod( 'dw_force_styles_size' ) ) {
        return ' !important';
    }
    return '';
}


/**
 * Add font_face to head for custom font family
*/
function dw_custom_font_face(){

	$font1_name = get_theme_mod( 'dw_new_font_family1_name' );
	
    if (!empty( $font1_name ))
    {
        $font1_file_ttf = get_theme_mod( 'dw_new_font_family1_file_ttf' );
        $font1_file_woff = get_theme_mod( 'dw_new_font_family1_file_woff' );
        $font1_file_woff2 = get_theme_mod( 'dw_new_font_family1_file_woff2' );
        $font1_file_eot = get_theme_mod( 'dw_new_font_family1_file_eot' );

        if( $font1_file_ttf || $font1_file_woff || $font1_file_woff2 || $font1_file_eot ) {
            printf('@font-face{font-family:\'%s\';src:', $font1_name);

            if($font1_file_ttf)
                printf('url(\'%s\') format(\'truetype\')', $font1_file_ttf);

            if($font1_file_woff)
                printf(',url(\'%s\') format(\'woff\')', $font1_file_woff);

            if($font1_file_woff2)
                printf(',url(\'%s\') format(\'woff2\')', $font1_file_woff2);

            if($font1_file_eot)
                printf(',url(\'%s?#\') format(\'eot\')', $font1_file_eot);

            printf(';}' . PHP_EOL);
        }
    }

	$font2_name = get_theme_mod( 'dw_new_font_family2_name' );
    if (!empty( $font2_name ))
    {
        $font2_file_ttf = get_theme_mod( 'dw_new_font_family2_file_ttf' );
        $font2_file_woff = get_theme_mod( 'dw_new_font_family2_file_woff' );
        $font2_file_woff2 = get_theme_mod( 'dw_new_font_family2_file_woff2' );
        $font2_file_eot = get_theme_mod( 'dw_new_font_family2_file_eot' );

        if( $font2_file_ttf || $font2_file_woff || $font2_file_woff2 || $font2_file_eot ) {
            printf('@font-face{font-family:\'%s\';src:', $font2_name);

            if($font2_file_ttf)
                printf('url(\'%s\') format(\'truetype\')', $font2_file_ttf);

            if($font2_file_woff)
                printf(',url(\'%s\') format(\'woff\')', $font2_file_woff);

            if($font2_file_woff2)
                printf(',url(\'%s\') format(\'woff2\')', $font2_file_woff2);

            if($font2_file_eot)
                printf(',url(\'%s?#\') format(\'eot\')', $font2_file_eot);

            printf(';}' . PHP_EOL);
        }
    }

}


/**
 * Allow upload fonts for custom fonts
 */
function dw_allow_font_mime_types( $mimes ) {

    // New allowed mime types.
    $mimes['ttf'] = 'application/x-font-ttf';
    $mimes['woff'] = 'application/x-font-woff';
    $mimes['woff2'] = 'application/x-font-woff';
    $mimes['eot'] = 'application/vnd.ms-fontobject';


    return $mimes;
}
add_filter( 'upload_mimes', 'dw_allow_font_mime_types' );



/**
 * Custom font for wp admin
 */
add_action( 'admin_init', 'dw_change_wp_admin_font' );
function dw_change_wp_admin_font() {
    if( get_theme_mod( 'dw_enable_wp_admin_font' ) === '0' ) { // if admin font is disabled, we should remove our action from dw update info to change admin font
        remove_action( 'admin_enqueue_scripts', 'dw_admin_font_from_update_info' );
    }
    else {

        $font_name = get_theme_mod( 'dw_wp_admin_font' );
        if ( $font_name && 'default' !== $font_name ) {  // if font is set to default, we apply IrSans font from our dw update info
            add_action( 'admin_head', 'dw_apply_wp_admin_font' );
        }
    }
}

function dw_apply_wp_admin_font(){
    echo '<link rel="stylesheet" href="' . DW_FONTS_URL . 'dw-fonts/dw-fonts.css" type="text/css" media="all">' . PHP_EOL;
    $font_name = get_theme_mod( 'dw_wp_admin_font' );

    echo '<style>';
    dw_custom_font_face();
    echo 'body, body.rtl, .rtl #wpadminbar, body.rtl #wpwrap ,body.rtl .press-this a.wp-switch-editor, .rtl h1, .rtl h2, .rtl h3, .rtl h4, .rtl h5, .rtl h6, .rtl #wpwrap  h1, .rtl #wpwrap  h2, .rtl #wpwrap  h3,
          .rtl #wpwrap  h4, .rtl #wpwrap h5, .rtl #wpwrap  h6, #tinymce, input, textarea, .rtl .vc_ui-font-open-sans, .rtl .editor-styles-wrapper {
                font-family: ' . $font_name . ',tahoma !important;
            }
            .rtl #wpadminbar * {
                font-family: ' . $font_name . ',tahoma;
            }';
    echo '</style>';

}