source: theme-switcher/trunk/theme-switcher.php @ 324274

Revision 324274, 6.1 KB checked in by filosofo, 17 months ago (diff)

remove notices for undefined variables

Line 
1<?php
2
3/*
4Plugin Name: Theme Switcher
5Plugin URI: http://wordpress.org/extend/plugins/theme-switcher/
6Description: Allow your readers to switch themes.
7Version: 1.0
8Author: Ryan Boren
9Author URI: http://ryan.boren.me/
10
11Adapted from Alex King's style switcher.
12http://www.alexking.org/software/wordpress/
13
14To use, add the "Theme Switcher" widget to your sidebar,
15or call wp_theme_switcher() directly, like so:
16
17  <li>Themes:
18        <?php wp_theme_switcher(); ?>
19  </li>
20
21This will create a list of themes for your readers to select.
22
23If you would like a dropdown box rather than a list, add this:
24
25  <li>Themes:
26        <?php wp_theme_switcher('dropdown'); ?>
27  </li>
28
29
30*/
31
32class ThemeSwitcherWidget extends WP_Widget {
33        function ThemeSwitcherWidget()
34        {
35                return $this->WP_Widget('theme-switcher-widget', __('Theme Switcher Widget', 'theme-switcher'), array('description' => __('A widget with options for switching themes.', 'theme-switcher')));
36        }
37
38        function widget($args, $instance)
39        {
40                global $theme_switcher;
41                $type = isset( $instance['displaytype'] ) ? $instance['displaytype'] : '';
42                $title = empty( $instance['title'] ) ? __('Theme Switcher', 'theme-switcher') : $instance['title'];
43                echo $args['before_widget'];
44                echo $args['before_title'] . $title . $args['after_title'];
45                echo $theme_switcher->theme_switcher_markup($type, $instance);
46                echo $args['after_widget'];
47        }
48
49        function update($new_instance)
50        {
51                return $new_instance;
52        }
53
54        function form($instance)
55        {
56                $type = isset( $instance['displaytype'] ) ? $instance['displaytype'] : '';
57                $title = isset( $instance['title'] ) ? $instance['title'] : '';
58                ?>
59                <p>
60                        <label for="<?php echo $this->get_field_id('title'); ?>">
61                                <span><?php _e('Title:', 'theme-switcher'); ?></span>
62                                <input type="text" name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo esc_attr($title); ?>" />
63                        </label>
64                </p>
65                       
66                <p><label for="<?php echo $this->get_field_id('displaytype'); ?>"><?php _e('Display themes as:', 'theme-switcher'); ?></label></p>
67                <p>
68                        <span><input type="radio" name="<?php echo $this->get_field_name('displaytype'); ?>" value="list" <?php
69                                if ( 'list' == $type ) {
70                                        echo ' checked="checked"';
71                                }
72                        ?> /> <?php _e('List', 'theme-switcher'); ?></span>
73                        <span><input type="radio" name="<?php echo $this->get_field_name('displaytype'); ?>" value="dropdown" <?php
74                                if ( 'dropdown' == $type ) {
75                                        echo ' checked="checked"';
76                                }
77                        ?>/> <?php _e('Dropdown', 'theme-switcher'); ?></span>
78                </p>
79                <?php
80        }
81}
82
83class ThemeSwitcher {
84
85        function ThemeSwitcher()
86        {
87                add_action('init', array(&$this, 'set_theme_cookie'));
88                add_action('widgets_init', array(&$this, 'event_widgets_init'));
89               
90                add_filter('stylesheet', array(&$this, 'get_stylesheet'));
91                add_filter('template', array(&$this, 'get_template'));
92        }
93
94        function event_widgets_init()
95        {
96                register_widget('ThemeSwitcherWidget');
97        }
98       
99        function get_stylesheet($stylesheet = '') {
100                $theme = $this->get_theme();
101
102                if (empty($theme)) {
103                        return $stylesheet;
104                }
105
106                $theme = get_theme($theme);
107
108                // Don't let people peek at unpublished themes.
109                if (isset($theme['Status']) && $theme['Status'] != 'publish')
110                        return $template;               
111               
112                if (empty($theme)) {
113                        return $stylesheet;
114                }
115
116                return $theme['Stylesheet'];
117        }
118
119        function get_template($template) {
120                $theme = $this->get_theme();
121
122                if (empty($theme)) {
123                        return $template;
124                }
125
126                $theme = get_theme($theme);
127               
128                if ( empty( $theme ) ) {
129                        return $template;
130                }
131
132                // Don't let people peek at unpublished themes.
133                if (isset($theme['Status']) && $theme['Status'] != 'publish')
134                        return $template;               
135
136                return $theme['Template'];
137        }
138
139        function get_theme() {
140                if ( ! empty($_COOKIE["wptheme" . COOKIEHASH] ) ) {
141                        return $_COOKIE["wptheme" . COOKIEHASH];
142                } else {
143                        return '';
144                }
145        }
146
147        function set_theme_cookie() {
148                load_plugin_textdomain('theme-switcher');
149                $expire = time() + 30000000;
150                if ( ! empty($_GET["wptheme"] ) ) {
151                        setcookie(
152                                "wptheme" . COOKIEHASH,
153                                stripslashes($_GET["wptheme"]),
154                                $expire,
155                                COOKIEPATH
156                        );
157                        $redirect = remove_query_arg('wptheme');
158                        wp_redirect($redirect);
159                        exit;
160                }
161        }
162       
163        function theme_switcher_markup($style = "text", $instance = array()) {
164                if ( ! $theme_data = wp_cache_get('themes-data', 'theme-switcher') ) {
165                        $themes = (array) get_themes();
166                        if ( function_exists('is_site_admin') ) {
167                                $allowed_themes = (array) get_site_option( 'allowedthemes' );
168                                foreach( $themes as $key => $theme ) {
169                                    if( isset( $allowed_themes[ wp_specialchars( $theme[ 'Stylesheet' ] ) ] ) == false ) {
170                                                unset( $themes[ $key ] );
171                                    }
172                                }
173                        }
174
175                        $default_theme = get_current_theme();
176
177                        $theme_data = array();
178                        foreach ((array) $themes as $theme_name => $data) {
179                                // Skip unpublished themes.
180                                if (empty($theme_name) || isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
181                                        continue;
182                                $theme_data[add_query_arg('wptheme', $theme_name, get_option('home'))] = $theme_name;
183                        }
184                       
185                        asort($theme_data);
186
187                        wp_cache_set('themes-data', $theme_data, 'theme-switcher');
188                }
189
190                $ts = '<ul id="themeswitcher">'."\n";           
191
192                if ( $style == 'dropdown' ) {
193                        $ts .= '<li>' . "\n\t" . '<select name="themeswitcher" onchange="location.href=this.options[this.selectedIndex].value;">'."\n";
194                }
195
196                foreach ($theme_data as $url => $theme_name) {
197                        if (
198                                ! empty($_COOKIE["wptheme" . COOKIEHASH]) && $_COOKIE["wptheme" . COOKIEHASH] == $theme_name ||
199                                empty($_COOKIE["wptheme" . COOKIEHASH]) && ($theme_name == $default_theme)
200                        ) {
201                                $pattern = 'dropdown' == $style ? '<option value="%1$s" selected="selected">%2$s</option>' : '<li>%2$s</li>';
202                        } else {
203                                $pattern = 'dropdown' == $style ? '<option value="%1$s">%2$s</option>' : '<li><a href="%1$s">%2$s</a></li>';
204                        }                               
205                        $ts .= sprintf($pattern,
206                                esc_attr($url),
207                                esc_html($theme_name)
208                        );
209
210                }
211
212                if ( 'dropdown' == $style ) {
213                        $ts .= "</select>\n</li>\n";
214                }
215                $ts .= '</ul>';
216                return $ts;
217        }
218}
219
220$theme_switcher = new ThemeSwitcher();
221
222function wp_theme_switcher($type = '')
223{
224        global $theme_switcher;
225        echo $theme_switcher->theme_switcher_markup($type);
226}
Note: See TracBrowser for help on using the repository browser.