Multi Device Switcher - Version 1.4.0

Version Description

  • edited: edit readme
  • added: add cookies 'multi-device-switcher', 'disable-switcher'
  • new features: Disable Switcher
  • fixed: fix get_options_userAgent(), multi_device_switcher_get_options()
  • fixed: add reserved words validate
  • new features: Multi Device Switcher Command
  • added: add option settings into Theme Customizer
  • fixed: refactoring by the PHP_CodeSniffer
Download this release

Release Info

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

Code changes from version 1.3.0 to 1.4.0

languages/multi-device-switcher-ja.mo CHANGED
Binary file
languages/multi-device-switcher-ja.po CHANGED
@@ -3,7 +3,7 @@ msgstr ""
3
  "Project-Id-Version: Multi Device Switcher\n"
4
  "Report-Msgid-Bugs-To: \n"
5
  "POT-Creation-Date: 2012-01-17 22:01+0900\n"
6
- "PO-Revision-Date: 2014-09-11 20:02+0900\n"
7
  "Last-Translator: \n"
8
  "Language-Team: Thingsym\n"
9
  "Language: ja_JP\n"
@@ -98,6 +98,18 @@ msgstr "PC"
98
  msgid "Are you sure you want to delete %1$s ?"
99
  msgstr "%1$s を削除してよろしいですか?"
100
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  msgid "Donationware"
102
  msgstr "ドネーションウェア (寄付)"
103
 
3
  "Project-Id-Version: Multi Device Switcher\n"
4
  "Report-Msgid-Bugs-To: \n"
5
  "POT-Creation-Date: 2012-01-17 22:01+0900\n"
6
+ "PO-Revision-Date: 2015-01-15 17:26+0900\n"
7
  "Last-Translator: \n"
8
  "Language-Team: Thingsym\n"
9
  "Language: ja_JP\n"
98
  msgid "Are you sure you want to delete %1$s ?"
99
  msgstr "%1$s を削除してよろしいですか?"
100
 
101
+ msgid "Disable Switcher"
102
+ msgstr "切り替え無効"
103
+
104
+ msgid "Path"
105
+ msgstr "パス"
106
+
107
+ msgid "Regex mode"
108
+ msgstr "正規表現モード"
109
+
110
+ msgid "Enable Regex"
111
+ msgstr "正規表現を有効化"
112
+
113
  msgid "Donationware"
114
  msgstr "ドネーションウェア (寄付)"
115
 
languages/multi-device-switcher.pot CHANGED
@@ -98,6 +98,18 @@ msgstr ""
98
  msgid "Are you sure you want to delete %1$s ?"
99
  msgstr ""
100
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  msgid "Donationware"
102
  msgstr ""
103
 
98
  msgid "Are you sure you want to delete %1$s ?"
99
  msgstr ""
100
 
101
+ msgid "Disable Switcher"
102
+ msgstr ""
103
+
104
+ msgid "Path"
105
+ msgstr ""
106
+
107
+ msgid "Regex mode"
108
+ msgstr ""
109
+
110
+ msgid "Enable Regex"
111
+ msgstr ""
112
+
113
  msgid "Donationware"
114
  msgstr ""
115
 
multi-device-switcher.php CHANGED
@@ -3,7 +3,7 @@
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.3.0
7
  Author: thingsym
8
  Author URI: http://www.thingslabo.com/
9
  License: GPL2
@@ -32,154 +32,162 @@ Domain Path: /languages/
32
  class Multi_Device_Switcher {
33
 
34
  public function __construct() {
35
- add_action('init', array(&$this, 'session'));
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  $userAgent = $this->get_options_userAgent();
38
- $this->device = '';
39
- $server_ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
40
 
41
- foreach ( array_reverse($userAgent) as $key => $val ) {
42
- if ( !preg_match( "/^custom_switcher_/", $key ) )
43
  continue;
44
- if ( $userAgent[$key] && preg_match( '/' . implode( '|', $userAgent[$key] ) . '/i', $server_ua) ) {
 
45
  $this->device = $key;
46
  break;
47
  }
48
  }
49
 
50
- if ( !$this->device ) {
51
- if ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $server_ua) ) {
52
  $this->device = 'game';
53
  }
54
- elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $server_ua) ) {
55
  $this->device = 'tablet';
56
  }
57
- elseif ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $server_ua) ) {
58
  $this->device = 'smart';
59
  }
60
- elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $server_ua) ) {
61
  $this->device = 'mobile';
62
  }
63
  }
64
 
65
- if ($this->device) {
66
- load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
67
- add_filter('stylesheet', array(&$this, 'get_stylesheet'));
68
- add_filter('template', array(&$this, 'get_template'));
69
- add_action('wp_footer', array(&$this, 'add_pc_switcher'));
 
 
 
 
 
70
  }
71
 
72
- if ( isset($_COOKIE['pc-switcher']) ) {
73
- remove_filter('stylesheet', array(&$this, 'get_stylesheet'));
74
- remove_filter('template', array(&$this, 'get_template'));
75
  }
76
  }
77
 
78
  public function get_options_userAgent() {
79
- $options = get_option('multi_device_switcher_options');
80
- $default_options = multi_device_switcher_get_default_options();
81
-
82
- if ( !isset( $options['userAgent_smart'] ) )
83
- $options['userAgent_smart'] = $default_options['userAgent_smart'];
84
- if ( !isset( $options['userAgent_tablet'] ) )
85
- $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
86
- if ( !isset( $options['userAgent_mobile'] ) )
87
- $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
88
- if ( !isset( $options['userAgent_game'] ) )
89
- $options['userAgent_game'] = $default_options['userAgent_game'];
90
-
91
- if ( $options['userAgent_smart'] )
92
- $userAgent['smart'] = preg_split( "/,\s*/", $options['userAgent_smart'] );
93
- if ( $options['userAgent_tablet'] )
94
- $userAgent['tablet'] = preg_split( "/,\s*/", $options['userAgent_tablet'] );
95
- if ( $options['userAgent_mobile'] )
96
- $userAgent['mobile'] = preg_split( "/,\s*/", $options['userAgent_mobile'] );
97
- if ( $options['userAgent_game'] )
98
- $userAgent['game'] = preg_split( "/,\s*/", $options['userAgent_game'] );
99
 
100
  foreach ( $options as $key => $val ) {
101
- if ( !preg_match( "/^custom_switcher_userAgent_/", $key ) )
102
  continue;
 
103
 
104
- $custom_switcher_name = preg_replace("/^custom_switcher_userAgent_/", '', $key);
105
-
106
- if ($val)
107
- $userAgent['custom_switcher_' . $custom_switcher_name] = preg_split( "/,\s*/", $val );
108
  }
109
 
110
  return $userAgent;
111
  }
112
 
113
- public function get_stylesheet($stylesheet = '') {
114
  $name = $this->get_device_theme();
115
 
116
- if ( empty($name) )
117
  return $stylesheet;
 
118
 
119
  $themes = wp_get_themes();
120
  foreach ( $themes as $t ) {
121
- if ( $name == $t->get('Name') ) {
122
  $theme = $t;
123
  break;
124
  }
125
  }
126
 
127
- if ( empty($theme) )
128
  return $stylesheet;
 
129
 
130
- if ( $theme->get('Status') != 'publish' )
131
  return $stylesheet;
 
132
 
133
  return $theme['Stylesheet'];
134
  }
135
 
136
- public function get_template($template = '') {
137
  $name = $this->get_device_theme();
138
 
139
- if ( empty($name) )
140
  return $template;
 
141
 
142
  $themes = wp_get_themes();
143
  foreach ( $themes as $t ) {
144
- if ( $name == $t->get('Name') ) {
145
  $theme = $t;
146
  break;
147
  }
148
  }
149
 
150
- if ( empty( $theme ) )
151
  return $template;
 
152
 
153
- if ( $theme->get('Status') != 'publish' )
154
  return $template;
 
155
 
156
  return $theme['Template'];
157
  }
158
 
