Multi Device Switcher - Version 1.5.0

Version Description

  • fixed: refactoring by the PHP_CodeSniffer
  • fixed: change admin functions to class/object methods
Download this release

Release Info

Developer thingsym
Plugin Icon wp plugin Multi Device Switcher
Version 1.5.0
Comparing to
See all releases

Code changes from version 1.4.2 to 1.5.0

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