PluginProbe
Multi Device Switcher / 1.6.2
Multi Device Switcher v1.6.2
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 1.6.2, at multi-device-switcher.php

1,210 lines 39.0 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.6.2
7 * Author: thingsym
8 * Author URI: http://www.thingslabo.com/
9 * License: GPL2
10 * Text Domain: multi-device-switcher
11 * Domain Path: /languages/
12 */
13
14 /**
15 * Copyright 2012 thingsym (http://www.thingslabo.com/)
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
30 */
31
32 if ( ! defined( 'ABSPATH' ) ) {
33 exit;
34 }
35
36 class Multi_Device_Switcher {
37 protected $option_group = 'multi_device_switcher';
38 protected $option_name = 'multi_device_switcher_options';
39
40 protected $page_title = 'Multi Device Switcher';
41 protected $menu_title = 'Multi Device Switcher';
42 protected $menu_slug = 'multi-device-switcher';
43 protected $capability = 'switch_themes';
44
45 protected $textdomain = 'multi-device-switcher';
46
47 protected $cookie_name_multi_device_switcher = 'multi-device-switcher';
48 protected $cookie_name_disable_switcher = 'disable-switcher';
49 protected $cookie_name_pc_switcher = 'pc-switcher';
50
51 public $device = '';
52
53 public function __construct() {
54 add_action( 'init', array( $this, 'load_textdomain' ) );
55 add_action( 'init', array( $this, 'init' ) );
56
57 if ( ! is_admin() ) {
58 add_filter( 'wp_headers', array( $this, 'add_header_vary' ) );
59 add_action( 'plugins_loaded', array( $this, 'switch_theme' ) );
60 }
61
62 add_action( 'admin_init', array( $this, 'admin_init' ) );
63 add_action( 'admin_menu', array( $this, 'add_option_page' ) );
64 add_action( 'customize_register', array( $this, 'customize_register' ) );
65 add_action( 'plugins_loaded', array( $this, 'load_file' ) );
66 }
67
68 /**
69 * Initialize.
70 *
71 * Hooks to init
72 *
73 * @access public
74 *
75 * @since 1.6.0
76 */
77 public function init() {
78 add_filter( 'option_page_capability_' . $this->option_group, array( $this, 'option_page_capability' ) );
79 add_filter( 'plugin_action_links_' . plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ), array( $this, 'plugin_action_links' ) );
80
81 add_shortcode( 'multi', array( $this, 'shortcode_display_switcher' ) );
82 }
83
84 public function switch_theme() {
85 if ( isset( $_COOKIE[ $this->cookie_name_disable_switcher ] ) ) {
86 add_action( 'wp_headers', array( $this, 'set_cookie_rest_disable_switcher' ));
87 }
88
89 if ( $this->is_disable_switcher() ) {
90 add_action( 'wp_headers', array( $this, 'set_cookie_enable_disable_switcher' ));
91 return;
92 }
93
94 add_action( 'init', array( $this, 'session' ) );
95
96 $userAgent = $this->get_options_userAgent();
97 $server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
98
99 foreach ( array_reverse( $userAgent ) as $key => $val ) {
100 if ( ! preg_match( '/^custom_switcher_/', $key ) ) {
101 continue;
102 }
103 if ( $userAgent[ $key ] && preg_match( '/' . implode( '|', $userAgent[ $key ] ) . '/i', $server_ua ) ) {
104 $this->device = $key;
105 break;
106 }
107 }
108
109 if ( ! $this->device ) {
110 if ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $server_ua ) ) {
111 $this->device = 'game';
112 }
113 elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $server_ua ) ) {
114 $this->device = 'tablet';
115 }
116 elseif ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $server_ua ) ) {
117 $this->device = 'smart';
118 }
119 elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $server_ua ) ) {
120 $this->device = 'mobile';
121 }
122 }
123
124 if ( $this->device ) {
125 add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
126 add_filter( 'template', array( $this, 'get_template' ) );
127 add_action( 'wp_footer', array( $this, 'add_pc_switcher' ) );
128 add_action( 'wp_headers', array( $this, 'set_cookie_switch_theme' ));
129 }
130 else {
131 add_action( 'wp_headers', array( $this, 'set_cookie_normal_theme' ));
132 }
133
134 if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
135 remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
136 remove_filter( 'template', array( $this, 'get_template' ) );
137 }
138 }
139
140 public function get_options_userAgent() {
141 $options = $this->get_options();
142
143 $userAgent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'], -1, PREG_SPLIT_NO_EMPTY );
144 $userAgent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'], -1, PREG_SPLIT_NO_EMPTY );
145 $userAgent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'], -1, PREG_SPLIT_NO_EMPTY );
146 $userAgent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'], -1, PREG_SPLIT_NO_EMPTY );
147
148 foreach ( $options as $key => $val ) {
149 if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) {
150 continue;
151 }
152
153 $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key );
154 $userAgent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val, -1, PREG_SPLIT_NO_EMPTY );
155 }
156
157 return $userAgent;
158 }
159
160 public function get_stylesheet( $stylesheet = '' ) {
161 $name = $this->get_device_theme();
162
163 if ( empty( $name ) ) {
164 return $stylesheet;
165 }
166
167 $themes = wp_get_themes();
168 foreach ( $themes as $t ) {
169 if ( $name === $t->get( 'Name' ) ) {
170 $theme = $t;
171 break;
172 }
173 }
174
175 if ( empty( $theme ) ) {
176 return $stylesheet;
177 }
178
179 if ( 'publish' !== $theme->get( 'Status' ) ) {
180 return $stylesheet;
181 }
182
183 return $theme['Stylesheet'];
184 }
185
186 public function get_template( $template = '' ) {
187 $name = $this->get_device_theme();
188
189 if ( empty( $name ) ) {
190 return $template;
191 }
192
193 $themes = wp_get_themes();
194 foreach ( $themes as $t ) {
195 if ( $name === $t->get( 'Name' ) ) {
196 $theme = $t;
197 break;
198 }
199 }
200
201 if ( empty( $theme ) ) {
202 return $template;
203 }
204
205 if ( 'publish' !== $theme->get( 'Status' ) ) {
206 return $template;
207 }
208
209 return $theme['Template'];
210 }
211
212 public function get_device_theme() {
213 $options = $this->get_options();
214
215 if ( 'smart' === $this->device ) {
216 return $options['theme_smartphone'];
217 }
218 elseif ( 'tablet' === $this->device ) {
219 return $options['theme_tablet'];
220 }
221 elseif ( 'mobile' === $this->device ) {
222 return $options['theme_mobile'];
223 }
224 elseif ( 'game' === $this->device ) {
225 return $options['theme_game'];
226 }
227 else {
228 foreach ( $options as $key => $val ) {
229 if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
230 continue;
231 }
232
233 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
234
235 if ( 'custom_switcher_' . $custom_switcher_name === $this->device ) {
236 return $options[ $key ];
237 }
238 }
239 }
240
241 return;
242 }
243
244 public function set_cookie_rest_disable_switcher() {
245 setcookie( $this->cookie_name_disable_switcher, null, time() - 3600, '/', '', is_ssl(), false );
246 }
247
248 public function set_cookie_enable_disable_switcher() {
249 setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
250 setcookie( $this->cookie_name_disable_switcher, 1, null, '/', '', is_ssl(), false );
251 }
252
253 public function set_cookie_switch_theme() {
254 setcookie( $this->cookie_name_multi_device_switcher, preg_replace( '/^custom_switcher_/', '', $this->device ), null, '/', '', is_ssl(), false );
255 }
256
257 public function set_cookie_normal_theme() {
258 setcookie( $this->cookie_name_multi_device_switcher, null, time() - 3600, '/', '', is_ssl(), false );
259 }
260
261 public function session() {
262 if ( isset( $_GET['pc-switcher'] ) ) {
263 setcookie( $this->cookie_name_pc_switcher, $_GET['pc-switcher'] ? 1 : '', null, '/', '', is_ssl(), false );
264
265 $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
266
267 unset( $_GET['pc-switcher'] );
268 if ( ! empty( $_GET ) ) {
269 $uri .= '?' . http_build_query( $_GET );
270 }
271
272 wp_redirect( esc_url( $uri ) );
273 exit;
274 }
275 }
276
277 public function add_pc_switcher( $pc_switcher = 0 ) {
278 $options = $this->get_options();
279 $name = $this->get_device_theme();
280
281 if ( $options['pc_switcher'] ) {
282 $pc_switcher = 1;
283 }
284
285 if ( $pc_switcher && $name && 'None' !== $name ) {
286 if ( $options['default_css'] ) {
287 wp_enqueue_style(
288 'pc-switcher-options',
289 plugins_url() . '/multi-device-switcher/pc-switcher.css',
290 false,
291 '2013-03-20'
292 );
293 }
294
295 $uri = is_ssl() ? 'https://' : 'http://';
296 $uri .= $_SERVER['HTTP_HOST'];
297
298 if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
299 $uri .= add_query_arg( 'pc-switcher', 0 );
300 ?>
301 <div class="pc-switcher"><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'Mobile', $this->textdomain ); ?></a><span class="active"><?php esc_html_e( 'PC', $this->textdomain ); ?></span></div>
302 <?php
303 }
304 else {
305 $uri .= add_query_arg( 'pc-switcher', 1 );
306 ?>
307 <div class="pc-switcher"><span class="active"><?php esc_html_e( 'Mobile', $this->textdomain ); ?></span><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'PC', $this->textdomain ); ?></a></div>
308 <?php
309 }
310 }
311 }
312
313 public function is_multi_device( $device = '' ) {
314 if ( $device === $this->device ) {
315 return true;
316 }
317 if ( 'custom_switcher_' . $device === $this->device ) {
318 return true;
319 }
320
321 return false;
322 }
323
324 public function is_pc_switcher() {
325 return isset( $_COOKIE[ $this->cookie_name_pc_switcher ] );
326 }
327
328 public function is_disable_switcher( $disable = false ) {
329 $options = $this->get_options();
330 $disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY );
331
332 foreach ( $disable_path as $path ) {
333 if ( $options['enable_regex'] ) {
334 if ( preg_match( '/' . $path . '/i', $_SERVER['REQUEST_URI'] ) ) {
335 $disable = true;
336 break;
337 }
338 }
339 else {
340 if ( preg_match( '/^' . preg_quote( $path , '/' ) . '$/i', $_SERVER['REQUEST_URI'] ) ) {
341 $disable = true;
342 break;
343 }
344 }
345 }
346
347 return $disable;
348 }
349
350 public function shortcode_display_switcher( $atts, $content = '' ) {
351 $atts = shortcode_atts( array(
352 'device' => '',
353 ), $atts );
354
355 if ( empty( $atts['device'] ) && ( $this->is_multi_device( $atts['device'] ) || $this->is_pc_switcher() ) ) {
356 return $content;
357 }
358 elseif ( ! empty( $atts['device'] ) && $this->is_multi_device( $atts['device'] ) && ! $this->is_pc_switcher() ) {
359 return $content;
360 }
361
362 return '';
363 }
364
365 /**
366 * Add HTTP/1.1 Vary header.
367 *
368 * @since 1.1.1
369 *
370 */
371 public function add_header_vary( $headers ) {
372 $headers['Vary'] = apply_filters( 'multi_device_switcher_add_header_vary', 'User-Agent' );
373 return $headers;
374 }
375
376 /**
377 * Properly enqueue scripts for our multi_device_switcher options page.
378 *
379 * This function is attached to the admin_enqueue_scripts action hook.
380 *
381 * @since 1.0
382 *
383 */
384 public function admin_enqueue_scripts( $hook_suffix ) {
385 wp_enqueue_script(
386 'multi-device-switcher-options',
387 plugins_url() . '/multi-device-switcher/multi-device-switcher.js',
388 array( 'jquery', 'jquery-ui-tabs' ),
389 '2011-08-22'
390 );
391 }
392
393 /**
394 * Properly enqueue styles for our multi_device_switcher options page.
395 *
396 * This function is attached to the admin_enqueue_styles action hook.
397 *
398 * @since 1.0
399 *
400 */
401 public function admin_enqueue_styles( $hook_suffix ) {
402 wp_enqueue_style(
403 'multi-device-switcher-options',
404 plugins_url() . '/multi-device-switcher/multi-device-switcher.css',
405 false,
406 '2011-08-22'
407 );
408 }
409
410 /**
411 * Register the form setting for our multi_device_switcher array.
412 *
413 * This function is attached to the admin_init action hook.
414 *
415 * This call to register_setting() registers a validation callback, validate(),
416 * which is used when the option is saved, to ensure that our option values are complete, properly
417 * formatted, and safe.
418 *
419 * @since 1.0
420 */
421 public function admin_init() {
422 // If we have no options in the database, let's add them now.
423 if ( false === $this->get_options() ) {
424 add_option( $this->option_name );
425 }
426
427 register_setting(
428 $this->option_group,
429 $this->option_name,
430 array( $this, 'validate' )
431 );
432 }
433
434 /**
435 * Change the capability required to save the 'multi_device_switcher' options group.
436 *
437 * @see admin_init() First parameter to register_setting() is the name of the options group.
438 * @see add_option_page() The edit_theme_options capability is used for viewing the page.
439 *
440 * By default, the options groups for all registered settings require the manage_options capability.
441 * By default, only administrators have either of these capabilities, but the desire here is
442 * to allow for finer-grained control for roles and users.
443 *
444 * @param string $capability The capability used for the page, which is manage_options by default.
445 * @return string The capability to actually use.
446 */
447 public function option_page_capability() {
448 return $this->capability;
449 }
450
451 /**
452 * Add our options page to the admin menu, including some help documentation.
453 *
454 * This function is attached to the admin_menu action hook.
455 *
456 * @since 1.0
457 */
458 public function add_option_page() {
459
460 $page_hook = add_theme_page(
461 __( $this->page_title, $this->textdomain ),
462 __( $this->menu_title, $this->textdomain ),
463 $this->option_page_capability(),
464 $this->menu_slug,
465 array( $this, 'render_option_page' )
466 );
467
468 if ( ! $page_hook ) {
469 return;
470 }
471
472 add_action( 'load-' . $page_hook , array( $this, 'page_hook_suffix' ) );
473 }
474
475 /**
476 * Page Hook Suffix
477 *
478 * This function is attached to the load-** action hook.
479 *
480 * @since 1.2.4
481 */
482 public function page_hook_suffix() {
483 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
484 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
485 }
486
487 /**
488 * Set link to customizer section on the plugins page.
489 *
490 * Hooks to plugin_action_links_{$plugin_file}
491 *
492 * @see https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
493 *
494 * @access public
495 *
496 * @param array $links An array of plugin action links.
497 *
498 * @return array $links
499 *
500 * @since 1.6.0
501 */
502 public function plugin_action_links( $links = array() ) {
503 $settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', $this->textdomain ) . '</a>';
504
505 array_unshift( $links, $settings_link );
506
507 return $links;
508 }
509
510 /**
511 * Returns the default options.
512 *
513 * @since 1.0
514 */
515 public function get_default_options() {
516 $default_theme_options = array(
517 'pc_switcher' => 1,
518 'default_css' => 1,
519 'theme_smartphone' => 'None',
520 'theme_tablet' => 'None',
521 'theme_mobile' => 'None',
522 'theme_game' => 'None',
523 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko',
524 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko',
525 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
526 'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, PlayStation 4, Nitro, Nintendo 3DS, Nintendo Wii, Nintendo WiiU, Xbox',
527 'disable_path' => '',
528 'enable_regex' => 0,
529 );
530
531 return $default_theme_options;
532 }
533
534 /**
535 * Returns the options array.
536 *
537 * @since 1.0
538 */
539 public function get_options() {
540 $options = get_option( $this->option_name );
541 $default_options = $this->get_default_options();
542
543 if ( ! isset( $options['pc_switcher'] ) ) {
544 $options['pc_switcher'] = $default_options['pc_switcher'];
545 }
546 if ( ! isset( $options['default_css'] ) ) {
547 $options['default_css'] = $default_options['default_css'];
548 }
549
550 if ( ! isset( $options['theme_smartphone'] ) ) {
551 $options['theme_smartphone'] = $default_options['theme_smartphone'];
552 }
553 if ( ! isset( $options['theme_tablet'] ) ) {
554 $options['theme_tablet'] = $default_options['theme_tablet'];
555 }
556 if ( ! isset( $options['theme_mobile'] ) ) {
557 $options['theme_mobile'] = $default_options['theme_mobile'];
558 }
559 if ( ! isset( $options['theme_game'] ) ) {
560 $options['theme_game'] = $default_options['theme_game'];
561 }
562
563 if ( ! isset( $options['userAgent_smart'] ) ) {
564 $options['userAgent_smart'] = $default_options['userAgent_smart'];
565 }
566 if ( ! isset( $options['userAgent_tablet'] ) ) {
567 $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
568 }
569 if ( ! isset( $options['userAgent_mobile'] ) ) {
570 $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
571 }
572 if ( ! isset( $options['userAgent_game'] ) ) {
573 $options['userAgent_game'] = $default_options['userAgent_game'];
574 }
575
576 if ( ! isset( $options['disable_path'] ) ) {
577 $options['disable_path'] = $default_options['disable_path'];
578 }
579 if ( ! isset( $options['enable_regex'] ) ) {
580 $options['enable_regex'] = $default_options['enable_regex'];
581 }
582
583 return $options;
584 }
585
586 /**
587 * Load textdomain
588 *
589 * @access public
590 *
591 * @return void
592 *
593 * @since 1.6.0
594 */
595 public function load_textdomain() {
596 load_plugin_textdomain(
597 'multi-device-switcher',
598 false,
599 dirname( plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) . '/languages/'
600 );
601 }
602
603 /**
604 * Rendering Options Setting Page.
605 *
606 * @since 1.0
607 */
608 public function render_option_page() {
609 ?>
610 <div class="wrap">
611 <div id="icon-themes" class="icon32"><br></div>
612 <h2><?php esc_html_e( 'Multi Device Switcher', $this->textdomain ); ?></h2>
613 <?php settings_errors(); ?>
614
615 <form method="post" action="options.php">
616 <?php
617 settings_fields( 'multi_device_switcher' );
618 $options = $this->get_options();
619
620 $default_theme = wp_get_theme()->get( 'Name' );
621 $themes = wp_get_themes();
622 $theme_names = array();
623
624 if ( count( $themes ) ) {
625 foreach ( $themes as $t ) {
626 $theme_names[] = $t->get( 'Name' );
627 }
628 natcasesort( $theme_names );
629 }
630 ?>
631
632 <div id="admin-tabs">
633 <fieldset id="Theme" class="options">
634 <h3 class="label"><?php esc_html_e( 'Theme', $this->textdomain ); ?></h3>
635 <table class="form-table">
636 <tr><th scope="row"><?php esc_html_e( 'Smart Phone Theme', $this->textdomain ); ?></th>
637 <td>
638
639 <?php
640 if ( count( $theme_names ) ) {
641 $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
642
643 if ( ( 'None' === $options['theme_smartphone'] ) || ( '' === $options['theme_smartphone'] ) ) {
644 $html .= '<option value="None" selected="selected">' . __( 'None', $this->textdomain ) . '</option>';
645 }
646 else {
647 $html .= '<option value="None">' . __( 'None', $this->textdomain ) . '</option>';
648 }
649
650 foreach ( $theme_names as $theme_name ) {
651 if ( $default_theme === $theme_name ) {
652 continue;
653 }
654 if ( $options['theme_smartphone'] === $theme_name ) {
655 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
656 }
657 else {
658 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
659 }
660 }
661 $html .= '</select>';
662 }
663 echo $html;
664 ?>
665 </td>
666 </tr>
667 <tr><th scope="row"><?php esc_html_e( 'Tablet PC Theme', $this->textdomain ); ?></th>
668 <td>
669
670 <?php
671 if ( count( $theme_names ) ) {
672 $html = '<select name="multi_device_switcher_options[theme_tablet]">';
673
674 if ( ( 'None' === $options['theme_tablet'] ) || ( '' === $options['theme_tablet'] ) ) {
675 $html .= '<option value="None" selected="selected">' . __( 'None', $this->textdomain ) . '</option>';
676 }
677 else {
678 $html .= '<option value="None">' . __( 'None', $this->textdomain ) . '</option>';
679 }
680
681 foreach ( $theme_names as $theme_name ) {
682 if ( $default_theme === $theme_name ) {
683 continue;
684 }
685 if ( $options['theme_tablet'] === $theme_name ) {
686 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
687 }
688 else {
689 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
690 }
691 }
692 $html .= '</select>';
693 }
694 echo $html;
695 ?>
696 </td>
697 </tr>
698 <tr><th scope="row"><?php esc_html_e( 'Mobile Phone Theme', $this->textdomain ); ?></th>
699 <td>
700
701 <?php
702 if ( count( $theme_names ) ) {
703 $html = '<select name="multi_device_switcher_options[theme_mobile]">';
704
705 if ( ( 'None' === $options['theme_mobile'] ) || ( '' === $options['theme_mobile'] ) ) {
706 $html .= '<option value="None" selected="selected">' . __( 'None', $this->textdomain ) . '</option>';
707 }
708 else {
709 $html .= '<option value="None">' . __( 'None', $this->textdomain ) . '</option>';
710 }
711
712 foreach ( $theme_names as $theme_name ) {
713 if ( $default_theme === $theme_name ) {
714 continue;
715 }
716 if ( $options['theme_mobile'] === $theme_name ) {
717 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
718 }
719 else {
720 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
721 }
722 }
723 $html .= '</select>';
724 }
725 echo $html;
726 ?>
727 </td>
728 </tr>
729 <tr><th scope="row"><?php esc_html_e( 'Game Platforms Theme', $this->textdomain ); ?></th>
730 <td>
731
732 <?php
733 if ( count( $theme_names ) ) {
734 $html = '<select name="multi_device_switcher_options[theme_game]">';
735
736 if ( ( 'None' === $options['theme_game'] ) || ( '' === $options['theme_game'] ) ) {
737 $html .= '<option value="None" selected="selected">' . __( 'None', $this->textdomain ) . '</option>';
738 }
739 else {
740 $html .= '<option value="None">' . __( 'None', $this->textdomain ) . '</option>';
741 }
742
743 foreach ( $theme_names as $theme_name ) {
744 if ( $default_theme === $theme_name ) {
745 continue;
746 }
747 if ( $options['theme_game'] === $theme_name ) {
748 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
749 }
750 else {
751 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
752 }
753 }
754 $html .= '</select>';
755 }
756 echo $html;
757 ?>
758 </td>
759 </tr>
760 </table>
761
762 <h3><?php esc_html_e( 'Custom Switcher Theme', $this->textdomain ); ?></h3>
763 <table class="form-table">
764
765 <?php
766 foreach ( $options as $custom_switcher_option => $custom_switcher_theme ) {
767 if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
768 continue;
769 }
770
771 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
772 ?>
773
774 <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
775 <td>
776
777 <?php
778 if ( count( $theme_names ) ) {
779 $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
780
781 if ( ( 'None' === $custom_switcher_theme ) || ( '' === $custom_switcher_theme ) ) {
782 $html .= '<option value="None" selected="selected">' . __( 'None', $this->textdomain ) . '</option>';
783 }
784 else {
785 $html .= '<option value="None">' . __( 'None', $this->textdomain ) . '</option>';
786 }
787
788 foreach ( $theme_names as $theme_name ) {
789 if ( $default_theme === $theme_name ) {
790 continue;
791 }
792 if ( $custom_switcher_theme === $theme_name ) {
793 $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
794 }
795 else {
796 $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
797 }
798 }
799 $html .= '</select>';
800 $html .= ' <span class="submit"><input type="submit" name="multi_device_switcher_options[delete_custom_switcher_' . $custom_switcher_name . ']" value="' . __( 'Delete', $this->textdomain ) . '" onclick="return confirm(\'' . esc_html( sprintf( __( 'Are you sure you want to delete %1$s ?', $this->textdomain ), $custom_switcher_name ) ) . '\');" class="button"></span>';
801 }
802 echo $html;
803 ?>
804 </td>
805 </tr>
806
807 <?php
808 }
809 ?>
810
811 <tr><th scope="row"><?php esc_html_e( 'Add Custom Switcher', $this->textdomain ); ?></th>
812 <td>
813 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add Custom Switcher', $this->textdomain ); ?></span></legend>
814 <input type="text" name="multi_device_switcher_options[custom_switcher]" id="custom-switcher" value="" size="24">
815 <span class="submit"><input type="submit" name="multi_device_switcher_options[add_custom_switcher]" value="<?php esc_html_e( 'Add', $this->textdomain ); ?>" class="button"></span><br>
816 <?php esc_html_e( '20 characters max, alphanumeric', $this->textdomain ); ?>
817 </td>
818 </tr>
819 </table>
820
821 </fieldset>
822
823 <fieldset id="UserAgent" class="options">
824 <h3 class="label"><?php esc_html_e( 'UserAgent', $this->textdomain ); ?></h3>
825 <p><?php esc_html_e( 'Enter Comma-separated values (csv) format.', $this->textdomain ); ?></p>
826
827 <table class="form-table">
828 <tr><th scope="row"><?php esc_html_e( 'Smart Phone', $this->textdomain ); ?></th>
829 <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_smart'] ); ?></textarea></td>
830 </tr>
831 <tr><th scope="row"><?php esc_html_e( 'Tablet PC', $this->textdomain ); ?></th>
832 <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_tablet'] ); ?></textarea></td>
833 </tr>
834 <tr><th scope="row"><?php esc_html_e( 'Mobile Phone', $this->textdomain ); ?></th>
835 <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_mobile'] ); ?></textarea></td>
836 </tr>
837 <tr><th scope="row"><?php esc_html_e( 'Game Platforms', $this->textdomain ); ?></th>
838 <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_game'] ); ?></textarea></td>
839 </tr>
840 <tr><th></th>
841 <td><span class="submit"><input type="submit" name="multi_device_switcher_options[restore_UserAgent]" value="<?php esc_html_e( 'Reset Settings to Default UserAgent', $this->textdomain ); ?>" class="button"></span></td>
842 </tr>
843
844 </table>
845
846 <h3><?php esc_html_e( 'Custom Switcher UserAgent', $this->textdomain ); ?></h3>
847 <table class="form-table">
848 <?php
849 foreach ( $options as $custom_switcher_option => $custom_switcher_userAgent ) {
850 if ( ! preg_match( '/^custom_switcher_userAgent_/', $custom_switcher_option ) ) {
851 continue;
852 }
853
854 $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $custom_switcher_option );
855 ?>
856
857 <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
858 <td><textarea name="multi_device_switcher_options[<?php echo esc_attr( $custom_switcher_option ); ?>]" rows="4" cols="42"><?php echo esc_textarea( $custom_switcher_userAgent ); ?></textarea></td>
859 </tr>
860 <?php
861 }
862 ?>
863
864 </table>
865 </fieldset>
866
867 <fieldset id="PC-Switcher" class="options">
868 <h3 class="label"><?php esc_html_e( 'PC Switcher', $this->textdomain ); ?></h3>
869
870 <table class="form-table">
871 <tr><th scope="row"><?php esc_html_e( 'Add PC Switcher', $this->textdomain ); ?></th>
872 <td>
873 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add PC Switcher', $this->textdomain ); ?></span></legend>
874 <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.', $this->textdomain ); ?></label>
875 </td>
876 </tr>
877 <tr><th scope="row"><?php esc_html_e( 'Add default CSS', $this->textdomain ); ?></th>
878 <td>
879 <legend class="screen-reader-text"><span><?php esc_html_e( 'Add default CSS', $this->textdomain ); ?></span></legend>
880 <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.', $this->textdomain ); ?></label>
881 </td>
882 </tr>
883 </table>
884 </fieldset>
885
886 <fieldset id="Disable-Switcher" class="options">
887 <h3 class="label"><?php esc_html_e( 'Disable Switcher', $this->textdomain ); ?></h3>
888
889 <table class="form-table">
890 <tr><th scope="row"><?php esc_html_e( 'Path', $this->textdomain ); ?></th>
891 <td>
892 <legend class="screen-reader-text"><span><?php esc_html_e( 'Path', $this->textdomain ); ?></span></legend>
893 <?php echo esc_html( home_url() ); ?><br>
894 <textarea name="multi_device_switcher_options[disable_path]" rows="16" cols="42" wrap="off"><?php echo esc_textarea( $options['disable_path'] ); ?></textarea>
895 </td>
896 </tr>
897 <tr><th scope="row"><?php esc_html_e( 'Regex mode', $this->textdomain ); ?></th>
898 <td>
899 <legend class="screen-reader-text"><span><?php esc_html_e( 'Regex mode', $this->textdomain ); ?></span></legend>
900 <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', $this->textdomain ); ?></label>
901 </td>
902 </tr>
903 </table>
904 </fieldset>
905
906 </div>
907 <?php submit_button(); ?>
908 </form>
909 </div>
910
911 <div id="donate">
912 <h2><?php esc_html_e( 'Donationware', $this->textdomain ); ?></h2>
913 <p><?php esc_html_e( 'If you like this plugin, please donate to support development and maintenance.', $this->textdomain ); ?></p>
914 <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
915 <input type="hidden" name="cmd" value="_s-xclick">
916 <input type="hidden" name="hosted_button_id" value="9L53NELFMHTWW">
917 <table>
918 <tr><td><input type="hidden" name="on0" value="Donationware">Donationware</td></tr><tr><td><select name="os0">
919 <option value="1. Donate">1. Donate $3.00 USD</option>
920 <option value="2. Donate">2. Donate $5.00 USD</option>
921 <option value="3. Donate">3. Donate $7.00 USD</option>
922 <option value="4. Donate" selected="selected">4. Donate $10.00 USD</option>
923 <option value="5. Donate">5. Donate $20.00 USD</option>
924 <option value="6. Donate">6. Donate $30.00 USD</option>
925 <option value="7. Donate">7. Donate $40.00 USD</option>
926 <option value="8. Donate">8. Donate $50.00 USD</option>
927 <option value="9. Donate">9. Donate $60.00 USD</option>
928 <option value="10. Donate">10. Donate $70.00 USD</option>
929 </select> </td></tr>
930 </table>
931 <input type="hidden" name="currency_code" value="USD">
932 <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
933 <img alt="" border="0" src="https://www.paypalobjects.com/ja_JP/i/scr/pixel.gif" width="1" height="1">
934 </form>
935 </div>
936 <?php
937 }
938
939 /**
940 * Sanitize and validate form input. Accepts an array, return a sanitized array.
941 *
942 * @see admin_init()
943 *
944 * @since 1.0
945 */
946 public function validate( $input ) {
947 $output = $default_options = $this->get_default_options();
948
949 if ( isset( $input['theme_smartphone'] ) ) {
950 $output['theme_smartphone'] = $input['theme_smartphone'];
951 }
952 if ( isset( $input['theme_tablet'] ) ) {
953 $output['theme_tablet'] = $input['theme_tablet'];
954 }
955 if ( isset( $input['theme_mobile'] ) ) {
956 $output['theme_mobile'] = $input['theme_mobile'];
957 }
958 if ( isset( $input['theme_game'] ) ) {
959 $output['theme_game'] = $input['theme_game'];
960 }
961
962 if ( isset( $input['restore_UserAgent'] ) ) {
963 $output['userAgent_smart'] = $default_options['userAgent_smart'];
964 $output['userAgent_tablet'] = $default_options['userAgent_tablet'];
965 $output['userAgent_mobile'] = $default_options['userAgent_mobile'];
966 $output['userAgent_game'] = $default_options['userAgent_game'];
967 }
968 else {
969 if ( isset( $input['userAgent_smart'] ) ) {
970 $output['userAgent_smart'] = $input['userAgent_smart'];
971 }
972 if ( isset( $input['userAgent_tablet'] ) ) {
973 $output['userAgent_tablet'] = $input['userAgent_tablet'];
974 }
975 if ( isset( $input['userAgent_mobile'] ) ) {
976 $output['userAgent_mobile'] = $input['userAgent_mobile'];
977 }
978 if ( isset( $input['userAgent_game'] ) ) {
979 $output['userAgent_game'] = $input['userAgent_game'];
980 }
981 }
982
983 foreach ( $input as $key => $val ) {
984 if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
985 continue;
986 }
987
988 $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
989
990 if ( isset( $input[ 'custom_switcher_theme_' . $custom_switcher_name ] ) ) {
991 $output[ 'custom_switcher_theme_' . $custom_switcher_name ] = $input[ 'custom_switcher_theme_' . $custom_switcher_name ];
992 }
993 if ( isset( $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ] ) ) {
994 $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] = $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ];
995 }
996 }
997
998 foreach ( $input as $key => $val ) {
999 if ( ! preg_match( '/^delete_custom_switcher_/', $key ) ) {
1000 continue;
1001 }
1002
1003 $custom_switcher_name = preg_replace( '/^delete_custom_switcher_/', '', $key );
1004
1005 unset( $output[ 'custom_switcher_theme_' . $custom_switcher_name ] );
1006 unset( $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] );
1007 }
1008
1009 if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! isset( $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] ) ) {
1010 if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) )
1011 && preg_match( '/^[A-Za-z0-9]{1,20}$/', $input['custom_switcher'] ) ) {
1012 $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] = 'None';
1013 $output[ 'custom_switcher_userAgent_' . $input['custom_switcher'] ] = '';
1014 }
1015 }
1016
1017 $output['pc_switcher'] = isset( $input['pc_switcher'] ) ? $input['pc_switcher'] : 0;
1018 $output['default_css'] = isset( $input['default_css'] ) ? $input['default_css'] : 0;
1019
1020 $output['disable_path'] = isset( $input['disable_path'] ) ? $input['disable_path'] : '';
1021 $output['enable_regex'] = isset( $input['enable_regex'] ) ? $input['enable_regex'] : 0;
1022
1023 return apply_filters( 'multi_device_switcher_validate', $output, $input, $default_options );
1024 }
1025
1026 /**
1027 * plugin customization options
1028 *
1029 * @param $wp_customize Theme Customizer object
1030 * @return void
1031 *
1032 * @since 1.3.1
1033 */
1034 public function customize_register( $wp_customize ) {
1035 $options = $this->get_options();
1036 $default_theme_options = $this->get_default_options();
1037 $default_theme = wp_get_theme()->get( 'Name' );
1038 $themes = wp_get_themes();
1039
1040 $theme_names = array();
1041 $choices = array();
1042
1043 if ( count( $themes ) ) {
1044 foreach ( $themes as $t ) {
1045 $theme_names[] = $t->get( 'Name' );
1046 }
1047 natcasesort( $theme_names );
1048
1049 $choices['None'] = __( 'None', $this->textdomain );
1050 foreach ( $theme_names as $theme_name ) {
1051 if ( $default_theme === $theme_name ) {
1052 continue;
1053 }
1054 $choices[ $theme_name ] = $theme_name;
1055 }
1056 }
1057
1058 $switcher = array(
1059 'theme_smartphone' => __( 'Smart Phone Theme', $this->textdomain ),
1060 'theme_tablet' => __( 'Tablet PC Theme', $this->textdomain ),
1061 'theme_mobile' => __( 'Mobile Phone Theme', $this->textdomain ),
1062 'theme_game' => __( 'Game Platforms Theme', $this->textdomain ),
1063 );
1064
1065 $wp_customize->add_section( 'multi_device_switcher', array(
1066 'title' => __( 'Multi Device Switcher', $this->textdomain ),
1067 'priority' => 80,
1068 ) );
1069
1070 foreach ( $switcher as $name => $label ) {
1071 $wp_customize->add_setting( 'multi_device_switcher_options[' . $name . ']', array(
1072 'default' => $default_theme_options[ $name ],
1073 'type' => 'option',
1074 'capability' => $this->capability,
1075 ) );
1076
1077 $wp_customize->add_control( 'multi_device_switcher_options[' . $name . ']', array(
1078 'label' => $label,
1079 'section' => 'multi_device_switcher',
1080 'type' => 'select',
1081 'choices' => $choices,
1082 ) );
1083 }
1084
1085 foreach ( $options as $custom_switcher_option => $custom_switcher_theme ) {
1086 if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1087 continue;
1088 }
1089
1090 $label = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
1091
1092 $wp_customize->add_setting( 'multi_device_switcher_options[' . $custom_switcher_option . ']', array(
1093 'default' => __( 'None', $this->textdomain ),
1094 'type' => 'option',
1095 'capability' => $this->capability,
1096 ) );
1097
1098 $wp_customize->add_control( 'multi_device_switcher_options[' . $custom_switcher_option . ']', array(
1099 'label' => $label,
1100 'section' => 'multi_device_switcher',
1101 'type' => 'select',
1102 'choices' => $choices,
1103 ) );
1104
1105 }
1106 }
1107
1108 public function load_file() {
1109 /**
1110 * include PC Switcher Widget.
1111 *
1112 * @since 1.2
1113 *
1114 */
1115 require_once( dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/pc-switcher-widget.php' );
1116
1117 /**
1118 * include Multi Device Switcher Command
1119 *
1120 * @since 1.4
1121 *
1122 */
1123 if ( defined( 'WP_CLI' ) && WP_CLI ) {
1124 require_once( dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/wp-cli.php' );
1125 }
1126 }
1127 }
1128
1129 define( '__MULTI_DEVICE_SWITCHER_FILE__', __FILE__ );
1130
1131 if ( class_exists( 'Multi_Device_Switcher' ) ) {
1132 $multi_device_switcher = new Multi_Device_Switcher();
1133 };
1134
1135 /**
1136 * Add PC Switcher.
1137 *
1138 * @since 1.2
1139 *
1140 */
1141 function multi_device_switcher_add_pc_switcher() {
1142 global $multi_device_switcher;
1143 if ( is_object( $multi_device_switcher ) ) {
1144 $multi_device_switcher->add_pc_switcher( 1 );
1145 }
1146 }
1147
1148 /**
1149 * Return boolean whether a particular device.
1150 *
1151 * @since 1.2.4
1152 *
1153 */
1154 if ( ! function_exists( 'is_multi_device' ) ) :
1155
1156 function is_multi_device( $device = '' ) {
1157 global $multi_device_switcher;
1158 if ( is_object( $multi_device_switcher ) ) {
1159 return $multi_device_switcher->is_multi_device( $device );
1160 }
1161 }
1162 endif;
1163
1164 /**
1165 * Return the state of PC Switcher.
1166 *
1167 * @since 1.4.1
1168 *
1169 */
1170 if ( ! function_exists( 'is_pc_switcher' ) ) :
1171
1172 function is_pc_switcher() {
1173 global $multi_device_switcher;
1174 if ( is_object( $multi_device_switcher ) ) {
1175 return $multi_device_switcher->is_pc_switcher();
1176 }
1177 }
1178 endif;
1179
1180 /**
1181 * Return the state of disabled.
1182 *
1183 * @since 1.4.1
1184 *
1185 */
1186 if ( ! function_exists( 'is_disable_switcher' ) ) :
1187
1188 function is_disable_switcher() {
1189 global $multi_device_switcher;
1190 if ( is_object( $multi_device_switcher ) ) {
1191 return $multi_device_switcher->is_disable_switcher();
1192 }
1193 }
1194 endif;
1195
1196 /**
1197 * Returns the default options.
1198 *
1199 * @since 1.5.3
1200 */
1201 if ( ! function_exists( 'multi_device_switcher_get_default_options' ) ) :
1202
1203 function multi_device_switcher_get_default_options() {
1204 global $multi_device_switcher;
1205 if ( is_object( $multi_device_switcher ) ) {
1206 return $multi_device_switcher->get_default_options();
1207 }
1208 }
1209 endif;
1210