<?php
include_once get_template_directory() . '/anps-framework/classes/AnpsFramework.php';

class AnpsStyle extends AnpsFramework {
    public $error = null;

    /* Update google fonts */
    public function update_gfonts($redirect = true) {
        // Get google fonts
        $gfonts = wp_remote_get('https://astudio.si/preview/gfonts/get_fonts.php');
        if (is_wp_error($gfonts)) {
            return null;
        } else {
            $json_data = json_decode($gfonts['body'], true);
            $this->set_option($json_data['items'], 'google_fonts');
            if ($redirect) {
                header("Location: themes.php?page=theme_options&sub_page=theme_style_google_font");
            }
        }
    }

    /* Upload custom font */
    public function upload_font() {
        if (!isset($_FILES['font']['error'])) {
            $this->error = __('Invalid upload!', 'industrial');
            return;
        }
        switch ($_FILES['font']['error']) {
            case UPLOAD_ERR_OK:
                break;
            default:
                $this->error = __('Invalid upload!', 'industrial');
                return;
        }

        $explode_file_upload = explode(".", $_FILES['font']['name']);
        if ($explode_file_upload[1] !== 'zip') {
            $this->error = __('Only ZIP uploads are supported!', 'industrial');
            return;
        }

        $fonts_zip_target = get_template_directory() . '/fonts/' . $_FILES['font']['name'];
        if (file_exists($fonts_zip_target)) {
            $this->error = __('File with same name already exists.', 'industrial');
            return;
        }

        // Change upload directory
        add_filter('upload_dir', array($this, 'upload_directory'));

        $movefile = wp_handle_upload($_FILES['font'], array('test_form' => false));
        if (isset($movefile['error']) && !empty($movefile['error'])) {
            $this->error = $movefile['error'];
            return;
        }

        $zip = new ZipArchive();
        $x = $zip->open($fonts_zip_target);
        if ($x === true) {
            $zip->extractTo(get_template_directory() . '/fonts');
            $zip->close();
            unlink($fonts_zip_target);
        }

        $this->update_custom_fonts();
        header("Location: themes.php?page=theme_options&sub_page=theme_style_custom_font");
    }

    /* Get all files and save it to options */
    public function update_custom_fonts() {
        $dir = get_template_directory() . '/fonts';
        $fonts = array();
        if ($handle = opendir($dir)) {
            while (($entry = readdir($handle)) !== false) {
                if (is_dir("{$dir}/${entry}")) continue;
                $parts = explode('.', $entry);
                $fonts[$parts[0]] = $parts[0];
            }
            closedir($handle);
        }
        $this->set_option($fonts, 'custom_fonts');
    }

    /* New upload directory */
    public function upload_directory($upload) {
        $upload['subdir'] = '/fonts';
        $upload['path'] = get_template_directory() . $upload['subdir'];
        $upload['url'] = get_template_directory_uri() . $upload['subdir'];
        $upload['error'] = $upload['error'];
        return $upload;
    }

    /* Get all fonts */
    public function get_fonts($type = 'all') {
        $fonts = array(
            'System fonts' => array(
                array('value' => 'AVN', 'name' => 'آوینی'),
				array('value' => 'IS-UL', 'name' => 'ایران سنس خیلی باریک'),
				array('value' => 'IS-L', 'name' => 'ایران سنس باریک'),
				array('value' => 'IS-R', 'name' => 'ایران سنس معمولی'),
				array('value' => 'IS-M', 'name' => 'ایران سنس متوسط'),
				array('value' => 'IS-B', 'name' => 'ایران سنس ضخیم'),
				array('value' => 'IS-BL', 'name' => 'ایران سنس خیلی ضخیم'),
				array('value' => 'IYL', 'name' => 'ایران یکان باریک'),
				array('value' => 'IYB', 'name' => 'ایران یکان ضخیم'),
				array('value' => 'XSB', 'name' => 'زیرو سنس ضخیم'),
				array('value' => 'VZR', 'name' => 'وزیر'),
				array('value' => 'HLV', 'name' => 'هلوتیکا'),
				array('value' => 'YBL', 'name' => 'یکان بخ باریک'),
				array('value' => 'YBR', 'name' => 'یکان بخ معمولی'),
				array('value' => 'YBB', 'name' => 'یکان بخ ضخیم'),
				array('value' => 'YBH', 'name' => 'یکان بخ ضخیم تر'),
				array('value' => 'YBF', 'name' => 'یکان بخ توپر'),
				array('value' => 'YKN', 'name' => 'یکان وب')
            ),
            'Custom fonts' => get_option($this->prefix . 'custom_fonts') ?: array(),
            'Google fonts' => get_option($this->prefix . 'google_fonts') ?: array()
        );
        return isset($fonts[$type]) ? $fonts[$type] : $fonts;
    }

    /* Save fonts options */
    public function anps_save_fonts() {
        $names = array('font_type_1', 'font_type_2', 'font_type_navigation');
        foreach ($_POST as $name => $value) {
            if (!in_array($name, $names)) continue;
            $fonts = explode('|', $value);
            update_option($name, $fonts[0]);
            update_option(str_replace('type', 'source', $name), $fonts[1]);
        }
        header("Location: themes.php?page=theme_options&sub_page=theme_style&sub_page=typography");
    }
}

$anps_style = new AnpsStyle();
