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

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