<?php

/**
 * Alpha Custom Fonts Addons
 *
 * @author     D-THEMES
 * @package    WP Alpha Core FrameWork
 * @subpackage Core
 * @since    1.0
 */
defined( 'ABSPATH' ) || die;

if ( ! class_exists( 'Alpha_Custom_Fonts' ) ) {
	class Alpha_Custom_Fonts extends Alpha_Base {

		/**
		 * Constructor
		 *
		 * @since 1.0
		 */
		public function __construct() {
			// Add theme options
			add_filter( 'alpha_customize_fields', array( $this, 'add_customize_fields' ) );

			// Allow uploading font file types
			if ( current_user_can( 'manage_options' ) ) {
				add_filter( 'upload_mimes', array( $this, 'mime_types' ) );
			}

			// Merge with dynamic styles
			add_filter( 'alpha_dynamic_style', array( $this, 'add_font_css' ) );

			// Add custom fonts to kirki control
			add_filter( 'alpha_kirki_typo_control_choices', array( $this, 'add_kirki_font_params' ) );

			// Add custom fonts to elementor control
			add_filter( 'elementor/fonts/groups', array( $this, 'add_elementor_font_groups' ) );
			add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_font_params' ) );

		}

		/**
		 * Add fields for custom fonts
		 *
		 * @param {Array} $fields
		 *
		 * @since 1.0
		 */
		public function add_customize_fields( $fields ) {
			// Custom Fonts Upload
			$fields['cs_typo_custom_title'] = array(
				'section' => 'typo',
				'type'    => 'custom',
				'default' => '<h3 class="options-custom-title">' . esc_html__( 'Custom Fonts', 'alpha-core' ) . '</h3>',
			);
			$fields['cs_typo_custom_desc']  = array(
				'section' => 'typo',
				'type'    => 'custom',
				'default' => '<p style="margin: 0;">' . esc_html__( 'Upload custom font. All files are not necessary but are recommended for full browser support. You can upload as many custom fonts as you need. Click the "Add new custom font" button for additional upload boxes.', 'alpha-core' ) . '</p>',
			);
			$fields['typo_user_custom']     = array(
				'section'   => 'typo',
				'type'      => 'repeater',
				'row_label' => array(
					'type'  => 'field',
					'value' => esc_attr__( 'custom font', 'alpha-core' ),
					'field' => 'name',
				),
				'default'   => array(),
				'fields'    => array(
					'name'   => array(
						'type'  => 'text',
						'label' => esc_html__( 'Font Name', 'alpha-core' ),
					),
					'woff2'  => array(
						'type'  => 'upload',
						'label' => esc_html__( 'WOFF2', 'alpha-core' ),
					),
					'woff'   => array(
						'type'  => 'upload',
						'label' => esc_html__( 'WOFF', 'alpha-core' ),
					),
					'ttf'    => array(
						'type'  => 'upload',
						'label' => esc_html__( 'TTF', 'alpha-core' ),
					),
					'SVG'    => array(
						'type'  => 'upload',
						'label' => esc_html__( 'SVG', 'alpha-core' ),
					),
					'weight' => array(
						'type'  => 'text',
						'label' => esc_html__( 'Font Weight', 'alpha-core' ),
					),
				),
			);

			return $fields;
		}

		/**
		 * Add options to kirki typography control
		 *
		 * @param {Array} $params
		 *
		 * @since 1.0
		 */
		public function add_kirki_font_params( $params ) {

			if ( ! function_exists( 'alpha_get_option' ) ) {
				return $params;
			}

			$custom_fonts = alpha_get_option( 'typo_user_custom' );

			if ( is_array( $custom_fonts ) && count( $custom_fonts ) ) {
				$choices_map = array();
				foreach ( $custom_fonts as $font ) {
					if ( isset( $font['name'] ) ) {
						$choices_map[] = array(
							'id'   => $font['name'],
							'text' => $font['name'],
						);
					}
				}

				if ( $choices_map ) {
					$params['fonts'] = array(
						'families' => array(
							'custom' => array(
								'text'     => esc_html__( 'Custom Fonts', 'alpha-core' ),
								'children' => $choices_map,
							),
						),
					);
				}
			}

			return $params;
		}

		/**
		 * Add 'Custom' group to elementor typography control
		 *
		 * @param {Array} $groups
		 *
		 * @since 1.0
		 */
		public function add_elementor_font_groups( $groups ) {

			if ( ! function_exists( 'alpha_get_option' ) ) {
				return $groups;
			}

			$groups['custom'] = esc_html__( 'Custom', 'alpha-core' );
			return $groups;
		}

		/**
		 * Add options to elementor typography control
		 *
		 * @param {Array} $params
		 *
		 * @since 1.0
		 */
		public function add_elementor_font_params( $params ) {

			if ( ! function_exists( 'alpha_get_option' ) ) {
				return $params;
			}

			$custom_fonts = alpha_get_option( 'typo_user_custom' );

			if ( is_array( $custom_fonts ) && count( $custom_fonts ) ) {
				foreach ( $custom_fonts as $font ) {
					if ( isset( $font['name'] ) ) {
						$params[ $font['name'] ] = 'custom';
					}
				}
			}

			return $params;
		}

		/**
		 * Allow uploading font file types.
		 *
		 * @param array $mimes The mime types allowed.
		 * @access public
		 */
		public function mime_types( $mimes ) {

			$mimes['woff2'] = 'font/woff2';
			$mimes['woff']  = 'font/woff';
			$mimes['ttf']   = 'font/ttf';
			$mimes['svg']   = 'font/svg';

			return $mimes;
		}

		/**
		 * Merge font style with dynamic styles
		 *
		 * @param {String} $style
		 *
		 * @since 1.0
		 */
		public function add_font_css( $style ) {
			$font_face = '';

			$custom_fonts = get_theme_mod( 'typo_user_custom' );

			if ( ! is_array( $custom_fonts ) || ! count( $custom_fonts ) ) {
				return $style;
			}

			foreach ( $custom_fonts as $font ) {
				$urls = array();
				if ( ! empty( $font['woff2'] ) ) {
					$urls[] = 'url(' . esc_url( wp_get_attachment_url( $font['woff2'] ) ) . ')';
				}
				if ( ! empty( $font['woff'] ) ) {
					$urls[] = 'url(' . esc_url( wp_get_attachment_url( $font['woff'] ) ) . ')';
				}
				if ( ! empty( $font['ttf'] ) ) {
					$urls[] = 'url(' . esc_url( wp_get_attachment_url( $font['ttf'] ) ) . ')';
				}
				if ( ! empty( $font['svg'] ) ) {
					$urls[] = 'url(' . esc_url( wp_get_attachment_url( $font['svg'] ) ) . ')';
				}

				$font_face .= '@font-face {' . "\n";
				$font_face .= 'font-family:"' . esc_attr( $font['name'] ) . '";' . "\n";
				$font_face .= 'src:';
				$font_face .= implode( ', ', $urls ) . ';';
				if ( ! empty( $font['weight'] ) ) {
					$font_face .= 'font-weight:' . esc_attr( $font['weight'] ) . ';' . "\n";
				}
				if ( alpha_get_option( 'font_face_display' ) ) {
					$font_face .= 'font-display:swap;' . "\n";
				}
				$font_face .= '}' . "\n";

			}

			return $font_face . $style;
		}
	}
	Alpha_Custom_Fonts::get_instance();
}
