<?php
/**
 * Fired during plugin activation
 *
 * @link        https://wpsmartpost.com/
 * @since      2.0.0
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/includes
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Custom post class to register the carousel.
 */
class Smart_Post_Show_Pro_Custom_Font_Type {

	/**
	 * The single instance of the class.
	 *
	 * @var self
	 * @since 2.0.0
	 */
	private static $instance;

	/**
	 * Allows for accessing single instance of class. Class should only be constructed once per call.
	 *
	 * @since 1.0.0
	 * @static
	 * @return self Main instance.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Register post carousel custom font post type
	 */
	public function smart_post_custom_font_post_type() {
		if ( post_type_exists( 'sp_smart_post_font' ) ) {
			return;
		}
		$capability = apply_filters( 'pcp_user_role_permission', 'manage_options' );
		$show_ui    = current_user_can( $capability ) ? true : false;
		// Set the Post carousel builder post type labels.
		$labels =
			array(
				'name'               => __( 'Custom Font', 'smart-post-show-pro' ),
				'singular_name'      => __( 'Custom Font', 'smart-post-show-pro' ),
				'menu_name'          => __( 'Custom Font', 'smart-post-show-pro' ),
				'all_items'          => __( 'Custom Fonts', 'smart-post-show-pro' ),
				'add_new'            => __( 'Add New', 'smart-post-show-pro' ),
				'add_new_item'       => __( 'Font Family Name', 'smart-post-show-pro' ),
				'edit'               => __( 'Edit', 'smart-post-show-pro' ),
				'edit_item'          => __( 'Edit Custom Font', 'smart-post-show-pro' ),
				'new_item'           => __( 'New Custom Font', 'smart-post-show-pro' ),
				'search_items'       => __( 'Search Custom Font', 'smart-post-show-pro' ),
				'not_found'          => __( 'No Custom Font found', 'smart-post-show-pro' ),
				'not_found_in_trash' => __( 'No Custom Font found in Trash', 'smart-post-show-pro' ),
			);
		// Set the Post carousel post type arguments.
		$args =
			array(
				'labels'              => $labels,
				'public'              => false,
				'supports'            => array( 'title' ),
				'show_in_rest'        => true,
				'hierarchical'        => false,
				'rewrite'             => false,
				'show_ui'             => $show_ui,
				'show_in_menu'        => '/edit.php?post_type=sp_post_carousel',
				'show_in_menu'        => false,
				'show_in_rest'        => true,
				'show_in_nav_menu'    => true,
				'menu_position'       => 6,
				'exclude_from_search' => true,
				'capability_type'     => 'page',
			);

		register_post_type( 'sp_smart_post_font', $args );
	}

	/**
	 * Font file upload validation.
	 *
	 * @param array  $data       File data.
	 * @param string $file       File name.
	 * @param string $filename   File name.
	 * @param array  $mimes      Allowed mime types.
	 * @param string $real_mime  Real mime type.
	 * @return array $data       File data.
	 */
	public function smart_font_correct_filetypes( $data, $file, $filename, $mimes, $real_mime ) {
		if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
			return $data;
		}

		$wp_file_type = wp_check_filetype( $filename, $mimes );

