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

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