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

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