<?php
/*
Plugin Name: Admin Theme Preview
Plugin URI: http://bitbybit.wordpress.com/code/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.1.0
Author: Bitbybit
Author URI: http://bitbybit.wordpress.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')) {
		return('yay');
	} else {
		return('nay');
	}
	
	//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") {
	if ( levelcheck() == "nay" ) die('<h4>Error: You do not have the required permissions to preview themes</h4>');

        $themes = get_themes();
        $current_preview_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 "Previewing Theme: ";
                        // $ts .= '<li>'."\n"
                        $ts = ''
                                . '       <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 ($current_preview_theme == $theme_name) {
					$live = "--LIVE-- ";
				} else {
					$live = "";
				}

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

				if ($current_preview_theme == $theme_name) {
					$live = '<span style="font-weight:bold;color:#009933;">--LIVE--</span> ';
				} else {
					$live = '';
				}

                                $display = htmlspecialchars($theme_name);
                                
                                if ((!empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && $_COOKIE["wpadmintheme" . COOKIEHASH] == $theme_name)
                                                || (empty($_COOKIE["wpadmintheme" . COOKIEHASH]) && ($theme_name == $current_preview_theme))) {
                                        $ts .= '<li>'.$live.'<a href="' . get_settings('home') . '/' . 'index.php' . '?wpadmintheme='.urlencode($theme_name).'">' . '<strong>'.$display.'</strong></a>&nbsp;&nbsp;<span style="font-weight:bold;color:#ff0000;">&lArr; Currently Previewing</span></li>'."\n";
                                }        else {
                                        $ts .= '<li>'.$live.'<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 theme preview toolbar to top of active page if valid user
function atp_footer() {
	if ( levelcheck() == "yay"  ) {
	echo <<<END
	<div style="background-color:#ccc; border:1px solid #888; color: #444; clear:both; padding:.5%;">
END;
	wp_admin_preview('dropdown');
	echo <<<END
	</div>
END;
	}
}

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

?>
