source: trunk/www.guidonia.net/wp/wp-content/plugins/theme-switcher-reloaded/theme-switcher-reloaded.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 14.8 KB
Line 
1<?php
2/* Plugin Name: Theme Switcher Reloaded
3Plugin URI: http://themebot.com/webmaster-tools/1-wordpress-theme-switcher-reloaded
4Description: Efficient, feature-rich theme switcher plugin with widget. Sponsored by Themebot for use in the <a href="http://themebot.com/website-templates/wordpress-themes">WordPress Themes</a> section.
5Version: 1.0
6Author: Themebot
7Author URI: http://themebot.com
8
9Adapted from Ryan Boren's theme switcher and Jared Bangs Theme Switcher Widget.
10http://wordpress.org/extend/plugins/theme-switcher/#post-536
11http://freepressblog.org/wordpress-plugins-2/wordpress-theme-switcher-widget/
12... which was adapted from Alex King's style switcher.
13http://www.alexking.org/software/wordpress/
14Modified by kingler from 72pines http://code.72pines.org on Feb 13, 2008
15Sponsored by Themebot http://themebot.com/ */
16
17function widget_themeswitcher_init() {
18
19 // Check for the required plugin functions. This will prevent fatal
20 // errors occurring when you deactivate the dynamic-sidebar plugin.
21 if ( !function_exists('register_sidebar_widget') ) {
22 return;
23 }
24
25 load_plugin_textdomain('ts_theme_switcher', 'wp-content/plugins/theme-switcher-reloaded');
26
27 function ts_set_theme_cookie() {
28 $expire = time() + 3600*1000; // cookie set to expire in 1 hour
29 if (!empty($_GET["wptheme"])) {
30 setcookie("wptheme" . COOKIEHASH,
31 stripslashes($_GET["wptheme"]),
32 $expire,
33 COOKIEPATH
34 );
35
36 $redirect = get_settings('home').'/';
37
38 /*if (function_exists('wp_redirect'))
39 wp_redirect($redirect);
40 else
41 header("Location: ". $redirect);
42
43 exit;*/
44 }
45 }
46
47 function ts_get_theme() {
48 if (!empty($_GET["wptheme"]))
49 return $_GET["wptheme"];
50 else
51 if (!empty($_COOKIE["wptheme" . COOKIEHASH])) {
52 return $_COOKIE["wptheme" . COOKIEHASH];
53 } else {
54 return '';
55 }
56 }
57
58 function ts_get_template($template) {
59 $theme = ts_get_theme();
60
61 if (empty($theme)) {
62 return $template;
63 }
64
65 $theme = ts_get_one_theme($theme);
66
67 if (empty($theme)) {
68 return $template;
69 }
70
71 // Don't let people peek at unpublished themes.
72 if (isset($theme['Status']) && $theme['Status'] != 'publish')
73 return $template;
74
75 return $theme['Template'];
76 }
77
78 function ts_get_stylesheet($stylesheet) {
79 $theme = ts_get_theme();
80
81 if (empty($theme)) {
82 return $stylesheet;
83 }
84
85 $theme = ts_get_one_theme($theme);
86
87 // Don't let people peek at unpublished themes.
88 if (isset($theme['Status']) && $theme['Status'] != 'publish')
89 return $stylesheet;
90
91 if (empty($theme)) {
92 return $stylesheet;
93 }
94
95 return $theme['Stylesheet'];
96 }
97
98 function wp_theme_switcher($style = "dropdown") {
99 $themes = ts_get_all_themes();
100
101 $selected_theme = ts_get_theme();
102
103 $default_theme = ts_get_current_theme();
104
105 if (count($themes) > 1) {
106 $theme_names = array_keys($themes);
107 natcasesort($theme_names);
108
109 $ts = '<ul id="themeswitcher">'."\n";
110
111 if ($style == 'dropdown') {
112 $ts .= '<li>'."\n"
113 . ' <select style="width:140px" name="themeswitcher" onchange="location.href=\''.get_settings('home').'/index.php?wptheme=\' + this.options[this.selectedIndex].value;">'."\n" ;
114
115 foreach ($theme_names as $theme_name) {
116 // Skip unpublished themes.
117 if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
118 continue;
119
120 if ((!empty($selected_theme) && $selected_theme == $theme_name)
121 || (empty($selected_theme) && ($theme_name == $default_theme))) {
122 $ts .= ' <option value="'.$theme_name.'" selected="selected">'
123 . htmlspecialchars($theme_name)
124 . '</option>'."\n"
125 ;
126 } else {
127 $ts .= ' <option value="'.$theme_name.'">'
128 . htmlspecialchars($theme_name)
129 . '</option>'."\n"
130 ;
131 }
132 }
133 $ts .= ' </select>'."\n"
134 . '</li>'."\n"
135 ;
136 } else {
137 foreach ($theme_names as $theme_name) {
138 // Skip unpublished themes.
139 if (isset($themes[$theme_name]['Status']) && $themes[$theme_name]['Status'] != 'publish')
140 continue;
141
142 $display = htmlspecialchars($theme_name);
143
144 if ((!empty($selected_theme) && $selected_theme == $theme_name)
145 || (empty($selected_theme) && ($theme_name == $default_theme))) {
146 $ts .= ' <li>'.$display.'</li>'."\n";
147 } else {
148 $ts .= ' <li><a href="'
149 .get_settings('home').'/'. 'index.php'
150 .'?wptheme='.urlencode($theme_name).'">'
151 .$display.'</a></li>'."\n";
152 }
153 }
154 }
155 $ts .= '</ul>';
156 }
157
158 echo $ts;
159 }
160
161 ts_set_theme_cookie();
162
163 add_filter('template', 'ts_get_template');
164 add_filter('stylesheet', 'ts_get_stylesheet');
165
166 // This is the function that outputs our little Google search form.
167 function widget_themeswitcher($args) {
168
169 // $args is an array of strings that help widgets to conform to
170 // the active theme: before_widget, before_title, after_widget,
171 // and after_title are the array keys. Default tags: li and h2.
172 extract($args);
173
174 // Each widget can store its own options. We keep strings here.
175 $options = get_option('widget_themeswitcher');
176 $displayStyle = $options['displayStyle'];
177 $title = $options['Title'];
178
179 // These lines generate our output. Widgets can be very complex
180 // but as you can see here, they can also be very, very simple.
181 echo $before_widget . $before_title . $title . $after_title;
182
183 if($displayStyle=='dropdown') {
184 wp_theme_switcher('dropdown');
185 }
186 else {
187 wp_theme_switcher('list');
188 }
189
190 echo $after_widget;
191 }
192
193 function widget_themeswitcher_control() {
194
195 // Get our options and see if we're handling a form submission.
196 $options = get_option('widget_themeswitcher');
197 if ( !is_array($options) ) {
198 $options = array('Title'=>__('Theme Switcher', 'ts_theme_switcher'), 'displayStyle'=>'dropdown', 'displayName'=>'full');
199 update_option('widget_themeswitcher', $options);
200 }
201 if ( $_POST['themeswitcher-submit'] ) {
202
203 // Remember to sanitize and format use input appropriately.
204 $options['displayStyle'] = strip_tags(stripslashes($_POST['themeswitcher-display']));
205 $options['displayName'] = strip_tags(stripslashes($_POST['themeswitcher-name']));
206 $options['Title'] = $_POST['themeswitcher-title'];
207 update_option('widget_themeswitcher', $options);
208 }
209
210 // Be sure you format your options to be valid HTML attributes.
211 $displayStyle = htmlspecialchars($options['displayStyle'], ENT_QUOTES);
212 $displayName = htmlspecialchars($options['displayName'], ENT_QUOTES);
213
214 // Here is our little form segment. Notice that we don't need a
215 // complete form. This will be embedded into the existing form.
216 echo '<h4>'.__('Display style:', 'ts_theme_switcher').'</h4>';
217 echo '<p><label for="themeswitcher-title">'.__('Title', 'ts_theme_switcher').': </label>';
218 echo '<input id="themeswitcher-title" name="themeswitcher-title" type="text" value="'.$options['Title'].'" /></p>';
219 echo '<p><input id="themeswitcher-display0" name="themeswitcher-display" type="radio" value="list"';
220 if($displayStyle=='list') {
221 echo ' checked="checked"';
222 }
223 echo ' /><label for="themeswitcher-display0">'.__('List', 'ts_theme_switcher').'</label></p>';
224 echo '<p><input id="themeswitcher-display1" name="themeswitcher-display" type="radio" value="dropdown"';
225 if($displayStyle=='dropdown') {
226 echo ' checked="checked"';
227 }
228 echo ' /><label for="themeswitcher-display1">'.__('Drop-Down', 'ts_theme_switcher').'</label></p>';
229
230 echo '<p><label for="themeswitcher-name0">'.__('Display theme names in ', 'ts_theme_switcher').': </label>';
231 echo '<p><input id="themeswitcher-name0" name="themeswitcher-name" type="radio" value="full"';
232 if($displayName=='full') {
233 echo ' checked="checked"';
234 }
235 echo ' /><label for="themeswitcher-name1">'.__('full name (default option)', 'ts_theme_switcher').'</label></p>';
236 echo '<p><input id="themeswitcher-name1" name="themeswitcher-name" type="radio" value="short"';
237 if($displayName=='short') {
238 echo ' checked="checked"';
239 }
240 echo ' /><label for="themeswitcher-display1">'.__('short name (faster loading)', 'ts_theme_switcher').'</label></p>';
241 echo '<input type="hidden" id="themeswitcher-submit" name="themeswitcher-submit" value="1" />';
242 }
243
244
245 // This registers our widget so it appears with the other available
246 // widgets and can be dragged and dropped into any active sidebars.
247 register_sidebar_widget(__('Theme Switcher', 'ts_theme_switcher'), 'widget_themeswitcher');
248
249 // This registers our optional widget control form. Because of this
250 // our widget will have a button that reveals a 300x150 pixel form.
251 register_widget_control(__('Theme Switcher', 'ts_theme_switcher'), 'widget_themeswitcher_control', 300, 250);
252
253}
254
255// Run our code later in case this loads prior to any required plugins.
256add_action('plugins_loaded', 'widget_themeswitcher_init');
257
258// helper function to look only at folder names, not loading the style.css files for performance.
259function ts_get_all_themes() {
260 global $ts_themes, $ts_broken_themes;
261
262 if ( isset($ts_themes) )
263 return $ts_themes;
264
265 // check the option for displaying full names or short names
266 $options = get_option('widget_themeswitcher');
267 $displayName = htmlspecialchars($options['displayName'], ENT_QUOTES);
268 if($displayName=='full')
269 return get_themes();
270
271 $themes = array();
272 $ts_broken_themes = array();
273 $theme_loc = $theme_root = get_theme_root();
274 if ( '/' != ABSPATH ) // don't want to replace all forward slashes, see Trac #4541
275 $theme_loc = str_replace(ABSPATH, '', $theme_root);
276
277 // Files in wp-content/themes directory and one subdir down
278 $themes_dir = @ opendir($theme_root);
279 if ( !$themes_dir )
280 return false;
281
282 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
283 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
284 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
285 continue;
286 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
287 $found_stylesheet = false;
288 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
289 if ( $theme_file == 'style.css' ) {
290 $theme_files[] = $theme_dir . '/' . $theme_file;
291 $found_stylesheet = true;
292 break;
293 }
294 }
295 @closedir($stylish_dir);
296 if ( !$found_stylesheet ) { // look for themes in that dir
297 $subdir = "$theme_root/$theme_dir";
298 $subdir_name = $theme_dir;
299 $theme_subdir = @ opendir( $subdir );
300 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
301 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
302 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
303 continue;
304 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
305 $found_stylesheet = false;
306 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
307 if ( $theme_file == 'style.css' ) {
308 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
309 $found_stylesheet = true;
310 break;
311 }
312 }
313 @closedir($stylish_dir);
314 }
315 }
316 @closedir($theme_subdir);
317 $ts_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
318 }
319 }
320 }
321 @closedir($theme_dir);
322
323 if ( !$themes_dir || !$theme_files )
324 return $themes;
325
326 sort($theme_files);
327
328 foreach ( (array) $theme_files as $theme_file ) {
329 if ( !is_readable("$theme_root/$theme_file") ) {
330 $ts_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
331 continue;
332 }
333
334 $name = dirname($theme_file);
335 $title = dirname($theme_file);
336 $template = trim(dirname($theme_file));
337 $stylesheet = dirname($theme_file);
338
339 if ( !file_exists("$theme_root/$template/index.php") ) {
340 $parent_dir = dirname(dirname($theme_file));
341 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
342 $template = "$parent_dir/$template";
343 } else {
344 $ts_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
345 continue;
346 }
347 }
348
349 // Check for theme name collision. This occurs if a theme is copied to
350 // a new theme directory and the theme header is not updated. Whichever
351 // theme is first keeps the name. Subsequent themes get a suffix applied.
352 // The Default and Classic themes always trump their pretenders.
353 if ( isset($themes[$name]) ) {
354 if ( ('default' == $name || 'classic' == $name) &&
355 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
356 // If another theme has claimed to be one of our default themes, move
357 // them aside.
358 $suffix = $themes[$name]['Stylesheet'];
359 $new_name = "$name/$suffix";
360 $themes[$new_name] = $themes[$name];
361 $themes[$new_name]['Name'] = $new_name;
362 } else {
363 $name = "$name/$stylesheet";
364 }
365 }
366
367 $themes[$name] = array('Name' => $name, 'Title' => $title, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Status' => 'publish');
368 }
369
370 // Resolve theme dependencies.
371 $theme_names = array_keys($themes);
372
373 foreach ( (array) $theme_names as $theme_name ) {
374 $themes[$theme_name]['Parent Theme'] = '';
375 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
376 foreach ( (array) $theme_names as $parent_theme_name ) {
377 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
378 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
379 break;
380 }
381 }
382 }
383 }
384
385 $ts_themes = $themes;
386
387 return $themes;
388}
389
390// helper function to get the current theme/template from the database
391function ts_get_current_theme() {
392 $themes = ts_get_all_themes();
393 $theme_names = array_keys($themes);
394 $current_template = get_option('template');
395 $current_stylesheet = get_option('stylesheet');
396
397 // check the option for displaying full names or short names
398 $options = get_option('widget_themeswitcher');
399 $displayName = htmlspecialchars($options['displayName'], ENT_QUOTES);
400 if($displayName=='full')
401 $current_theme = 'WordPress Default';
402 else
403 $current_theme = 'default';
404
405 if ( $themes ) {
406 foreach ( (array) $theme_names as $theme_name ) {
407 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
408 $themes[$theme_name]['Template'] == $current_template ) {
409 $current_theme = $themes[$theme_name]['Name'];
410 break;
411 }
412 }
413 }
414
415 return $current_theme;
416}
417
418// helper function to replace get_theme()
419function ts_get_one_theme($theme) {
420 $themes = ts_get_all_themes();
421
422 if ( array_key_exists($theme, $themes) )
423 return $themes[$theme];
424
425 return NULL;
426}
427
428?>
Note: See TracBrowser for help on using the repository browser.