<?php
/**
 * fallback redux class
 */
if (!class_exists('Redux') && !class_exists('ReduxFramework')) {

    global $bestel_opt;

    class Redux {

        public static $hasOptions = false;

        public static function setArgs($option, $args) {
            $options = get_option($option, false);
            if (!empty($options)) {
                self::$hasOptions = true;
            }
        }

        public static function setSection($option, $args) {
            if (isset($args['fields']) && !empty($args['fields']) && !self::$hasOptions) {
                foreach ($args['fields'] as $field) {
                    if (isset($field['default']) && isset($field['id'])) {
                        $id = $field['id'];
                        $updateArr = get_option($option, array());
                        if (is_array($field['default'])) {
                            foreach ($field['default'] as $key => $val) {
                                $updateArr[$id][$key] = $val;
                            }
                            update_option($option, $updateArr);
                        } else {
                            $updateArr[$id] = $field['default'];
                            update_option($option, $updateArr);
                        }
                    }
                }
            }
        }

    }

    function bestel_redux_fallback_init_var() {
        global $bestel_opt;
        if (!is_admin() && !isset($bestel_opt)) {
            $bestel_opt = get_option('bestel_opt');
            foreach ($bestel_opt as $yskey => $ysvalue) {
                if ($ysvalue == 'on') {
                    $bestel_opt[$yskey] = true;
                } elseif ($ysvalue == 'off') {
                    $bestel_opt[$yskey] = false;
                }
            }
        }
    }

    add_action('init', 'bestel_redux_fallback_init_var', 1);
}

function bestel_redux_get_infoData() {
    $transient_name = 'check_smart_update_theme';
    $info_updated = get_transient($transient_name);
    if (false === $info_updated) {
        $my_theme = wp_get_theme('bestel');
        if ($my_theme->exists()) {
            $themever = $my_theme->get('Version');
            $themename = $my_theme->get('Name');
        } else {
            $themever = '1.0';
            $themename = 'Bestel';
        }
        $url = 'http://updates.smartdatasoft.net/check_for_updates.php';
        $response = wp_remote_post($url, array(
            'method' => 'POST',
            'timeout' => 45,
            'redirection' => 5,
            'blocking' => true,
            'headers' => array(),
            'body' => array(
                'purchase_key' => 'null',
                'operation' => 'check_update',
                'domain' => filter_input(INPUT_SERVER, 'HTTP_HOST') ,
                'module' => 'wp-bestel',
                'version' => $themever,
                'theme_name' => $themename,
            ),
            'cookies' => array()
                )
        );
        if (!is_wp_error($response) && isset($response['response']['code']) && $response['response']['code'] == 200) {
            set_transient($transient_name, $response, MONTH_IN_SECONDS);
        }
    } else {
        
    }
}

add_action('admin_init', 'bestel_redux_get_infoData', 1);
