<?php
if (!function_exists('faced_social_menu')) {
    /**
     * Add social icons menu
     *
     * @since 1.0.0
     *
     */
    function faced_social_menu()
    {
        if (has_nav_menu('social-menu')) :
            wp_nav_menu(array(
                'theme_location' => 'social-menu',
                'container' => 'ul',
                'menu_class' => 'social-menu'
            ));
        endif;
    }
}


if (!function_exists('faced_custom_body_class')) {
    /**
     * Add sidebar class in body
     *
     * @since 1.0.0
     *
     */
    function faced_custom_body_class($classes)
    {
        global $faced_theme_options;
        if (!empty($faced_theme_options['faced-enable-sticky-sidebar']) &&  $faced_theme_options['faced-enable-sticky-sidebar'] == 1) {
            $classes[] = 'ct-sticky-sidebar';
        }

        if (!empty($faced_theme_options['faced-font-awesome-version-loading'])) {
            $classes[] = 'faced-fontawesome-' . $faced_theme_options['faced-font-awesome-version-loading'];
        }

        return $classes;
    }
}

add_filter('body_class', 'faced_custom_body_class');



if (!function_exists('faced_excerpt_more')) :
    /**
     * Remove ... From Excerpt
     *
     * @since 1.0.0
     */
    function faced_excerpt_more($more)
    {
        if (!is_admin()) {
            return '';
        }
    }
endif;
add_filter('excerpt_more', 'faced_excerpt_more');


if (!function_exists('faced_alter_excerpt')) :
    /**
     * Filter to change excerpt length size
     *
     * @since 1.0.0
     */
    function faced_alter_excerpt($length)
    {
        if (is_admin()) {
            return $length;
        }
        global $faced_theme_options;
        if (!empty($faced_theme_options['faced-excerpt-length'])) {
            return absint($faced_theme_options['faced-excerpt-length']);
        }
        return 25;
    }
endif;
add_filter('excerpt_length', 'faced_alter_excerpt');


if (!function_exists('faced_tag_cloud_widget')) :
    /**
     * Function to modify tag clouds font size
     *
     * @param none
     * @return array $args
     *
     * @since 1.0.0
     *
     */
    function faced_tag_cloud_widget($args)
    {
        $args['largest'] = 0.9; //largest tag
        $args['smallest'] = 0.9; //smallest tag
        $args['unit'] = 'rem'; //tag font unit
        return $args;
    }
endif;
add_filter('widget_tag_cloud_args', 'faced_tag_cloud_widget');


/**
 * Google Fonts
 *
 * @param null
 * @return array
 *
 * @since Faced 1.0.0
 *
 */
if (!function_exists('faced_google_fonts')) :
    function faced_google_fonts()
    {
        $faced_google_fonts = array(
            'Libre+Baskerville' => 'Libre Baskerville',
            'Merriweather:400,400italic,300,900,700' => 'Merriweather',
            'Montserrat:400,700' => 'Montserrat',
            'Muli:400,300italic,300' => 'Muli',
            'Mulish:wght@300;400;500;600;700' => 'Mulish',
            'Open+Sans:400,400italic,600,700' => 'Open Sans',
            'Open+Sans+Condensed:300,300italic,700' => 'Open Sans Condensed',
            'Oswald:400,300,700' => 'Oswald',
            'Oxygen:400,300,700' => 'Oxygen',
            'Poppins:400,500,600,700' => 'Poppins',
            'Roboto:400,500,300,700,400italic' => 'Roboto',
            'Voltaire' => 'Voltaire',
            'Yanone+Kaffeesatz:400,300,700' => 'Yanone Kaffeesatz'
        );
        return apply_filters('faced_google_fonts', $faced_google_fonts);
    }
endif;


/**
 * Enqueue the list of fonts.
 */
function faced_customizer_fonts()
{
    wp_enqueue_style('faced_customizer_fonts', 'https://fonts.googleapis.com/css?family=Libre+Baskerville|Merriweather|Montserrat|Muli|Mulish|Open+Sans|Open+Sans+Condensed|Oswald|Oxygen|Poppins|Roboto|Voltaire|Yanone+Kaffeesatz', array(), null);
}

add_action('customize_controls_print_styles', 'faced_customizer_fonts');
add_action('customize_preview_init', 'faced_customizer_fonts');