159
  public function get_device_theme() {
160
- $options = get_option('multi_device_switcher_options');
161
 
162
- if ($this->device == 'smart') {
163
  return $options['theme_smartphone'];
164
  }
165
- elseif ($this->device == 'tablet') {
166
  return $options['theme_tablet'];
167
  }
168
- elseif ($this->device == 'mobile') {
169
  return $options['theme_mobile'];
170
  }
171
- elseif ($this->device == 'game') {
172
  return $options['theme_game'];
173
  }
174
  else {
175
  foreach ( $options as $key => $val ) {
176
- if ( !preg_match( "/^custom_switcher_theme_/", $key ) )
177
  continue;
 
178
 
179
- $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
180
 
181
- if ( $this->device == 'custom_switcher_' . $custom_switcher_name ) {
182
- return $options[$key];
183
  }
184
  }
185
  }
@@ -188,58 +196,86 @@ class Multi_Device_Switcher {
188
  }
189
 
190
  public function session() {
191
- if ( isset($_GET['pc-switcher']) ) {
192
  setcookie( 'pc-switcher', $_GET['pc-switcher'] ? 1 : '', null, '/' );
193
 
194
  $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
195
 
196
  unset( $_GET['pc-switcher'] );
197
- if ( !empty($_GET) )
198
- $uri = $uri . '?' . http_build_query($_GET);
 
199
 
200
- wp_redirect( esc_attr($uri) );
201
  exit;
202
  }
203
  }
204
 
205
- public function add_pc_switcher($pc_switcher = 0) {
206
- $options = get_option('multi_device_switcher_options');
207
  $name = $this->get_device_theme();
208
 
209
- if ($options['pc_switcher'])
210
  $pc_switcher = 1;
 
211
 
212
- if ( $pc_switcher && $name && $name != 'None' ) {
213
- if ($options['default_css'])
214
  wp_enqueue_style( 'pc-switcher-options', plugins_url() . '/multi-device-switcher/pc-switcher.css', false, '2013-03-20' );
 
215
 
216
- if ( isset($_COOKIE['pc-switcher']) ) {
217
- $uri = add_query_arg( 'pc-switcher', 0 );
218
  ?>
219
- <div class="pc-switcher"><a href="<?php echo $uri; ?>"><?php _e( 'Mobile', 'multi-device-switcher' ); ?></a><span class="active"><?php _e( 'PC', 'multi-device-switcher' ); ?></span></div>
220
  <?php
221
  }
222
  else {
223
- $uri = add_query_arg( 'pc-switcher', 1 );
224
  ?>
225
- <div class="pc-switcher"><span class="active"><?php _e( 'Mobile', 'multi-device-switcher' ); ?></span><a href="<?php echo $uri; ?>"><?php _e( 'PC', 'multi-device-switcher' ); ?></a></div>
226
  <?php
227
  }
228
  }
229
  }
230
 
231
  public function is_multi_device( $device = '' ) {
232
- if ( $this->device == $device )
233
  return (boolean) 1;
234
- if ( $this->device == 'custom_switcher_' . $device )
 
235
  return (boolean) 1;
 
236
 
237
  return (boolean) 0;
238
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  }
240
 
241
- if ( !is_admin() )
242
  $multi_device_switcher = new Multi_Device_Switcher();
 
243
 
244
  /**
245
  * Add HTTP/1.1 Vary header.
@@ -248,7 +284,7 @@ if ( !is_admin() )
248
  *
249
  */
250
  function multi_device_switcher_add_header_vary( $headers ) {
251
- if ( !is_admin() ) {
252
  $headers['Vary'] = 'User-Agent';
253
  return $headers;
254
  }
@@ -263,7 +299,7 @@ add_filter( 'wp_headers', 'multi_device_switcher_add_header_vary' );
263
  */
264
  function multi_device_switcher_add_pc_switcher() {
265
  global $multi_device_switcher;
266
- $multi_device_switcher->add_pc_switcher(1);
267
  }
268
 
269
  /**
@@ -272,11 +308,11 @@ function multi_device_switcher_add_pc_switcher() {
272
  * @since 1.2.4
273
  *
274
  */
275
- if ( !function_exists( 'is_multi_device' ) ) :
276
 
277
  function is_multi_device( $device = '' ) {
278
  global $multi_device_switcher;
279
- return $multi_device_switcher->is_multi_device($device);
280
  }
281
  endif;
282
 
@@ -317,8 +353,9 @@ function multi_device_switcher_admin_enqueue_styles( $hook_suffix ) {
317
  */
318
  function multi_device_switcher_init() {
319
  // If we have no options in the database, let's add them now.
320
- if ( false === multi_device_switcher_get_options() )
321
  add_option( 'multi_device_switcher_options' );
 
322
 
323
  register_setting(
324
  'multi_device_switcher', // Options group, see settings_fields() call in multi_device_switcher_render_page()
@@ -355,7 +392,7 @@ add_filter( 'option_page_capability_multi_device_switcher', 'multi_device_switch
355
  * @since 1.0
356
  */
357
  function multi_device_switcher_add_page() {
358
- load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
359
 
360
  add_filter( 'plugin_action_links', 'multi_device_switcher_plugin_action_links', 10, 2 );
361
 
@@ -367,8 +404,9 @@ function multi_device_switcher_add_page() {
367
  'multi_device_switcher_render_page' // Function that renders the options page
368
  );
369
 
370
- if ( !$page_hook )
371
  return;
 
372
 
373
  add_action( 'load-' . $page_hook , 'multi_device_switcher_page_hook_suffix' );
374
  }
@@ -392,8 +430,9 @@ function multi_device_switcher_page_hook_suffix() {
392
  * @since 1.2
393
  */
394
  function multi_device_switcher_plugin_action_links( $links, $file ) {
395
- if ( $file != plugin_basename( __FILE__ ))
396
  return $links;
 
397
 
398
  $settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', 'multi-device-switcher' ) . '</a>';
399
 
@@ -419,6 +458,8 @@ function multi_device_switcher_get_default_options() {
419
  'userAgent_tablet' => 'iPad, Kindle, Sony Tablet, Nexus 7',
420
  'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
421
  'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, PlayStation 4, Nitro, Nintendo 3DS, Nintendo Wii, Nintendo WiiU, Xbox',
 
 
422
  );
423
 
424
  return $default_theme_options;
@@ -433,28 +474,45 @@ function multi_device_switcher_get_options() {
433
  $options = get_option( 'multi_device_switcher_options' );
434
  $default_options = multi_device_switcher_get_default_options();
435
 
436
- if ( !isset( $options['pc_switcher'] ) )
437
  $options['pc_switcher'] = $default_options['pc_switcher'];
438
- if ( !isset( $options['default_css'] ) )
 
439
  $options['default_css'] = $default_options['default_css'];
 
440
 
441
- if ( !isset( $options['theme_smartphone'] ) )
442
  $options['theme_smartphone'] = $default_options['theme_smartphone'];
443
- if ( !isset( $options['theme_tablet'] ) )
 
444
  $options['theme_tablet'] = $default_options['theme_tablet'];
445
- if ( !isset( $options['theme_mobile'] ) )
 
446
  $options['theme_mobile'] = $default_options['theme_mobile'];
447
- if ( !isset( $options['theme_game'] ) )
 
448
  $options['theme_game'] = $default_options['theme_game'];
 
449
 
450
- if ( !isset( $options['userAgent_smart'] ) )
451
  $options['userAgent_smart'] = $default_options['userAgent_smart'];
452
- if ( !isset( $options['userAgent_tablet'] ) )
 
453
  $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
454
- if ( !isset( $options['userAgent_mobile'] ) )
 
455
  $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
456
- if ( !isset( $options['userAgent_game'] ) )
 
457
  $options['userAgent_game'] = $default_options['userAgent_game'];
 
 
 
 
 
 
 
 
458
 
459
  return $options;
460
  }
@@ -468,7 +526,7 @@ function multi_device_switcher_render_page() {
468
  ?>
469
  <div class="wrap">
470
  <div id="icon-themes" class="icon32"><br></div>
471
- <h2><?php printf( __( 'Multi Device Switcher', 'multi-device-switcher' ), wp_get_theme()->get('Name') ); ?></h2>
472
  <?php settings_errors(); ?>
473
 
474
  <form method="post" action="options.php">
@@ -476,15 +534,15 @@ function multi_device_switcher_render_page() {
476
  settings_fields( 'multi_device_switcher' );
477
  $options = multi_device_switcher_get_options();
478
 
479
- $default_theme = wp_get_theme()->get('Name');
480
  $themes = wp_get_themes();
481
  $theme_names = array();
482
 
483
- if ( count($themes) ) {
484
  foreach ( $themes as $t ) {
485
- $theme_names[] = $t->get('Name');
486
  }
487
- natcasesort($theme_names);
488
  }
489
  ?>
490
 
@@ -496,10 +554,10 @@ function multi_device_switcher_render_page() {
496
  <td>
497
 
498
  <?php
499
- if ( count($theme_names) ) {
500
  $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
501
 
502
- if ( ($options['theme_smartphone'] == 'None') || ($options['theme_smartphone'] == '') ) {
503
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
504
  }
505
  else {
@@ -507,13 +565,14 @@ function multi_device_switcher_render_page() {
507
  }
508
 
509
  foreach ( $theme_names as $theme_name ) {
510
- if ( $default_theme == $theme_name )
511
  continue;
 
512
  if ( $options['theme_smartphone'] == $theme_name ) {
513
- $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
514
  }
515
  else {
516
- $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
517
  }
518
  }
519
  $html .= '</select>';
@@ -526,10 +585,10 @@ function multi_device_switcher_render_page() {
526
  <td>
527
 
528
  <?php
529
- if ( count($theme_names) ) {
530
  $html = '<select name="multi_device_switcher_options[theme_tablet]">';
531
 
532
- if ( ($options['theme_tablet'] == 'None') || ($options['theme_tablet'] == '') ) {
533
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
534
  }
535
  else {
@@ -537,13 +596,14 @@ function multi_device_switcher_render_page() {
537
  }
538
 
539
  foreach ( $theme_names as $theme_name ) {
540
- if ( $default_theme == $theme_name )
541
  continue;
 
542
  if ( $options['theme_tablet'] == $theme_name ) {
543
- $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
544
  }
545
  else {
546
- $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
547
  }
548
  }
549
  $html .= '</select>';
@@ -556,10 +616,10 @@ function multi_device_switcher_render_page() {
556
  <td>
557
 
558
  <?php
559
- if ( count($theme_names) ) {
560
  $html = '<select name="multi_device_switcher_options[theme_mobile]">';
561
 
562
- if ( ($options['theme_mobile'] == 'None') || ($options['theme_mobile'] == '') ) {
563
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
564
  }
565
  else {
@@ -567,13 +627,14 @@ function multi_device_switcher_render_page() {
567
  }
568
 
569
  foreach ( $theme_names as $theme_name ) {
570
- if ( $default_theme == $theme_name )
571
  continue;
 
572
  if ( $options['theme_mobile'] == $theme_name ) {
573
- $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
574
  }
575
  else {
576
- $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
577
  }
578
  }
579
  $html .= '</select>';
@@ -586,10 +647,10 @@ function multi_device_switcher_render_page() {
586
  <td>
587
 
588
  <?php
589
- if ( count($theme_names) ) {
590
  $html = '<select name="multi_device_switcher_options[theme_game]">';
591
 
592
- if ( ($options['theme_game'] == 'None') || ($options['theme_game'] == '') ) {
593
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
594
  }
595
  else {
@@ -597,13 +658,14 @@ function multi_device_switcher_render_page() {
597
  }
598
 
599
  foreach ( $theme_names as $theme_name ) {
600
- if ( $default_theme == $theme_name )
601
  continue;
 
602
  if ( $options['theme_game'] == $theme_name ) {
603
- $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
604
  }
605
  else {
606
- $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
607
  }
608
  }
609
  $html .= '</select>';
@@ -618,23 +680,22 @@ function multi_device_switcher_render_page() {
618
  <table class="form-table">
619
 
620
  <?php
621
- foreach ( $options as $key => $val ) {
622
- if ( !preg_match( "/^custom_switcher_theme_/", $key ) )
623
  continue;
 
624
 
625
- $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
626
- $custom_switcher_option = $key;
627
- $custom_switcher_theme = $val;
628
  ?>
629
 
630
- <tr><th scope="row"><?php _e( $custom_switcher_name, 'multi-device-switcher' ); ?></th>
631
  <td>
632
 
633
  <?php
634
- if ( count($theme_names) ) {
635
  $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
636
 
637
- if ( ($custom_switcher_theme == 'None') || ($custom_switcher_theme == '') ) {
638
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
639
  }
640
  else {
@@ -642,17 +703,18 @@ function multi_device_switcher_render_page() {
642
  }
643
 
644
  foreach ( $theme_names as $theme_name ) {
645
- if ( $default_theme == $theme_name )
646
  continue;
 
647
  if ( $custom_switcher_theme == $theme_name ) {
648
- $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
649
  }
650
  else {
651
- $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
652
  }
653
  }
654
  $html .= '</select>';
655
- $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(\'' . sprintf( __( 'Are you sure you want to delete %1$s ?', 'multi-device-switcher' ), $custom_switcher_name ) . '\');" class="button"></span>';
656
  }
657
  echo $html;
658
  ?>
@@ -681,16 +743,16 @@ function multi_device_switcher_render_page() {
681
 
682
  <table class="form-table">
683
  <tr><th scope="row"><?php _e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
684
- <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo $options['userAgent_smart']; ?></textarea></td>
685
  </tr>
686
  <tr><th scope="row"><?php _e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
687
- <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo $options['userAgent_tablet']; ?></textarea></td>
688
  </tr>
689
  <tr><th scope="row"><?php _e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
690
- <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo $options['userAgent_mobile']; ?></textarea></td>
691
  </tr>
692
  <tr><th scope="row"><?php _e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
693
- <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo $options['userAgent_game']; ?></textarea></td>
694
  </tr>
695
  <tr><th></th>
696
  <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>
@@ -701,17 +763,16 @@ function multi_device_switcher_render_page() {
701
  <h3><?php _e( 'Custom Switcher UserAgent', 'multi-device-switcher' ); ?></h3>
702
  <table class="form-table">
703
  <?php
704
- foreach ( $options as $key => $val ) {
705
- if ( !preg_match( "/^custom_switcher_userAgent_/", $key ) )
706
  continue;
 
707
 
708
- $custom_switcher_name = preg_replace("/^custom_switcher_userAgent_/", '', $key);
709
- $custom_switcher_option = $key;
710
- $custom_switcher_userAgent = $val;
711
  ?>
712
 
713
- <tr><th scope="row"><?php _e( $custom_switcher_name, 'multi-device-switcher' ); ?></th>
714
- <td><textarea name="multi_device_switcher_options[<?php echo $custom_switcher_option; ?>]" rows="4" cols="42"><?php echo $custom_switcher_userAgent; ?></textarea></td>
715
  </tr>
716
  <?php
717
  }
@@ -727,13 +788,33 @@ function multi_device_switcher_render_page() {
727
  <tr><th scope="row"><?php _e( 'Add PC Switcher', 'multi-device-switcher' ); ?></th>
728
  <td>
729
  <legend class="screen-reader-text"><span><?php _e( 'Add PC Switcher', 'multi-device-switcher' ); ?></span></legend>
730
- <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>
731
  </td>
732
  </tr>
733
  <tr><th scope="row"><?php _e( 'Add default CSS', 'multi-device-switcher' ); ?></th>
734
  <td>
735
  <legend class="screen-reader-text"><span><?php _e( 'Add default CSS', 'multi-device-switcher' ); ?></span></legend>
736
- <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>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
737
  </td>
738
  </tr>
739
  </table>
@@ -783,72 +864,182 @@ function multi_device_switcher_render_page() {
783
  function multi_device_switcher_validate( $input ) {
784
  $output = $default_options = multi_device_switcher_get_default_options();
785
 
786
- if ( isset( $input['theme_smartphone'] ) )
787
  $output['theme_smartphone'] = $input['theme_smartphone'];
788
- if ( isset( $input['theme_tablet'] ) )
 
789
  $output['theme_tablet'] = $input['theme_tablet'];
790
- if ( isset( $input['theme_mobile'] ) )
 
791
  $output['theme_mobile'] = $input['theme_mobile'];
792
- if ( isset( $input['theme_game'] ) )
 
793
  $output['theme_game'] = $input['theme_game'];
 
794
 
795
- if (isset( $input['restore_UserAgent'] )) {
796
  $output['userAgent_smart'] = $default_options['userAgent_smart'];
797
  $output['userAgent_tablet'] = $default_options['userAgent_tablet'];
798
  $output['userAgent_mobile'] = $default_options['userAgent_mobile'];
799
  $output['userAgent_game'] = $default_options['userAgent_game'];
800
  }
801
  else {
802
- if ( isset( $input['userAgent_smart'] ) )
803
  $output['userAgent_smart'] = $input['userAgent_smart'];
804
- if ( isset( $input['userAgent_tablet'] ) )
 
805
  $output['userAgent_tablet'] = $input['userAgent_tablet'];
806
- if ( isset( $input['userAgent_mobile'] ) )
 
807
  $output['userAgent_mobile'] = $input['userAgent_mobile'];
808
- if ( isset( $input['userAgent_game'] ) )
 
809
  $output['userAgent_game'] = $input['userAgent_game'];
 
810
  }
811
 
812
  foreach ( $input as $key => $val ) {
813
- if ( !preg_match( "/^custom_switcher_theme_/", $key ) )
814
  continue;
 
815
 
816
- $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
817
 
818
- if ( isset( $input['custom_switcher_theme_' . $custom_switcher_name] ) )
819
- $output['custom_switcher_theme_' . $custom_switcher_name] = $input['custom_switcher_theme_' . $custom_switcher_name];
820
- if ( isset( $input['custom_switcher_userAgent_' . $custom_switcher_name] ) )
821
- $output['custom_switcher_userAgent_' . $custom_switcher_name] = $input['custom_switcher_userAgent_' . $custom_switcher_name];
 
 
822
  }
823
 
824
  foreach ( $input as $key => $val ) {
825
- if ( !preg_match( "/^delete_custom_switcher_/", $key ) )
826
  continue;
 
827
 
828
- $custom_switcher_name = preg_replace("/^delete_custom_switcher_/", '', $key);
829
 
830
- unset($output['custom_switcher_theme_' . $custom_switcher_name]);
831
- unset($output['custom_switcher_userAgent_' . $custom_switcher_name]);
832
  }
833
 
834
- if ( isset( $input['add_custom_switcher'] ) && !empty( $input['custom_switcher'] ) && !$output['custom_switcher_theme_' . $input['custom_switcher']] ) {
835
- if ( preg_match( "/^[A-Za-z0-9]{1,20}$/", $input['custom_switcher'] ) ) {
836
- $output['custom_switcher_theme_' . $input['custom_switcher']] = 'None';
837
- $output['custom_switcher_userAgent_' . $input['custom_switcher']] = '';
 
838
  }
839
  }
840
 
841
- $output['pc_switcher'] = isset($input['pc_switcher']) ? $input['pc_switcher'] : 0;
842
- $output['default_css'] = isset($input['default_css']) ? $input['default_css'] : 0;
 
 
 
843
 
844
  return apply_filters( 'multi_device_switcher_validate', $output, $input, $default_options );
845
  }
846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
847
  /**
848
  * include PC Switcher Widget.
849
  *
850
  * @since 1.2
851
  */
852
- require_once( dirname(__FILE__) . '/pc-switcher-widget.php' );
 
 
 
 
 
 
 
 
 
853
 
854
  ?>
3
  Plugin Name: Multi Device Switcher
4
  Plugin URI: https://github.com/thingsym/multi-device-switcher
5
  Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
6
+ Version: 1.4.0
7
  Author: thingsym
8
  Author URI: http://www.thingslabo.com/
9
  License: GPL2
32
  class Multi_Device_Switcher {
33
 
34
  public function __construct() {
35
+ $this->device = '';
36
+
37
+ if ( isset( $_COOKIE['disable-switcher'] ) ) {
38
+ setcookie( 'disable-switcher', null, time() - 3600, '/' );
39
+ }
40
+
41
+ if ( $this->get_disable() ) {
42
+ setcookie( 'disable-switcher', 1, null, '/' );
43
+ return;
44
+ }
45
+
46
+ add_action( 'init', array( &$this, 'session' ) );
47
 
48
  $userAgent = $this->get_options_userAgent();
49
+ $server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
 
50
 
51
+ foreach ( array_reverse( $userAgent ) as $key => $val ) {
52
+ if ( ! preg_match( '/^custom_switcher_/', $key ) ) {
53
  continue;
54
+ }
55
+ if ( $userAgent[ $key ] && preg_match( '/' . implode( '|', $userAgent[ $key ] ) . '/i', $server_ua ) ) {
56
  $this->device = $key;
57
  break;
58
  }
59
  }
60
 
61
+ if ( ! $this->device ) {
62
+ if ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $server_ua ) ) {
63
  $this->device = 'game';
64
  }
65
+ elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $server_ua ) ) {
66
  $this->device = 'tablet';
67
  }
68
+ elseif ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $server_ua ) ) {
69
  $this->device = 'smart';
70
  }
71
+ elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $server_ua ) ) {
72
  $this->device = 'mobile';
73
  }
74
  }
75
 
76
+ if ( $this->device ) {
77
+ load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' );
78
+ add_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
79
+ add_filter( 'template', array( &$this, 'get_template' ) );
80
+ add_action( 'wp_footer', array( &$this, 'add_pc_switcher' ) );
81
+
82
+ setcookie( 'multi-device-switcher', preg_replace( '/^custom_switcher_/', '', $this->device ), null, '/' );
83
+ }
84
+ else {
85
+ setcookie( 'multi-device-switcher', null, time() - 3600, '/' );
86
  }
87
 
88
+ if ( isset( $_COOKIE['pc-switcher'] ) ) {
89
+ remove_filter( 'stylesheet', array( &$this, 'get_stylesheet' ) );
90
+ remove_filter( 'template', array( &$this, 'get_template' ) );
91
  }
92
  }
93
 
94
  public function get_options_userAgent() {
95
+ $options = multi_device_switcher_get_options();
96
+
97
+ $userAgent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'] );
98
+ $userAgent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'] );
99
+ $userAgent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'] );
100
+ $userAgent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  foreach ( $options as $key => $val ) {
103
+ if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) {
104
  continue;
105
+ }
106
 
107
+ $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key );
108
+ $userAgent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val );
 
 
109
  }
110
 
111
  return $userAgent;
112
  }
113
 
114
+ public function get_stylesheet( $stylesheet = '' ) {
115
  $name = $this->get_device_theme();
116
 
117
+ if ( empty( $name ) ) {
118
  return $stylesheet;
119
+ }
120
 
121
  $themes = wp_get_themes();
122
  foreach ( $themes as $t ) {
123
+ if ( $name == $t->get( 'Name' ) ) {
124
  $theme = $t;
125
  break;
126
  }
127
  }
128
 
129
+ if ( empty( $theme ) ) {
130
  return $stylesheet;
131
+ }
132
 
133
+ if ( 'publish' != $theme->get( 'Status' ) ) {
134
  return $stylesheet;
135
+ }
136
 
137
  return $theme['Stylesheet'];
138
  }
139
 
140
+ public function get_template( $template = '' ) {
141
  $name = $this->get_device_theme();
142
 
143
+ if ( empty( $name ) ) {
144
  return $template;
145
+ }
146
 
147
  $themes = wp_get_themes();
148
  foreach ( $themes as $t ) {
149
+ if ( $name == $t->get( 'Name' ) ) {
150
  $theme = $t;
151
  break;
152
  }
153
  }
154
 
155
+ if ( empty( $theme ) ) {
156
  return $template;
157
+ }
158
 
159
+ if ( 'publish' != $theme->get( 'Status' ) ) {
160
  return $template;
161
+ }
162
 
163
  return $theme['Template'];
164
  }
165
 
166
  public function get_device_theme() {
167
+ $options = multi_device_switcher_get_options();
168
 
169
+ if ( 'smart' == $this->device ) {
170
  return $options['theme_smartphone'];
171
  }
172
+ elseif ( 'tablet' == $this->device ) {
173
  return $options['theme_tablet'];
174
  }
175
+ elseif ( 'mobile' == $this->device ) {
176
  return $options['theme_mobile'];
177
  }
178
+ elseif ( 'game' == $this->device ) {
179
  return $options['theme_game'];
180
  }
181
  else {
182
  foreach ( $options as $key => $val ) {
183
+ if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
184
  continue;
185
+ }
186
 
187
+ $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
188
 
189
+ if ( 'custom_switcher_' . $custom_switcher_name == $this->device ) {
190
+ return $options[ $key ];
191
  }
192
  }
193
  }
196
  }
197
 
198
  public function session() {
199
+ if ( isset( $_GET['pc-switcher'] ) ) {
200
  setcookie( 'pc-switcher', $_GET['pc-switcher'] ? 1 : '', null, '/' );
201
 
202
  $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
203
 
204
  unset( $_GET['pc-switcher'] );
205
+ if ( ! empty( $_GET ) ) {
206
+ $uri = $uri . '?' . http_build_query( $_GET );
207
+ }
208
 
209
+ wp_redirect( esc_url( $uri ) );
210
  exit;
211
  }
212
  }
213
 
214
+ public function add_pc_switcher( $pc_switcher = 0 ) {
215
+ $options = multi_device_switcher_get_options();
216
  $name = $this->get_device_theme();
217
 
218
+ if ( $options['pc_switcher'] ) {
219
  $pc_switcher = 1;
220
+ }
221
 
222
+ if ( $pc_switcher && $name && 'None' != $name ) {
223
+ if ( $options['default_css'] ) {
224
  wp_enqueue_style( 'pc-switcher-options', plugins_url() . '/multi-device-switcher/pc-switcher.css', false, '2013-03-20' );
225
+ }
226
 
227
+ if ( isset( $_COOKIE['pc-switcher'] ) ) {
228
+ $uri = get_home_url() . add_query_arg( 'pc-switcher', 0 );
229
  ?>
230
+ <div class="pc-switcher"><a href="<?php echo esc_url( $uri ); ?>"><?php _e( 'Mobile', 'multi-device-switcher' ); ?></a><span class="active"><?php _e( 'PC', 'multi-device-switcher' ); ?></span></div>
231
  <?php
232
  }
233
  else {
234
+ $uri = get_home_url() . add_query_arg( 'pc-switcher', 1 );
235
  ?>
236
+ <div class="pc-switcher"><span class="active"><?php _e( 'Mobile', 'multi-device-switcher' ); ?></span><a href="<?php echo esc_url( $uri ); ?>"><?php _e( 'PC', 'multi-device-switcher' ); ?></a></div>
237
  <?php
238
  }
239
  }
240
  }
241
 
242
  public function is_multi_device( $device = '' ) {
243
+ if ( $device == $this->device ) {
244
  return (boolean) 1;
245
+ }
246
+ if ( 'custom_switcher_' . $device == $this->device ) {
247
  return (boolean) 1;
248
+ }
249
 
250
  return (boolean) 0;
251
  }
252
+
253
+ public function get_disable( $disable = 0 ) {
254
+ $options = multi_device_switcher_get_options();
255
+ $disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY );
256
+
257
+ foreach ( $disable_path as $path ) {
258
+ if ( $options['enable_regex'] ) {
259
+ if ( preg_match( '/' . $path . '/i', $_SERVER['REQUEST_URI'] ) ) {
260
+ $disable = 1;
261
+ break;
262
+ }
263
+ }
264
+ else {
265
+ if ( preg_match( '/^' . preg_quote( $path , '/' ) . '$/i', $_SERVER['REQUEST_URI'] ) ) {
266
+ $disable = 1;
267
+ break;
268
+ }
269
+ }
270
+ }
271
+
272
+ return (boolean) $disable;
273
+ }
274
  }
275
 
276
+ if ( ! is_admin() ) {
277
  $multi_device_switcher = new Multi_Device_Switcher();
278
+ }
279
 
280
  /**
281
  * Add HTTP/1.1 Vary header.
284
  *
285
  */
286
  function multi_device_switcher_add_header_vary( $headers ) {
287
+ if ( ! is_admin() ) {
288
  $headers['Vary'] = 'User-Agent';
289
  return $headers;
290
  }
299
  */
300
  function multi_device_switcher_add_pc_switcher() {
301
  global $multi_device_switcher;
302
+ $multi_device_switcher->add_pc_switcher( 1 );
303
  }
304
 
305
  /**
308
  * @since 1.2.4
309
  *
310
  */
311
+ if ( ! function_exists( 'is_multi_device' ) ) :
312
 
313
  function is_multi_device( $device = '' ) {
314
  global $multi_device_switcher;
315
+ return $multi_device_switcher->is_multi_device( $device );
316
  }
317
  endif;
318
 
353
  */
354
  function multi_device_switcher_init() {
355
  // If we have no options in the database, let's add them now.
356
+ if ( false === multi_device_switcher_get_options() ) {
357
  add_option( 'multi_device_switcher_options' );
358
+ }
359
 
360
  register_setting(
361
  'multi_device_switcher', // Options group, see settings_fields() call in multi_device_switcher_render_page()
392
  * @since 1.0
393
  */
394
  function multi_device_switcher_add_page() {
395
+ load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' );
396
 
397
  add_filter( 'plugin_action_links', 'multi_device_switcher_plugin_action_links', 10, 2 );
398
 
404
  'multi_device_switcher_render_page' // Function that renders the options page
405
  );
406
 
407
+ if ( ! $page_hook ) {
408
  return;
409
+ }
410
 
411
  add_action( 'load-' . $page_hook , 'multi_device_switcher_page_hook_suffix' );
412
  }
430
  * @since 1.2
431
  */
432
  function multi_device_switcher_plugin_action_links( $links, $file ) {
433
+ if ( $file != plugin_basename( __FILE__ ) ) {
434
  return $links;
435
+ }
436
 
437
  $settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', 'multi-device-switcher' ) . '</a>';
438
 
458
  'userAgent_tablet' => 'iPad, Kindle, Sony Tablet, Nexus 7',
459
  'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
460
  'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, PlayStation 4, Nitro, Nintendo 3DS, Nintendo Wii, Nintendo WiiU, Xbox',
461
+ 'disable_path' => '',
462
+ 'enable_regex' => 0,
463
  );
464
 
465
  return $default_theme_options;
474
  $options = get_option( 'multi_device_switcher_options' );
475
  $default_options = multi_device_switcher_get_default_options();
476
 
477
+ if ( ! isset( $options['pc_switcher'] ) ) {
478
  $options['pc_switcher'] = $default_options['pc_switcher'];
479
+ }
480
+ if ( ! isset( $options['default_css'] ) ) {
481
  $options['default_css'] = $default_options['default_css'];
482
+ }
483
 
484
+ if ( ! isset( $options['theme_smartphone'] ) ) {
485
  $options['theme_smartphone'] = $default_options['theme_smartphone'];
486
+ }
487
+ if ( ! isset( $options['theme_tablet'] ) ) {
488
  $options['theme_tablet'] = $default_options['theme_tablet'];
489
+ }
490
+ if ( ! isset( $options['theme_mobile'] ) ) {
491
  $options['theme_mobile'] = $default_options['theme_mobile'];
492
+ }
493
+ if ( ! isset( $options['theme_game'] ) ) {
494
  $options['theme_game'] = $default_options['theme_game'];
495
+ }
496
 
497
+ if ( ! isset( $options['userAgent_smart'] ) ) {
498
  $options['userAgent_smart'] = $default_options['userAgent_smart'];
499
+ }
500
+ if ( ! isset( $options['userAgent_tablet'] ) ) {
501
  $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
502
+ }
503
+ if ( ! isset( $options['userAgent_mobile'] ) ) {
504
  $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
505
+ }
506
+ if ( ! isset( $options['userAgent_game'] ) ) {
507
  $options['userAgent_game'] = $default_options['userAgent_game'];
508
+ }
509
+
510
+ if ( ! isset( $options['disable_path'] ) ) {
511
+ $options['disable_path'] = $default_options['disable_path'];
512
+ }
513
+ if ( ! isset( $options['enable_regex'] ) ) {
514
+ $options['enable_regex'] = $default_options['enable_regex'];
515
+ }
516
 
517
  return $options;
518
  }
526
  ?>
527
  <div class="wrap">
528
  <div id="icon-themes" class="icon32"><br></div>
529
+ <h2><?php printf( __( 'Multi Device Switcher', 'multi-device-switcher' ), wp_get_theme()->get( 'Name' ) ); ?></h2>
530
  <?php settings_errors(); ?>
531
 
532
  <form method="post" action="options.php">
534
  settings_fields( 'multi_device_switcher' );
535
  $options = multi_device_switcher_get_options();
536
 
537
+ $default_theme = wp_get_theme()->get( 'Name' );
538
  $themes = wp_get_themes();
539
  $theme_names = array();
540
 
541
+ if ( count( $themes ) ) {
542
  foreach ( $themes as $t ) {
543
+ $theme_names[] = $t->get( 'Name' );
544
  }
545
+ natcasesort( $theme_names );
546
  }
547
  ?>
548
 
554
  <td>
555
 
556
  <?php
557
+ if ( count( $theme_names ) ) {
558
  $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
559
 
560
+ if ( ( 'None' == $options['theme_smartphone'] ) || ( '' == $options['theme_smartphone'] ) ) {
561
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
562
  }
563
  else {
565
  }
566
 
567
  foreach ( $theme_names as $theme_name ) {
568
+ if ( $default_theme == $theme_name ) {
569
  continue;
570
+ }
571
  if ( $options['theme_smartphone'] == $theme_name ) {
572
+ $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
573
  }
574
  else {
575
+ $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
576
  }
577
  }
578
  $html .= '</select>';
585
  <td>
586
 
587
  <?php
588
+ if ( count( $theme_names ) ) {
589
  $html = '<select name="multi_device_switcher_options[theme_tablet]">';
590
 
591
+ if ( ( 'None' == $options['theme_tablet'] ) || ( '' == $options['theme_tablet'] ) ) {
592
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
593
  }
594
  else {
596
  }
597
 
598
  foreach ( $theme_names as $theme_name ) {
599
+ if ( $default_theme == $theme_name ) {
600
  continue;
601
+ }
602
  if ( $options['theme_tablet'] == $theme_name ) {
603
+ $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
604
  }
605
  else {
606
+ $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
607
  }
608
  }
609
  $html .= '</select>';
616
  <td>
617
 
618
  <?php
619
+ if ( count( $theme_names ) ) {
620
  $html = '<select name="multi_device_switcher_options[theme_mobile]">';
621
 
622
+ if ( ( 'None' == $options['theme_mobile'] ) || ( '' == $options['theme_mobile'] ) ) {
623
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
624
  }
625
  else {
627
  }
628
 
629
  foreach ( $theme_names as $theme_name ) {
630
+ if ( $default_theme == $theme_name ) {
631
  continue;
632
+ }
633
  if ( $options['theme_mobile'] == $theme_name ) {
634
+ $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
635
  }
636
  else {
637
+ $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
638
  }
639
  }
640
  $html .= '</select>';
647
  <td>
648
 
649
  <?php
650
+ if ( count( $theme_names ) ) {
651
  $html = '<select name="multi_device_switcher_options[theme_game]">';
652
 
653
+ if ( ( 'None' == $options['theme_game'] ) || ( '' == $options['theme_game'] ) ) {
654
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
655
  }
656
  else {
658
  }
659
 
660
  foreach ( $theme_names as $theme_name ) {
661
+ if ( $default_theme == $theme_name ) {
662
  continue;
663
+ }
664
  if ( $options['theme_game'] == $theme_name ) {
665
+ $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
666
  }
667
  else {
668
+ $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
669
  }
670
  }
671
  $html .= '</select>';
680
  <table class="form-table">
681
 
682
  <?php
683
+ foreach ( $options as $custom_switcher_option => $custom_switcher_theme ) {
684
+ if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
685
  continue;
686
+ }
687
 
688
+ $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
 
 
689
  ?>
690
 
691
+ <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
692
  <td>
693
 
694
  <?php
695
+ if ( count( $theme_names ) ) {
696
  $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
697
 
698
+ if ( ( 'None' == $custom_switcher_theme ) || ( '' == $custom_switcher_theme ) ) {
699
  $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
700
  }
701
  else {
703
  }
704
 
705
  foreach ( $theme_names as $theme_name ) {
706
+ if ( $default_theme == $theme_name ) {
707
  continue;
708
+ }
709
  if ( $custom_switcher_theme == $theme_name ) {
710
+ $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
711
  }
712
  else {
713
+ $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
714
  }
715
  }
716
  $html .= '</select>';
717
+ $html .= ' <span class="submit"><input type="submit" name="multi_device_switcher_options[delete_custom_switcher_' . $custom_switcher_name . ']" value="' . __( 'Delete', 'multi-device-switcher' ) . '" onclick="return confirm(\'' . esc_html( sprintf( __( 'Are you sure you want to delete %1$s ?', 'multi-device-switcher' ), $custom_switcher_name ) ) . '\');" class="button"></span>';
718
  }
719
  echo $html;
720
  ?>
743
 
744
  <table class="form-table">
745
  <tr><th scope="row"><?php _e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
746
+ <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_smart'] ); ?></textarea></td>
747
  </tr>
748
  <tr><th scope="row"><?php _e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
749
+ <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_tablet'] ); ?></textarea></td>
750
  </tr>
751
  <tr><th scope="row"><?php _e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
752
+ <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_mobile'] ); ?></textarea></td>
753
  </tr>
754
  <tr><th scope="row"><?php _e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
755
+ <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_game'] ); ?></textarea></td>
756
  </tr>
757
  <tr><th></th>
758
  <td><span class="submit"><input type="submit" name="multi_device_switcher_options[restore_UserAgent]" value="<?php _e( 'Reset Settings to Default UserAgent', 'multi-device-switcher' ); ?>" class="button"></span></td>
763
  <h3><?php _e( 'Custom Switcher UserAgent', 'multi-device-switcher' ); ?></h3>
764
  <table class="form-table">
765
  <?php
766
+ foreach ( $options as $custom_switcher_option => $custom_switcher_userAgent ) {
767
+ if ( ! preg_match( '/^custom_switcher_userAgent_/', $custom_switcher_option ) ) {
768
  continue;
769
+ }
770
 
771
+ $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $custom_switcher_option );
 
 
772
  ?>
773
 
774
+ <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
775
+ <td><textarea name="multi_device_switcher_options[<?php echo esc_attr( $custom_switcher_option ); ?>]" rows="4" cols="42"><?php echo esc_textarea( $custom_switcher_userAgent ); ?></textarea></td>
776
  </tr>
777
  <?php
778
  }
788
  <tr><th scope="row"><?php _e( 'Add PC Switcher', 'multi-device-switcher' ); ?></th>
789
  <td>
790
  <legend class="screen-reader-text"><span><?php _e( 'Add PC Switcher', 'multi-device-switcher' ); ?></span></legend>
791
+ <label><input type="checkbox" name="multi_device_switcher_options[pc_switcher]" id="pc-switcher" value="1"<?php checked( 1, $options['pc_switcher'] ); ?>> <?php _e( 'Add a PC Switcher to the footer.', 'multi-device-switcher' ); ?></label>
792
  </td>
793
  </tr>
794
  <tr><th scope="row"><?php _e( 'Add default CSS', 'multi-device-switcher' ); ?></th>
795
  <td>
796
  <legend class="screen-reader-text"><span><?php _e( 'Add default CSS', 'multi-device-switcher' ); ?></span></legend>
797
+ <label><input type="checkbox" name="multi_device_switcher_options[default_css]" id="add-default-css" value="1"<?php checked( 1, $options['default_css'] ); ?>> <?php _e( 'Add a default CSS.', 'multi-device-switcher' ); ?></label>
798
+ </td>
799
+ </tr>
800
+ </table>
801
+ </fieldset>
802
+
803
+ <fieldset id="Disable-Switcher" class="options">
804
+ <h3 class="label"><?php _e( 'Disable Switcher', 'multi-device-switcher' ); ?></h3>
805
+
806
+ <table class="form-table">
807
+ <tr><th scope="row"><?php _e( 'Path', 'multi-device-switcher' ); ?></th>
808
+ <td>
809
+ <legend class="screen-reader-text"><span><?php _e( 'Path', 'multi-device-switcher' ); ?></span></legend>
810
+ <?php echo esc_html( home_url() ); ?>
811
+ <textarea name="multi_device_switcher_options[disable_path]" rows="16" cols="42" wrap="off"><?php echo esc_textarea( $options['disable_path'] ); ?></textarea>
812
+ </td>
813
+ </tr>
814
+ <tr><th scope="row"><?php _e( 'Regex mode', 'multi-device-switcher' ); ?></th>
815
+ <td>
816
+ <legend class="screen-reader-text"><span><?php _e( 'Regex mode', 'multi-device-switcher' ); ?></span></legend>
817
+ <label><input type="checkbox" name="multi_device_switcher_options[enable_regex]" id="enable-regex" value="1"<?php checked( 1, $options['enable_regex'] ); ?>> <?php _e( 'Enable Regex', 'multi-device-switcher' ); ?></label>
818
  </td>
819
  </tr>
820
  </table>
864
  function multi_device_switcher_validate( $input ) {
865
  $output = $default_options = multi_device_switcher_get_default_options();
866
 
867
+ if ( isset( $input['theme_smartphone'] ) ) {
868
  $output['theme_smartphone'] = $input['theme_smartphone'];
869
+ }
870
+ if ( isset( $input['theme_tablet'] ) ) {
871
  $output['theme_tablet'] = $input['theme_tablet'];
872
+ }
873
+ if ( isset( $input['theme_mobile'] ) ) {
874
  $output['theme_mobile'] = $input['theme_mobile'];
875
+ }
876
+ if ( isset( $input['theme_game'] ) ) {
877
  $output['theme_game'] = $input['theme_game'];
878
+ }
879
 
880
+ if ( isset( $input['restore_UserAgent'] ) ) {
881
  $output['userAgent_smart'] = $default_options['userAgent_smart'];
882
  $output['userAgent_tablet'] = $default_options['userAgent_tablet'];
883
  $output['userAgent_mobile'] = $default_options['userAgent_mobile'];
884
  $output['userAgent_game'] = $default_options['userAgent_game'];
885
  }
886
  else {
887
+ if ( isset( $input['userAgent_smart'] ) ) {
888
  $output['userAgent_smart'] = $input['userAgent_smart'];
889
+ }
890
+ if ( isset( $input['userAgent_tablet'] ) ) {
891
  $output['userAgent_tablet'] = $input['userAgent_tablet'];
892
+ }
893
+ if ( isset( $input['userAgent_mobile'] ) ) {
894
  $output['userAgent_mobile'] = $input['userAgent_mobile'];
895
+ }
896
+ if ( isset( $input['userAgent_game'] ) ) {
897
  $output['userAgent_game'] = $input['userAgent_game'];
898
+ }
899
  }
900
 
901
  foreach ( $input as $key => $val ) {
902
+ if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
903
  continue;
904
+ }
905
 
906
+ $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
907
 
908
+ if ( isset( $input[ 'custom_switcher_theme_' . $custom_switcher_name ] ) ) {
909
+ $output[ 'custom_switcher_theme_' . $custom_switcher_name ] = $input[ 'custom_switcher_theme_' . $custom_switcher_name ];
910
+ }
911
+ if ( isset( $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ] ) ) {
912
+ $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] = $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ];
913
+ }
914
  }
915
 
916
  foreach ( $input as $key => $val ) {
917
+ if ( ! preg_match( '/^delete_custom_switcher_/', $key ) ) {
918
  continue;
919
+ }
920
 
921
+ $custom_switcher_name = preg_replace( '/^delete_custom_switcher_/', '', $key );
922
 
923
+ unset( $output[ 'custom_switcher_theme_' . $custom_switcher_name ] );
924
+ unset( $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] );
925
  }
926
 
927
+ if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] ) {
928
+ if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) )
929
+ && preg_match( '/^[A-Za-z0-9]{1,20}$/', $input['custom_switcher'] ) ) {
930
+ $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] = 'None';
931
+ $output[ 'custom_switcher_userAgent_' . $input['custom_switcher'] ] = '';
932
  }
933
  }
934
 
935
+ $output['pc_switcher'] = isset( $input['pc_switcher'] ) ? $input['pc_switcher'] : 0;
936
+ $output['default_css'] = isset( $input['default_css'] ) ? $input['default_css'] : 0;
937
+
938
+ $output['disable_path'] = isset( $input['disable_path'] ) ? $input['disable_path'] : '';
939
+ $output['enable_regex'] = isset( $input['enable_regex'] ) ? $input['enable_regex'] : 0;
940
 
941
  return apply_filters( 'multi_device_switcher_validate', $output, $input, $default_options );
942
  }
943
 
944
+ /**
945
+ * plugin customization options
946
+ *
947
+ * @param $wp_customize Theme Customizer object
948
+ * @return void
949
+ *
950
+ * @since 1.3.1
951
+ */
952
+ function multi_device_switcher_customize_register( $wp_customize ) {
953
+ load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' );
954
+ $options = multi_device_switcher_get_options();
955
+ $default_theme_options = multi_device_switcher_get_default_options();
956
+ $default_theme = wp_get_theme()->get( 'Name' );
957
+ $themes = wp_get_themes();
958
+
959
+ $theme_names = array();
960
+ $choices = array();
961
+
962
+ if ( count( $themes ) ) {
963
+ foreach ( $themes as $t ) {
964
+ $theme_names[] = $t->get( 'Name' );
965
+ }
966
+ natcasesort( $theme_names );
967
+
968
+ $choices['None'] = __( 'None', 'multi-device-switcher' );
969
+ foreach ( $theme_names as $theme_name ) {
970
+ if ( $default_theme == $theme_name ) {
971
+ continue;
972
+ }
973
+ $choices[ $theme_name ] = $theme_name;
974
+ }
975
+ }
976
+
977
+ $switcher = array(
978
+ 'theme_smartphone' => __( 'Smart Phone Theme', 'multi-device-switcher' ),
979
+ 'theme_tablet' => __( 'Tablet PC Theme', 'multi-device-switcher' ),
980
+ 'theme_mobile' => __( 'Mobile Phone Theme', 'multi-device-switcher' ),
981
+ 'theme_game' => __( 'Game Platforms Theme', 'multi-device-switcher' ),
982
+ );
983
+
984
+ $wp_customize->add_section( 'multi_device_switcher', array(
985
+ 'title' => __( 'Multi Device Switcher', 'multi-device-switcher' ),
986
+ 'priority' => 80,
987
+ ) );
988
+
989
+ foreach ( $switcher as $name => $label ) {
990
+ $wp_customize->add_setting( 'multi_device_switcher_options[' . $name . ']', array(
991
+ 'default' => $default_theme_options[ $name ],
992
+ 'type' => 'option',
993
+ 'capability' => 'edit_theme_options',
994
+ ) );
995
+
996
+ $wp_customize->add_control( 'multi_device_switcher_options[' . $name . ']', array(
997
+ 'label' => $label,
998
+ 'section' => 'multi_device_switcher',
999
+ 'type' => 'select',
1000
+ 'choices' => $choices,
1001
+ ) );
1002
+ }
1003
+
1004
+ foreach ( $options as $custom_switcher_option => $custom_switcher_theme ) {
1005
+ if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1006
+ continue;
1007
+ }
1008
+
1009
+ $label = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
1010
+
1011
+ $wp_customize->add_setting( 'multi_device_switcher_options[' . $custom_switcher_option . ']', array(
1012
+ 'default' => __( 'None', 'multi-device-switcher' ),
1013
+ 'type' => 'option',
1014
+ 'capability' => 'edit_theme_options',
1015
+ ) );
1016
+
1017
+ $wp_customize->add_control( 'multi_device_switcher_options[' . $custom_switcher_option . ']', array(
1018
+ 'label' => $label,
1019
+ 'section' => 'multi_device_switcher',
1020
+ 'type' => 'select',
1021
+ 'choices' => $choices,
1022
+ ) );
1023
+
1024
+ }
1025
+ }
1026
+
1027
+ add_action( 'customize_register', 'multi_device_switcher_customize_register' );
1028
+
1029
  /**
1030
  * include PC Switcher Widget.
1031
  *
1032
  * @since 1.2
1033
  */
1034
+ require_once( dirname( __FILE__ ) . '/pc-switcher-widget.php' );
1035
+
1036
+ /**
1037
+ * include Multi Device Switcher Command
1038
+ *
1039
+ * @since 1.4
1040
+ */
1041
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
1042
+ require_once( dirname( __FILE__ ) . '/wp-cli.php' );
1043
+ }
1044
 
1045
  ?>
pc-switcher-widget.php CHANGED
@@ -3,7 +3,7 @@
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.3.0
7
  Author: thingsym
8
  Author URI: http://www.thingslabo.com/
9
  License: GPL2
@@ -34,54 +34,56 @@ Domain Path: /languages/
34
  *
35
  * @since 1.2
36
  */
37
- if ( class_exists('Multi_Device_Switcher') )
38
- add_action('widgets_init', 'PC_Switcher_load_widgets');
 
39
 
40
- function PC_Switcher_load_widgets() {
41
- register_widget('PC_Switcher');
42
  }
43
 
44
  class PC_Switcher extends WP_Widget {
45
 
46
  function __construct() {
47
- load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
48
- $widget_ops = array('classname' => 'widget_pc_switcher', 'description' => __( "Add the PC Switcher to a widget.", 'multi-device-switcher') );
49
- parent::__construct('pc-switcher', __('PC Switcher', 'multi-device-switcher'), $widget_ops);
50
  $this->alt_option_name = 'widget_pc_switcher';
51
 
52
- add_action( 'save_post', array(&$this, 'flush_widget_cache') );
53
- add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
54
- add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
55
  }
56
 
57
- function widget($args, $instance) {
58
- if ( !function_exists( 'multi_device_switcher_add_pc_switcher' ) )
59
  return;
 
60
 
61
  global $multi_device_switcher;
62
  $name = $multi_device_switcher->get_device_theme();
63
 
64
- if ( $name && $name != 'None' ) {
65
 
66
- $cache = wp_cache_get('widget_pc_switcher', 'widget');
67
 
68
- if ( !is_array($cache) )
69
  $cache = array();
 
70
 
71
- if ( isset($cache[ $args['widget_id'] ]) ) {
72
  echo $cache[ $args['widget_id'] ];
73
  return;
74
  }
75
 
76
  ob_start();
77
- extract($args);
78
 
79
- echo $before_widget;
80
  multi_device_switcher_add_pc_switcher();
81
- echo $after_widget;
82
 
83
  $cache[ $args['widget_id'] ] = ob_get_flush();
84
- wp_cache_set('widget_pc_switcher', $cache, 'widget');
85
  }
86
  }
87
 
@@ -91,14 +93,15 @@ class PC_Switcher extends WP_Widget {
91
  $this->flush_widget_cache();
92
 
93
  $alloptions = wp_cache_get( 'alloptions', 'options' );
94
- if ( isset($alloptions['widget_pc_switcher']) )
95
- delete_option('widget_pc_switcher');
 
96
 
97
  return $instance;
98
  }
99
 
100
  function flush_widget_cache() {
101
- wp_cache_delete('widget_pc_switcher', 'widget');
102
  }
103
 
104
  function form( $instance ) {
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.0
7
  Author: thingsym
8
  Author URI: http://www.thingslabo.com/
9
  License: GPL2
34
  *
35
  * @since 1.2
36
  */
37
+ if ( class_exists( 'Multi_Device_Switcher' ) ) {
38
+ add_action( 'widgets_init', 'pc_switcher_load_widgets' );
39
+ }
40
 
41
+ function pc_switcher_load_widgets() {
42
+ register_widget( 'PC_Switcher' );
43
  }
44
 
45
  class PC_Switcher extends WP_Widget {
46
 
47
  function __construct() {
48
+ load_plugin_textdomain( 'multi-device-switcher', false, 'multi-device-switcher/languages' );
49
+ $widget_ops = array( 'classname' => 'widget_pc_switcher', 'description' => __( 'Add the PC Switcher to a widget.', 'multi-device-switcher' ) );
50
+ parent::__construct( 'pc-switcher', __( 'PC Switcher', 'multi-device-switcher' ), $widget_ops );
51
  $this->alt_option_name = 'widget_pc_switcher';
52
 
53
+ add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
54
+ add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
55
+ add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
56
  }
57
 
58
+ function widget( $args, $instance ) {
59
+ if ( ! function_exists( 'multi_device_switcher_add_pc_switcher' ) ) {
60
  return;
61
+ }
62
 
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
 
70
+ if ( ! is_array( $cache ) ) {
71
  $cache = array();
72
+ }
73
 
74
+ if ( isset( $cache[ $args['widget_id'] ] ) ) {
75
  echo $cache[ $args['widget_id'] ];
76
  return;
77
  }
78
 
79
  ob_start();
 
80
 
81
+ echo $args['before_widget'];
82
  multi_device_switcher_add_pc_switcher();
83
+ echo $args['after_widget'];
84
 
85
  $cache[ $args['widget_id'] ] = ob_get_flush();
86
+ wp_cache_set( 'widget_pc_switcher', $cache, 'widget' );
87
  }
88
  }
89
 
93
  $this->flush_widget_cache();
94
 
95
  $alloptions = wp_cache_get( 'alloptions', 'options' );
96
+ if ( isset( $alloptions['widget_pc_switcher'] ) ) {
97
+ delete_option( 'widget_pc_switcher' );
98
+ }
99
 
100
  return $instance;
101
  }
102
 
103
  function flush_widget_cache() {
104
+ wp_cache_delete( 'widget_pc_switcher', 'widget' );
105
  }
106
 
107
  function form( $instance ) {
readme.md CHANGED
@@ -1,15 +1,16 @@
1
  # Introducing Multi Device Switcher
2
 
3
  This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
4
- This plugin detects if your site is being viewed by UserAgent, and switches to selected theme.
5
- The Custom Switcher can add every device.
6
 
7
  ## Features
8
 
9
  - Set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game), switches to selected theme.
10
- - Add every device by the Custom Switcher.
11
- - Add links 'Mobile' or 'PC' in the theme by the PC Switcher, switch to the default theme.
 
12
  - Can be using is_multi_device() function that detect of the device.
 
13
 
14
  ## How do I use it ?
15
 
@@ -26,6 +27,8 @@ The Custom Switcher can add every device.
26
  <img src="screenshot-1.png">
27
  <img src="screenshot-2.png">
28
  <img src="screenshot-3.png">
 
 
29
 
30
  ## How to add the Custom Switcher
31
 
@@ -67,13 +70,79 @@ There are three ways how to Using the PC Switcher.
67
  2. Configure settings. Check the checkbox 'Add a default CSS.' by PC Switcher option. If you want to customize CSS, uncheck the checkbox.
68
  3. Have fun!
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  ## UserAgent option Samples
71
 
72
  * [Default UserAgent](https://github.com/thingsym/multi-device-switcher/wiki/Default-UserAgent)
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  ## is_multi_device() function
75
 
76
- is_multi_device() function is a boolean function, meaning it returns either TRUE or FALSE. Works through the detection of the device by the Multi_Device_Switcher class.
77
 
78
  ### Usage
79
 
@@ -90,6 +159,8 @@ if ( function_exists( 'is_multi_device' ) ) {
90
  /* Display and echo smartphone specific stuff here */
91
  } elseif ( is_multi_device('tablet') ) {
92
  /* Display and echo tablet specific stuff here */
 
 
93
  }
94
  }
95
  ?>
@@ -111,6 +182,87 @@ if ( function_exists( 'is_multi_device' ) ) {
111
 
112
  (boolean) Return boolean whether a particular device.
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  ## Contributing
115
 
116
  ### Patches and Bug Fixes
@@ -146,6 +298,15 @@ You can send your own language pack to author.
146
 
147
  ## Changelog
148
 
 
 
 
 
 
 
 
 
 
149
  * Version 1.3.0
150
  * fixed: fix script, style, html and readme
151
  * new features: is_multi_device() function
1
  # Introducing Multi Device Switcher
2
 
3
  This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
4
+ This plugin detects if your site is being viewed by UserAgent, and switches to selected theme. The Custom Switcher can add every device.
 
5
 
6
  ## Features
7
 
8
  - Set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game), switches to selected theme.
9
+ - Add every device by the **Custom Switcher**.
10
+ - Add links 'Mobile' or 'PC' in the theme by the **PC Switcher**, switch to the default theme.
11
+ - Disable the switching of the theme by a particular URL by the **Disable Switcher**.
12
  - Can be using is_multi_device() function that detect of the device.
13
+ - **Multi Device Switcher Command** command-line tool (required WP-CLI)
14
 
15
  ## How do I use it ?
16
 
27
  <img src="screenshot-1.png">
28
  <img src="screenshot-2.png">
29
  <img src="screenshot-3.png">
30
+ <img src="screenshot-4.png">
31
+ <img src="screenshot-5.png">
32
 
33
  ## How to add the Custom Switcher
34
 
70
  2. Configure settings. Check the checkbox 'Add a default CSS.' by PC Switcher option. If you want to customize CSS, uncheck the checkbox.
71
  3. Have fun!
72
 
73
+ You can design the PC Switcher in the Style Sheet.
74
+
75
+ #### HTML output of the PC Switcher
76
+
77
+ ```
78
+ <div class="pc-switcher"><span class="active">Mobile</span><a href="http://DOMEIN/PATH/TO/?pc-switcher=1">PC</a></div>
79
+ ```
80
+
81
+ #### HTML output of the PC Switcher when switched
82
+
83
+ ```
84
+ <div class="pc-switcher"><a href="http://DOMEIN/PATH/TO/?pc-switcher=0">Mobile</a><span class="active">PC</span></div>
85
+ ```
86
+
87
+ ## How to use the Disable Switcher
88
+
89
+ The **Disable Switcher** disable the switching of the theme by a particular URL. If you match the access the url and a string or a regular expression (Regex mode), disable the switching of the theme. Regex mode is for advanced users.
90
+
91
+ 1. Go to the "Multi Device Switcher" options page through the 'Appearance' menu in WordPress.
92
+ 2. Enter the path to the line by line where you want to disable by Disable Switcher option. Check the checkbox 'Enable Regex', if you want to use a regular expression.
93
+ 3. Have fun!
94
+
95
+ ### Examples
96
+
97
+ ```
98
+ /sample-page
99
+ /2015/01/hello-world
100
+ ```
101
+
102
+ ##### Regex mode (in the case of regular expression)
103
+
104
+ ```
105
+ \/sample\-
106
+ \/2015\/01
107
+ ```
108
+
109
  ## UserAgent option Samples
110
 
111
  * [Default UserAgent](https://github.com/thingsym/multi-device-switcher/wiki/Default-UserAgent)
112
 
113
+
114
+ ## Detect the device by JavaScript
115
+
116
+ Multi Device Switcher set the Cookie that holds the state of the switch. You can get the Cookie and detect the device by JavaScript.
117
+
118
+ ### Cookie
119
+
120
+ * `multi-device-switcher` The name of the device is switched (value: null | device name)
121
+ * `disable-switcher` State of disabled (value: null | 1)
122
+ * `pc-switcher` State of the PC Switcher when switched (value: null | 1)
123
+
124
+ ### Examples
125
+
126
+ ```
127
+ <script src="http://DOMEIN/PATH/TO/jquery.cookie.js"></script>
128
+ <script>
129
+ (function($) {
130
+ $(function() {
131
+ if ( $.cookie( 'multi-device-switcher' ) == 'smart' ) {
132
+ /* smartphone specific stuff here */
133
+ } else if ( $.cookie( 'multi-device-switcher' ) == 'tablet' ) {
134
+ /* tablet specific stuff here */
135
+ } else {
136
+ /* pc or other stuff here */
137
+ }
138
+ });
139
+ })(jQuery);
140
+ </script>
141
+ ```
142
+
143
  ## is_multi_device() function
144
 
145
+ **is_multi_device()** function is a boolean function, meaning it returns either TRUE or FALSE. Works through the detection of the device by the Multi_Device_Switcher class.
146
 
147
  ### Usage
148
 
159
  /* Display and echo smartphone specific stuff here */
160
  } elseif ( is_multi_device('tablet') ) {
161
  /* Display and echo tablet specific stuff here */
162
+ } else {
163
+ /* Display and echo pc or other stuff here */
164
  }
165
  }
166
  ?>
182
 
183
  (boolean) Return boolean whether a particular device.
184
 
185
+ ## Multi Device Switcher Command
186
+
187
+ The **Multi Device Switcher Command** is command-line tool.
188
+
189
+ Add-on the Multi Device Switcher Command, when you activate the plugin "Multi Device Switcher". To use the Multi Device Switcher Command is WP-CLI required.
190
+
191
+ ### NAME
192
+
193
+ wp multi-device
194
+
195
+ ### DESCRIPTION
196
+
197
+ Multi Device Switcher Command
198
+
199
+ ### SYNOPSIS
200
+
201
+ wp multi-device <command>
202
+
203
+ ### SUBCOMMANDS
204
+
205
+ add add Custom Switcher
206
+ css turn on/off default CSS
207
+ delete delete Custom Switcher
208
+ pc-switcher turn on/off PC Switcher
209
+ reset reset Settings to Default UserAgent
210
+ status get status of settings
211
+ theme get or switch a theme
212
+ useragent get or set UserAgent
213
+
214
+
215
+ For more information about the Multi Device Switcher Command, see `wp help multi-device <SUBCOMMANDS>`.
216
+
217
+
218
+ ### Command examples
219
+
220
+ get status of settings
221
+
222
+ wp multi-device status
223
+
224
+ Active Theme: Twenty Fifteen | twentyfifteen
225
+ +--------------------------+-----------------+----------------+-------------------------------------------------------------------------+
226
+ | Device | Theme | Slug | UserAgent |
227
+ +--------------------------+-----------------+----------------+-------------------------------------------------------------------------+
228
+ | smartphone (Smart Phone) | Twenty Fourteen | twentyfourteen | iPhone, iPod, Android, dream, CUPCAKE, Windows Phone, webOS, BB10, Blac |
229
+ | | | | kBerry8707, BlackBerry9000, BlackBerry9300, BlackBerry9500, BlackBerry9 |
230
+ | | | | 530, BlackBerry9520, BlackBerry9550, BlackBerry9700, BlackBerry 93, Bla |
231
+ | | | | ckBerry 97, BlackBerry 99, BlackBerry 98 |
232
+ | tablet (Tablet PC) | Twenty Thirteen | twentythirteen | iPad, Kindle, Sony Tablet, Nexus 7 |
233
+ | mobile (Mobile Phone) | Twenty Twelve | twentytwelve | DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile |
234
+ | | | | , DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Noki |
235
+ | | | | a |
236
+ | game (Game Platforms) | Twenty Eleven | twentyeleven | PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, PlaySt |
237
+ | | | | ation 4, Nitro, Nintendo 3DS, Nintendo Wii, Nintendo WiiU, Xbox |
238
+ +--------------------------+-----------------+----------------+-------------------------------------------------------------------------+
239
+ PC Switcher: on
240
+ default CSS: on
241
+
242
+ switch twentyfifteen in theme of smartphone using theme slug
243
+
244
+ wp multi-device theme smartphone twentyfifteen
245
+
246
+ set UserAgent in theme of tablet
247
+
248
+ wp multi-device useragent tablet 'iPad, Kindle, Sony Tablet, Nexus 7'
249
+
250
+ add example Custom Switcher
251
+
252
+ wp multi-device add example
253
+
254
+ add example Custom Switcher. set twentyfifteen theme and UserAgent using theme slug
255
+
256
+ wp multi-device add example twentyfifteen 'iPad, Kindle, Sony Tablet, Nexus 7'
257
+
258
+ delete example Custom Switcher
259
+
260
+ wp multi-device delete example
261
+
262
+ turn on default CSS
263
+
264
+ wp multi-device css on
265
+
266
  ## Contributing
267
 
268
  ### Patches and Bug Fixes
298
 
299
  ## Changelog
300
 
301
+ * Version 1.4.0
302
+ * edited: edit readme
303
+ * added: add cookies 'multi-device-switcher', 'disable-switcher'
304
+ * new features: Disable Switcher
305
+ * fixed: fix get_options_userAgent(), multi_device_switcher_get_options()
306
+ * fixed: add reserved words validate
307
+ * new features: Multi Device Switcher Command
308
+ * added: add option settings into Theme Customizer
309
+ * fixed: refactoring by the PHP_CodeSniffer
310
  * Version 1.3.0
311
  * fixed: fix script, style, html and readme
312
  * new features: is_multi_device() function
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.0
9
- Stable tag: 1.3.0
10
  License: GPL2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
@@ -22,9 +22,11 @@ The Custom Switcher can add every device.
22
  = Features =
23
 
24
  * Set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game), switches to selected theme.
25
- * Add every device by the Custom Switcher.
26
- * Add links 'Mobile' or 'PC' in the theme by the PC Switcher, switch to the default theme.
27
- * Can be using is_multi_device() function that detect of the device.
 
 
28
 
29
  = Contributing =
30
 
@@ -65,6 +67,7 @@ You can send your own language pack to author.
65
  2. UserAgent option
66
  3. PC Switcher option
67
  4. PC Switcher button
 
68
 
69
  == Installation ==
70
 
@@ -113,13 +116,80 @@ There are three ways how to Using the PC Switcher.
113
  * Configure settings. Check the checkbox 'Add a default CSS.' by PC Switcher option. If you want to customize CSS, uncheck the checkbox.
114
  * Have fun!
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  = UserAgent option Samples =
117
 
118
  * [Default UserAgent](https://github.com/thingsym/multi-device-switcher/wiki/Default-UserAgent)
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  = is_multi_device() function =
121
 
122
- is_multi_device() function is a boolean function, meaning it returns either TRUE or FALSE. Works through the detection of the device by the Multi_Device_Switcher class.
123
 
124
  = Usage =
125
 
@@ -136,6 +206,8 @@ if ( function_exists( 'is_multi_device' ) ) {
136
  /* Display and echo smartphone specific stuff here */
137
  } elseif ( is_multi_device('tablet') ) {
138
  /* Display and echo tablet specific stuff here */
 
 
139
  }
140
  }
141
  ?>
@@ -157,9 +229,56 @@ if ( function_exists( 'is_multi_device' ) ) {
157
 
158
  (boolean) Return boolean whether a particular device.
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  == Changelog ==
162
 
 
 
 
 
 
 
 
 
 
163
  = 1.3.0 =
164
  * fixed: fix script, style, html and readme
165
  * new features: is_multi_device() function
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.1
9
+ Stable tag: 1.4.0
10
  License: GPL2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
22
  = Features =
23
 
24
  * Set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game), switches to selected theme.
25
+ * Add every device by the **Custom Switcher**.
26
+ * Add links 'Mobile' or 'PC' in the theme by the **PC Switcher**, switch to the default theme.
27
+ * Disable the switching of the theme for each url by the **Disable Switcher**.
28
+ * Can be using **is_multi_device()** function that detect of the device.
29
+ * **Multi Device Switcher Command** command-line tool (required WP-CLI)
30
 
31
  = Contributing =
32
 
67
  2. UserAgent option
68
  3. PC Switcher option
69
  4. PC Switcher button
70
+ 5. Disable Switcher
71
 
72
  == Installation ==
73
 
116
  * Configure settings. Check the checkbox 'Add a default CSS.' by PC Switcher option. If you want to customize CSS, uncheck the checkbox.
117
  * Have fun!
118
 
119
+
120
+ You can design the PC Switcher in the Style Sheet.
121
+
122
+ **HTML output of the PC Switcher**
123
+
124
+ `
125
+ <div class="pc-switcher"><span class="active">Mobile</span><a href="http://DOMEIN/PATH/TO/?pc-switcher=1">PC</a></div>
126
+ `
127
+
128
+ **HTML output of the PC Switcher when switched**
129
+
130
+ `
131
+ <div class="pc-switcher"><a href="http://DOMEIN/PATH/TO/?pc-switcher=0">Mobile</a><span class="active">PC</span></div>
132
+ `
133
+
134
+
135
+ = How to use the Disable Switcher =
136
+
137
+ The **Disable Switcher** disable the switching of the theme by a particular URL. If you match the access the url and a string or a regular expression (Regex mode), disable the switching of the theme. Regex mode is for advanced users.
138
+
139
+ 1. Go to the "Multi Device Switcher" options page through the 'Appearance' menu in WordPress.
140
+ 2. Enter the path to the line by line where you want to disable by Disable Switcher option. Check the checkbox 'Enable Regex', if you want to use a regular expression.
141
+ 3. Have fun!
142
+
143
+ = Examples =
144
+
145
+ `
146
+ /sample-page
147
+ /2015/01/hello-world
148
+ `
149
+
150
+ ##### Regex mode (in the case of regular expression)
151
+
152
+ `
153
+ \/sample\-
154
+ \/2015\/01
155
+ `
156
+
157
  = UserAgent option Samples =
158
 
159
  * [Default UserAgent](https://github.com/thingsym/multi-device-switcher/wiki/Default-UserAgent)
160
 
161
+ = Detect the device by JavaScript =
162
+
163
+ Multi Device Switcher set the Cookie that holds the state of the switch. You can get the Cookie and detect the device by JavaScript.
164
+
165
+ = Cookie =
166
+
167
+ * `multi-device-switcher` The name of the device is switched (value: null | device name)
168
+ * `disable-switcher` State of disabled (value: null | 1)
169
+ * `pc-switcher` State of the PC Switcher when switched (value: null | 1)
170
+
171
+ = Examples =
172
+
173
+ `
174
+ <script src="http://DOMEIN/PATH/TO/jquery.cookie.js"></script>
175
+ <script>
176
+ (function($) {
177
+ $(function() {
178
+ if ( $.cookie( 'multi-device-switcher' ) == 'smart' ) {
179
+ /* smartphone specific stuff here */
180
+ } else if ( $.cookie( 'multi-device-switcher' ) == 'tablet' ) {
181
+ /* tablet specific stuff here */
182
+ } else {
183
+ /* pc or other stuff here */
184
+ }
185
+ });
186
+ })(jQuery);
187
+ </script>
188
+ `
189
+
190
  = is_multi_device() function =
191
 
192
+ **is_multi_device()** function is a boolean function, meaning it returns either TRUE or FALSE. Works through the detection of the device by the Multi_Device_Switcher class.
193
 
194
  = Usage =
195
 
206
  /* Display and echo smartphone specific stuff here */
207
  } elseif ( is_multi_device('tablet') ) {
208
  /* Display and echo tablet specific stuff here */
209
+ } else {
210
+ /* Display and echo pc or other stuff here */
211
  }
212
  }
213
  ?>
229
 
230
  (boolean) Return boolean whether a particular device.
231
 
232
+ = Multi Device Switcher Command =
233
+
234
+ The **Multi Device Switcher Command** is command-line tool.
235
+
236
+ Add-on the Multi Device Switcher Command, when you activate the plugin "Multi Device Switcher". To use the Multi Device Switcher Command is WP-CLI required.
237
+
238
+ = NAME =
239
+
240
+ `
241
+ wp multi-device
242
+ `
243
+
244
+ = DESCRIPTION =
245
+
246
+ `
247
+ Multi Device Switcher Command
248
+ `
249
+
250
+ = SYNOPSIS =
251
+
252
+ `
253
+ wp multi-device <command>
254
+ `
255
+
256
+ = SUBCOMMANDS =
257
+
258
+ `
259
+ add add Custom Switcher
260
+ css turn on/off default CSS
261
+ delete delete Custom Switcher
262
+ pc-switcher turn on/off PC Switcher
263
+ reset reset Settings to Default UserAgent
264
+ status get status of settings
265
+ theme get or switch a theme
266
+ useragent get or set UserAgent
267
+ `
268
+
269
+ For more information about the Multi Device Switcher Command, see `wp help multi-device <SUBCOMMANDS>`.
270
 
271
  == Changelog ==
272
 
273
+ = 1.4.0 =
274
+ * edited: edit readme
275
+ * added: add cookies 'multi-device-switcher', 'disable-switcher'
276
+ * new features: Disable Switcher
277
+ * fixed: fix get_options_userAgent(), multi_device_switcher_get_options()
278
+ * fixed: add reserved words validate
279
+ * new features: Multi Device Switcher Command
280
+ * added: add option settings into Theme Customizer
281
+ * fixed: refactoring by the PHP_CodeSniffer
282
  = 1.3.0 =
283
  * fixed: fix script, style, html and readme
284
  * new features: is_multi_device() function
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file
screenshot-5.png ADDED
Binary file
uninstall.php CHANGED
@@ -1,7 +1,8 @@
1
  <?php
2
 
3
- if ( !defined( 'WP_UNINSTALL_PLUGIN' ) )
4
  exit();
 
5
 
6
  function multi_device_switcher_delete_plugin() {
7
  delete_option( 'multi_device_switcher_options' );
1
  <?php
2
 
3
+ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
4
  exit();
5
+ }
6
 
7
  function multi_device_switcher_delete_plugin() {
8
  delete_option( 'multi_device_switcher_options' );
wp-cli.php ADDED
@@ -0,0 +1,465 @@