PluginProbe
Theme Switcha – Easily Switch Themes for Development and Testing / 2.6
Theme Switcha – Easily Switch Themes for Development and Testing v2.6
3.4.5 trunk 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.3.1 3.3.2 All 33 releases
theme-switcha / inc / plugin-core.php

plugin-core.php in Theme Switcha – Easily Switch Themes for Development and Testing 2.6, at inc/plugin-core.php

634 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // Theme Switcha - Core
2
3 if (!defined('ABSPATH')) exit;
4
5 function theme_switcha_toolbar_add_menu() {
6
7 global $theme_switcha_options, $wp_admin_bar;
8
9 if (!current_user_can('switch_themes') || !is_object($wp_admin_bar) || !function_exists('is_admin_bar_showing') || !is_admin_bar_showing()) return;
10
11 $options = $theme_switcha_options;
12
13 $enable = (isset($options['enable_plugin']) && !empty($options['enable_plugin'])) ? 1 : 0;
14
15 $toolbar = (isset($options['enable_toolbar']) && !empty($options['enable_toolbar'])) ? 1 : 0;
16
17 if ($enable && $toolbar) {
18
19 $text = __('Choose a theme..', 'theme-switcha');
20
21 $title = theme_switcha_display_dropdown($text);
22
23 $wp_admin_bar->add_menu(array('id' => 'theme-switcha', 'title' => __('Theme Switcha', 'theme-switcha'), 'href' => false));
24
25 $wp_admin_bar->add_menu(array('id' => 'theme-switcha-menu', 'parent' => 'theme-switcha', 'title' => $title, 'href' => false));
26
27 }
28
29 }
30
31 function theme_switcha_check_cookie() {
32
33 if (isset($_GET['theme-switch']) && !empty($_GET['theme-switch'])) {
34
35 global $theme_switcha_options;
36
37 $options = $theme_switcha_options;
38
39 $expire = time() + (int) $options['cookie_expire'];
40
41 $theme = stripslashes($_GET['theme-switch']);
42
43 $domain = sanitize_text_field($_SERVER['HTTP_HOST']);
44
45 // setcookie($name, $value, $expires, $path, $domain, $secure, $httponly)
46
47 setcookie('theme_switcha_theme_'. COOKIEHASH, $theme, $expire, COOKIEPATH, $domain, false, true);
48
49 if (isset($_GET['passkey']) && !empty($_GET['passkey'])) {
50
51 $passkey = stripslashes($_GET['passkey']);
52
53 setcookie('theme_switcha_passkey_'. COOKIEHASH, $passkey, $expire, COOKIEPATH, $domain, false, true);
54
55 }
56
57 $params = array('theme-switch', 'passkey');
58 $redirect = esc_url_raw(remove_query_arg($params));
59 wp_safe_redirect($redirect);
60
61 exit;
62
63 }
64
65 }
66
67 function theme_switcha_core($current, $key = 'Template') {
68
69 global $theme_switcha_options;
70
71 $options = $theme_switcha_options;
72
73 if (!$options['enable_plugin']) return $current;
74
75 if (!theme_switcha_check_permissions($options)) return $current;
76
77 if (isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) {
78
79 $theme = $_COOKIE['theme_switcha_theme_'. COOKIEHASH];
80
81 } else {
82
83 return $current;
84
85 }
86
87 if (isset($theme) && !empty($theme)) {
88
89 if ((!is_admin()) || (is_admin() && $options['enable_admin'])) {
90
91 $theme_data = wp_get_theme($theme);
92
93 if (!empty($theme_data)) {
94
95 $theme_status = (isset($theme_data['Status'])) ? $theme_data['Status'] : false;
96
97 if ($theme_status && ($theme_status !== 'publish') && ($theme_status !== 'admin-only')) return $current;
98
99 return (string) $theme_data[$key];
100
101 }
102
103 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => 0));
104
105 foreach ($themes as $theme_data) {
106
107 if ((string) $theme_data['Stylesheet'] === $theme) {
108
109 $theme_status = (isset($theme_data['Status'])) ? $theme_data['Status'] : false;
110
111 if ($theme_status && ($theme_status !== 'publish') && ($theme_status !== 'admin-only')) return $current;
112
113 return (string) $theme_data[$key];
114
115 }
116
117 }
118
119 }
120
121 }
122
123 return $current;
124
125 }
126
127 function theme_switcha_check_permissions($options) {
128
129 switch ($options['allowed_users']) {
130
131 case 'admin' :
132
133 if (current_user_can('switch_themes')) return true;
134
135 break;
136
137 case 'passkey' :
138
139 if (current_user_can('switch_themes')) return true;
140
141 if (isset($_COOKIE['theme_switcha_passkey_'. COOKIEHASH]) && $_COOKIE['theme_switcha_passkey_'. COOKIEHASH] === $options['passkey']) return true;
142
143 break;
144
145 case 'everyone' :
146
147 return true;
148
149 break;
150
151 }
152
153 return false;
154
155 }
156
157 function theme_switcha_filter_template($current) {
158
159 return theme_switcha_core($current, 'Template');
160
161 }
162
163 function theme_switcha_filter_stylesheet($current) {
164
165 return theme_switcha_core($current, 'Stylesheet');
166
167 }
168
169 function theme_switcha_add_filters() {
170
171 add_filter('template', 'theme_switcha_filter_template');
172
173 add_filter('stylesheet', 'theme_switcha_filter_stylesheet');
174
175 }
176
177 function theme_switcha_active_theme() {
178
179 $get_theme = wp_get_theme();
180
181 $active_theme = $get_theme->get('Name');
182
183 if (isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) {
184
185 $active_theme = $_COOKIE['theme_switcha_theme_'. COOKIEHASH];
186
187 }
188
189 return $active_theme;
190
191 }
192
193 function theme_switcha_get_theme_names() {
194
195 $blog_id = get_current_blog_id();
196
197 $blog_id = is_int($blog_id) ? $blog_id : 0;
198
199 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => $blog_id));
200
201 $theme_names = array_keys($themes);
202
203 $theme_names = array_map('strval', $theme_names);
204
205 natcasesort($theme_names);
206
207 return $theme_names;
208
209 }
210
211 function theme_switcha_truncate($string, $length = 10, $dots = '...') {
212
213 return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
214
215 }
216
217 function theme_switcha_check_enabled() {
218
219 global $theme_switcha_options;
220
221 $options = $theme_switcha_options;
222
223 $cookie_passkey = (isset($_COOKIE['theme_switcha_passkey_'. COOKIEHASH]) && $_COOKIE['theme_switcha_passkey_'. COOKIEHASH]) ? $_COOKIE['theme_switcha_passkey_'. COOKIEHASH] : null;
224
225 $switch_themes = (current_user_can('switch_themes')) ? true : false;
226
227 $allowed_users = (isset($options['allowed_users'])) ? $options['allowed_users'] : null;
228
229 $enable_plugin = (isset($options['enable_plugin']) && $options['enable_plugin']) ? true : false;
230
231 $enable_cookie = (($switch_themes) || (($allowed_users === 'passkey') && ($cookie_passkey === $options['passkey']))) ? true : false;
232
233 $enable_user = (($switch_themes) || ($enable_cookie) || ($allowed_users === 'everyone')) ? true : false;
234
235 $enabled = ($enable_plugin && $enable_user) ? true : false;
236
237 return $enabled;
238
239 }
240
241 function theme_switcha_display_themes() {
242
243 if (!is_admin()) return;
244
245 global $theme_switcha_options;
246
247 $options = $theme_switcha_options;
248
249 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => 0));
250
251 $themes = apply_filters('theme_switcha_themes', $themes);
252
253 $default_theme = wp_get_theme();
254
255 $default_screenshot = THEME_SWITCHA_URL .'img/screenshot.png';
256
257 $enable_admin = (isset($options['enable_admin']) && $options['enable_admin']) ? ' enable-admin' : '';
258
259 $enable_plugin = (isset($options['enable_plugin']) && $options['enable_plugin']) ? ' enable-plugin' : '';
260
261 $current_theme = (!empty($enable_plugin) && isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) ? $_COOKIE['theme_switcha_theme_'. COOKIEHASH] : $default_theme->Stylesheet;
262
263 $passkey = (isset($options['passkey'])) ? $options['passkey'] : '';
264
265 $base_url = trailingslashit(get_bloginfo('url'));
266
267 if (empty($enable_plugin)) return;
268
269 //
270
271 $output = '<div id="theme-switcha" class="theme-switcha-thumbs">';
272
273 foreach($themes as $theme) {
274
275 if (($theme->Status !== 'publish') && ($theme->Status !== 'admin-only')) continue;
276
277 $src = ($theme->get_screenshot()) ? $theme->get_screenshot() : $default_screenshot;
278
279 $dir = ($theme->get_stylesheet()) ? $theme->get_stylesheet() : get_stylesheet();
280
281 $params = array('theme-switch' => $dir, 'passkey' => $passkey);
282
283 $href = add_query_arg($params, $base_url);
284
285 $title = ($theme->Version !== '') ? esc_attr__('Version ', 'theme-switcha') . $theme->Version .' : ' : '';
286
287 $title .= theme_switcha_truncate($theme->Description, 120);
288
289 $name = ($theme->Name !== '') ? $theme->Name : esc_attr__('Untitled', 'theme-switcha');
290
291 $text = theme_switcha_truncate($name, 20);
292
293 $active = ((string) $theme->Stylesheet === $current_theme) ? ' theme-active' : '';
294
295 $admin = ($theme->Name === $default_theme->Name) ? ' <span class="theme-admin">'. esc_html__('Admin Theme', 'theme-switcha') .'</span>' : '';
296
297 $parent = ($theme->parent() && $theme->parent()->Name !== '') ? ' <span class="theme-child">'. esc_html__('Child Theme', 'theme-switcha') .'</span>' : '';
298
299 $output .= '<a target="_blank" rel="noopener noreferrer" class="theme-screenshot theme-'. $dir . $enable_plugin . $enable_admin . $active .'" href="'. esc_url($href) .'" title="'. esc_attr($title) .'" data-switched="'. esc_attr($name) .'">';
300
301 $output .= '<img src="'. esc_url($src) .'" alt="" />'. esc_html($text) . $admin . $parent .'</a>';
302
303 }
304
305 $output .= '</div>';
306
307 return $output;
308
309 }
310
311 function theme_switcha_display_thumbs() {
312
313 global $theme_switcha_options;
314
315 $options = $theme_switcha_options;
316
317 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => 0));
318
319 $themes = apply_filters('theme_switcha_themes', $themes);
320
321 $default_theme = wp_get_theme();
322
323 $default_screenshot = THEME_SWITCHA_URL .'img/screenshot.png';
324
325 $enable_plugin = (isset($options['enable_plugin']) && $options['enable_plugin']) ? ' enable-plugin' : '';
326
327 $current_theme = (!empty($enable_plugin) && isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) ? $_COOKIE['theme_switcha_theme_'. COOKIEHASH] : $default_theme->Stylesheet;
328
329 $base_url = get_permalink();
330
331 if (empty($enable_plugin)) return;
332
333 //
334
335 $output = '<div id="theme-switcha" class="theme-switcha-thumbs">';
336
337 $output .= (!theme_switcha_check_enabled()) ? '<p>'. esc_html__('Theme switching currently disabled.', 'theme-switcha') .'</p>' : '';
338
339 foreach($themes as $theme) {
340
341 if (($theme->Status !== 'publish') && ($theme->Status !== 'admin-only')) continue;
342
343 if ((!current_user_can('switch_themes')) && ($theme->Status === 'admin-only')) continue;
344
345 $src = ($theme->get_screenshot()) ? $theme->get_screenshot() : $default_screenshot;
346
347 $dir = ($theme->get_stylesheet()) ? $theme->get_stylesheet() : get_stylesheet();
348
349 $params = array('theme-switch' => $dir);
350
351 $href = add_query_arg($params, $base_url);
352
353 $title = ($theme->Version !== '') ? esc_attr__('Version ', 'theme-switcha') . $theme->Version .' : ' : '';
354
355 $title .= theme_switcha_truncate($theme->Description, 120);
356
357 $name = ($theme->Name !== '') ? $theme->Name : esc_attr__('Untitled', 'theme-switcha');
358
359 $text = theme_switcha_truncate($name, 20);
360
361 $active = ((string) $theme->Stylesheet === $current_theme) ? ' theme-active' : '';
362
363 $parent = ($theme->parent() && $theme->parent()->Name !== '') ? ' <span class="theme-child">'. esc_html__('Child Theme', 'theme-switcha') .'</span>' : '';
364
365 $output .= '<a class="theme-screenshot theme-'. $dir . $active .'" href="'. esc_url($href) .'" title="'. esc_attr($title) .'">';
366
367 $output .= '<img src="'. esc_url($src) .'" alt="" />'. esc_html($text) . $parent .'</a>';
368
369 }
370
371 $output .= '</div>';
372
373 return $output;
374
375 }
376
377 function theme_switcha_frontend_thumb_styles() {
378
379 $styles = '.theme-switcha-thumbs{font-family:sans-serif;text-align:center}';
380 $styles .= '.theme-screenshot[style]:link,.theme-screenshot[style]:visited{color:#efefef!important}';
381 $styles .= '.theme-screenshot[style]:hover,.theme-screenshot[style]:active,.theme-screenshot[style]:focus{color:#fff!important}';
382 $styles .= '.theme-switcha-thumbs{margin-top:30px}.theme-screenshot:link,.theme-screenshot:visited{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;position:relative;display:inline-block;width:200px;height:auto;margin:0 10px 10px 0;padding:10px;font-size:12px;line-height:18px;text-decoration:none;text-align:center;cursor:pointer;border:0;border-radius:2px;color:#efefef;background-color:#555;opacity:.8;-webkit-transition:opacity .2s ease-in-out;-moz-transition:opacity .2s ease-in-out;transition:opacity .2s ease-in-out}.theme-screenshot:active,.theme-screenshot:focus,.theme-screenshot:hover{opacity:1;color:#fff}.theme-active:link,.theme-active:visited{background-color:#696}.theme-screenshot img{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;display:block;width:100%;height:auto;margin:0 0 8px;padding:0;border-radius:2px;border:1px solid #777;box-shadow:0 0 10px 0 rgba(0,0,0,.7)}.theme-admin,.theme-child{position:absolute;left:0;width:100%;height:50px;line-height:50px}.theme-admin{top:0;background-color:rgba(0,0,0,.7)}.theme-child{top:50px;background-color:rgba(153,51,51,.7)}';
383
384 $styles = apply_filters('theme_switcha_styles_thumb', $styles);
385
386 return '<style type="text/css">'. $styles .'</style>';
387
388 }
389
390 function theme_switcha_display_thumbs_shortcode($attr, $content = null) {
391
392 extract(shortcode_atts(array(
393 'style' => 'true',
394 ), $attr));
395
396 $output = theme_switcha_display_thumbs();
397
398 if ($style === 'true') {
399
400 $styles = theme_switcha_frontend_thumb_styles();
401
402 $output = $styles . $output;
403
404 }
405
406 return $output;
407
408 }
409 add_shortcode('theme_switcha_thumbs', 'theme_switcha_display_thumbs_shortcode');
410
411 function theme_switcha_display_list($display) {
412
413 global $theme_switcha_options;
414
415 $options = $theme_switcha_options;
416
417 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => 0));
418
419 $themes = apply_filters('theme_switcha_themes', $themes);
420
421 $default_theme = wp_get_theme();
422
423 $enable_plugin = (isset($options['enable_plugin']) && $options['enable_plugin']) ? ' enable-plugin' : '';
424
425 $current_theme = (!empty($enable_plugin) && isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) ? $_COOKIE['theme_switcha_theme_'. COOKIEHASH] : $default_theme->Stylesheet;
426
427 $base_url = get_permalink();
428
429 if (empty($enable_plugin)) return;
430
431 //
432
433 $output = (!theme_switcha_check_enabled()) ? '<p>'. esc_html__('Theme switching currently disabled.', 'theme-switcha') .'</p>' : '';
434
435 $output .= '<ul id="theme-switcha" class="theme-switcha-'. $display .'">';
436
437 foreach($themes as $theme) {
438
439 if (($theme->Status !== 'publish') && ($theme->Status !== 'admin-only')) continue;
440
441 if ((!current_user_can('switch_themes')) && ($theme->Status === 'admin-only')) continue;
442
443 $dir = ($theme->get_stylesheet()) ? $theme->get_stylesheet() : get_stylesheet();
444
445 $params = array('theme-switch' => $dir);
446
447 $href = add_query_arg($params, $base_url);
448
449 $title = ($theme->Version !== '') ? esc_attr__('Version ', 'theme-switcha') . $theme->Version .' : ' : '';
450
451 $title .= theme_switcha_truncate($theme->Description, 120);
452
453 $text = ($theme->Name !== '') ? $theme->Name : esc_attr__('Untitled', 'theme-switcha');
454
455 $active = ((string) $theme->Stylesheet === $current_theme) ? ' active-theme' : '';
456
457 $output .= '<li><a class="theme-'. $dir . $active .'" href="'. esc_url($href) .'" title="'. esc_attr($title) .'">'. esc_html($text) .'</a></li>';
458
459 }
460
461 $output .= '</ul>';
462
463 return $output;
464 }
465
466 function theme_switcha_frontend_list_styles($display) {
467
468 if ($display === 'list') {
469
470 $styles = '.active-theme{font-weight:bold}';
471
472 } else {
473
474 $styles = '.theme-switcha-list{margin-left:0;padding:0}.theme-switcha-list li{display:inline-block;margin:0 5px 5px 0}.theme-switcha-list a:link,.theme-switcha-list a:visited{display:inline-block;padding:5px 10px;border:1px solid #fbf0cb;border-radius:2px;color:#777;background-color:#fefaed;text-decoration:none}.theme-switcha-list a:hover,.theme-switcha-list a:active,.theme-switcha-list a:focus{color:#777;background-color:#fbf0cb}';
475
476 }
477
478 $styles = apply_filters('theme_switcha_styles_list', $styles);
479
480 return '<style type="text/css">'. $styles .'</style>';
481
482 }
483
484 function theme_switcha_display_list_shortcode($attr, $content = null) {
485
486 extract(shortcode_atts(array(
487 'display' => 'list',
488 'style' => 'true',
489 ), $attr));
490
491 $output = theme_switcha_display_list($display);
492
493 if ($style === 'true') {
494
495 $styles = theme_switcha_frontend_list_styles($display);
496
497 $output = $styles . $output;
498
499 }
500
501 return $output;
502
503 }
504 add_shortcode('theme_switcha_list', 'theme_switcha_display_list_shortcode');
505
506 function theme_switcha_display_dropdown($text, $widget = false) {
507
508 global $theme_switcha_options;
509
510 $options = $theme_switcha_options;
511
512 $themes = wp_get_themes(array('errors' => false , 'allowed' => true, 'blog_id' => 0));
513
514 $themes = apply_filters('theme_switcha_themes', $themes);
515
516 $default_theme = wp_get_theme();
517
518 $enable_plugin = (isset($options['enable_plugin']) && $options['enable_plugin']) ? ' enable-plugin' : '';
519
520 $current_theme = (!empty($enable_plugin) && isset($_COOKIE['theme_switcha_theme_'. COOKIEHASH])) ? $_COOKIE['theme_switcha_theme_'. COOKIEHASH] : $default_theme->Stylesheet;
521
522 $protocol = is_ssl() ? 'https://' : 'http://';
523
524 $base_url = esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
525
526 if (strpos($base_url, '/wp-admin/') !== false) $base_url = get_home_url();
527
528 if (empty($enable_plugin)) return;
529
530 //
531
532 $output = (!theme_switcha_check_enabled()) ? '<p>'. esc_html__('Theme switching currently disabled.', 'theme-switcha') .'</p>' : '';
533
534 if ($widget) {
535
536 $output .= '<select id="theme-switcha" class="theme-switcha-dropdown" onChange="window.open( this.options[ this.selectedIndex ].value, \'_blank\');">';
537
538 } else {
539
540 $output .= '<select id="theme-switcha" class="theme-switcha-dropdown" onChange="window.document.location.href=this.options[this.selectedIndex].value;">';
541
542 }
543
544 //
545
546 $output .= ($text !== 'disable') ? '<option value="'. esc_url($base_url) .'">'. $text .'</option>' : '';
547
548 foreach($themes as $theme) {
549
550 if (($theme->Status !== 'publish') && ($theme->Status !== 'admin-only')) continue;
551
552 if ((!current_user_can('switch_themes')) && ($theme->Status === 'admin-only')) continue;
553
554 $dir = ($theme->get_stylesheet()) ? $theme->get_stylesheet() : get_stylesheet();
555
556 $params = array('theme-switch' => $dir);
557
558 $href = add_query_arg($params, $base_url);
559
560 $text = ($theme->Name !== '') ? $theme->Name : esc_attr__('Untitled', 'theme-switcha');
561
562 $active = ((string) $theme->Stylesheet === $current_theme) ? ' selected="selected"' : '';
563
564 $output .= '<option value="'. esc_url($href) .'"'. $active .'>'. esc_html($text) .'</option>';
565
566 }
567
568 $output .= '</select>';
569
570 return $output;
571 }
572
573 function theme_switcha_display_dropdown_shortcode($attr, $content = null) {
574
575 extract(shortcode_atts(array(
576 'text' => 'Choose a theme..',
577 ), $attr));
578
579 $output = theme_switcha_display_dropdown($text);
580
581 return $output;
582
583 }
584 add_shortcode('theme_switcha_select', 'theme_switcha_display_dropdown_shortcode');
585
586 function theme_switcha_display_dropdown_echo() {
587
588 $text = __('Choose a theme..', 'theme-switcha');
589
590 echo theme_switcha_display_dropdown($text, true);
591
592 }
593
594 function theme_switcha_dashboard_widget() {
595
596 global $theme_switcha_options;
597
598 $enable = isset($theme_switcha_options['enable_plugin']) ? $theme_switcha_options['enable_plugin'] : 0;
599
600 if ($enable && theme_switcha_check_permissions($theme_switcha_options)) {
601
602 wp_add_dashboard_widget('theme_switcha_dashboard_widget', __('Theme Switcha', 'theme-switcha'), 'theme_switcha_display_dropdown_echo');
603
604 }
605
606 }
607
608 function theme_switcha_display_text_link($attr, $content = null) {
609
610 global $theme_switcha_options;
611
612 $options = $theme_switcha_options;
613
614 $passkey = isset($options['passkey']) ? $options['passkey'] : null;
615
616 $protocol = is_ssl() ? 'https://' : 'http://';
617
618 $base_url = esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
619
620 if (strpos($base_url, '/wp-admin/') !== false) $base_url = get_home_url();
621
622 extract(shortcode_atts(array('theme' => theme_switcha_active_theme(), 'text' => 'Switch Theme'), $attr));
623
624 $params = array('theme-switch' => $theme, 'passkey' => $passkey);
625
626 $href = add_query_arg($params, $base_url);
627
628 $output = '<a href="'. esc_url($href) .'">'. esc_html($text) .'</a>';
629
630 return $output;
631
632 }
633 add_shortcode('theme_switcha_link', 'theme_switcha_display_text_link');
634