		if ( 'ttf' === $wp_file_type['ext'] ) {
			$data['ext']  = 'ttf';
			$data['type'] = 'font/ttf';
		}
		return $data;
	}


	/**
	 * Add custom font file types to the upload.
	 *
	 * @param array $file_types Allowed file types.
	 * @return array $file_types Allowed file types.
	 */
	public function smart_add_file_types_to_uploads( $file_types ) {
		if ( 'sp_smart_post_font' === get_post_type() || current_user_can( 'manage_options' ) ) {
			$new_filetypes          = array();
			$new_filetypes['woff']  = 'font/woff';
			$new_filetypes['woff2'] = 'font/woff2';
			$new_filetypes['ttf']   = 'font/ttf';
			$new_filetypes['svg']   = 'image/svg+xml';
			$new_filetypes['eot']   = 'font/ttf';
			$file_types             = array_merge( $file_types, $new_filetypes );
			return $file_types;
		} else {
			return $file_types;
		}
	}

	/**
	 * Font face markup.
	 *
	 * @param array  $settings custom font type meta data array.
	 * @param string $font_name custom font type meta name.
	 * @return string
	 */
	public function get_smart_font_face( $settings, $font_name ) {
		$font_src = isset( $settings['font_src'] ) ? $settings['font_src'] : '';
		$weight   = isset( $settings['font_weight'] ) ? $settings['font_weight'] : '';

		$font_face = '@font-face {
            font-family: "' . str_replace( ' ', '-', $font_name ) . '";
            font-weight: ' . $weight . ';
            src: url(' . esc_url( $font_src ) . ');
        }';
		return $font_face;
	}

	/**
	 * Add meta box to custom font post type.
	 *
	 * @return void
	 */
	public function smart_post_custom_font_add_meta_boxes() {
		add_meta_box(
			'sp-pcp-custom-font-id',
			esc_html__( 'Font Variations', 'smart-post-show-pro' ),
			array( $this, 'add_custom_font_meta_box' ),
			'sp_smart_post_font',
			'advanced'
		);
	}

	/**
	 * Meta data settings markup function
	 *
	 * @param array  $arr Meta data Settings.
	 * @param string $font_name Meta data font name.
	 * @return void
	 */
	public function set_meta_data_markup( $arr = array(), $font_name = '' ) {
		$weight   = isset( $arr['font_weight'] ) ? $arr['font_weight'] : '';
		$style    = isset( $arr['font_style'] ) ? $arr['font_style'] : '';
		$font_src = isset( $arr['font_src'] ) ? $arr['font_src'] : '';

		$styles = '';
		if ( ! empty( $arr ) ) {
			$font_face = $this->get_smart_font_face( $arr, $font_name );
			echo '<style type="text/css">' . wp_kses_post( $font_face ) . '</style>';
			$styles = 'font-family: ' . str_replace( ' ', '-', $font_name ) . '; font-weight: ' . $weight;
		}
		?>
		<div class="sp-pcp-custom-font-container<?php echo esc_html( empty( $arr ) ? '-copy-item' : '-list-item content-open' ); ?>">
			<div class="sp-pcp-custom-font-header flex-between">
				<div class="sp-pcp-header-text-left"><?php echo esc_html__( 'Font Weight', 'smart-post-show-pro' ); ?></div>
				<div class="sp-pcp-header-icon-right flex-row">
					<div class="sp-pcp-icon close-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
							<path d="M15.4173 4.58334L4.58398 15.4167M4.58398 4.58334L15.4173 15.4167" stroke="#757575" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
						</svg>
					</div>
					<div class="sp-pcp-icon dropdown-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
							<path d="M5 7.50004C5 7.50004 8.68242 12.5 10 12.5C11.3177 12.5 15 7.5 15 7.5" stroke="#757575" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
						</svg>
					</div>
				</div>
			</div>
			<div class="sp-pcp-custom-font-content-wrapper">
				<div class="sp-pcp-custom-font-content">
					<div class="sp-pcp-custom-font-file">
						<span class="sp-pcp-font-file-uploader">
							<span class="sp-pcp-font-file-uploader-btn">
								<?php echo esc_html__( 'Choose File', 'smart-post-show-pro' ); ?>
							</span>
							<span class="sp-pcp-font-file-uploader-text"><?php echo esc_html__( 'No File Chosen', 'smart-post-show-pro' ); ?></span>
						</span>
						<span class="select-file-name">
							<span class="select-file-name-url"><?php echo esc_html( $font_src ); ?></span>
							<input name="font_src" type="hidden" value="<?php echo esc_url( $font_src ); ?>" />
						</span>
						<div class="sp-pcp-custom-font-file-note note"><strong><?php echo esc_html__( 'Note:', 'smart-post-show-pro' ); ?></strong> <?php echo esc_html__( 'Supported file types: .otf, .ttf, .woff, .woff2, .svg', 'smart-post-show-pro' ); ?></div>
					</div>
					<div class="sp-pcp-custom-font-preview-box-field <?php echo isset( $font_src ) ? esc_attr( 'display-flex' ) : 'display-none'; ?>">
						<div class="sp-pcp-custom-font-preview-box-label">
							<label>Preview</label>
						</div>
						<div class="sp-pcp-custom-font-preview-box">
							<div class="sp-pcp-font-preview-text" style="<?php echo esc_attr( $styles ); ?>">
								The quick brown fox jumps over the lazy dog
							</div>
						</div>
					</div>
					<div class="sp-pcp-custom-font-settings flex-row">
						<div class="sp-pcp-font-settings-field">
							<label class="sp-pcp-font-settings-label"><?php echo esc_html__( 'Font Style:', 'smart-post-show-pro' ); ?></label>
							<div class="sp-pcp-select-field custom-icon">
								<select name="font_style">
									<option <?php selected( $style, 'normal' ); ?> value="normal"><?php echo esc_html__( 'normal', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $style, 'italic' ); ?> value="italic"><?php echo esc_html__( 'italic', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $style, 'oblique' ); ?> value="oblique"><?php echo esc_html__( 'oblique', 'smart-post-show-pro' ); ?></option>
								</select>
								<span class="sp-pcp-select-custom-icon">
									<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"><path d="M5 7.50004C5 7.50004 8.68242 12.5 10 12.5C11.3177 12.5 15 7.5 15 7.5" stroke="#757575" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/></svg>
								</span>
							</div>
						</div>
						<div class="sp-pcp-font-settings-field">
							<label class="sp-pcp-font-settings-label"><?php echo esc_html__( 'Font weight:', 'smart-post-show-pro' ); ?></label>
							<div class="sp-pcp-select-field custom-icon">
								<select name="font_weight">
									<option <?php selected( $weight, '100' ); ?> value="100"><?php echo esc_html__( '100', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '200' ); ?> value="200"><?php echo esc_html__( '200', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '300' ); ?> value="300"><?php echo esc_html__( '300', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '400' ); ?> value="400"><?php echo esc_html__( '400 Regular', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '500' ); ?> value="500"><?php echo esc_html__( '500', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '600' ); ?> value="600"><?php echo esc_html__( '600', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '700' ); ?> value="700"><?php echo esc_html__( '700 Bold', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '800' ); ?> value="800"><?php echo esc_html__( '800', 'smart-post-show-pro' ); ?></option>
									<option <?php selected( $weight, '900' ); ?> value="900"><?php echo esc_html__( '900', 'smart-post-show-pro' ); ?></option>
								</select>
								<span class="sp-pcp-select-custom-icon">
									<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none"><path d="M5 7.50004C5 7.50004 8.68242 12.5 10 12.5C11.3177 12.5 15 7.5 15 7.5" stroke="#757575" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/></svg>
								</span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Save meta box of custom font post type.
	 *
	 * @param int $post_id Post Id.
	 * @return void
	 */
	public function smart_post_custom_font_save_meta_boxes( $post_id ) {
		// Verify nonce.
		if ( ! isset( $_POST['sp_custom_font_nonce'] ) ||
			! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['sp_custom_font_nonce'] ) ), 'sp_pcp_font_meta_box' ) ) {
			return;
		}

		// Initialize settings array.
		$settings = array();
		$weight   = isset( $_POST['font_weight'] ) ? sanitize_text_field( wp_unslash( $_POST['font_weight'] ) ) : '';
		$style    = isset( $_POST['font_style'] ) ? sanitize_text_field( wp_unslash( $_POST['font_style'] ) ) : '';
		$font_src = isset( $_POST['font_src'] ) ? sanitize_text_field( wp_unslash( $_POST['font_src'] ) ) : '';

		if ( isset( $weight ) && is_array( $weight ) ) {
			foreach ( $weight as $i => $value ) {
				if ( ( isset( $weight[ $i ] ) &&
					! empty( $style[ $i ] ) ) ||
					( ! empty( $font_src[ $i ] ) ) ) {
							$temp                = array();
							$temp['font_weight'] = isset( $weight[ $i ] ) ? sanitize_text_field( $weight[ $i ] ) : '';
							$temp['font_style']  = isset( $style[ $i ] ) ? sanitize_text_field( $style[ $i ] ) : '';
							$temp['font_src']    = isset( $font_src[ $i ] ) ? sanitize_text_field( $font_src[ $i ] ) : '';
							$settings[]          = $temp;
				}
			}
		}
				update_post_meta( $post_id, '__sp_font_settings', $settings );
	}

	/**
	 * Custom meta box render html.
	 *
	 * @param array|object $post post array.
	 * @return void
	 */
	public function add_custom_font_meta_box( $post ) {
		wp_nonce_field( 'sp_pcp_font_meta_box', 'sp_custom_font_nonce' );
		$settings = get_post_meta( $post->ID, '__sp_font_settings', true );
		$settings = is_array( $settings ) ? $settings : array();
		$logo     = SP_PCP_URL . 'admin/img/logo.svg';

		$meta_heading  = '<!-- Heading start -->
		<div class="sp-pcp-blocks-settings-page-header">
			<div class="sp-pcp-blocks-settings-page-header-left">
				<img src="' . $logo . '">
				</div>
				<div class="sp-pcp-blocks-settings-page-header-right">
					<a target="_blank" href="https://shapedplugin.com/support/">
						<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none"><path fill="#fff" d="M4.934 7.921c.09.025.184.038.283.038h.193c.016 0 .031-.002.047-.003.515 1.61 1.632 2.728 2.93 2.728 1.17 0 2.194-.912 2.761-2.273.193-.137.36-.29.488-.457.062-.005.124-.013.182-.028.443-.11.77-.486.77-.937v-.76c0-.45-.327-.828-.77-.937v-.867c0-1.784-1.543-3.235-3.442-3.235-1.898 0-3.442 1.451-3.442 3.235v.871c-.432.116-.75.49-.75.932v.761c0 .444.318.817.75.933Zm3.442-6.056c1.352 0 2.475.932 2.685 2.147-.586-.893-1.565-1.478-2.675-1.478-1.126 0-2.118.603-2.702 1.518.193-1.235 1.326-2.187 2.692-2.187ZM5.652 6.62c.11.036.236.093.37.187 0 0-.32-1.908.358-2.175 0 0 .936.454 2.006.484 1.07-.03 2.007-.484 2.007-.484.679.267.357 2.175.357 2.175.127-.088.246-.145.35-.181v.63a5.274 5.274 0 0 1-.23.793c-.436.303-1.057.517-1.757.577a.448.448 0 0 0-.376-.206H8.14a.45.45 0 1 0 0 .899h.597a.446.446 0 0 0 .396-.243c.52-.04.993-.157 1.401-.328-.52.88-1.288 1.439-2.147 1.439-1.305 0-2.401-1.286-2.734-3.036v-.53Z"></path><path fill="#fff" fill-rule="evenodd" d="M14.886 14.104v.413a.285.285 0 0 1-.286.285H2.17a.285.285 0 0 1-.284-.285v-.413a4.305 4.305 0 0 1 3.683-3.842l.01-.001c.004 0 .009 0 .013-.002l.18-.02.002.01v.008l.005.043.002.01c.011.101.04.199.088.293.316.796 1.34 2.305 1.942 3.162l.124-1.78a1.045 1.045 0 0 1-.424-.413.822.822 0 0 1-.112-.4s.45.2.908.2c.026 0 .052 0 .079-.002.026.002.052.002.078.002.459 0 .908-.2.908-.2a.822.822 0 0 1-.112.4 1.04 1.04 0 0 1-.424.413l.124 1.78c.602-.857 1.626-2.365 1.942-3.162a.872.872 0 0 0 .088-.293l.003-.023a.561.561 0 0 0 .004-.039l.001-.009.182.02.012.002.01.001a4.304 4.304 0 0 1 3.684 3.842Z" clip-rule="evenodd"></path></svg>
						Support
					</a>
				</div>
			</div>
		<!-- Heading end -->';
		$meta_heading .= '<div class="sp-pcp-custom-font-type-wrapper">
			<div class="sp-pcp-custom-font-content-container">';

		echo wp_kses_post( $meta_heading );

		// Set empty metadata.
		$this->set_meta_data_markup();

		if ( is_array( $settings ) && ! empty( $settings ) ) {
			foreach ( $settings as $key => $item ) {
				$this->set_meta_data_markup( $item, $post->post_title );
			}
		}

		echo wp_kses_post( '</div>' );
		echo wp_kses_post( '<span class="btn sp-pcp-font-variations-action add-btn"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"><g id="add"><path id="icon" d="M11 13H5V11H11V5H13V11H19V13H13V19H11V13Z" fill="#F05D31"/></g></svg>' . esc_html__( ' Add Variation Font', 'smart-post-show-pro' ) . '</span>' );
		echo wp_kses_post( '</div>' );
	}
}