add_action(
    'customize_controls_print_styles',
    function () {
?>
    <style>
        <?php
        $arr = array('Libre+Baskerville', 'Merriweather', 'Montserrat', 'Muli','Mulish', 'Open+Sans', 'Open+Sans+Condensed', 'Oswald', 'Oxygen', 'Poppins', 'Roboto', 'Voltaire', 'Yanone+Kaffeesatz');

        foreach ($arr as $font) {
            $font_family = str_replace("+", " ", $font);
            echo '.customize-control select option[value*="' . $font . '"] {font-family: ' . $font_family . '; font-size: 22px;}';
        }
        ?>
    </style>
<?php
    }
);


if (!function_exists('faced_editor_assets')) {
    /**
     * Add styles and fonts for the editor.
     */
    function faced_editor_assets()
    {
        wp_enqueue_style('faced-fonts', faced_fonts_url(), array(), null);

        /* Paragraph Font Options */
        $faced_custom_css = '';
        global $faced_theme_options;
        $faced_theme_options = faced_get_options_value();
        $faced_google_fonts = faced_google_fonts();

        $faced_body_fonts = esc_attr($faced_theme_options['faced-font-family-url']);
        $faced_heading_fonts = esc_attr($faced_theme_options['faced-font-heading-family-url']);

        $faced_google_fonts_enqueue = array(
            $faced_body_fonts,
            $faced_heading_fonts,
        );
        $faced_google_fonts_enqueue_uniques = array_unique($faced_google_fonts_enqueue);
        foreach ($faced_google_fonts_enqueue_uniques as $faced_google_fonts_enqueue_unique) {
            wp_enqueue_style(
                $faced_google_fonts_enqueue_unique,
                '//fonts.googleapis.com/css?family=' . $faced_google_fonts_enqueue_unique . '',
                array(),
                ''
            );
        }
        if (!empty($faced_body_fonts)) {
            $faced_font_family = esc_attr($faced_google_fonts[$faced_body_fonts]);
            if (!empty($faced_font_family)) {
                $faced_custom_css .= ".editor-styles-wrapper .wp-block-table td, .editor-styles-wrapper .wp-block-table th, .editor-styles-wrapper, .editor-styles-wrapper .wp-block-button__link, .editor-styles-wrapper ul li, .editor-styles-wrapper ol li, .editor-styles-wrapper p, .editor-styles-wrapper .editor-block-list__block-edit, .editor-block-list__block  { font-family: '{$faced_font_family}'; }";
            }
        }

        /* Heading H1 Font Option */
        if (!empty($faced_heading_fonts)) {
            $faced_heading_font_family = $faced_google_fonts[$faced_heading_fonts];
            if (!empty($faced_heading_font_family)) {
                $faced_custom_css .= ".editor-post-title__block .editor-post-title__input , .editor-styles-wrapper h1, .editor-styles-wrapper h2, .editor-styles-wrapper h3, .editor-styles-wrapper h4, .editor-styles-wrapper h5, .editor-styles-wrapper h6 { font-family: '{$faced_heading_font_family}'; }";
            }
        }

        wp_add_inline_style($faced_google_fonts_enqueue_unique, $faced_custom_css);
    }

    add_action('enqueue_block_editor_assets', 'faced_editor_assets');
}

/**
 * Image sanitization callback example.
 *
 * Sanitization callback for 'image' type controls. This callback sanitizes `$checked`
 * as a boolean value, either TRUE or FALSE.
 *
 * @param bool $checked Whether the image is checked.
 * @return bool Whether the image is checked.
 */
if ( !function_exists('faced_sanitize_image') ) :
	function faced_sanitize_image( $image, $setting ) {
		/*
         * Array of valid image file types.
         *
         * The array includes image mime types that are included in wp_get_mime_types()
         */
		$mimes = array(
			'jpg|jpeg|jpe' => 'image/jpeg',
			'gif'          => 'image/gif',
			'png'          => 'image/png',
			'bmp'          => 'image/bmp',
			'tif|tiff'     => 'image/tiff',
			'ico'          => 'image/x-icon'
		);
		// Return an array with file extension and mime_type.
		$file = wp_check_filetype( $image, $mimes );
		// If $image has a valid mime_type, return it; otherwise, return the default.
		return ( $file['ext'] ? $image : $setting->default );
	}
endif;