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

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