<?php
/*
Plugin Name: Admin Theme Preview
Plugin URI: http://mindnority.com/software/wordpress/plugins/admin-theme-preview/
Description: Allows admins to preview installed themes. <br />Once plugin is activated, go to <a href="themes.php?page=admin-theme-preview.php">Presentation &gt;&gt; Theme Preview</a> to use.
Version: 2.0.0
Author: Mr. Syndromes
Author URI: http://mindnority.com

Installation Instructions: Save this file into a new file called "admin-theme-preview.php" and place it in your wp-content/plugins/ directory.  Activate the plugin in the wordpress Plugins page, then navigate to "Presentation -> Theme Preview" to preview your themes. 

Upgrade Instructions: Overwrite old version with new version :P

Lineage:  Basically just a slightly re-worked/combined version of Ryan Boren's (http://boren.nu/) Theme Preview plugin and Theme Switcher plugin (which was in turn adapted from Alex King's (http://www.alexking.org/) Style Switcher plugin).  They did all the work, I just put a wrapper on the thing :P 

*/ 

/* Code follows */

// Start things off by setting cookie
ts_set_theme_cookie();

/* Functions */

function levelcheck() {
	if (!current_user_can('switch_themes') || !current_user_can('edit_themes')) die('<h4>Error: You do not have the required permissions to preview themes</h4>');
}

function ts_set_theme_cookie() {
        $expire = time() + 31536000;

        if (!empty($_GET["wpadmintheme"])) {
                setcookie("wpadmintheme" . COOKIEHASH, stripslashes($_GET["wpadmintheme"]), $expire, COOKIEPATH);

                $redirect = get_settings('home').'/';

                if (function_exists('wp_redirect')) {
			// Fix this at some point
                        // wp_redirect($redirect);
			header("Refresh: 0;url=$redirect");
                } else {
			header("Refresh: 0;url=$redirect");
		}
        }
}

function ts_get_theme() {
        if (!empty($_COOKIE["wpadmintheme" . COOKIEHASH])) {
                return $_COOKIE["wpadmintheme" . COOKIEHASH];
        }        else {
                return '';
        }
}

function ts_get_template($template) {
        $theme = ts_get_theme();

        if (empty($theme)) {
                return $template;
        }

        $theme = get_theme($theme);
        
        if (empty($theme)) {
                return $template;
        }

        // Don't let people peek at unpublished themes.
        if (isset($theme['Status']) && $theme['Status'] != 'publish')
                return $template;                

        return $theme['Template'];
}

function ts_get_stylesheet($stylesheet) {
        $theme = ts_get_theme();

        if (empty($theme)) {
                return $stylesheet;
        }

        $theme = get_theme($theme);

        // Don't let people peek at unpublished themes.
        if (isset($theme['Status']) && $theme['Status'] != 'publish')
                return $template;                
        
        if (empty($theme)) {
                return $stylesheet;
        }

        return $theme['Stylesheet'];
	}

function wp_admin_preview($style = "text") {
	levelcheck();

        $themes = get_themes();

        $default_theme = get_current_theme();

        if (count($themes) > 1) {
                $theme_names = array_keys($themes);
                natcasesort($theme_names);

                $ts = '<ul id="themeswitcher">'."\n";                

                if ($style == 'dropdown') {
			echo "Quick Select Theme:";
                        $ts .= '<li>'."\n"
                                . '       <select name="themeswitcher" onchange="location.href=\''.get_settings('home').'/index.php?wpadmintheme=\' + this.options[this.selectedIndex].value;">'."\n"        ;

                        foreach ($theme_names as $theme_name) {
                                // Skip unpublished themes.
                                if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
                                        continue;
                                        
                                if ((!empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && $_COOKIE["wpadmintheme" . COOKIEHASH] == $theme_name)
                                                || (empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && ($theme_name == $default_theme))) {
                                        $ts .= '               <option value="'.$theme_name.'" selected="selected">'
                                                . htmlspecialchars($theme_name)
                                                . '</option>'."\n"
                                                ;
                                }        else {
                                        $ts .= '               <option value="'.$theme_name.'">'
                                                . htmlspecialchars($theme_name)
                                                . '</option>'."\n"
                                                ;
                                }                                
                        }
                        $ts .= '       </select>'."\n"
                                . '</li>'."\n"
                                ;
                }        else {
			echo "Available Themes:"; 
                        foreach ($theme_names as $theme_name) {
                                // Skip unpublished themes.
                                if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
                                        continue;

                                $display = htmlspecialchars($theme_name);
                                
                                if ((!empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && $_COOKIE["wpadmintheme" . COOKIEHASH] == $theme_name)
                                                || (empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && ($theme_name == $default_theme))) {
                                        $ts .= '       <li><strong><em>'.$display.'</em></strong>&nbsp;&nbsp;&lArr; Currently Selected</li>'."\n";
                                }        else {
                                        $ts .= '       <li><a href="'
                                                .get_settings('home').'/'. 'index.php'
                                                .'?wpadmintheme='.urlencode($theme_name).'">'
                                                .$display.'</a></li>'."\n";
                                }
                        }
                }
                $ts .= '</ul>';
        }

        echo $ts;
}


// Add options menu for this plugin
function admin_theme_preview() {
	if (function_exists('add_submenu_page')) {
	add_submenu_page('themes.php', 'Admin Theme Preview', 'Theme Preview', 9, 'admin-theme-preview.php', 'admin_options_page');
	}
}

// Populate options menu
function admin_options_page() {
	if (isset($_POST['info_update'])) { ?>
		<div class="updated"><p><strong>Theme Selected</strong></p></div>
	<?php } ?>

	<div class="wrap"><ul><li>
	<h2>Admin Theme Preview</h2>

	<?php if (function_exists('wp_admin_preview')) {
		// wp_admin_preview('dropdown'); echo "<br />";
		wp_admin_preview();
	} else {
        	echo "<h4>Error! The Admin Theme Preview plugin is not currently activated or installed.<br />Please make sure the plugin is installed and activated in your Plugin Management page before proceeding.</h4>";
	}

	echo "</li></ul></div>";
}

add_action('admin_menu', 'admin_theme_preview');
add_filter('template', 'ts_get_template');
add_filter('stylesheet', 'ts_get_stylesheet');

?>
