PluginProbe
Multi Device Switcher / trunk
Multi Device Switcher vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 All 34 releases
multi-device-switcher / multi-device-switcher.php

multi-device-switcher.php in Multi Device Switcher trunk, at multi-device-switcher.php

1,572 lines 42.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Multi Device Switcher
4 * Plugin URI: https://github.com/thingsym/multi-device-switcher
5 * Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
6 * Version: 1.8.7
7 * Author: thingsym
8 * Author URI: https://www.thingslabo.com/
9 * License: GPLv2 or later
10 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11 * Text Domain: multi-device-switcher
12 * Domain Path: /languages/
13 *
14 * @package Multi_Device_Switcher
15 */
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Core class Multi_Device_Switcher
23 *
24 * @since 1.0.0
25 */
26 class Multi_Device_Switcher {
27
28 /**
29 * Public variable.
30 *
31 * @access public
32 *
33 * @var string $option_group The group name of option
34 */
35 public $option_group = 'multi_device_switcher';
36
37 /**
38 * Public variable.
39 *
40 * @access public
41 *
42 * @var string $option_name The option name
43 */
44 public $option_name = 'multi_device_switcher_options';
45
46 /**
47 * Public variable.
48 *
49 * @access public
50 *
51 * @var string $capability The types of capability
52 */
53 public $capability = 'switch_themes';
54
55 /**
56 * Public variable.
57 *
58 * @access public
59 *
60 * @var string $cookie_name_multi_device_switcher
61 */
62 public $cookie_name_multi_device_switcher = 'multi-device-switcher';
63
64 /**
65 * Public variable.
66 *
67 * @access public
68 *
69 * @var string $cookie_name_disable_switcher
70 */
71 public $cookie_name_disable_switcher = 'disable-switcher';
72
73 /**
74 * Public variable.
75 *
76 * @access public
77 *
78 * @var string $cookie_name_pc_switcher
79 */
80 public $cookie_name_pc_switcher = 'pc-switcher';
81
82 /**
83 * Public variable.
84 *
85 * @access public
86 *
87 * @var array $default_options {
88 * default options
89 *
90 * @type bool pc_switcher
91 * @type bool default_css
92 * @type string theme_smartphone
93 * @type string theme_tablet
94 * @type string theme_mobile
95 * @type string theme_game
96 * @type string userAgent_smart
97 * @type string userAgent_tablet
98 * @type string userAgent_mobile
99 * @type string userAgent_game
100 * @type string disable_path
101 * @type bool enable_regex
102 * }
103 *
104 * @since 1.7.0
105 */
106 public $default_options = array(
107 'pc_switcher' => 1,
108 'default_css' => 1,
109 'theme_smartphone' => 'None',
110 'theme_tablet' => 'None',
111 'theme_mobile' => 'None',
112 'theme_game' => 'None',
113 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko',
114 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko',
115 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
116 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox',
117 'disable_path' => '',
118 'enable_regex' => 0,
119 );
120
121 /**
122 * Public variable.
123 *
124 * @access public
125 *
126 * @var string $device
127 */
128 public $device = '';
129
130 /**
131 * Public variable.
132 *
133 * @access public
134 *
135 * @var array|null $plugin_data
136 */
137 public $plugin_data = array();
138
139 /**
140 * Constructor
141 *
142 * @access public
143 *
144 * @since 1.0.0
145 */
146 public function __construct() {
147 add_action( 'init', array( $this, 'load_plugin_data' ) );
148 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
149 add_action( 'plugins_loaded', array( $this, 'init' ) );
150
151 if ( ! is_admin() ) {
152 add_filter( 'wp_headers', array( $this, 'add_header_vary' ) );
153 add_action( 'plugins_loaded', array( $this, 'detect_device' ) );
154 add_action( 'plugins_loaded', array( $this, 'switch_theme' ) );
155 }
156
157 add_action( 'admin_init', array( $this, 'register_settings' ) );
158 add_action( 'admin_menu', array( $this, 'add_option_page' ) );
159 add_action( 'customize_register', array( $this, 'customizer' ) );
160 add_action( 'plugins_loaded', array( $this, 'load_file' ) );
161 }
162
163 /**
164 * Initialize.
165 *
166 * Hooks to plugins_loaded
167 *
168 * @access public
169 *
170 * @since 1.6.0
171 */
172 public function init() {
173 add_filter( 'option_page_capability_' . $this->option_group, array( $this, 'option_page_capability' ) );
174 add_filter( 'plugin_action_links_' . plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ), array( $this, 'plugin_action_links' ) );
175 add_filter( 'plugin_row_meta', array( $this, 'plugin_metadata_links' ), 10, 2 );
176
177 add_shortcode( 'multi', array( $this, 'shortcode_display_switcher' ) );
178 }
179
180 /**
181 * Detect device.
182 *
183 * @access public
184 *
185 * @return void
186 *
187 * @since 1.7.0
188 */
189 public function detect_device() {
190 if ( isset( $_COOKIE[ $this->cookie_name_disable_switcher ] ) ) {
191 add_action( 'wp_headers', array( $this, 'set_cookie_rest_disable_switcher' ) );
192 }
193
194 if ( $this->is_disable_switcher() ) {
195 add_action( 'wp_headers', array( $this, 'set_cookie_enable_disable_switcher' ) );
196 return;
197 }
198
199 add_action( 'init', array( $this, 'session' ) );
200
201 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
202 $server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
203 $user_agent = $this->get_options_user_agent();
204
205 foreach ( array_reverse( $user_agent ) as $key => $val ) {
206 if ( ! preg_match( '/^custom_switcher_/', $key ) ) {
207 continue;
208 }
209 if ( $user_agent[ $key ] && preg_match( '/' . implode( '|', $user_agent[ $key ] ) . '/i', $server_ua ) ) {
210 $this->device = $key;
211 break;
212 }
213 }
214
215 if ( ! $this->device ) {
216 if ( $user_agent['game'] && preg_match( '/' . implode( '|', $user_agent['game'] ) . '/i', $server_ua ) ) {
217 $this->device = 'game';
218 }
219 elseif ( $user_agent['tablet'] && preg_match( '/' . implode( '|', $user_agent['tablet'] ) . '/i', $server_ua ) ) {
220 $this->device = 'tablet';
221 }
222 elseif ( $user_agent['smart'] && preg_match( '/' . implode( '|', $user_agent['smart'] ) . '/i', $server_ua ) ) {
223 $this->device = 'smart';
224 }
225 elseif ( $user_agent['mobile'] && preg_match( '/' . implode( '|', $user_agent['mobile'] ) . '/i', $server_ua ) ) {
226 $this->device = 'mobile';
227 }
228 }
229
230 /**
231 * Action hook: multi_device_switcher/detect_device.
232 *
233 * @since 1.7.0
234 */
235 do_action( 'multi_device_switcher/detect_device' );
236 }
237
238 /**
239 * Switch theme.
240 *
241 * @access public
242 *
243 * @return void
244 *
245 * @since 1.0.0
246 */
247 public function switch_theme() {
248 if ( $this->device ) {
249 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
250 add_filter( 'template', array( $this, 'get_template' ) );
251 add_action( 'wp_footer', array( $this, 'add_pc_switcher' ) );
252 add_action( 'wp_headers', array( $this, 'set_cookie_switch_theme' ) );
253 }
254 else {
255 add_action( 'wp_headers', array( $this, 'set_cookie_normal_theme' ) );
256 }
257
258 if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
259 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
260 remove_filter( 'template', array( $this, 'get_template' ) );
261 }
262 }
263
264 /**
265 * Gets UserAgents.
266 *
267 * @access public
268 *
269 * @return array
270 *
271 * @since 1.0.0
272 */
273 public function get_options_user_agent() {
274 $options = $this->get_options();
275
276 $user_agent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'], -1, PREG_SPLIT_NO_EMPTY );
277 $user_agent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'], -1, PREG_SPLIT_NO_EMPTY );
278 $user_agent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'], -1, PREG_SPLIT_NO_EMPTY );
279 $user_agent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'], -1, PREG_SPLIT_NO_EMPTY );
280
281 foreach ( (array) $options as $key => $val ) {
282 if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) {
283 continue;
284 }
285
286 $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key );
287
288 $user_agent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val, -1, PREG_SPLIT_NO_EMPTY );
289 }
290
291 return $user_agent;
292 }
293
294 /**
295 * Gets stylesheet.
296 *
297 * @access public
298 *
299 * @param string $stylesheet
300 *
301 * @return string
302 *
303 * @since 1.0.0
304 */
305 public function get_stylesheet( $stylesheet = '' ) {
306 $name = $this->get_device_theme();
307
308 if ( empty( $name ) ) {
309 return $stylesheet;
310 }
311
312 $themes = wp_get_themes();
313 foreach ( $themes as $t ) {
314 if ( $name === $t->get( 'Name' ) ) {
315 $theme = $t;
316 break;
317 }
318 }
319
320 if ( empty( $theme ) ) {
321 return $stylesheet;
322 }
323
324 if ( 'publish' !== $theme->get( 'Status' ) ) {
325 return $stylesheet;
326 }
327
328 return $theme['Stylesheet'];
329 }
330
331 /**
332 * Gets template.
333 *
334 * @access public
335 *
336 * @param string $template
337 *
338 * @return string
339 *
340 * @since 1.0.0
341 */
342 public function get_template( $template = '' ) {
343 $name = $this->get_device_theme();
344
345 if ( empty( $name ) ) {
346 return $template;
347 }
348
349 $themes = wp_get_themes();
350 foreach ( $themes as $t ) {
351 if ( $name === $t->get( 'Name' ) ) {
352 $theme = $t;
353 break;
354 }
355 }
356
357 if ( empty( $theme ) ) {
358 return $template;
359 }
360
361 if ( 'publish' !== $theme->get( 'Status' ) ) {
362 return $template;
363 }
364
365 return $theme['Template'];
366 }
367
368 /**
369 * Gets template by device.
370 *
371 * @access public
372 *
373 * @return string
374 *
375 * @since 1.0.0
376 */
377 public function get_device_theme() {
378 $options = $this->get_options();
379
380 if ( 'smart' === $this->device ) {
381 return $options['theme_smartphone'];
382 }
383 elseif ( 'tablet' === $this->device ) {
384 return $options['theme_tablet'];
385 }
386 elseif ( 'mobile' === $this->device ) {
387 return $options['theme_mobile'];
388 }
389 elseif ( 'game' === $this->device ) {
390 return $options['theme_game'];
391 }
392 else {
393 foreach ( (array) $options as $key => $val ) {
394 if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
395 continue;
396 }
397
398 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
399
400 if ( 'custom_switcher_' . $custom_switcher_name === $this->device ) {
401 return $options[ $key ];
402 }
403 }
404 }
405
406 return '';
407 }
408
409 /**
410 * Reset cookie for disable_switcher.
411 *
412 * @access public
413 *
414 * @return void
415 *
416 * @since 1.0.0
417 */
418 public function set_cookie_rest_disable_switcher() {
419 setcookie( $this->cookie_name_disable_switcher, '', time() - 3600, '/', '', is_ssl(), false );
420 }
421
422 /**
423 * Set enable to cookie for disable_switcher.
424 *
425 * @access public
426 *
427 * @return void
428 *
429 * @since 1.0.0
430 */
431 public function set_cookie_enable_disable_switcher() {
432 setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
433 setcookie( $this->cookie_name_disable_switcher, '1', 0, '/', '', is_ssl(), false );
434 }
435
436 /**
437 * Set switched theme to cookie.
438 *
439 * @access public
440 *
441 * @return void
442 *
443 * @since 1.0.0
444 */
445 public function set_cookie_switch_theme() {
446 $device = preg_replace( '/^custom_switcher_/', '', $this->device );
447 setcookie( $this->cookie_name_multi_device_switcher, (string) $device, 0, '/', '', is_ssl(), false );
448 }
449
450 /**
451 * Reset cookie for normal theme.
452 *
453 * @access public
454 *
455 * @return void
456 *
457 * @since 1.0.0
458 */
459 public function set_cookie_normal_theme() {
460 setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
461 }
462
463 /**
464 * Set session to cookie for pc switcher.
465 *
466 * @access public
467 *
468 * @return void
469 *
470 * @since 1.0.0
471 */
472 public function session() {
473 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
474 if ( isset( $_GET['pc-switcher'] ) ) {
475 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
476 setcookie( $this->cookie_name_pc_switcher, sanitize_text_field( wp_unslash( $_GET['pc-switcher'] ) ) ? '1' : '', 0, '/', '', is_ssl(), false );
477
478 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
479 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
480 $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
481 }
482
483 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
484 unset( $_GET['pc-switcher'] );
485 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
486 if ( ! empty( $_GET ) ) {
487 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
488 $uri = $uri . '?' . http_build_query( $_GET );
489 }
490
491 wp_safe_redirect( esc_url( $uri ) );
492 exit;
493 }
494 }
495
496 /**
497 * Enqueue styles for default_css
498 *
499 * @access public
500 *
501 * @return void
502 *
503 * @since 1.8.5
504 */
505 public function enqueue_styles() {
506 wp_enqueue_style(
507 'pc-switcher-options',
508 plugins_url() . '/multi-device-switcher/pc-switcher.css',
509 array(),
510 $this->plugin_data['Version'],
511 'all'
512 );
513 }
514
515 /**
516 * Add pc switcher button.
517 *
518 * @access public
519 *
520 * @param bool $pc_switcher
521 *
522 * @return void
523 *
524 * @since 1.0.0
525 */
526 public function add_pc_switcher( $pc_switcher = 0 ) {
527 $options = $this->get_options();
528 $name = $this->get_device_theme();
529
530 if ( $options['pc_switcher'] ) {
531 $pc_switcher = 1;
532 }
533
534 if ( $pc_switcher && $name && 'None' !== $name ) {
535 if ( $options['default_css'] ) {
536 $this->enqueue_styles();
537 }
538
539 $uri = is_ssl() ? 'https://' : 'http://';
540
541 if ( isset( $_SERVER['HTTP_HOST'] ) ) {
542 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
543 $uri .= $_SERVER['HTTP_HOST'];
544 }
545
546 if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
547 $uri .= add_query_arg( 'pc-switcher', 0 );
548 ?>
549 <div class="pc-switcher"><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'Mobile', 'multi-device-switcher' ); ?></a><span class="active"><?php esc_html_e( 'PC', 'multi-device-switcher' ); ?></span></div>
550 <?php
551 }
552 else {
553 $uri .= add_query_arg( 'pc-switcher', 1 );
554 ?>
555 <div class="pc-switcher"><span class="active"><?php esc_html_e( 'Mobile', 'multi-device-switcher' ); ?></span><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'PC', 'multi-device-switcher' ); ?></a></div>
556 <?php
557 }
558 }
559 }
560
561 /**
562 * Check device.
563 *
564 * @access public
565 *
566 * @param string $device
567 *
568 * @return bool
569 *
570 * @since 1.0.0
571 */
572 public function is_multi_device( $device = '' ) {
573 if ( $device === $this->device ) {
574 return true;
575 }
576 if ( 'custom_switcher_' . $device === $this->device ) {
577 return true;
578 }
579
580 return false;
581 }
582
583 /**
584 * Whether pc switcher enabled.
585 *
586 * @access public
587 *
588 * @return bool
589 *
590 * @since 1.0.0
591 */
592 public function is_pc_switcher() {
593 return isset( $_COOKIE[ $this->cookie_name_pc_switcher ] );
594 }
595
596 /**
597 * Whether theme switch disabled.
598 *
599 * @access public
600 *
601 * @param bool $disable
602 *
603 * @return bool
604 *
605 * @since 1.0.0
606 */
607 public function is_disable_switcher( $disable = false ) {
608 $options = $this->get_options();
609 $disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY );
610
611 if ( isset( $_SERVER['REQUEST_URI'] ) ) {
612 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
613 $request_uri = $_SERVER['REQUEST_URI'];
614 }
615
616 foreach ( (array) $disable_path as $path ) {
617 if ( $options['enable_regex'] ) {
618 if ( preg_match( '/' . $path . '/i', $request_uri ) ) {
619 $disable = true;
620 break;
621 }
622 }
623 // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
624 else {
625 if ( preg_match( '/^' . preg_quote( (string) $path, '/' ) . '$/i', $request_uri ) ) {
626 $disable = true;
627 break;
628 }
629 }
630 }
631
632 return $disable;
633 }
634
635 /**
636 * Shortcode.
637 *
638 * @access public
639 *
640 * @param array $atts
641 * @param string $content
642 *
643 * @return string
644 *
645 * @since 1.0.0
646 */
647 public function shortcode_display_switcher( $atts, $content = '' ) {
648 $atts = shortcode_atts(
649 array(
650 'device' => '',
651 ),
652 $atts
653 );
654
655 if ( empty( $atts['device'] ) && ( $this->is_multi_device( $atts['device'] ) || $this->is_pc_switcher() ) ) {
656 return $content;
657 }
658 elseif ( ! empty( $atts['device'] ) && $this->is_multi_device( $atts['device'] ) && ! $this->is_pc_switcher() ) {
659 return $content;
660 }
661
662 return '';
663 }
664
665 /**
666 * Add HTTP Vary header.
667 *
668 * @access public
669 *
670 * @param string $headers
671 *
672 * @return string
673 *
674 * @since 1.1.1
675 */
676 public function add_header_vary( $headers ) {
677 /**
678 * Filter hook: multi_device_switcher/add_header_vary.
679 *
680 * @param string 'User-Agent' header name.
681 *
682 * @since 1.6.2
683 */
684 $headers['Vary'] = apply_filters( 'multi_device_switcher/add_header_vary', 'User-Agent' );
685 return $headers;
686 }
687
688 /**
689 * Enqueue scripts.
690 *
691 * Hooks to admin_enqueue_scripts.
692 *
693 * @access public
694 *
695 * @since 1.0.0
696 */
697 public function admin_enqueue_scripts() {
698 wp_enqueue_script(
699 'multi-device-switcher-options',
700 plugins_url() . '/multi-device-switcher/multi-device-switcher.js',
701 array( 'jquery', 'jquery-ui-tabs' ),
702 $this->plugin_data['Version'],
703 false
704 );
705 }
706
707 /**
708 * Enqueue styles.
709 *
710 * Hooks to admin_enqueue_styles.
711 *
712 * @access public
713 *
714 * @since 1.0.0
715 */
716 public function admin_enqueue_styles() {
717 wp_enqueue_style(
718 'multi-device-switcher-options',
719 plugins_url() . '/multi-device-switcher/multi-device-switcher.css',
720 array(),
721 $this->plugin_data['Version'],
722 'all'
723 );
724 }
725
726 /**
727 * Register the form setting.
728 *
729 * Hooks to admin_init.
730 *
731 * @access public
732 *
733 * @return void
734 *
735 * @since 1.0
736 */
737 public function register_settings() {
738 if ( is_null( $this->get_options() ) ) {
739 add_option( $this->option_name );
740 }
741
742 register_setting(
743 $this->option_group,
744 $this->option_name,
745 array( $this, 'validate_options' )
746 );
747 }
748
749 /**
750 * Returns capability.
751 *
752 * @access public
753 *
754 * @return string
755 *
756 * @since 1.0.0
757 */
758 public function option_page_capability() {
759 return $this->capability;
760 }
761
762 /**
763 * Adds option page.
764 *
765 * @access public
766 *
767 * @return void
768 *
769 * @since 1.0.0
770 */
771 public function add_option_page() {
772 $page_hook = add_theme_page(
773 __( 'Multi Device Switcher', 'multi-device-switcher' ),
774 __( 'Multi Device Switcher', 'multi-device-switcher' ),
775 $this->option_page_capability(),
776 'multi-device-switcher',
777 array( $this, 'render_option_page' )
778 );
779
780 if ( ! $page_hook ) {
781 return;
782 }
783
784 add_action( 'load-' . $page_hook, array( $this, 'page_hook_suffix' ) );
785 }
786
787 /**
788 * Page Hook Suffix.
789 *
790 * Hooks to load-{$page_hook}.
791 *
792 * @access public
793 *
794 * @return void
795 *
796 * @since 1.2.4
797 */
798 public function page_hook_suffix() {
799 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
800 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
801 }
802
803 /**
804 * Set link to customizer section on the plugins page.
805 *
806 * Hooks to plugin_action_links_{$plugin_file}
807 *
808 * @see https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
809 *
810 * @access public
811 *
812 * @param array $links An array of plugin action links.
813 *
814 * @return array $links
815 *
816 * @since 1.6.0
817 */
818 public function plugin_action_links( $links = array() ) {
819 $settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', 'multi-device-switcher' ) . '</a>';
820
821 array_unshift( $links, $settings_link );
822
823 return $links;
824 }
825
826 /**
827 * Set links below a plugin on the Plugins page.
828 *
829 * Hooks to plugin_row_meta
830 *
831 * @see https://developer.wordpress.org/reference/hooks/plugin_row_meta/
832 *
833 * @access public
834 *
835 * @param array $links An array of the plugin's metadata.
836 * @param string $file Path to the plugin file relative to the plugins directory.
837 *
838 * @return array $links
839 *
840 * @since 1.8.1
841 */
842 public function plugin_metadata_links( $links, $file ) {
843 if ( $file === plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) {
844 $links[] = '<a href="https://github.com/sponsors/thingsym">' . __( 'Become a sponsor', 'multi-device-switcher' ) . '</a>';
845 }
846
847 return $links;
848 }
849
850 /**
851 * Returns the default options.
852 *
853 * @access public
854 *
855 * @return array|null
856 *
857 * @since 1.0.0
858 */
859 public function get_default_options() {
860 return $this->default_options;
861 }
862
863 /**
864 * Returns the options array or value.
865 *
866 * @access public
867 *
868 * @param string $option_name Optional. The option name.
869 *
870 * @return array|null
871 *
872 * @since 1.0.0
873 */
874 public function get_options( $option_name = null ) {
875 $options = get_option( $this->option_name, $this->default_options );
876 $options = array_merge( $this->default_options, $options );
877
878 if ( is_null( $option_name ) ) {
879 /**
880 * Filter hook: multi_device_switcher/get_options.
881 *
882 * @param array $options The options.
883 *
884 * @since 1.7.0
885 */
886 return apply_filters( 'multi_device_switcher/get_options', $options );
887 }
888
889 if ( array_key_exists( $option_name, $options ) ) {
890 /**
891 * Filter hook: multi_device_switcher/get_option.
892 *
893 * @param mixed $option The value of option.
894 * @param string $option_name The option name via argument.
895 *
896 * @since 1.7.0
897 */
898 return apply_filters( 'multi_device_switcher/get_option', $options[ $option_name ], $option_name );
899 }
900
901 return null;
902 }
903
904 /**
905 * Load textdomain
906 *
907 * @access public
908 *
909 * @return boolean
910 *
911 * @since 1.6.0
912 */
913 public function load_textdomain() {
914 return load_plugin_textdomain(
915 'multi-device-switcher',
916 false,
917 'multi-device-switcher/languages'
918 );
919 }
920
921 /**
922 * Load plugin data
923 *
924 * @access public
925 *
926 * @return boolean
927 *
928 * @since 1.8.1
929 */
930 public function load_plugin_data() {
931 if ( ! function_exists( 'get_plugin_data' ) ) {
932 require_once ABSPATH . 'wp-admin/includes/plugin.php';
933 }
934
935 $this->plugin_data = get_plugin_data( __MULTI_DEVICE_SWITCHER_FILE__ );
936
937 if ( ! $this->plugin_data ) {
938 return false;
939 }
940
941 return true;
942 }
943
944 /**
945 * Display option page.
946 *
947 * @access public
948 *
949 * @return void
950 *
951 * @since 1.0.0
952 */
953 public function render_option_page() {
954 ?>
955 <div class="wrap">
956 <div id="icon-themes" class="icon32"><br></div>
957 <h2><?php esc_html_e( 'Multi Device Switcher', 'multi-device-switcher' ); ?></h2>
958 <?php settings_errors(); ?>
959
960 <form method="post" action="options.php">
961 <?php
962 settings_fields( 'multi_device_switcher' );
963 $options = $this->get_options();
964
965 $default_theme = wp_get_theme()->get( 'Name' );
966 $themes = wp_get_themes();
967 $theme_names = array();
968
969 if ( count( $themes ) ) {
970 foreach ( $themes as $t ) {
971 $theme_names[] = $t->get( 'Name' );
972 }
973 natcasesort( $theme_names );
974 }
975 ?>
976
977 <div id="admin-tabs">
978 <fieldset id="Theme" class="options">
979 <h3 class="label"><?php esc_html_e( 'Theme', 'multi-device-switcher' ); ?></h3>
980 <table class="form-table">
981 <tr><th scope="row"><?php esc_html_e( 'Smart Phone Theme', 'multi-device-switcher' ); ?></th>
982 <td>
983
984 <?php
985 $html = '';
986 if ( count( $theme_names ) ) {
987 $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
988
989 if ( ( 'None' === $options['theme_smartphone'] ) || ( '' === $options['theme_smartphone'] ) ) {
990 $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
991 }
992 else {
993 $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
994 }
995
996 foreach ( $theme_names as $theme_name ) {
997 if ( $default_theme === $theme_name ) {
998 continue;
999 }
1000 if ( $options['theme_smartphone'] === $theme_name ) {
1001 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1002 }
1003 else {
1004 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1005 }
1006 }
1007 $html .= '</select>';
1008 }
1009 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1010 echo $html;
1011 ?>
1012
1013 </td>
1014 </tr>
1015 <tr><th scope="row"><?php esc_html_e( 'Tablet PC Theme', 'multi-device-switcher' ); ?></th>
1016 <td>
1017
1018 <?php
1019 if ( count( $theme_names ) ) {
1020 $html = '<select name="multi_device_switcher_options[theme_tablet]">';
1021
1022 if ( ( 'None' === $options['theme_tablet'] ) || ( '' === $options['theme_tablet'] ) ) {
1023 $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1024 }
1025 else {
1026 $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1027 }
1028
1029 foreach ( $theme_names as $theme_name ) {
1030 if ( $default_theme === $theme_name ) {
1031 continue;
1032 }
1033 if ( $options['theme_tablet'] === $theme_name ) {
1034 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1035 }
1036 else {
1037 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1038 }
1039 }
1040 $html .= '</select>';
1041 }
1042 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1043 echo $html;
1044 ?>
1045
1046 </td>
1047 </tr>
1048 <tr><th scope="row"><?php esc_html_e( 'Mobile Phone Theme', 'multi-device-switcher' ); ?></th>
1049 <td>
1050
1051 <?php
1052 if ( count( $theme_names ) ) {
1053 $html = '<select name="multi_device_switcher_options[theme_mobile]">';
1054
1055 if ( ( 'None' === $options['theme_mobile'] ) || ( '' === $options['theme_mobile'] ) ) {
1056 $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1057 }
1058 else {
1059 $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1060 }
1061
1062 foreach ( $theme_names as $theme_name ) {
1063 if ( $default_theme === $theme_name ) {
1064 continue;
1065 }
1066 if ( $options['theme_mobile'] === $theme_name ) {
1067 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1068 }
1069 else {
1070 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1071 }
1072 }
1073 $html .= '</select>';
1074 }
1075 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1076 echo $html;
1077 ?>
1078
1079 </td>
1080 </tr>
1081 <tr><th scope="row"><?php esc_html_e( 'Game Platforms Theme', 'multi-device-switcher' ); ?></th>
1082 <td>
1083
1084 <?php
1085 if ( count( $theme_names ) ) {
1086 $html = '<select name="multi_device_switcher_options[theme_game]">';
1087
1088 if ( ( 'None' === $options['theme_game'] ) || ( '' === $options['theme_game'] ) ) {
1089 $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1090 }
1091 else {
1092 $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1093 }
1094
1095 foreach ( $theme_names as $theme_name ) {
1096 if ( $default_theme === $theme_name ) {
1097 continue;
1098 }
1099 if ( $options['theme_game'] === $theme_name ) {
1100 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1101 }
1102 else {
1103 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1104 }
1105 }
1106 $html .= '</select>';
1107 }
1108 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1109 echo $html;
1110 ?>
1111
1112 </td>
1113 </tr>
1114 </table>
1115
1116 <h3><?php esc_html_e( 'Custom Switcher Theme', 'multi-device-switcher' ); ?></h3>
1117 <table class="form-table">
1118
1119 <?php
1120 foreach ( (array) $options as $custom_switcher_option => $custom_switcher_theme ) {
1121 if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1122 continue;
1123 }
1124
1125 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
1126 ?>
1127
1128 <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
1129 <td>
1130
1131 <?php
1132 if ( count( $theme_names ) ) {
1133 $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
1134
1135 if ( ( 'None' === $custom_switcher_theme ) || ( '' === $custom_switcher_theme ) ) {
1136 $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1137 }
1138 else {
1139 $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1140 }
1141
1142 foreach ( $theme_names as $theme_name ) {
1143 if ( $default_theme === $theme_name ) {
1144 continue;
1145 }
1146 if ( $custom_switcher_theme === $theme_name ) {
1147 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1148 }
1149 else {
1150 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1151 }
1152 }
1153 $html .= '</select>';
1154 /* translators: confirm: 1: custom switcher name */
1155 $html .= ' <span class="submit"><input type="submit" name="multi_device_switcher_options[delete_custom_switcher_' . $custom_switcher_name . ']" value="' . __( 'Delete', 'multi-device-switcher' ) . '" onclick="return confirm(\'' . esc_html( sprintf( __( 'Are you sure you want to delete %1$s ?', 'multi-device-switcher' ), $custom_switcher_name ) ) . '\');" class="button"></span>';
1156 }
1157 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1158 echo $html;
1159 ?>
1160 </td>
1161 </tr>
1162
1163 <?php
1164 }
1165 ?>
1166
1167 <tr><th scope="row"><?php esc_html_e( 'Add Custom Switcher', 'multi-device-switcher' ); ?></th>
1168 <td>
1169 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add Custom Switcher', 'multi-device-switcher' ); ?></span></legend>
1170 <input type="text" name="multi_device_switcher_options[custom_switcher]" id="custom-switcher" value="" size="24">
1171 <span class="submit"><input type="submit" name="multi_device_switcher_options[add_custom_switcher]" value="<?php esc_html_e( 'Add', 'multi-device-switcher' ); ?>" class="button"></span><br>
1172 <?php esc_html_e( '20 characters max, alphanumeric', 'multi-device-switcher' ); ?>
1173 </td>
1174 </tr>
1175 </table>
1176
1177 </fieldset>
1178
1179 <fieldset id="UserAgent" class="options">
1180 <h3 class="label"><?php esc_html_e( 'UserAgent', 'multi-device-switcher' ); ?></h3>
1181 <p><?php esc_html_e( 'Enter Comma-separated values (csv) format.', 'multi-device-switcher' ); ?></p>
1182
1183 <table class="form-table">
1184 <tr><th scope="row"><?php esc_html_e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
1185 <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_smart'] ); ?></textarea></td>
1186 </tr>
1187 <tr><th scope="row"><?php esc_html_e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
1188 <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_tablet'] ); ?></textarea></td>
1189 </tr>
1190 <tr><th scope="row"><?php esc_html_e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
1191 <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_mobile'] ); ?></textarea></td>
1192 </tr>
1193 <tr><th scope="row"><?php esc_html_e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
1194 <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_game'] ); ?></textarea></td>
1195 </tr>
1196 <tr><th></th>
1197 <td><span class="submit"><input type="submit" name="multi_device_switcher_options[restore_UserAgent]" value="<?php esc_html_e( 'Reset Settings to Default UserAgent', 'multi-device-switcher' ); ?>" class="button"></span></td>
1198 </tr>
1199
1200 </table>
1201
1202 <h3><?php esc_html_e( 'Custom Switcher UserAgent', 'multi-device-switcher' ); ?></h3>
1203 <table class="form-table">
1204 <?php
1205 foreach ( (array) $options as $custom_switcher_option => $custom_switcher_user_agent ) {
1206 if ( ! preg_match( '/^custom_switcher_userAgent_/', $custom_switcher_option ) ) {
1207 continue;
1208 }
1209
1210 $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $custom_switcher_option );
1211 ?>
1212
1213 <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
1214 <td><textarea name="multi_device_switcher_options[<?php echo esc_attr( $custom_switcher_option ); ?>]" rows="4" cols="42"><?php echo esc_textarea( $custom_switcher_user_agent ); ?></textarea></td>
1215 </tr>
1216 <?php
1217 }
1218 ?>
1219
1220 </table>
1221 </fieldset>
1222
1223 <fieldset id="PC-Switcher" class="options">
1224 <h3 class="label"><?php esc_html_e( 'PC Switcher', 'multi-device-switcher' ); ?></h3>
1225
1226 <table class="form-table">
1227 <tr><th scope="row"><?php esc_html_e( 'Add PC Switcher', 'multi-device-switcher' ); ?></th>
1228 <td>
1229 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add PC Switcher', 'multi-device-switcher' ); ?></span></legend>
1230 <label><input type="checkbox" name="multi_device_switcher_options[pc_switcher]" id="pc-switcher" value="1"<?php checked( 1, $options['pc_switcher'] ); ?>> <?php esc_html_e( 'Add a PC Switcher to the footer.', 'multi-device-switcher' ); ?></label>
1231 </td>
1232 </tr>
1233 <tr><th scope="row"><?php esc_html_e( 'Add default CSS', 'multi-device-switcher' ); ?></th>
1234 <td>
1235 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add default CSS', 'multi-device-switcher' ); ?></span></legend>
1236 <label><input type="checkbox" name="multi_device_switcher_options[default_css]" id="add-default-css" value="1"<?php checked( 1, $options['default_css'] ); ?>> <?php esc_html_e( 'Add a default CSS.', 'multi-device-switcher' ); ?></label>
1237 </td>
1238 </tr>
1239 </table>
1240 </fieldset>
1241
1242 <fieldset id="Disable-Switcher" class="options">
1243 <h3 class="label"><?php esc_html_e( 'Disable Switcher', 'multi-device-switcher' ); ?></h3>
1244
1245 <table class="form-table">
1246 <tr><th scope="row"><?php esc_html_e( 'Path', 'multi-device-switcher' ); ?></th>
1247 <td>
1248 <legend class="screen-reader-text"><span><?php esc_html_e( 'Path', 'multi-device-switcher' ); ?></span></legend>
1249 <?php echo esc_html( home_url() ); ?><br>
1250 <textarea name="multi_device_switcher_options[disable_path]" rows="16" cols="42" wrap="off"><?php echo esc_textarea( $options['disable_path'] ); ?></textarea>
1251 </td>
1252 </tr>
1253 <tr><th scope="row"><?php esc_html_e( 'Regex mode', 'multi-device-switcher' ); ?></th>
1254 <td>
1255 <legend class="screen-reader-text"><span><?php esc_html_e( 'Regex mode', 'multi-device-switcher' ); ?></span></legend>
1256 <label><input type="checkbox" name="multi_device_switcher_options[enable_regex]" id="enable-regex" value="1"<?php checked( 1, $options['enable_regex'] ); ?>> <?php esc_html_e( 'Enable Regex', 'multi-device-switcher' ); ?></label>
1257 </td>
1258 </tr>
1259 </table>
1260 </fieldset>
1261
1262 </div>
1263 <?php submit_button(); ?>
1264 </form>
1265 </div>
1266
1267 <?php
1268 }
1269
1270 /**
1271 * Validate options.
1272 *
1273 * @access public
1274 *
1275 * @param array $input
1276 *
1277 * @return array
1278 */
1279 public function validate_options( $input ) {
1280 $output = $this->default_options;
1281
1282 if ( isset( $input['theme_smartphone'] ) ) {
1283 $output['theme_smartphone'] = $input['theme_smartphone'];
1284 }
1285 if ( isset( $input['theme_tablet'] ) ) {
1286 $output['theme_tablet'] = $input['theme_tablet'];
1287 }
1288 if ( isset( $input['theme_mobile'] ) ) {
1289 $output['theme_mobile'] = $input['theme_mobile'];
1290 }
1291 if ( isset( $input['theme_game'] ) ) {
1292 $output['theme_game'] = $input['theme_game'];
1293 }
1294
1295 if ( isset( $input['restore_UserAgent'] ) ) {
1296 $output['userAgent_smart'] = $this->default_options['userAgent_smart'];
1297 $output['userAgent_tablet'] = $this->default_options['userAgent_tablet'];
1298 $output['userAgent_mobile'] = $this->default_options['userAgent_mobile'];
1299 $output['userAgent_game'] = $this->default_options['userAgent_game'];
1300 }
1301 else {
1302 if ( isset( $input['userAgent_smart'] ) ) {
1303 $output['userAgent_smart'] = $input['userAgent_smart'];
1304 }
1305 if ( isset( $input['userAgent_tablet'] ) ) {
1306 $output['userAgent_tablet'] = $input['userAgent_tablet'];
1307 }
1308 if ( isset( $input['userAgent_mobile'] ) ) {
1309 $output['userAgent_mobile'] = $input['userAgent_mobile'];
1310 }
1311 if ( isset( $input['userAgent_game'] ) ) {
1312 $output['userAgent_game'] = $input['userAgent_game'];
1313 }
1314 }
1315
1316 foreach ( $input as $key => $val ) {
1317 if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
1318 continue;
1319 }
1320
1321 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
1322
1323 if ( isset( $input[ 'custom_switcher_theme_' . $custom_switcher_name ] ) ) {
1324 $output[ 'custom_switcher_theme_' . $custom_switcher_name ] = $input[ 'custom_switcher_theme_' . $custom_switcher_name ];
1325 }
1326 if ( isset( $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ] ) ) {
1327 $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] = $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ];
1328 }
1329 }
1330
1331 foreach ( $input as $key => $val ) {
1332 if ( ! preg_match( '/^delete_custom_switcher_/', $key ) ) {
1333 continue;
1334 }
1335
1336 $custom_switcher_name = preg_replace( '/^delete_custom_switcher_/', '', $key );
1337
1338 unset( $output[ 'custom_switcher_theme_' . $custom_switcher_name ] );
1339 unset( $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] );
1340 }
1341
1342 if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! isset( $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] ) ) {
1343 if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true )
1344 && preg_match( '/^[A-Za-z0-9]{1,20}$/', $input['custom_switcher'] ) ) {
1345 $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] = 'None';
1346 $output[ 'custom_switcher_userAgent_' . $input['custom_switcher'] ] = '';
1347 }
1348 }
1349
1350 $output['pc_switcher'] = isset( $input['pc_switcher'] ) ? $input['pc_switcher'] : 0;
1351 $output['default_css'] = isset( $input['default_css'] ) ? $input['default_css'] : 0;
1352
1353 $output['disable_path'] = isset( $input['disable_path'] ) ? $input['disable_path'] : '';
1354 $output['enable_regex'] = isset( $input['enable_regex'] ) ? $input['enable_regex'] : 0;
1355
1356 /**
1357 * Filter hook: multi_device_switcher/validate_options.
1358 *
1359 * @param array $options The options.
1360 * @param array $input The options.
1361 * @param array $default The options.
1362 *
1363 * @since 1.7.0
1364 */
1365 return apply_filters( 'multi_device_switcher/validate_options', $output, $input, $this->default_options );
1366 }
1367
1368 /**
1369 * Register customize options to Customizer.
1370 *
1371 * @access public
1372 *
1373 * @param object $wp_customize
1374 *
1375 * @return void
1376 *
1377 * @since 1.3.1
1378 */
1379 public function customizer( $wp_customize ) {
1380 $options = $this->get_options();
1381 $default_theme_options = $this->default_options;
1382 $default_theme = wp_get_theme()->get( 'Name' );
1383 $themes = wp_get_themes();
1384
1385 $theme_names = array();
1386 $choices = array();
1387
1388 if ( count( $themes ) ) {
1389 foreach ( $themes as $t ) {
1390 $theme_names[] = $t->get( 'Name' );
1391 }
1392 natcasesort( $theme_names );
1393
1394 $choices['None'] = __( 'None', 'multi-device-switcher' );
1395 foreach ( $theme_names as $theme_name ) {
1396 if ( $default_theme === $theme_name ) {
1397 continue;
1398 }
1399 $choices[ $theme_name ] = $theme_name;
1400 }
1401 }
1402
1403 $switcher = array(
1404 'theme_smartphone' => __( 'Smart Phone Theme', 'multi-device-switcher' ),
1405 'theme_tablet' => __( 'Tablet PC Theme', 'multi-device-switcher' ),
1406 'theme_mobile' => __( 'Mobile Phone Theme', 'multi-device-switcher' ),
1407 'theme_game' => __( 'Game Platforms Theme', 'multi-device-switcher' ),
1408 );
1409
1410 $wp_customize->add_section(
1411 'multi_device_switcher',
1412 array(
1413 'title' => __( 'Multi Device Switcher', 'multi-device-switcher' ),
1414 'priority' => 80,
1415 )
1416 );
1417
1418 foreach ( $switcher as $name => $label ) {
1419 $wp_customize->add_setting(
1420 'multi_device_switcher_options[' . $name . ']',
1421 array(
1422 'default' => $default_theme_options[ $name ],
1423 'type' => 'option',
1424 'capability' => $this->capability,
1425 )
1426 );
1427
1428 $wp_customize->add_control(
1429 'multi_device_switcher_options[' . $name . ']',
1430 array(
1431 'label' => $label,
1432 'section' => 'multi_device_switcher',
1433 'type' => 'select',
1434 'choices' => $choices,
1435 )
1436 );
1437 }
1438
1439 foreach ( (array) $options as $custom_switcher_option => $custom_switcher_theme ) {
1440 if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1441 continue;
1442 }
1443
1444 $label = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
1445
1446 $wp_customize->add_setting(
1447 'multi_device_switcher_options[' . $custom_switcher_option . ']',
1448 array(
1449 'default' => __( 'None', 'multi-device-switcher' ),
1450 'type' => 'option',
1451 'capability' => $this->capability,
1452 )
1453 );
1454
1455 $wp_customize->add_control(
1456 'multi_device_switcher_options[' . $custom_switcher_option . ']',
1457 array(
1458 'label' => $label,
1459 'section' => 'multi_device_switcher',
1460 'type' => 'select',
1461 'choices' => $choices,
1462 )
1463 );
1464
1465 }
1466 }
1467
1468 /**
1469 * Load files.
1470 *
1471 * @access public
1472 *
1473 * @since 1.0.0
1474 */
1475 public function load_file() {
1476 /**
1477 * Include PC Switcher Widget.
1478 *
1479 * @since 1.2
1480 */
1481 require_once dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/pc-switcher-widget.php';
1482
1483 /**
1484 * Include Multi Device Switcher Command
1485 *
1486 * @since 1.4
1487 */
1488 if ( defined( 'WP_CLI' ) && WP_CLI ) {
1489 require_once dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/wp-cli.php';
1490 }
1491 }
1492 }
1493
1494 define( '__MULTI_DEVICE_SWITCHER_FILE__', __FILE__ );
1495
1496 if ( class_exists( 'Multi_Device_Switcher' ) ) {
1497 global $multi_device_switcher;
1498 $multi_device_switcher = new Multi_Device_Switcher();
1499 }
1500
1501 /**
1502 * Add PC Switcher.
1503 *
1504 * @since 1.2
1505 */
1506 function multi_device_switcher_add_pc_switcher() {
1507 global $multi_device_switcher;
1508 if ( is_object( $multi_device_switcher ) ) {
1509 $multi_device_switcher->add_pc_switcher( 1 );
1510 }
1511 }
1512
1513 /**
1514 * Return boolean whether a particular device.
1515 *
1516 * @since 1.2.4
1517 */
1518 if ( ! function_exists( 'is_multi_device' ) ) :
1519
1520 function is_multi_device( $device = '' ) {
1521 global $multi_device_switcher;
1522 if ( is_object( $multi_device_switcher ) ) {
1523 return $multi_device_switcher->is_multi_device( $device );
1524 }
1525 }
1526 endif;
1527
1528 /**
1529 * Return the state of PC Switcher.
1530 *
1531 * @since 1.4.1
1532 */
1533 if ( ! function_exists( 'is_pc_switcher' ) ) :
1534
1535 function is_pc_switcher() {
1536 global $multi_device_switcher;
1537 if ( is_object( $multi_device_switcher ) ) {
1538 return $multi_device_switcher->is_pc_switcher();
1539 }
1540 }
1541 endif;
1542
1543 /**
1544 * Return the state of disabled.
1545 *
1546 * @since 1.4.1
1547 */
1548 if ( ! function_exists( 'is_disable_switcher' ) ) :
1549
1550 function is_disable_switcher() {
1551 global $multi_device_switcher;
1552 if ( is_object( $multi_device_switcher ) ) {
1553 return $multi_device_switcher->is_disable_switcher();
1554 }
1555 }
1556 endif;
1557
1558 /**
1559 * Returns the default options.
1560 *
1561 * @since 1.5.3
1562 */
1563 if ( ! function_exists( 'multi_device_switcher_get_default_options' ) ) :
1564
1565 function multi_device_switcher_get_default_options() {
1566 global $multi_device_switcher;
1567 if ( is_object( $multi_device_switcher ) ) {
1568 return $multi_device_switcher->get_default_options();
1569 }
1570 }
1571 endif;
1572