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

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