<?php
if(!defined('ABSPATH')) {
	die("Don't call this file directly.");
}

if ( ! class_exists( 'ZnCustomIconsExport' ) ) {
	class ZnCustomIconsExport
	{

		private $file_name = 'custom_icons.txt';

		function __construct(){
		}

		function render_page() {
		}

		function do_deploy(){


		}

		function do_export(){

			$filename = 'all_custom_icons.zip';

			header( 'Content-Description: File Transfer' );
			header( 'Content-Disposition: attachment; filename=' . $filename );
			header( 'Content-Type: application/zip; charset=' . get_option( 'blog_charset' ), true );

			$icons = $this->get_icons();

			if ( empty( $icons ) ) { return; }

			$this->zipFilesAndDownload( $icons, $filename );

			die();
		}

		function get_icons(){
			return false;
		}

		function zipFilesAndDownload( $files, $archive_file_name )
		{
			$upload_dir = wp_upload_dir();

			$file_path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR  . $archive_file_name;
			$zip = new ZipArchive();
			//create the file and throw the error if unsuccessful
			if ($zip->open($file_path, ZIPARCHIVE::CREATE )!==TRUE) {
				die("cannot open <$archive_file_name>\n");
			}
			//add each files of $file_name array to archive
			foreach( $files as $file )
			{

				$image_path = get_attached_file( $file->ID );
				$name = str_replace( trailingslashit( $upload_dir['basedir']) , '' , $image_path );
				$name = 'zn-content' .DIRECTORY_SEPARATOR .'uploads' . DIRECTORY_SEPARATOR . $name;

				// Check and Get the modified image
				// $resampled_image = $this->generate_image( $image_path );

				// if( ! empty( $resampled_image ) ) {
				// 	$zip->addFromString( $name, $resampled_image );
				// }
				// else {
				$zip->addFile( $image_path, $name );
				// }



			}
			$zip->close();
			//then send the headers to foce download the zip file
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename=$archive_file_name");
			header("Pragma: no-cache");
			header("Expires: 0");
			readfile("$file_path");
			unlink($file_path);
			exit;
		}

	}
	
}


?>