<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php 

class Beruco_Custom_Fonts {
	
	private static $_instance = null;

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'beruco_addon_admin_menu' ) );		
	}
	
	public static function beruco_addon_admin_menu(){
		add_submenu_page( 
			'beruco-welcome', 
			esc_html__( 'Custom Fonts', 'beruco-addon' ),
			esc_html__( 'Custom Fonts', 'beruco-addon' ), 
			'manage_options', 
			'beruco-fonts', 
			array( 'Beruco_Custom_Fonts', 'beruco_fonts_admin_page' )
		);
	}
	
	public static function beruco_fonts_admin_page(){
		$beruco_theme = wp_get_theme();
	?>
		<div class="beruco-settings-wrap">
			<div class="beruco-header-bar">
				<div class="beruco-header-left">
					<div class="beruco-admin-logo-inline">
						<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/brand-logo.png' ); ?>" alt="beruco-logo">
					</div><!-- .beruco-admin-logo-inline -->
					<h2 class="title"><?php esc_html_e( 'Beruco Custom Fonts', 'beruco-addon' ); ?><span class="beruco-version"><?php echo esc_attr( $beruco_theme->get( 'Version' ) ); ?></span></h2>
				</div><!-- .beruco-header-left -->
				<div class="beruco-header-right">
					<a href="<?php echo esc_url( 'https://wordpress.zozothemes.com/beruco/' ); ?>" class="button beruco-btn"><?php esc_html_e( 'Live Demo', 'beruco-addon' ); ?></a>
				</div><!-- .beruco-header-right -->
			</div><!-- .beruco-header-bar -->
			
			<div class="beruco-settings-tabs beruco-custom-sidebar-wrap">
				<div id="beruco-general" class="beruco-settings-tab active">
					<div class="container">
						<div class="row">
							<div class="col-4">
								<div class="media admin-box">
									<div class="admin-box-icon mr-3">
										<span class="dashicons dashicons-welcome-widgets-menus"></span>								
									</div>
									<div class="media-body admin-box-info">
										<h3 class="admin-box-title"><?php esc_html_e( 'Add Custom Fonts', 'beruco-addon' ); ?></h3>
										<div class="admin-box-content">
											<?php esc_html_e( 'You can add custom fonts here. Also we give you option to remove or delete custom fonts.', 'beruco-addon' ); ?>
										</div>	
										<form action="" method="post" enctype="multipart/form-data">
											<?php wp_nonce_field( 'beruco-)(&(*@#*%@*', 'beruco_custom_font_nonce' ); ?>
											<input type="file" name="beruco_custom_fonts" id="beruco-custom-fonts" class="beruco-custom-fonts" />
										</form>
										<a href="#" class="beruco-btn btn-default beruco-custom-fonts-upload"><?php esc_html_e( 'Upload Font', 'beruco-addon' ); ?></a>
										<ol class="admin-instruction-list">
											<li><?php esc_html_e( 'Notes: Custom fonts should be in this following format. .eot, .otf, .svg, .ttf, .wof', 'beruco-addon' ) ?></li>
											<li><?php esc_html_e( 'Font folder name only show as font name in theme option. So make folder name and font name are should be the same but font name like slug type.', 'beruco-addon' ) ?></li>
											<li><?php printf( '%1$s <strong>%2$s</strong> %3$s <strong>%4$s</strong>', esc_html__( 'Eg: Font folder name is -', 'beruco-addon' ), esc_html__( 'Wonder Land', 'beruco-addon' ), esc_html__( ' font name like', 'beruco-addon' ), esc_html__( ' wonder-land.eot, wonder-land.otf ...', 'beruco-addon' ) ); ?></li>
										</ol>
									</div>
								</div>
							</div>
							<div class="col-8">
								<div class="admin-box">
									<h3 class="admin-box-title font-title"><?php esc_html_e( 'Custom Fonts', 'beruco-addon' ); ?></h3>
									<?php
									//delete_option( 'beruco_custom_fonts' );
									if (isset($_POST['beruco_custom_font_nonce']) && wp_verify_nonce($_POST['beruco_custom_font_nonce'], 'beruco-)(&(*@#*%@*')) {
										Beruco_Custom_Fonts::beruco_upload_font();
									}

									if (isset($_POST['beruco_custom_font_remove_nonce']) && wp_verify_nonce($_POST['beruco_custom_font_remove_nonce'], 'beruco-(*&^&%^%@!')) {
										Beruco_Custom_Fonts::beruco_font_delete();
									}

									$custom_fonts = get_option('beruco_custom_fonts');
									?>
									<?php if (!empty($custom_fonts)) : ?>
										<form action="" method="post" enctype="multipart/form-data">
											<?php wp_nonce_field('beruco-(*&^&%^%@!', 'beruco_custom_font_remove_nonce'); ?>
											<input type="hidden" name="beruco_font_remove_name" id="beruco-font-remove-name" value="" />
											<table class="beruco-admin-table beruco-custom-font-table">
												<thead>
													<tr>
														<td><?php echo esc_html__('Font Name', 'beruco-addon'); ?></td>
														<td><?php echo esc_html__('CSS', 'beruco-addon'); ?></td>
														<td><?php echo esc_html__('Delete', 'beruco-addon'); ?></td>
													</tr>
												</thead>
												<tbody>
													<?php
													foreach ($custom_fonts as $font_slug => $font_name) {
													?>
														<tr>
															<td><?php echo esc_html($font_name); ?></td>
															<td>font-family: '<?php echo esc_html($font_name); ?>';</td>
															<td class="text-center"><a href="#" data-font="<?php echo esc_attr($font_slug); ?>" class="beruco-font-remove"><span class="dashicons dashicons-trash"></span></a></td>
														</tr>
													<?php
													}
													?>
												</tbody>
									</table>
									</form>
									<?php else: ?>
									<p><?php esc_html_e( 'Sorry! No custom fonts available.', 'beruco-addon' ); ?></p>
								<?php endif; ?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			
		</div>
	<?php
	}
	
	public static function beruco_upload_font(){
		if ( isset( $_FILES['beruco_custom_fonts'] ) ) {
			// The nonce was valid and the user has the capabilities, it is safe to continue.
			
			$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/octet-stream', 'application/x-rar-compressed');
			$file_type = $_FILES['beruco_custom_fonts']['type'];
			
			if( in_array( $file_type, $accepted_types ) ){
				// These files need to be included as dependencies when on the front end.
				
				require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
				require_once( ABSPATH . 'wp-admin/includes/file.php' );
				require_once( ABSPATH . 'wp-admin/includes/media.php' );
				
				// Let WordPress handle the upload.
				//delete_option( 'beruco_custom_fonts' );
				// Remember, 'pharmy_image_upload' is the name of our file input in our form above.
				$font_name = pathinfo($_FILES['beruco_custom_fonts']['name'], PATHINFO_FILENAME);
				$font_slug = sanitize_title( $font_name );
				if ( get_option( 'beruco_custom_fonts' ) ) {
					$custom_fonts_names = get_option( 'beruco_custom_fonts' ); 
					$custom_fonts_names = array_merge( $custom_fonts_names, array( $font_slug => $font_name ) );
				}else{
					$custom_fonts_names = array( $font_slug => $font_name );
				}
				WP_Filesystem();
				$destination = wp_upload_dir();
				$destination_path = $destination['basedir'] . '/custom-fonts/';
				$unzipfile = unzip_file( $_FILES['beruco_custom_fonts']['tmp_name'], $destination_path);
				
				update_option( 'beruco_custom_fonts', $custom_fonts_names );				
			}else{
				echo esc_html__( 'Invalid File Type', 'beruco-addon' );
			}
		}
	}
	
	public static function beruco_font_delete(){			
		$font_id = esc_attr( $_POST['beruco_font_remove_name'] );		
		$destination = wp_upload_dir();
		$custom_fonts = get_option( 'beruco_custom_fonts' );		
		if ( array_key_exists( $font_id, $custom_fonts ) ){
			$font_name = $custom_fonts[$font_id];
			$destination_path = $destination['basedir'] . '/custom-fonts/' . $font_name;	
			unset($custom_fonts[$font_id]);
			update_option( 'beruco_custom_fonts', $custom_fonts );
			self::rmdir_recurse( $destination_path );
		}
	}
	
	public static function rmdir_recurse($path) {
		$path = rtrim($path, '/').'/';
		$handle = opendir($path);
		while(false !== ($file = readdir($handle))) {
			if($file != '.' and $file != '..' ) {
				$fullpath = $path.$file;
				if(is_dir($fullpath)) self::rmdir_recurse($fullpath); else unlink($fullpath);
			}
		}
		closedir($handle);
		rmdir($path);
	}	
	
	public static function get_instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

} Beruco_Custom_Fonts::get_instance();