-
Notifications
You must be signed in to change notification settings - Fork 2
/
theme-switcher.php
117 lines (99 loc) · 3.34 KB
/
theme-switcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
Plugin Name: TOXID Themeswitcher
Plugin URI: http://toxid.org/
Description: Switch theme for TOXID-cURL.
Version: 1.1
Author: Joscha Krug
Author URI: http://www.marmalade.de
mostly copied from Theme-Switcher-Plugin by
Author: Ryan Boren
Author URI: http://ryan.boren.me/
Adapted from Ryan Boren theme switcher.
http://ryan.boren.me/
*/
class ToxidThemeSwitcher {
/** @var string|false */
protected $theme_param = null;
/** @var WP_Theme|false */
protected $theme = null;
function __construct()
{
add_filter('stylesheet', array(&$this, 'get_stylesheet'));
add_filter('template', array(&$this, 'get_template'));
add_filter('preview_page_link', array(&$this, 'add_preview_theme'));
add_filter('preview_post_link', array(&$this, 'add_preview_theme'));
add_action('admin_init', array(&$this, 'init_admin'));
}
function add_preview_theme($link)
{
$theme = urlencode(get_option('toxid_preview_theme'));
$link .= (strpos($link, '?') === false ? '?' : '&') . 'wptheme=' . $theme;
return $link;
}
function init_admin()
{
register_setting('general', 'toxid_preview_theme');
add_settings_section('toxid-settings', 'TOXID', '__return_false', 'general');
add_settings_field('toxid_preview_theme', 'Theme used for previews', array(&$this, 'admin_toxid_preview_theme_field'), 'general', 'toxid-settings');
}
function admin_toxid_preview_theme_field()
{
$themes = array_keys(wp_get_themes());
$currentTheme = get_option('toxid_preview_theme');
echo '<select name="toxid_preview_theme">';
echo '<option>' . __('None') . '</option>';
foreach ($themes as $theme) {
printf('<option value="%s" %s>%s</option>', esc_attr($theme), ($theme == $currentTheme ? 'selected' : ''), esc_html($theme));
}
echo '</select>';
}
function get_stylesheet($stylesheet = '')
{
if ($theme = $this->get_theme()) {
return $theme['Stylesheet'];
}
return $stylesheet;
}
function get_template($template) {
if ($theme = $this->get_theme()) {
return $theme['Template'];
}
return $template;
}
/**
* set theme-switch url param
* default value is false
*
* @return string|false
*/
protected function _get_theme_param()
{
if ($this->theme_param === null) {
$this->theme_param = !empty($_GET['wptheme']) ? $_GET['wptheme'] : false;
}
return $this->theme_param;
}
/**
* returns requested WP_Theme object if set
* default value is false
*
* @return WP_Theme|bool
*/
public function get_theme()
{
if ($this->theme === null) {
$this->theme = false;
if ($theme_name = $this->_get_theme_param()) {
/** @var array $themes */
$themes = wp_get_themes();
// check if theme is set, but don't let people peek at unpublished themes.
if (isset($themes[$theme_name]) && !(isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')) {
$this->theme = $themes[$theme_name];
}
}
}
return $this->theme;
}
}
$theme_switcher = new ToxidThemeSwitcher();