/**
 * Checks theme and framework versions
 * @uses $theme $version
 */
function themify_check_version()
{
    global $theme, $version, $notifications;
    // In case user has chosen to disable the upgrader
    if (!defined('THEMIFY_UPGRADER')) {
        define('THEMIFY_UPGRADER', true);
    }
    if (!THEMIFY_UPGRADER) {
        return;
    }
    // Check wheter current theme is parent theme or child theme
    $check_theme_name = is_child_theme() ? $theme->parent()->Name : $theme->display('Name');
    $check_theme_version = is_child_theme() ? $theme->parent()->Version : $theme->display('Version');
    $theme_name = '' != $theme->display('Template') ? $theme->display('Template') : strtolower($check_theme_name);
    $theme_version = $check_theme_version;
    //If it was a successful update
    if ($_GET['success'] == 'true' && $_GET['upgrade_type'] == 'theme') {
        $notifications .= '<p class="success">' . sprintf(__('Successfully updated %s to version %s.', 'themify'), $theme_name, $theme_version) . '</p>';
    }
    if ($_GET['success'] == 'true' && $_GET['upgrade_type'] == 'framework') {
        $notifications .= '<p class="success">' . sprintf(__('Successfully updated the Themify framework to version %s', 'themify'), $version) . '</p>';
    }
    /**
     * If we already know there's a new version, we don't need to check
     * @var stdClass newF
     * @var stdClass newT
     * newF
     * 		version
     * 		url
     * 		class
     * 		target
     * newT
     * 		login
     * 		version
     * 		url
     * 		class
     * 		target
     * */
    $newF = get_transient('themify_new_framework');
    $newT = get_transient('themify_new_theme');
    if (is_object($newF) || is_object($newT)) {
        if (is_object($newT) && $theme_version < $newT->version) {
            //echo "<p>newT is an object</p>";
            if ($_GET['page'] == 'themify') {
                $notifications .= sprintf(__('<p class="update %s">%s version %s is now available. <a href="%s" title="" class="%s" target="%s">Update now</a> or view the <a href="http://themify.me/logs/%s-changelogs" title="" class="" target="_blank">change log</a> for details.</p>', 'themify'), $newT->login, ucwords($theme_name), $newT->version, $newT->url, $newT->class, $newT->target, $theme_name);
            } else {
                $notifications .= '<p class="update">' . sprintf(__('%s version %s is now available. Visit the <a href="admin.php?page=themify">%s page</a> to update.', 'themify'), ucwords($theme_name), $newT->version, ucwords($theme_name)) . '</p>';
            }
        }
        if (is_object($newF) && $version < $newF->version) {
            //echo "<p>newF is an object</p>";
            if ($_GET['page'] == 'themify') {
                $notifications .= '<p class="update">' . sprintf(__('Framework version %s is now available. <a href="%s" title="" class="%s" target="%s">Update now</a> or view the <a href="http://themify.me/logs/framework-changelogs">change log</a> for details.', 'themify'), $newF->version, $newF->url, $newF->class, $newF->target) . '</p>';
            } else {
                $notifications .= '<p class="update">' . sprintf(__('Framework version %s is now available. Visit the <a href="admin.php?page=themify">%s page</a> to update.', 'themify'), $newF->version, ucwords($theme_name)) . '</p>';
            }
        }
        echo '<div class="notifications" style="">' . $notifications . '</div><style type="text/css">.notifications p.update {background: #F9F2C6;border: 1px solid #F2DE5B;} .notifications p{width: 765px;margin: 15px 0 0 5px;padding: 10px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;}</style>';
    }
    if (is_object($newF) && is_object($newT)) {
        //we don't have to do anything else
        return;
    } else {
        $notifications = '';
    }
    //If we didn't knew there was a new version already, let's see if it's 24hs since last check
    $current_theme = get_transient('themify_update_check_theme');
    $current_framework = get_transient('themify_update_check_framework');
    if (is_object($current_theme) && is_object($current_framework)) {
        //if theme version was checked not long ago
        if (60 * 1 > time() - $current_theme->lastChecked) {
            //echo 'Theme Version: Last checked on: '  . date('l jS \of F Y h:i:s A', $current_theme->lastChecked);
            //return;
            $theme_recently_checked = true;
        } else {
            $theme_recently_checked = false;
        }
        //if framework version was checked not long ago
        if (60 * 1 > time() - $current_framework->lastChecked) {
            //echo 'Framework Version: Last checked on: '  . date('l jS \of F Y h:i:s A', $current_framework->lastChecked);
            //return;
            $framework_recently_checked = true;
        } else {
            $framework_recently_checked = false;
        }
        //theme and framework were recently checked and no version was available, return
        if ($theme_recently_checked && $framework_recently_checked) {
            return;
        }
    }
    /**
     * Utilizes WordPress HTTP API
     * http://codex.wordpress.org/Function_API/wp_remote_request
     */
    $versions_url = 'http://themify.me/versions/versions.xml';
    $response = wp_remote_get($versions_url);
    if (is_wp_error($response)) {
        //echo '<h4>Can\'t load ' . $versions_url . '</h4><p>' . $response->get_error_code(). '</p>';
        return;
    }
    //if xml was successfully retrieved, let's delete the transients for theme and framework
    delete_transient('themify_update_check_theme');
    delete_transient('themify_update_check_framework');
    //Load string to be converted later into an array with themify_xml2array
    $versions = $response['body'];
    $newVersionFramework = false;
    $newVersionTheme = false;
    //Begin check
    if (isset($versions) && $versions != "") {
        $versions = themify_xml2array($versions);
        foreach ($versions['versions']['_c']['version'] as $update) {
            if ($update['_a']['free'] == 'true') {
                $login = "";
            } else {
                $login = "******";
            }
            $latest = str_replace(".", "", trim($update['_v']));
            if ($update['_a']['name'] == 'themify' && !is_object($newF)) {
                //Compares framework version
                if (str_replace(".", "", trim($version)) < $latest) {
                    /**
                     * Checks for WordPress' unzip_file
                     * http://codex.wordpress.org/Function_Reference/unzip_file
                     */
                    if (function_exists('unzip_file')) {
                        $class = "upgrade-framework";
                        $target = "";
                        $url = "#";
                    } else {
                        $class = "";
                        $target = "_blank";
                        $url = "http://themify.me/files/themify/themify.zip";
                    }
                    $notifications .= sprintf(__("<p class='update %s'>Framework version %s is now available. <a href='%s' title='' class='%s' target='%s'>Update now</a> or view the <a href='http://themify.me/logs/framework-changelogs' title='' class='' target='_blank'>change log</a> for details.</p>", 'themify'), $login, $update['_v'], $url, $class, $target);
                    //store variable indicating there is a new version of framework
                    $newFrameworkStore = new stdClass();
                    $newFrameworkStore->version = $update['_v'];
                    $newFrameworkStore->url = $url;
                    $newFrameworkStore->class = $class;
                    $newFrameworkStore->target = $target;
                    set_transient('themify_new_framework', $newFrameworkStore);
                    //echo 'new update for framework stored';
                    $newVersionFramework = true;
                }
            } else {
                if ($update['_a']['name'] == strtolower(trim($theme_name)) && !is_object($newT)) {
                    //Compares theme version
                    if (str_replace(".", "", $theme_version) < $latest) {
                        /**
                         * Checks for WordPress' unzip_file
                         * http://codex.wordpress.org/Function_Reference/unzip_file
                         */
                        if (function_exists('unzip_file')) {
                            $class = "upgrade-theme";
                            $target = "";
                            $url = "#";
                        } else {
                            $class = "";
                            $target = "blank";
                            $url = "http://themify.me/files/" . $theme_name . "/" . $theme_name . ".zip";
                        }
                        $notifications .= sprintf(__("<p class='update %s'>%s version %s is now available. <a href='%s' title='' class='%s' target='%s'>Update now</a> or view the <a href='http://themify.me/logs/%s-changelogs' title='' class='' target='_blank'>change log</a> for details.</p>", 'themify'), $login, ucwords($theme_name), $update['_v'], $url, $class, $target, $theme_name);
                        //store variable indicating there is a new version of theme
                        $newThemeStore = new stdClass();
                        $newThemeStore->login = $login;
                        $newThemeStore->version = $update['_v'];
                        $newThemeStore->url = $url;
                        $newThemeStore->class = $class;
                        $newThemeStore->target = $target;
                        set_transient('themify_new_theme', $newThemeStore);
                        //echo 'new update for theme stored';
                        $newVersionTheme = true;
                    }
                }
            }
        }
    }
    if (!$newVersionFramework && !$newVersionTheme) {
        //echo 'new update scheduled';
        themify_set_update_cookie('theme');
        themify_set_update_cookie('framework');
    }
    echo '<div class="notifications">' . $notifications . '</div>';
}
$version = "1.1.3";
$theme = get_theme_data(TEMPLATEPATH . '/style.css');
/* 	Data Config
/***************************************************************************/
init_db();
$data = get_data();
/*	Generate Config from XML
/***************************************************************************/
/**
 *  @var String $the_config_file
 */
