<?php

class DragBlock_Font_Form_Messages
{
	public static function admin_notice_embed_font_success()
	{
		// stop here
		$theme_name  = wp_get_theme()->get('Name');
		$font_family = '';

		if (
			!empty($_POST['nonce']) &&
			wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'dragblock_font_library') &&
			isset($_POST['selection-data'])
		) {
			$data = json_decode(sanitize_text_field(wp_unslash($_POST['selection-data'])), true);
			if (!empty($data)) {
				$font_names = array();
				foreach ($data as $font_family) {
					$font_names[] = $font_family['family'];
				}
				$font_family = implode(', ', $font_names);
			}
		}

		if (
			!empty($_POST['nonce']) &&
			wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'dragblock_font_library') &&
			isset($_POST['font-name'])
		) {
			$font_family = sanitize_text_field(wp_unslash($_POST['font-name']));
		}
		$font_family = esc_html($font_family);
		$theme_name = esc_html($theme_name);
		$message = sprintf(esc_html__('%1$s font added to the DragBlock\'s font library.', 'dragblock'), $font_family, $theme_name);
		$url = esc_attr('admin.php?page=' . DRAGBLOCK_FONT_LIB_SLUG);
		$link = "<a href=\"{$url}\">" . esc_html__('Font Library', 'dragblock') . '</a>';
		$notice = "<div class=\"notice notice-success is-dismissible\"><p>{$message}{$link}</p></div>";

		echo wp_kses_post($notice);
	}

	public static function admin_notice_embed_font_permission_error()
	{
		$theme_name  = wp_get_theme()->get('Name');
		$font_family = '';

		if (
			!empty($_POST['nonce']) &&
			wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'dragblock_font_library') &&
			isset($_POST['selection-data'])
		) {
			$data = json_decode(sanitize_text_field(wp_unslash($_POST['selection-data'])), true);
			if (!empty($data) && !empty($data['family'])) {
				$font_family = $data['family'];
			}
		}
		if (
			!empty($_POST['nonce']) &&
			wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'dragblock_font_library') &&
			isset($_POST['font-name'])
		) {
			$font_family = sanitize_text_field(wp_unslash($_POST['font-name']));
		}

		$font_family = esc_html($font_family);
		$theme_name = esc_html($theme_name);
		$message = sprintf(esc_html__('Error adding %1$s font to the DragBlock\'s font library. WordPress lack permissions to write the font assets.', 'dragblock'), $font_family, $theme_name);
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);
	}

	public static function admin_notice_embed_font_file_error()
	{
		$theme_name = wp_get_theme()->get('Name');
		$font_family = '';


		if (
			!empty($_POST['nonce']) &&
			wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'dragblock_font_library') &&
			isset($_POST['font-name'])
		) {
			$font_family = sanitize_text_field(wp_unslash($_POST['font-name']));
		}

		$font_family = esc_html($font_family);
		$theme_name = esc_html($theme_name);

		$message = sprintf(esc_html__('Error adding %1$s font to the DragBlock\'s font library. The uploaded file is not valid.', 'dragblock'), $font_family, $theme_name);
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);
	}

	public static function admin_notice_font_asset_removal_error()
	{
		$theme_name = wp_get_theme()->get('Name');
		$theme_name = esc_html($theme_name);
		$message = sprintf(esc_html__('Error removing font asset. WordPress lacks permissions to remove these font assets.', 'dragblock'), $theme_name);
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);
	}

	public static function admin_notice_manage_fonts_permission_error()
	{
		$theme_name = wp_get_theme()->get('Name');
		$theme_name = esc_html($theme_name);
		$message = sprintf(esc_html__('Error handling font changes. WordPress lack permissions to manage the font assets.', 'dragblock'), $theme_name);
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);	
	}

	public static function admin_notice_delete_font_success()
	{
		$theme_name = wp_get_theme()->get('Name');
		$theme_name = esc_html($theme_name);
		$message = sprintf(esc_html__('Font definition removed from the DragBlock\'s font library.', 'dragblock'), $theme_name);
		$notice = "<div class=\"notice notice-success is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);	
	}

	public static function admin_notice_file_edit_error()
	{
		$message = esc_html('Error: `DISALLOW_FILE_EDIT` cannot be enabled in wp-config.php to make modifications to local files.');
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);		
	}

	public static function admin_notice_user_cant_edit_theme()
	{
		$message = esc_html('Error: You do not have sufficient permission to edit local files.');
		$notice = "<div class=\"notice notice-error is-dismissible\"><p>{$message}</p></div>";

		echo wp_kses_post($notice);	
	}
}
