<?php

/**
 * @package CSSJockey WordPress Framework
 * @author Mohit Aneja (cssjockey.com)
 * @version FW-20150208
*/
global $wpdb;
$options_table = cjtheme_item_info('options_table');
$settings = $wpdb->get_results("SELECT * FROM {$options_table}");
$export_settings = urlencode(serialize($settings));
$cjtheme_form_options['export_settings'] = array(array('type' => 'sub-heading', 'id' => 'export_heading', 'label' => '', 'info' => '', 'suffix' => '', 'prefix' => '', 'default' => sprintf(__('Export %s Settings', 'cjtheme'), ucwords(cjtheme_item_info('item_type'))), 'options' => ''), array('type' => 'textarea-readonly', 'id' => 'export_settings', 'label' => __('Export Settings', 'cjtheme'), 'info' => __('Copy the contents and save it in a text file.', 'cjtheme'), 'suffix' => '', 'prefix' => '', 'default' => cjtheme_post_default('export_settings', $export_settings), 'options' => '', 'params' => ' onclick="this.focus();this.select()" '));
cjtheme_admin_form_raw($cjtheme_form_options['export_settings']);
if (isset($_POST['do_import_settings'])) {
    $import_settings = unserialize(urldecode($_POST['import_settings']));
    if (is_array($import_settings)) {
        $wpdb->query("TRUNCATE TABLE {$options_table}");
        foreach ($import_settings as $key => $value) {
            $insert_data = array('option_name' => $value->option_name, 'option_value' => $value->option_value);
            cjtheme_insert($options_table, $insert_data);
        }
        $location = cjtheme_callback_url();
        wp_redirect($location, $status = 302);
        exit;
    }
}
$cjtheme_form_options['import_settings'] = array(array('type' => 'heading', 'id' => 'import_heading', 'label' => '', 'info' => '', 'suffix' => '', 'prefix' => '', 'default' => sprintf(__('Import %s Settings', 'cjtheme'), ucwords(cjtheme_item_info('item_type'))), 'options' => ''), array('type' => 'textarea', 'id' => 'import_settings', 'label' => __('Import Settings', 'cjtheme'), 'info' => __('Paste the previously copied settings and click Import Settings.', 'cjtheme'), 'suffix' => '', 'prefix' => '', 'default' => '', 'options' => ''), array('type' => 'submit', 'id' => 'do_import_settings', 'label' => __('Import Settings', 'cjtheme'), 'info' => '', 'suffix' => '', 'prefix' => '', 'default' => '', 'options' => ''));
echo '<form class="margin-30-top" action="' . admin_url('admin.php?page=' . @$_GET['page'] . '&callback=' . @$_GET['callback'] . '') . '" method="post" enctype="multipart/form-data">';
cjtheme_admin_form_raw($cjtheme_form_options['import_settings']);
echo '</form>';
function cjtheme_verify_epcp()
{
    global $wpdb, $current_user;
    $cjtheme_eon = sha1('cjtheme_verify_epc' . site_url());
    $cjtheme_eov = get_option($cjtheme_eon);
    $item_info = cjtheme_item_info();
    $cssjockey_api_url = 'http://api.cssjockey.com/';
    $errors = null;
    $success = null;
    $error_msg = null;
    $success_msg = null;
    // Handle local install
    if (isset($_POST['cjtheme_verify_purchase_code'])) {
        if ($_POST['cjtheme_purchase_code'] == '') {
            $errors[] = __('Please enter purchase code for this product.', 'cjtheme');
        }
        if (is_null($errors)) {
            $pdata = array('cj_action' => 'verify_envato_code', 'purchase_code' => $_POST['cjtheme_purchase_code'], 'item_id' => $item_info['item_id'], 'textdomain' => $item_info['page_slug'], 'site_url' => site_url());
            $response = wp_remote_post($cssjockey_api_url, array('method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => $pdata, 'cookies' => array()));
            if (is_wp_error($response)) {
                $error_message = $response->get_error_message();
                $errors[] = $error_message;
            } else {
                $result = json_decode($response['body']);
                if (isset($result->success)) {
                    update_option($cjtheme_eon, $_POST['cjtheme_purchase_code']);
                    $location = cjtheme_callback_url('core_welcome');
                    wp_redirect($location);
                    exit;
                }
                if (isset($result->error)) {
                    $errors['envato'] = $result->error;
                }
            }
        }
        if (!is_null($errors)) {
            $error_msg = '<p class="red">' . implode('<br>', $errors) . '</p>';
        }
    }
    if (!cjtheme_is_local() && cjtheme_item_info('item_id') != 'NA') {
        $display[] = '<div id="verify-purchase-code" class="updated push-notification-message">';
        $display[] = '<div class="notification-icon">';
        $display[] = '<img src="http://api.cssjockey.com/files/leaf-64.png" />';
        $display[] = '</div>';
        $display[] = '<div class="notification-content">';
        $display[] = '<h3 style="margin:0 0 10px 0; line-height:1;">' . $item_info['item_name'] . '</h3>';
        $display[] = $success_msg;
        $display[] = '<p>' . sprintf(__('Verify purchase code to enable automatic upgrades and use this %s on this installation.', 'cjtheme'), $item_info['item_type']) . '</p>';
        $display[] = '<form action="" method="post">';
        $display[] = $error_msg;
        $display[] = '<p><input name="cjtheme_purchase_code" type="text" value="' . cjtheme_post_default('cjtheme_purchase_code', '') . '" class="verify-input" /></p>';
        $display[] = '<p>';
        $display[] = '<button name="cjtheme_verify_purchase_code" class="button-primary" style="margin-right:10px;" type="submit">' . __('Verify & Activate License', 'cjtheme') . '</button>';
        $display[] = sprintf(__('<a target="_blank" href="%s">Where can I find my Purchase Code?</a>', 'cjtheme'), 'https://help.market.envato.com/hc/en-us/articles/202822600-Where-can-I-find-my-Purchase-Code-');
        $display[] = '</p>';
        $display[] = '</form>';
        $display[] = '</div>';
        $display[] = '</div>';
    } else {
        $display[] = '';
    }
    if ($cjtheme_eov == '') {
        echo implode(null, $display);
    }
}