$the_config_file = is_file(TEMPLATEPATH . "/custom-config.xml") ? 'custom-config.xml' : 'theme-config.xml';
$file = fopen(TEMPLATEPATH . "/" . $the_config_file, 'r');
$config = fread($file, filesize(TEMPLATEPATH . "/" . $the_config_file));
fclose($file);
$config = themify_xml2array($config);
$config = $config['config']['_c'];
/*	Dynamic panel creation
	/**************************************************/
$panels = $config['panel'];
unset($config['panel']);
if (is_array($panels)) {
    foreach ($panels as $panel) {
        $config['panel'][strtolower($panel['_a']['title'])] = $panel['_c'];
    }
}
/**
 * Load Shortcodes
 * @since 1.1.3
 * @package themify
 */
/**
 * Checks theme and framework versions
 *
 * @param string $area
 */
function themify_check_version($area = 'top')
{
    // In case user has chosen to disable the upgrader
    if (!defined('THEMIFY_UPGRADER')) {
        define('THEMIFY_UPGRADER', true);
    }
    if (!THEMIFY_UPGRADER) {
        return;
    }
    $theme = wp_get_theme();
    // Setup variables to collect markup
    $notifications = '';
    $theme_notifications = '';
    $fw_notifications = '';
    // Setup variables for updater.
    $theme_name = $theme->get_template();
    $theme_hash = md5($theme_name);
    $theme_version = is_child_theme() ? $theme->parent()->Version : $theme->display('Version');
    // Setup theme name for display purposes.
    $theme_label = is_child_theme() ? $theme->parent()->Name : $theme->display('Name');
    // If we already know there's a new version, we don't need to check, just use these objects:
    /**
     * @var stdClass newF
     * newF
     * 		version
     * 		url
     * 		class
     * 		target
     */
    $newF = get_transient('themify_new_framework');
    /**
     * @var stdClass newT
     * newT
     * 		login
     * 		version
     * 		url
     * 		class
     * 		target
     */
    $newT = get_transient('themify_new_theme' . $theme_hash);
    // Check if one of them are objects. If they're not, the transient expired so we'll check again
    if (is_object($newF) || is_object($newT)) {
        if (is_object($newT) && $theme_version < $newT->version) {
            if ($_GET['page'] == 'themify') {
                if (isset($area) && 'tab' == $area) {
                    $theme_notifications = sprintf(__('<a href="%s" title="" class="%s updateready" target="%s">Update Now</a> <p class="update %s">%s version %s is n
ow available. View the <a href="http://themify.me/logs/%s-changelogs" target="_blank">change log</a> for details.</p>', 'themify'), $newT->url, isset($area) && 'tab' == $area ? $newT->class . ' button big-button' : $newT->class, $newT->target, $newT->login, $theme_label, $newT->version, $theme_name);
                } else {
                    $theme_notifications = sprintf(__('<p class="update %s">%s version %s is now available. <a href="%s" class="%s" target="%s">Update Now</a> or view the <a href="http://themify.me/logs/%s-changelogs" target="_blank">change log</a> for details.</p>', 'themify'), $newT->login, $theme_label, $newT->version, $newT->url, isset($area) && 'tab' == $area ? $newT->class . ' button big-button' : $newT->class, $newT->target, $theme_name);
                }
            } else {
                $theme_notifications = '<p class="update">' . sprintf(__('%s version %s is now available. Visit the <a href="admin.php?page=themify">%s page</a> to update.', 'themify'), $theme_label, $newT->version, $theme_label) . '</p>';
            }
        }
        if (is_object($newF) && THEMIFY_VERSION < $newF->version) {
            if ($_GET['page'] == 'themify') {
                if (isset($area) && 'tab' == $area) {
                    $fw_notifications = '';
                } else {
                    $fw_notifications = '<p class="update">' . sprintf(__('Framework version %s is now available. <a href="%s" title="" class="%s" target="%s">Update Now</a> or view the <a href="http://themify.me/logs/framework-changelogs">change log</a> for details.', 'themify'), $newF->version, $newF->url, isset($area) && 'tab' == $area ? $newF->class . ' button big-button' : $newF->class, $newF->target) . '</p>';
                }
            } else {
                $fw_notifications = '<p class="update">' . sprintf(__('Framework version %s is now available. Visit the <a href="admin.php?page=themify">%s page</a> to update.', 'themify'), $newF->version, $theme_label) . '</p>';
            }
        }
        if ('' != $theme_notifications) {
            $notifications .= $theme_notifications;
        } else {
            $notifications .= $fw_notifications;
        }
        if (isset($area) && 'tab' == $area) {
            echo $notifications;
        } else {
            echo '<div class="notifications">' . $notifications . '</div>';
        }
    }
    if (is_object($newF) && is_object($newT)) {
        //we don't have to do anything else
        return;
    } else {
        $notifications = '';
    }
    //If we didn't knew there was a new version already, let's see if it's 24hs since last check
    $current_theme = get_transient('themify_update_check_theme' . $theme_hash);
    $current_framework = get_transient('themify_update_check_framework');
    if (is_object($current_theme) && is_object($current_framework)) {
        //if theme version was checked not long ago
        if (60 * 1 > time() - $current_theme->lastChecked) {
            //echo 'Theme Version: Last checked on: '  . date('l jS \of F Y h:i:s A', $current_theme->lastChecked);
            //return;
            $theme_recently_checked = true;
        } else {
            $theme_recently_checked = false;
        }
        //if framework version was checked not long ago
        if (60 * 1 > time() - $current_framework->lastChecked) {
            //echo 'Framework Version: Last checked on: '  . date('l jS \of F Y h:i:s A', $current_framework->lastChecked);
            //return;
            $framework_recently_checked = true;
        } else {
            $framework_recently_checked = false;
        }
        //theme and framework were recently checked and no version was available, return
        if ($theme_recently_checked && $framework_recently_checked) {
            return;
        }
    }
    /**
     * Utilizes WordPress HTTP API
     * http://codex.wordpress.org/Function_API/wp_remote_request
     */
    $versions_url = 'http://themify.me/versions/versions.xml';
    $response = wp_remote_get($versions_url);
    if (is_wp_error($response)) {
        //echo '<h4>Can\'t load ' . $versions_url . '</h4><p>' . $response->get_error_code(). '</p>';
        return;
    }
    //if xml was successfully retrieved, let's delete the transients for theme and framework
    delete_transient('themify_update_check_theme' . $theme_hash);
    delete_transient('themify_update_check_framework');
    //Load string to be converted later into an array with themify_xml2array
    $versions = $response['body'];
    $newVersionFramework = false;
    $newVersionTheme = false;
    //Begin check
    if (isset($versions) && '' != $versions) {
        $versions = themify_xml2array($versions);
        $theme_notifications = '';
        $fw_notifications = '';
        foreach ($versions['versions']['_c']['version'] as $update) {
            if (isset($update['_a']['free']) && 'true' == $update['_a']['free']) {
                $login = '';
            } else {
                $login = '******';
            }
            $latest = str_replace(".", "", trim($update['_v']));
            if ($update['_a']['name'] == 'themify' && !is_object($newF)) {
                //Compares framework version
                if (str_replace('.', '', trim(THEMIFY_VERSION)) < $latest) {
                    /**
                     * Checks for WordPress' unzip_file
                     * http://codex.wordpress.org/Function_Reference/unzip_file
                     */
                    if (function_exists('unzip_file')) {
                        $url = '#';
                        $class = 'upgrade-framework';
                        $target = '';
                    } else {
                        $url = 'http://themify.me/files/themify/themify.zip';
                        $class = '';
                        $target = '_blank';
                    }
                    if (isset($area) && 'tab' == $area) {
                        $fw_notifications = '';
                    } else {
                        $fw_notifications = sprintf(__('<p class="update %s">Framework version %s is now available. <a href="%s" class="%s" target="%s">Update Now</a> or view the <a href="http://themify.me/logs/framework-changelogs" target="_blank">change log</a> for details.</p>', 'themify'), $login, $update['_v'], $url, $class, $target);
                    }
                    //store variable indicating there is a new version of framework
                    $newFrameworkStore = new stdClass();
                    $newFrameworkStore->version = $update['_v'];
                    $newFrameworkStore->url = $url;
                    $newFrameworkStore->class = $class;
                    $newFrameworkStore->target = $target;
                    set_transient('themify_new_framework', $newFrameworkStore);
                    //echo 'new update for framework stored';
                    $newVersionFramework = true;
                }
            } else {
                if ($update['_a']['name'] == strtolower(trim($theme_name)) && !is_object($newT)) {
                    //Compares theme version
                    if (str_replace(".", "", $theme_version) < $latest) {
                        /**
                         * Checks for WordPress' unzip_file
                         * http://codex.wordpress.org/Function_Reference/unzip_file
                         */
                        if (function_exists('unzip_file')) {
                            $url = '#';
                            $class = 'upgrade-theme';
                            $target = '';
                        } else {
                            $url = 'http://themify.me/files/' . $theme_name . '/' . $theme_name . '.zip';
                            $class = '';
                            $target = '_blank';
                        }
                        if (isset($area) && 'tab' == $area) {
                            $theme_notifications = sprintf(__("<a href='%s' title='' class='%s updateready' target='%s'>Update Now</a> <p class='update %s'>%s version %s is now available. View the <a href='http://themify.me/logs/%s-changelogs' title='' class='' target='_blank'>change log</a> for details.</p>", 'themify'), $url, $class, $target, $login, $theme_label, $update['_v'], $theme_name);
                        } else {
                            $theme_notifications = sprintf(__("<p class='update %s'>%s version %s is now available. <a href='%s' title='' class='%s' target='%s'>Update Now</a> or view the <a href='http://themify.me/logs/%s-changelogs' title='' class='' target='_blank'>change log</a> for details.</p>", 'themify'), $login, $theme_label, $update['_v'], $url, $class, $target, $theme_name);
                        }
                        //store variable indicating there is a new version of theme
                        $newThemeStore = new stdClass();
                        $newThemeStore->login = $login;
                        $newThemeStore->version = $update['_v'];
                        $newThemeStore->url = $url;
                        $newThemeStore->class = $class;
                        $newThemeStore->target = $target;
                        set_transient('themify_new_theme' . $theme_hash, $newThemeStore);
                        //echo 'new update for theme stored';
                        $newVersionTheme = true;
                    }
                }
            }
        }
    }
    if (!$newVersionFramework && !$newVersionTheme) {
        //echo 'new update scheduled';
        themify_set_update_cookie('theme');
        themify_set_update_cookie('framework');
    }
    if ('' != $theme_notifications) {
        $notifications .= $theme_notifications;
    } else {
        $notifications .= $fw_notifications;
    }
    if (isset($area) && 'tab' == $area) {
        if ('' == $theme_notifications) {
            if (function_exists('unzip_file')) {
                $url = '#';
                $class = 'upgrade-theme';
                $target = '';
            } else {
                $url = 'http://themify.me/files/' . $theme_name . '/' . $theme_name . '.zip';
                $class = '';
                $target = '_blank';
            }
            $theme_notifications = sprintf(__('<p class="update %s"><a href="%s" class="%s" target="%s">Re-install Theme</a></p><p>Re-install the theme with the latest version.</p>', 'themify'), isset($login) ? $login . ' reinstalltheme' : $newT->login . ' reinstalltheme', isset($url) ? $url : $newT->url, isset($class) ? $class . ' button big-button' : $newT->class . ' button big-button', isset($target) ? $target : $newT->target);
            $notifications .= $theme_notifications;
        }
        echo $notifications;
    } else {
        echo '<div class="notifications">' . $notifications . '</div>';
    }
}