<?php

class SchematicUpgrade {

    public function __construct() {
        if (!is_admin()) {
            return false;
        }

        $this->versionUpgrade();

        if (!isSchematicInstalled()) {
            return false;
        }

        global $schematicApi, $schematic_file;

        if ($schematicApi) {
            Puc_v4_Factory::buildUpdateChecker(
                    $schematicApi->get_plugin_update(), $schematic_file, 'wp-schema'
            );

            add_filter('puc_request_update_result-wp-schema', array($this, 'filterUpdateResult'));

            add_filter('puc_request_info_result-wp-schema', array($this, 'resultFilter'));
        }
    }

    public function filterUpdateResult($update) {
        $update->download_url = add_query_arg(array('api_key' => get_option('_schematic_api_key')), $update->download_url);
        return $update;
    }

    public function resultFilter($res) {
     
        if ($res && isset($res->key) && $res->key) {
            update_option('_'.getPluginSlug().'_d_l', $res->key);
        }

        return $res;
    }

    public function versionUpgrade() {
        $currentVersion = get_option('_schematic_version');

        if (!$currentVersion or $currentVersion !== WPSCHEMA_VERSION) {
            update_option('_schematic_version', WPSCHEMA_VERSION);

            if (!defined('DOING_AJAX')) {
                
            }
        }
    }

}

new SchematicUpgrade();


