PluginProbe
Multi Device Switcher / trunk
Multi Device Switcher vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 All 34 releases
← All changes | multi-device-switcher.php +1399 -518 1.1.0trunk View file →
@@ -1,690 +1,1571 @@
1 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.1.0
7 -Author: thingsym
8 -Author URI: http://www.thingslabo.com/
9 -License: GPL2
10 -*/
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.8.7
7 + * Author: thingsym
8 + * Author URI: https://www.thingslabo.com/
9 + * License: GPLv2 or later
10 + * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11 + * Text Domain: multi-device-switcher
12 + * Domain Path: /languages/
13 + *
14 + * @package Multi_Device_Switcher
15 + */
11 16
12 -/*
13 - Copyright 2012 thingsym (http://www.thingslabo.com/)
17 +if ( ! defined( 'ABSPATH' ) ) {
18 + exit;
19 +}
14 20
15 - This program is free software; you can redistribute it and/or modify
16 - it under the terms of the GNU General Public License as published by
17 - the Free Software Foundation; either version 2 of the License, or
18 - (at your option) any later version.
21 +/**
22 + * Core class Multi_Device_Switcher
23 + *
24 + * @since 1.0.0
25 + */
26 +class Multi_Device_Switcher {
19 27
20 - This program is distributed in the hope that it will be useful,
21 - but WITHOUT ANY WARRANTY; without even the implied warranty of
22 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 - GNU General Public License for more details.
28 + /**
29 + * Public variable.
30 + *
31 + * @access public
32 + *
33 + * @var string $option_group The group name of option
34 + */
35 + public $option_group = 'multi_device_switcher';
24 36
25 - You should have received a copy of the GNU General Public License
26 - along with this program; if not, write to the Free Software
27 - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 -*/
37 + /**
38 + * Public variable.
39 + *
40 + * @access public
41 + *
42 + * @var string $option_name The option name
43 + */
44 + public $option_name = 'multi_device_switcher_options';
29 45
30 -class Multi_Device_Switcher {
46 + /**
47 + * Public variable.
48 + *
49 + * @access public
50 + *
51 + * @var string $capability The types of capability
52 + */
53 + public $capability = 'switch_themes';
31 54
55 + /**
56 + * Public variable.
57 + *
58 + * @access public
59 + *
60 + * @var string $cookie_name_multi_device_switcher
61 + */
62 + public $cookie_name_multi_device_switcher = 'multi-device-switcher';
63 +
64 + /**
65 + * Public variable.
66 + *
67 + * @access public
68 + *
69 + * @var string $cookie_name_disable_switcher
70 + */
71 + public $cookie_name_disable_switcher = 'disable-switcher';
72 +
73 + /**
74 + * Public variable.
75 + *
76 + * @access public
77 + *
78 + * @var string $cookie_name_pc_switcher
79 + */
80 + public $cookie_name_pc_switcher = 'pc-switcher';
81 +
82 + /**
83 + * Public variable.
84 + *
85 + * @access public
86 + *
87 + * @var array $default_options {
88 + * default options
89 + *
90 + * @type bool pc_switcher
91 + * @type bool default_css
92 + * @type string theme_smartphone
93 + * @type string theme_tablet
94 + * @type string theme_mobile
95 + * @type string theme_game
96 + * @type string userAgent_smart
97 + * @type string userAgent_tablet
98 + * @type string userAgent_mobile
99 + * @type string userAgent_game
100 + * @type string disable_path
101 + * @type bool enable_regex
102 + * }
103 + *
104 + * @since 1.7.0
105 + */
106 + public $default_options = array(
107 + 'pc_switcher' => 1,
108 + 'default_css' => 1,
109 + 'theme_smartphone' => 'None',
110 + 'theme_tablet' => 'None',
111 + 'theme_mobile' => 'None',
112 + 'theme_game' => 'None',
113 + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko',
114 + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko',
115 + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
116 + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox',
117 + 'disable_path' => '',
118 + 'enable_regex' => 0,
119 + );
120 +
121 + /**
122 + * Public variable.
123 + *
124 + * @access public
125 + *
126 + * @var string $device
127 + */
128 + public $device = '';
129 +
130 + /**
131 + * Public variable.
132 + *
133 + * @access public
134 + *
135 + * @var array|null $plugin_data
136 + */
137 + public $plugin_data = array();
138 +
139 + /**
140 + * Constructor
141 + *
142 + * @access public
143 + *
144 + * @since 1.0.0
145 + */
32 146 public function __construct() {
147 + add_action( 'init', array( $this, 'load_plugin_data' ) );
148 + add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
149 + add_action( 'plugins_loaded', array( $this, 'init' ) );
33 150
34 - $userAgent = $this->get_options_userAgent();
35 - $this->device = '';
151 + if ( ! is_admin() ) {
152 + add_filter( 'wp_headers', array( $this, 'add_header_vary' ) );
153 + add_action( 'plugins_loaded', array( $this, 'detect_device' ) );
154 + add_action( 'plugins_loaded', array( $this, 'switch_theme' ) );
155 + }
36 156
37 - if ( $userAgent['smart'] && preg_match( '/' . implode( '|', $userAgent['smart'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
38 - $this->device = 'smart';
157 + add_action( 'admin_init', array( $this, 'register_settings' ) );
158 + add_action( 'admin_menu', array( $this, 'add_option_page' ) );
159 + add_action( 'customize_register', array( $this, 'customizer' ) );
160 + add_action( 'plugins_loaded', array( $this, 'load_file' ) );
161 + }
162 +
163 + /**
164 + * Initialize.
165 + *
166 + * Hooks to plugins_loaded
167 + *
168 + * @access public
169 + *
170 + * @since 1.6.0
171 + */
172 + public function init() {
173 + add_filter( 'option_page_capability_' . $this->option_group, array( $this, 'option_page_capability' ) );
174 + add_filter( 'plugin_action_links_' . plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ), array( $this, 'plugin_action_links' ) );
175 + add_filter( 'plugin_row_meta', array( $this, 'plugin_metadata_links' ), 10, 2 );
176 +
177 + add_shortcode( 'multi', array( $this, 'shortcode_display_switcher' ) );
178 + }
179 +
180 + /**
181 + * Detect device.
182 + *
183 + * @access public
184 + *
185 + * @return void
186 + *
187 + * @since 1.7.0
188 + */
189 + public function detect_device() {
190 + if ( isset( $_COOKIE[ $this->cookie_name_disable_switcher ] ) ) {
191 + add_action( 'wp_headers', array( $this, 'set_cookie_rest_disable_switcher' ) );
39 192 }
40 - elseif ( $userAgent['tablet'] && preg_match( '/' . implode( '|', $userAgent['tablet'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
41 - $this->device = 'tablet';
193 +
194 + if ( $this->is_disable_switcher() ) {
195 + add_action( 'wp_headers', array( $this, 'set_cookie_enable_disable_switcher' ) );
196 + return;
42 197 }
43 - elseif ( $userAgent['mobile'] && preg_match( '/' . implode( '|', $userAgent['mobile'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
44 - $this->device = 'mobile';
198 +
199 + add_action( 'init', array( $this, 'session' ) );
200 +
201 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
202 + $server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
203 + $user_agent = $this->get_options_user_agent();
204 +
205 + foreach ( array_reverse( $user_agent ) as $key => $val ) {
206 + if ( ! preg_match( '/^custom_switcher_/', $key ) ) {
207 + continue;
208 + }
209 + if ( $user_agent[ $key ] && preg_match( '/' . implode( '|', $user_agent[ $key ] ) . '/i', $server_ua ) ) {
210 + $this->device = $key;
211 + break;
212 + }
45 213 }
46 - elseif ( $userAgent['game'] && preg_match( '/' . implode( '|', $userAgent['game'] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
47 - $this->device = 'game';
214 +
215 + if ( ! $this->device ) {
216 + if ( $user_agent['game'] && preg_match( '/' . implode( '|', $user_agent['game'] ) . '/i', $server_ua ) ) {
217 + $this->device = 'game';
218 + }
219 + elseif ( $user_agent['tablet'] && preg_match( '/' . implode( '|', $user_agent['tablet'] ) . '/i', $server_ua ) ) {
220 + $this->device = 'tablet';
221 + }
222 + elseif ( $user_agent['smart'] && preg_match( '/' . implode( '|', $user_agent['smart'] ) . '/i', $server_ua ) ) {
223 + $this->device = 'smart';
224 + }
225 + elseif ( $user_agent['mobile'] && preg_match( '/' . implode( '|', $user_agent['mobile'] ) . '/i', $server_ua ) ) {
226 + $this->device = 'mobile';
227 + }
48 228 }
229 +
230 + /**
231 + * Action hook: multi_device_switcher/detect_device.
232 + *
233 + * @since 1.7.0
234 + */
235 + do_action( 'multi_device_switcher/detect_device' );
236 + }
237 +
238 + /**
239 + * Switch theme.
240 + *
241 + * @access public
242 + *
243 + * @return void
244 + *
245 + * @since 1.0.0
246 + */
247 + public function switch_theme() {
248 + if ( $this->device ) {
249 + add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
250 + add_filter( 'template', array( $this, 'get_template' ) );
251 + add_action( 'wp_footer', array( $this, 'add_pc_switcher' ) );
252 + add_action( 'wp_headers', array( $this, 'set_cookie_switch_theme' ) );
253 + }
49 254 else {
50 - foreach ( $userAgent as $key => $val ) {
51 - if ( ! preg_match( "/^custom_switcher_/", $key ) )
52 - continue;
53 - if ($userAgent[$key] && preg_match( '/' . implode( '|', $userAgent[$key] ) . '/i', $_SERVER['HTTP_USER_AGENT']) ) {
54 - $this->device = $key;
55 - break;
56 - }
57 - }
255 + add_action( 'wp_headers', array( $this, 'set_cookie_normal_theme' ) );
58 256 }
59 257
60 - if ($this->device) {
61 - add_filter('stylesheet', array(&$this, 'get_stylesheet'));
62 - add_filter('template', array(&$this, 'get_template'));
258 + if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
259 + remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
260 + remove_filter( 'template', array( $this, 'get_template' ) );
63 261 }
64 262 }
65 263
66 - public function get_options_userAgent() {
67 - $options = get_option('multi_device_switcher_options');
68 - $default_options = multi_device_switcher_get_default_options();
264 + /**
265 + * Gets UserAgents.
266 + *
267 + * @access public
268 + *
269 + * @return array
270 + *
271 + * @since 1.0.0
272 + */
273 + public function get_options_user_agent() {
274 + $options = $this->get_options();
69 275
70 - if ( ! isset( $options['userAgent_smart'] ) )
71 - $options['userAgent_smart'] = $default_options['userAgent_smart'];
72 - if ( ! isset( $options['userAgent_tablet'] ) )
73 - $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
74 - if ( ! isset( $options['userAgent_mobile'] ) )
75 - $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
76 - if ( ! isset( $options['userAgent_game'] ) )
77 - $options['userAgent_game'] = $default_options['userAgent_game'];
276 + $user_agent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'], -1, PREG_SPLIT_NO_EMPTY );
277 + $user_agent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'], -1, PREG_SPLIT_NO_EMPTY );
278 + $user_agent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'], -1, PREG_SPLIT_NO_EMPTY );
279 + $user_agent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'], -1, PREG_SPLIT_NO_EMPTY );
78 280
79 - if ( $options['userAgent_smart'] )
80 - $userAgent['smart'] = preg_split( "/,\s*/", $options['userAgent_smart'] );
81 - if ( $options['userAgent_tablet'] )
82 - $userAgent['tablet'] = preg_split( "/,\s*/", $options['userAgent_tablet'] );
83 - if ( $options['userAgent_mobile'] )
84 - $userAgent['mobile'] = preg_split( "/,\s*/", $options['userAgent_mobile'] );
85 - if ( $options['userAgent_game'] )
86 - $userAgent['game'] = preg_split( "/,\s*/", $options['userAgent_game'] );
87 -
88 - foreach ( $options as $key => $val ) {
89 - if ( ! preg_match( "/^custom_switcher_userAgent_/", $key ) )
281 + foreach ( (array) $options as $key => $val ) {
282 + if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) {
90 283 continue;
284 + }
91 285
92 - $custom_switcher_name = preg_replace("/^custom_switcher_userAgent_/", '', $key);
286 + $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key );
93 287
94 - if ($val)
95 - $userAgent['custom_switcher_' . $custom_switcher_name] = preg_split( "/,\s*/", $val );
288 + $user_agent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val, -1, PREG_SPLIT_NO_EMPTY );
96 289 }
97 290
98 - return $userAgent;
291 + return $user_agent;
99 292 }
100 293
101 - public function get_stylesheet($stylesheet = '') {
294 + /**
295 + * Gets stylesheet.
296 + *
297 + * @access public
298 + *
299 + * @param string $stylesheet
300 + *
301 + * @return string
302 + *
303 + * @since 1.0.0
304 + */
305 + public function get_stylesheet( $stylesheet = '' ) {
102 306 $name = $this->get_device_theme();
103 307
104 - if ( empty($name) )
308 + if ( empty( $name ) ) {
105 309 return $stylesheet;
310 + }
106 311
107 312 $themes = wp_get_themes();
108 313 foreach ( $themes as $t ) {
109 - if ($name == $t->get('Name')) {
314 + if ( $name === $t->get( 'Name' ) ) {
110 315 $theme = $t;
111 316 break;
112 317 }
113 318 }
114 319
115 - if ( empty($theme) )
320 + if ( empty( $theme ) ) {
116 321 return $stylesheet;
322 + }
117 323
118 - if ( $theme->get('Status') != 'publish' )
324 + if ( 'publish' !== $theme->get( 'Status' ) ) {
119 325 return $stylesheet;
326 + }
120 327
121 328 return $theme['Stylesheet'];
122 329 }
123 330
124 - public function get_template($template = '') {
331 + /**
332 + * Gets template.
333 + *
334 + * @access public
335 + *
336 + * @param string $template
337 + *
338 + * @return string
339 + *
340 + * @since 1.0.0
341 + */
342 + public function get_template( $template = '' ) {
125 343 $name = $this->get_device_theme();
126 344
127 - if ( empty($name) )
345 + if ( empty( $name ) ) {
128 346 return $template;
347 + }
129 348
130 349 $themes = wp_get_themes();
131 350 foreach ( $themes as $t ) {
132 - if ($name == $t->get('Name')) {
351 + if ( $name === $t->get( 'Name' ) ) {
133 352 $theme = $t;
134 353 break;
135 354 }
136 355 }
137 356
138 - if ( empty( $theme ) )
357 + if ( empty( $theme ) ) {
139 358 return $template;
359 + }
140 360
141 - if ( $theme->get('Status') != 'publish' )
361 + if ( 'publish' !== $theme->get( 'Status' ) ) {
142 362 return $template;
363 + }
143 364
144 365 return $theme['Template'];
145 366 }
146 367
368 + /**
369 + * Gets template by device.
370 + *
371 + * @access public
372 + *
373 + * @return string
374 + *
375 + * @since 1.0.0
376 + */
147 377 public function get_device_theme() {
148 - $options = get_option('multi_device_switcher_options');
378 + $options = $this->get_options();
149 379
150 - if ($this->device == 'smart') {
380 + if ( 'smart' === $this->device ) {
151 381 return $options['theme_smartphone'];
152 382 }
153 - elseif ($this->device == 'tablet') {
383 + elseif ( 'tablet' === $this->device ) {
154 384 return $options['theme_tablet'];
155 385 }
156 - elseif ($this->device == 'mobile') {
386 + elseif ( 'mobile' === $this->device ) {
157 387 return $options['theme_mobile'];
158 388 }
159 - elseif ($this->device == 'game') {
389 + elseif ( 'game' === $this->device ) {
160 390 return $options['theme_game'];
161 391 }
162 392 else {
163 - foreach ( $options as $key => $val ) {
164 - if ( ! preg_match( "/^custom_switcher_theme_/", $key ) )
393 + foreach ( (array) $options as $key => $val ) {
394 + if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
165 395 continue;
396 + }
166 397
167 - $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
398 + $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
168 399
169 - if ($this->device == 'custom_switcher_' . $custom_switcher_name) {
170 - return $options[$key];
400 + if ( 'custom_switcher_' . $custom_switcher_name === $this->device ) {
401 + return $options[ $key ];
171 402 }
172 403 }
173 404 }
174 405
175 - return;
406 + return '';
176 407 }
177 -}
178 408
179 -if ( ! is_admin() )
180 - $multi_device_switcher = new Multi_Device_Switcher();
409 + /**
410 + * Reset cookie for disable_switcher.
411 + *
412 + * @access public
413 + *
414 + * @return void
415 + *
416 + * @since 1.0.0
417 + */
418 + public function set_cookie_rest_disable_switcher() {
419 + setcookie( $this->cookie_name_disable_switcher, '', time() - 3600, '/', '', is_ssl(), false );
420 + }
181 421
182 -/**
183 - * Properly enqueue scripts for our multi_device_switcher options page.
184 - *
185 - * This function is attached to the admin_enqueue_scripts action hook.
186 - *
187 - * @since 1.0
188 - *
189 - */
190 -function multi_device_switcher_admin_enqueue_scripts( $hook_suffix ) {
191 - wp_enqueue_script('jquery-ui-tabs');
192 - wp_enqueue_script( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.js', array( 'jquery' ), '2011-08-22' );
193 -}
422 + /**
423 + * Set enable to cookie for disable_switcher.
424 + *
425 + * @access public
426 + *
427 + * @return void
428 + *
429 + * @since 1.0.0
430 + */
431 + public function set_cookie_enable_disable_switcher() {
432 + setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
433 + setcookie( $this->cookie_name_disable_switcher, '1', 0, '/', '', is_ssl(), false );
434 + }
194 435
195 -/**
196 - * Properly enqueue styles for our multi_device_switcher options page.
197 - *
198 - * This function is attached to the admin_enqueue_styles action hook.
199 - *
200 - * @since 1.0
201 - *
202 - */
203 -function multi_device_switcher_admin_enqueue_styles( $hook_suffix ) {
204 - wp_enqueue_style( 'multi-device-switcher-options', WP_PLUGIN_URL . '/multi-device-switcher/multi-device-switcher.css', false, '2011-08-22' );
205 - wp_enqueue_style( 'thickbox', includes_url() . '/js/thickbox/thickbox.css', false, '20090514' );
206 -}
436 + /**
437 + * Set switched theme to cookie.
438 + *
439 + * @access public
440 + *
441 + * @return void
442 + *
443 + * @since 1.0.0
444 + */
445 + public function set_cookie_switch_theme() {
446 + $device = preg_replace( '/^custom_switcher_/', '', $this->device );
447 + setcookie( $this->cookie_name_multi_device_switcher, (string) $device, 0, '/', '', is_ssl(), false );
448 + }
207 449
208 -/**
209 - * Register the form setting for our multi_device_switcher array.
210 - *
211 - * This function is attached to the admin_init action hook.
212 - *
213 - * This call to register_setting() registers a validation callback, multi_device_switcher_validate(),
214 - * which is used when the option is saved, to ensure that our option values are complete, properly
215 - * formatted, and safe.
216 - *
217 - * @since 1.0
218 - */
219 -function multi_device_switcher_init() {
220 - // If we have no options in the database, let's add them now.
221 - if ( false === multi_device_switcher_get_options() )
222 - add_option( 'multi_device_switcher_options' );
450 + /**
451 + * Reset cookie for normal theme.
452 + *
453 + * @access public
454 + *
455 + * @return void
456 + *
457 + * @since 1.0.0
458 + */
459 + public function set_cookie_normal_theme() {
460 + setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
461 + }
223 462
224 - register_setting(
225 - 'multi_device_switcher', // Options group, see settings_fields() call in multi_device_switcher_render_page()
226 - 'multi_device_switcher_options', // Database option, see multi_device_switcher_get_options()
227 - 'multi_device_switcher_validate' // The sanitization callback, see multi_device_switcher_validate()
228 - );
229 -}
463 + /**
464 + * Set session to cookie for pc switcher.
465 + *
466 + * @access public
467 + *
468 + * @return void
469 + *
470 + * @since 1.0.0
471 + */
472 + public function session() {
473 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
474 + if ( isset( $_GET['pc-switcher'] ) ) {
475 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
476 + setcookie( $this->cookie_name_pc_switcher, sanitize_text_field( wp_unslash( $_GET['pc-switcher'] ) ) ? '1' : '', 0, '/', '', is_ssl(), false );
230 477
231 -add_action( 'admin_init', 'multi_device_switcher_init' );
478 + if ( isset( $_SERVER['REQUEST_URI'] ) ) {
479 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
480 + $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
481 + }
232 482
233 -/**
234 - * Change the capability required to save the 'multi_device_switcher' options group.
235 - *
236 - * @see multi_device_switcher_init() First parameter to register_setting() is the name of the options group.
237 - * @see multi_device_switcher_add_page() The edit_theme_options capability is used for viewing the page.
238 - *
239 - * By default, the options groups for all registered settings require the manage_options capability.
240 - * By default, only administrators have either of these capabilities, but the desire here is
241 - * to allow for finer-grained control for roles and users.
242 - *
243 - * @param string $capability The capability used for the page, which is manage_options by default.
244 - * @return string The capability to actually use.
245 - */
246 -function multi_device_switcher_page_capability( $capability ) {
247 - return 'edit_theme_options';
248 -}
249 -add_filter( 'option_page_capability_multi_device_switcher', 'multi_device_switcher_page_capability' );
483 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
484 + unset( $_GET['pc-switcher'] );
485 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
486 + if ( ! empty( $_GET ) ) {
487 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
488 + $uri = $uri . '?' . http_build_query( $_GET );
489 + }
250 490
251 -/**
252 - * Add our options page to the admin menu, including some help documentation.
253 - *
254 - * This function is attached to the admin_menu action hook.
255 - *
256 - * @since 1.0
257 - */
258 -function multi_device_switcher_add_page() {
259 - load_plugin_textdomain('multi-device-switcher', false, 'multi-device-switcher/languages');
491 + wp_safe_redirect( esc_url( $uri ) );
492 + exit;
493 + }
494 + }
260 495
261 - $theme_page = add_theme_page(
262 - __( 'Multi Device Switcher', 'multi-device-switcher' ), // Name of page
263 - __( 'Multi Device Switcher', 'multi-device-switcher' ), // Label in menu
264 - 'manage_options', // Capability required
265 - 'multi-device-switcher', // Menu slug, used to uniquely identify the page
266 - 'multi_device_switcher_render_page' // Function that renders the options page
267 - );
496 + /**
497 + * Enqueue styles for default_css
498 + *
499 + * @access public
500 + *
501 + * @return void
502 + *
503 + * @since 1.8.5
504 + */
505 + public function enqueue_styles() {
506 + wp_enqueue_style(
507 + 'pc-switcher-options',
508 + plugins_url() . '/multi-device-switcher/pc-switcher.css',
509 + array(),
510 + $this->plugin_data['Version'],
511 + 'all'
512 + );
513 + }
268 514
269 - if ( ! $theme_page )
270 - return;
515 + /**
516 + * Add pc switcher button.
517 + *
518 + * @access public
519 + *
520 + * @param bool $pc_switcher
521 + *
522 + * @return void
523 + *
524 + * @since 1.0.0
525 + */
526 + public function add_pc_switcher( $pc_switcher = 0 ) {
527 + $options = $this->get_options();
528 + $name = $this->get_device_theme();
271 529
272 - $help = '';
530 + if ( $options['pc_switcher'] ) {
531 + $pc_switcher = 1;
532 + }
273 533
274 - if ($help)
275 - add_contextual_help( $theme_page, $help );
534 + if ( $pc_switcher && $name && 'None' !== $name ) {
535 + if ( $options['default_css'] ) {
536 + $this->enqueue_styles();
537 + }
276 538
277 - add_action( "admin_print_scripts-$theme_page", 'multi_device_switcher_admin_enqueue_scripts' );
278 - add_action( "admin_print_styles-$theme_page", 'multi_device_switcher_admin_enqueue_styles' );
279 -}
280 -add_action( 'admin_menu', 'multi_device_switcher_add_page' );
539 + $uri = is_ssl() ? 'https://' : 'http://';
281 540
282 -/**
283 - * Returns the default options.
284 - *
285 - * @since 1.0
286 - */
287 -function multi_device_switcher_get_default_options() {
288 - $default_theme_options = array(
289 - 'theme_smartphone' => 'None',
290 - 'theme_tablet' => 'None',
291 - 'theme_mobile' => 'None',
292 - 'theme_game' => 'None',
293 - 'userAgent_smart' => 'iPhone, iPod, Android, dream, CUPCAKE, Windows Phone, webOS, BlackBerry8707, BlackBerry9000, BlackBerry9300, BlackBerry9500, BlackBerry9530, BlackBerry9520, BlackBerry9550, BlackBerry9700, BlackBerry9800',
294 - 'userAgent_tablet' => 'iPad',
295 - 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
296 - 'userAgent_game' => 'PlayStation Portable, PlayStation Vita, PSP, PS2, PLAYSTATION 3, Nitro, Nintendo 3DS, Nintendo Wii',
297 - );
541 + if ( isset( $_SERVER['HTTP_HOST'] ) ) {
542 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
543 + $uri .= $_SERVER['HTTP_HOST'];
544 + }
298 545
299 - return $default_theme_options;
300 -}
546 + if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
547 + $uri .= add_query_arg( 'pc-switcher', 0 );
548 + ?>
549 +<div class="pc-switcher"><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'Mobile', 'multi-device-switcher' ); ?></a><span class="active"><?php esc_html_e( 'PC', 'multi-device-switcher' ); ?></span></div>
550 + <?php
551 + }
552 + else {
553 + $uri .= add_query_arg( 'pc-switcher', 1 );
554 + ?>
555 +<div class="pc-switcher"><span class="active"><?php esc_html_e( 'Mobile', 'multi-device-switcher' ); ?></span><a href="<?php echo esc_url( $uri ); ?>"><?php esc_html_e( 'PC', 'multi-device-switcher' ); ?></a></div>
556 + <?php
557 + }
558 + }
559 + }
301 560
302 -/**
303 - * Returns the options array.
304 - *
305 - * @since 1.0
306 - */
307 -function multi_device_switcher_get_options() {
308 - $options = get_option( 'multi_device_switcher_options' );
309 - $default_options = multi_device_switcher_get_default_options();
561 + /**
562 + * Check device.
563 + *
564 + * @access public
565 + *
566 + * @param string $device
567 + *
568 + * @return bool
569 + *
570 + * @since 1.0.0
571 + */
572 + public function is_multi_device( $device = '' ) {
573 + if ( $device === $this->device ) {
574 + return true;
575 + }
576 + if ( 'custom_switcher_' . $device === $this->device ) {
577 + return true;
578 + }
310 579
311 - if ( ! isset( $options['theme_smartphone'] ) )
312 - $options['theme_smartphone'] = $default_options['theme_smartphone'];
313 - if ( ! isset( $options['theme_tablet'] ) )
314 - $options['theme_tablet'] = $default_options['theme_tablet'];
315 - if ( ! isset( $options['theme_mobile'] ) )
316 - $options['theme_mobile'] = $default_options['theme_mobile'];
317 - if ( ! isset( $options['theme_game'] ) )
318 - $options['theme_game'] = $default_options['theme_game'];
580 + return false;
581 + }
319 582
320 - if ( ! isset( $options['userAgent_smart'] ) )
321 - $options['userAgent_smart'] = $default_options['userAgent_smart'];
322 - if ( ! isset( $options['userAgent_tablet'] ) )
323 - $options['userAgent_tablet'] = $default_options['userAgent_tablet'];
324 - if ( ! isset( $options['userAgent_mobile'] ) )
325 - $options['userAgent_mobile'] = $default_options['userAgent_mobile'];
326 - if ( ! isset( $options['userAgent_game'] ) )
327 - $options['userAgent_game'] = $default_options['userAgent_game'];
583 + /**
584 + * Whether pc switcher enabled.
585 + *
586 + * @access public
587 + *
588 + * @return bool
589 + *
590 + * @since 1.0.0
591 + */
592 + public function is_pc_switcher() {
593 + return isset( $_COOKIE[ $this->cookie_name_pc_switcher ] );
594 + }
328 595
329 - return $options;
330 -}
596 + /**
597 + * Whether theme switch disabled.
598 + *
599 + * @access public
600 + *
601 + * @param bool $disable
602 + *
603 + * @return bool
604 + *
605 + * @since 1.0.0
606 + */
607 + public function is_disable_switcher( $disable = false ) {
608 + $options = $this->get_options();
609 + $disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY );
331 610
332 -/**
333 - * Rendering Options Setting Page.
334 - *
335 - * @since 1.0
336 - */
337 -function multi_device_switcher_render_page() {
338 - ?>
339 - <div class="wrap" style="width: 60%; float: left;">
340 - <div id="icon-themes" class="icon32"><br></div>
341 - <h2><?php printf( __( 'Multi Device Switcher', 'multi-device-switcher' ), wp_get_theme()->get('Name') ); ?></h2>
611 + if ( isset( $_SERVER['REQUEST_URI'] ) ) {
612 + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
613 + $request_uri = $_SERVER['REQUEST_URI'];
614 + }
615 +
616 + foreach ( (array) $disable_path as $path ) {
617 + if ( $options['enable_regex'] ) {
618 + if ( preg_match( '/' . $path . '/i', $request_uri ) ) {
619 + $disable = true;
620 + break;
621 + }
622 + }
623 + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
624 + else {
625 + if ( preg_match( '/^' . preg_quote( (string) $path, '/' ) . '$/i', $request_uri ) ) {
626 + $disable = true;
627 + break;
628 + }
629 + }
630 + }
631 +
632 + return $disable;
633 + }
634 +
635 + /**
636 + * Shortcode.
637 + *
638 + * @access public
639 + *
640 + * @param array $atts
641 + * @param string $content
642 + *
643 + * @return string
644 + *
645 + * @since 1.0.0
646 + */
647 + public function shortcode_display_switcher( $atts, $content = '' ) {
648 + $atts = shortcode_atts(
649 + array(
650 + 'device' => '',
651 + ),
652 + $atts
653 + );
654 +
655 + if ( empty( $atts['device'] ) && ( $this->is_multi_device( $atts['device'] ) || $this->is_pc_switcher() ) ) {
656 + return $content;
657 + }
658 + elseif ( ! empty( $atts['device'] ) && $this->is_multi_device( $atts['device'] ) && ! $this->is_pc_switcher() ) {
659 + return $content;
660 + }
661 +
662 + return '';
663 + }
664 +
665 + /**
666 + * Add HTTP Vary header.
667 + *
668 + * @access public
669 + *
670 + * @param string $headers
671 + *
672 + * @return string
673 + *
674 + * @since 1.1.1
675 + */
676 + public function add_header_vary( $headers ) {
677 + /**
678 + * Filter hook: multi_device_switcher/add_header_vary.
679 + *
680 + * @param string 'User-Agent' header name.
681 + *
682 + * @since 1.6.2
683 + */
684 + $headers['Vary'] = apply_filters( 'multi_device_switcher/add_header_vary', 'User-Agent' );
685 + return $headers;
686 + }
687 +
688 + /**
689 + * Enqueue scripts.
690 + *
691 + * Hooks to admin_enqueue_scripts.
692 + *
693 + * @access public
694 + *
695 + * @since 1.0.0
696 + */
697 + public function admin_enqueue_scripts() {
698 + wp_enqueue_script(
699 + 'multi-device-switcher-options',
700 + plugins_url() . '/multi-device-switcher/multi-device-switcher.js',
701 + array( 'jquery', 'jquery-ui-tabs' ),
702 + $this->plugin_data['Version'],
703 + false
704 + );
705 + }
706 +
707 + /**
708 + * Enqueue styles.
709 + *
710 + * Hooks to admin_enqueue_styles.
711 + *
712 + * @access public
713 + *
714 + * @since 1.0.0
715 + */
716 + public function admin_enqueue_styles() {
717 + wp_enqueue_style(
718 + 'multi-device-switcher-options',
719 + plugins_url() . '/multi-device-switcher/multi-device-switcher.css',
720 + array(),
721 + $this->plugin_data['Version'],
722 + 'all'
723 + );
724 + }
725 +
726 + /**
727 + * Register the form setting.
728 + *
729 + * Hooks to admin_init.
730 + *
731 + * @access public
732 + *
733 + * @return void
734 + *
735 + * @since 1.0
736 + */
737 + public function register_settings() {
738 + if ( is_null( $this->get_options() ) ) {
739 + add_option( $this->option_name );
740 + }
741 +
742 + register_setting(
743 + $this->option_group,
744 + $this->option_name,
745 + array( $this, 'validate_options' )
746 + );
747 + }
748 +
749 + /**
750 + * Returns capability.
751 + *
752 + * @access public
753 + *
754 + * @return string
755 + *
756 + * @since 1.0.0
757 + */
758 + public function option_page_capability() {
759 + return $this->capability;
760 + }
761 +
762 + /**
763 + * Adds option page.
764 + *
765 + * @access public
766 + *
767 + * @return void
768 + *
769 + * @since 1.0.0
770 + */
771 + public function add_option_page() {
772 + $page_hook = add_theme_page(
773 + __( 'Multi Device Switcher', 'multi-device-switcher' ),
774 + __( 'Multi Device Switcher', 'multi-device-switcher' ),
775 + $this->option_page_capability(),
776 + 'multi-device-switcher',
777 + array( $this, 'render_option_page' )
778 + );
779 +
780 + if ( ! $page_hook ) {
781 + return;
782 + }
783 +
784 + add_action( 'load-' . $page_hook, array( $this, 'page_hook_suffix' ) );
785 + }
786 +
787 + /**
788 + * Page Hook Suffix.
789 + *
790 + * Hooks to load-{$page_hook}.
791 + *
792 + * @access public
793 + *
794 + * @return void
795 + *
796 + * @since 1.2.4
797 + */
798 + public function page_hook_suffix() {
799 + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
800 + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
801 + }
802 +
803 + /**
804 + * Set link to customizer section on the plugins page.
805 + *
806 + * Hooks to plugin_action_links_{$plugin_file}
807 + *
808 + * @see https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
809 + *
810 + * @access public
811 + *
812 + * @param array $links An array of plugin action links.
813 + *
814 + * @return array $links
815 + *
816 + * @since 1.6.0
817 + */
818 + public function plugin_action_links( $links = array() ) {
819 + $settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', 'multi-device-switcher' ) . '</a>';
820 +
821 + array_unshift( $links, $settings_link );
822 +
823 + return $links;
824 + }
825 +
826 + /**
827 + * Set links below a plugin on the Plugins page.
828 + *
829 + * Hooks to plugin_row_meta
830 + *
831 + * @see https://developer.wordpress.org/reference/hooks/plugin_row_meta/
832 + *
833 + * @access public
834 + *
835 + * @param array $links An array of the plugin's metadata.
836 + * @param string $file Path to the plugin file relative to the plugins directory.
837 + *
838 + * @return array $links
839 + *
840 + * @since 1.8.1
841 + */
842 + public function plugin_metadata_links( $links, $file ) {
843 + if ( $file === plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) {
844 + $links[] = '<a href="https://github.com/sponsors/thingsym">' . __( 'Become a sponsor', 'multi-device-switcher' ) . '</a>';
845 + }
846 +
847 + return $links;
848 + }
849 +
850 + /**
851 + * Returns the default options.
852 + *
853 + * @access public
854 + *
855 + * @return array|null
856 + *
857 + * @since 1.0.0
858 + */
859 + public function get_default_options() {
860 + return $this->default_options;
861 + }
862 +
863 + /**
864 + * Returns the options array or value.
865 + *
866 + * @access public
867 + *
868 + * @param string $option_name Optional. The option name.
869 + *
870 + * @return array|null
871 + *
872 + * @since 1.0.0
873 + */
874 + public function get_options( $option_name = null ) {
875 + $options = get_option( $this->option_name, $this->default_options );
876 + $options = array_merge( $this->default_options, $options );
877 +
878 + if ( is_null( $option_name ) ) {
879 + /**
880 + * Filter hook: multi_device_switcher/get_options.
881 + *
882 + * @param array $options The options.
883 + *
884 + * @since 1.7.0
885 + */
886 + return apply_filters( 'multi_device_switcher/get_options', $options );
887 + }
888 +
889 + if ( array_key_exists( $option_name, $options ) ) {
890 + /**
891 + * Filter hook: multi_device_switcher/get_option.
892 + *
893 + * @param mixed $option The value of option.
894 + * @param string $option_name The option name via argument.
895 + *
896 + * @since 1.7.0
897 + */
898 + return apply_filters( 'multi_device_switcher/get_option', $options[ $option_name ], $option_name );
899 + }
900 +
901 + return null;
902 + }
903 +
904 + /**
905 + * Load textdomain
906 + *
907 + * @access public
908 + *
909 + * @return boolean
910 + *
911 + * @since 1.6.0
912 + */
913 + public function load_textdomain() {
914 + return load_plugin_textdomain(
915 + 'multi-device-switcher',
916 + false,
917 + 'multi-device-switcher/languages'
918 + );
919 + }
920 +
921 + /**
922 + * Load plugin data
923 + *
924 + * @access public
925 + *
926 + * @return boolean
927 + *
928 + * @since 1.8.1
929 + */
930 + public function load_plugin_data() {
931 + if ( ! function_exists( 'get_plugin_data' ) ) {
932 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
933 + }
934 +
935 + $this->plugin_data = get_plugin_data( __MULTI_DEVICE_SWITCHER_FILE__ );
936 +
937 + if ( ! $this->plugin_data ) {
938 + return false;
939 + }
940 +
941 + return true;
942 + }
943 +
944 + /**
945 + * Display option page.
946 + *
947 + * @access public
948 + *
949 + * @return void
950 + *
951 + * @since 1.0.0
952 + */
953 + public function render_option_page() {
954 + ?>
955 +<div class="wrap">
956 +<div id="icon-themes" class="icon32"><br></div>
957 +<h2><?php esc_html_e( 'Multi Device Switcher', 'multi-device-switcher' ); ?></h2>
342 958 <?php settings_errors(); ?>
343 959
344 - <form method="post" action="options.php">
345 - <?php
346 - settings_fields( 'multi_device_switcher' );
347 - $options = multi_device_switcher_get_options();
960 +<form method="post" action="options.php">
961 + <?php
962 + settings_fields( 'multi_device_switcher' );
963 + $options = $this->get_options();
348 964
349 - $default_theme = wp_get_theme()->get('Name');
350 - $themes = wp_get_themes();
351 - $theme_names = array();
965 + $default_theme = wp_get_theme()->get( 'Name' );
966 + $themes = wp_get_themes();
967 + $theme_names = array();
352 968
353 - if (count($themes)) {
354 - foreach ( $themes as $t ) {
355 - $theme_names[] = $t->get('Name');
356 - }
357 - natcasesort($theme_names);
358 - }
359 - ?>
969 + if ( count( $themes ) ) {
970 + foreach ( $themes as $t ) {
971 + $theme_names[] = $t->get( 'Name' );
972 + }
973 + natcasesort( $theme_names );
974 + }
975 + ?>
360 976
361 - <div id="admin-tabs">
362 - <fieldset id="Theme" class="options">
363 - <h3 class="label"><?php _e( 'Theme', 'multi-device-switcher' ); ?></h3>
364 - <table class="form-table">
365 - <tr><th scope="row"><?php _e( 'Smart Phone Theme', 'multi-device-switcher' ); ?></th>
366 - <td>
977 +<div id="admin-tabs">
978 +<fieldset id="Theme" class="options">
979 +<h3 class="label"><?php esc_html_e( 'Theme', 'multi-device-switcher' ); ?></h3>
980 +<table class="form-table">
981 + <tr><th scope="row"><?php esc_html_e( 'Smart Phone Theme', 'multi-device-switcher' ); ?></th>
982 + <td>
367 983
368 - <?php
369 - if (count($theme_names)) {
370 - $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
984 + <?php
985 + $html = '';
986 + if ( count( $theme_names ) ) {
987 + $html = '<select name="multi_device_switcher_options[theme_smartphone]">';
371 988
372 - if (($options['theme_smartphone'] == 'None') || ($options['theme_smartphone'] == '')) {
373 - $html .= '<option value="None" selected="selected">None</option>';
374 - }
375 - else {
376 - $html .= '<option value="None">None</option>';
377 - }
989 + if ( ( 'None' === $options['theme_smartphone'] ) || ( '' === $options['theme_smartphone'] ) ) {
990 + $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
991 + }
992 + else {
993 + $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
994 + }
378 995
379 - foreach ($theme_names as $theme_name) {
380 - if ($default_theme == $theme_name)
381 - continue;
382 - if ($options['theme_smartphone'] == $theme_name) {
383 - $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
384 - }
385 - else {
386 - $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
387 - }
388 - }
389 - $html .= '</select>';
996 + foreach ( $theme_names as $theme_name ) {
997 + if ( $default_theme === $theme_name ) {
998 + continue;
390 999 }
391 - echo $html;
392 - ?>
393 - </td>
394 - </tr>
395 - <tr><th scope="row"><?php _e( 'Tablet PC Theme', 'multi-device-switcher' ); ?></th>
396 - <td>
1000 + if ( $options['theme_smartphone'] === $theme_name ) {
1001 + $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1002 + }
1003 + else {
1004 + $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1005 + }
1006 + }
1007 + $html .= '</select>';
1008 + }
1009 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1010 + echo $html;
1011 + ?>
397 1012
398 - <?php
399 - if (count($theme_names)) {
400 - $html = '<select name="multi_device_switcher_options[theme_tablet]">';
1013 + </td>
1014 + </tr>
1015 + <tr><th scope="row"><?php esc_html_e( 'Tablet PC Theme', 'multi-device-switcher' ); ?></th>
1016 + <td>
401 1017
402 - if (($options['theme_tablet'] == 'None') || ($options['theme_tablet'] == '')) {
403 - $html .= '<option value="None" selected="selected">None</option>';
404 - }
405 - else {
406 - $html .= '<option value="None">None</option>';
407 - }
1018 + <?php
1019 + if ( count( $theme_names ) ) {
1020 + $html = '<select name="multi_device_switcher_options[theme_tablet]">';
408 1021
409 - foreach ($theme_names as $theme_name) {
410 - if ($default_theme == $theme_name)
411 - continue;
412 - if ($options['theme_tablet'] == $theme_name) {
413 - $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
414 - }
415 - else {
416 - $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
417 - }
418 - }
419 - $html .= '</select>';
1022 + if ( ( 'None' === $options['theme_tablet'] ) || ( '' === $options['theme_tablet'] ) ) {
1023 + $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1024 + }
1025 + else {
1026 + $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1027 + }
1028 +
1029 + foreach ( $theme_names as $theme_name ) {
1030 + if ( $default_theme === $theme_name ) {
1031 + continue;
420 1032 }
421 - echo $html;
422 - ?>
423 - </td>
424 - </tr>
425 - <tr><th scope="row"><?php _e( 'Mobile Phone Theme', 'multi-device-switcher' ); ?></th>
426 - <td>
1033 + if ( $options['theme_tablet'] === $theme_name ) {
1034 + $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1035 + }
1036 + else {
1037 + $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1038 + }
1039 + }
1040 + $html .= '</select>';
1041 + }
1042 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1043 + echo $html;
1044 + ?>
427 1045
428 - <?php
429 - if (count($theme_names)) {
430 - $html = '<select name="multi_device_switcher_options[theme_mobile]">';
1046 + </td>
1047 + </tr>
1048 + <tr><th scope="row"><?php esc_html_e( 'Mobile Phone Theme', 'multi-device-switcher' ); ?></th>
1049 + <td>
431 1050
432 - if (($options['theme_mobile'] == 'None') || ($options['theme_mobile'] == '')) {
433 - $html .= '<option value="None" selected="selected">None</option>';
434 - }
435 - else {
436 - $html .= '<option value="None">None</option>';
437 - }
1051 + <?php
1052 + if ( count( $theme_names ) ) {
1053 + $html = '<select name="multi_device_switcher_options[theme_mobile]">';
438 1054
439 - foreach ($theme_names as $theme_name) {
440 - if ($default_theme == $theme_name)
441 - continue;
442 - if ($options['theme_mobile'] == $theme_name) {
443 - $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
444 - }
445 - else {
446 - $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
447 - }
448 - }
449 - $html .= '</select>';
1055 + if ( ( 'None' === $options['theme_mobile'] ) || ( '' === $options['theme_mobile'] ) ) {
1056 + $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1057 + }
1058 + else {
1059 + $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1060 + }
1061 +
1062 + foreach ( $theme_names as $theme_name ) {
1063 + if ( $default_theme === $theme_name ) {
1064 + continue;
450 1065 }
451 - echo $html;
452 - ?>
453 - </td>
454 - </tr>
455 - <tr><th scope="row"><?php _e( 'Game Platforms Theme', 'multi-device-switcher' ); ?></th>
456 - <td>
1066 + if ( $options['theme_mobile'] === $theme_name ) {
1067 + $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1068 + }
1069 + else {
1070 + $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1071 + }
1072 + }
1073 + $html .= '</select>';
1074 + }
1075 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1076 + echo $html;
1077 + ?>
457 1078
458 - <?php
459 - if (count($theme_names)) {
460 - $html = '<select name="multi_device_switcher_options[theme_game]">';
1079 + </td>
1080 + </tr>
1081 + <tr><th scope="row"><?php esc_html_e( 'Game Platforms Theme', 'multi-device-switcher' ); ?></th>
1082 + <td>
461 1083
462 - if (($options['theme_game'] == 'None') || ($options['theme_game'] == '')) {
463 - $html .= '<option value="None" selected="selected">None</option>';
464 - }
465 - else {
466 - $html .= '<option value="None">None</option>';
467 - }
1084 + <?php
1085 + if ( count( $theme_names ) ) {
1086 + $html = '<select name="multi_device_switcher_options[theme_game]">';
468 1087
469 - foreach ($theme_names as $theme_name) {
470 - if ($default_theme == $theme_name)
471 - continue;
472 - if ($options['theme_game'] == $theme_name) {
473 - $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
474 - }
475 - else {
476 - $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
477 - }
478 - }
479 - $html .= '</select>';
1088 + if ( ( 'None' === $options['theme_game'] ) || ( '' === $options['theme_game'] ) ) {
1089 + $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1090 + }
1091 + else {
1092 + $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1093 + }
1094 +
1095 + foreach ( $theme_names as $theme_name ) {
1096 + if ( $default_theme === $theme_name ) {
1097 + continue;
480 1098 }
481 - echo $html;
482 - ?>
483 - </td>
484 - </tr>
485 - </table>
1099 + if ( $options['theme_game'] === $theme_name ) {
1100 + $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1101 + }
1102 + else {
1103 + $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
1104 + }
1105 + }
1106 + $html .= '</select>';
1107 + }
1108 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1109 + echo $html;
1110 + ?>
486 1111
487 - <h3><?php _e( 'Custom Switcher Theme', 'multi-device-switcher' ); ?></h3>
488 - <table class="form-table">
1112 + </td>
1113 + </tr>
1114 +</table>
489 1115
490 - <?php
491 - foreach ( $options as $key => $val ) {
492 - if ( ! preg_match( "/^custom_switcher_theme_/", $key ) )
493 - continue;
1116 +<h3><?php esc_html_e( 'Custom Switcher Theme', 'multi-device-switcher' ); ?></h3>
1117 +<table class="form-table">
494 1118
495 - $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
496 - $custom_switcher_option = $key;
497 - $custom_switcher_theme = $val;
1119 + <?php
1120 + foreach ( (array) $options as $custom_switcher_option => $custom_switcher_theme ) {
1121 + if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1122 + continue;
1123 + }
1124 +
1125 + $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
498 1126 ?>
499 1127
500 - <tr><th scope="row"><?php _e( $custom_switcher_name, 'multi-device-switcher' ); ?></th>
501 - <td>
1128 + <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
1129 + <td>
502 1130
503 1131 <?php
504 - if (count($theme_names)) {
505 - $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
1132 + if ( count( $theme_names ) ) {
1133 + $html = '<select name="multi_device_switcher_options[' . $custom_switcher_option . ']">';
506 1134
507 - if (($custom_switcher_theme == 'None') || ($custom_switcher_theme == '')) {
508 - $html .= '<option value="None" selected="selected">None</option>';
1135 + if ( ( 'None' === $custom_switcher_theme ) || ( '' === $custom_switcher_theme ) ) {
1136 + $html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1137 + }
1138 + else {
1139 + $html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
1140 + }
1141 +
1142 + foreach ( $theme_names as $theme_name ) {
1143 + if ( $default_theme === $theme_name ) {
1144 + continue;
509 1145 }
1146 + if ( $custom_switcher_theme === $theme_name ) {
1147 + $html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';
1148 + }
510 1149 else {
511 - $html .= '<option value="None">None</option>';
1150 + $html .= '<option value="' . esc_attr( $theme_name ) . '">' . esc_html( $theme_name ) . '</option>';
512 1151 }
513 -
514 - foreach ($theme_names as $theme_name) {
515 - if ($default_theme == $theme_name)
516 - continue;
517 - if ($custom_switcher_theme == $theme_name) {
518 - $html .= '<option value="' . $theme_name . '" selected="selected">' . htmlspecialchars($theme_name) . '</option>';
519 - }
520 - else {
521 - $html .= '<option value="' . $theme_name . '">' . htmlspecialchars($theme_name) . '</option>';
522 - }
523 - }
524 - $html .= '</select>';
525 - $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 ) . '\');""></span>';
526 1152 }
527 - echo $html;
1153 + $html .= '</select>';
1154 + /* translators: confirm: 1: custom switcher name */
1155 + $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>';
1156 + }
1157 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1158 + echo $html;
528 1159 ?>
529 - </td>
530 - </tr>
1160 + </td>
1161 + </tr>
531 1162
532 1163 <?php
533 - }
534 - ?>
1164 + }
1165 + ?>
535 1166
536 - <tr><th scope="row"><?php _e( 'Add Custom Switcher', 'multi-device-switcher' ); ?></th>
537 - <td>
538 - <fieldset><legend class="screen-reader-text"><span><?php _e( 'Add Custom Switcher', 'thingscms' ); ?></span></legend>
539 - <input type="text" name="multi_device_switcher_options[custom_switcher]" id="custom-switcher" value="" size="24" />
540 - <span class="submit"><input type="submit" name="multi_device_switcher_options[add_custom_switcher]" value="<?php _e( 'Add', 'multi-device-switcher' ); ?>"></span><br>
541 - <?php _e( '20 characters max, alphanumeric', 'multi-device-switcher' ); ?>
542 - </td>
543 - </tr>
544 - </table>
1167 + <tr><th scope="row"><?php esc_html_e( 'Add Custom Switcher', 'multi-device-switcher' ); ?></th>
1168 + <td>
1169 + <legend class="screen-reader-text"><span><?php esc_html_e( 'Add Custom Switcher', 'multi-device-switcher' ); ?></span></legend>
1170 + <input type="text" name="multi_device_switcher_options[custom_switcher]" id="custom-switcher" value="" size="24">
1171 + <span class="submit"><input type="submit" name="multi_device_switcher_options[add_custom_switcher]" value="<?php esc_html_e( 'Add', 'multi-device-switcher' ); ?>" class="button"></span><br>
1172 + <?php esc_html_e( '20 characters max, alphanumeric', 'multi-device-switcher' ); ?>
1173 + </td>
1174 + </tr>
1175 +</table>
545 1176
546 - </fieldset>
1177 +</fieldset>
547 1178
548 - <fieldset id="UserAgent" class="options">
549 - <h3 class="label"><?php _e( 'UserAgent', 'multi-device-switcher' ); ?></h3>
550 - <p><?php _e( 'Enter Comma-separated values (csv) format.', 'multi-device-switcher' ); ?></p>
1179 +<fieldset id="UserAgent" class="options">
1180 +<h3 class="label"><?php esc_html_e( 'UserAgent', 'multi-device-switcher' ); ?></h3>
1181 +<p><?php esc_html_e( 'Enter Comma-separated values (csv) format.', 'multi-device-switcher' ); ?></p>
551 1182
552 - <table class="form-table">
553 - <tr><th scope="row"><?php _e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
554 - <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo $options['userAgent_smart']; ?></textarea></td>
555 - </tr>
556 - <tr><th scope="row"><?php _e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
557 - <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo $options['userAgent_tablet']; ?></textarea></td>
558 - </tr>
559 - <tr><th scope="row"><?php _e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
560 - <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo $options['userAgent_mobile']; ?></textarea></td>
561 - </tr>
562 - <tr><th scope="row"><?php _e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
563 - <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo $options['userAgent_game']; ?></textarea></td>
564 - </tr>
565 - <tr><th></th>
566 - <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' ); ?>"></span></td>
567 - </tr>
1183 +<table class="form-table">
1184 + <tr><th scope="row"><?php esc_html_e( 'Smart Phone', 'multi-device-switcher' ); ?></th>
1185 + <td><textarea name="multi_device_switcher_options[userAgent_smart]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_smart'] ); ?></textarea></td>
1186 + </tr>
1187 + <tr><th scope="row"><?php esc_html_e( 'Tablet PC', 'multi-device-switcher' ); ?></th>
1188 + <td><textarea name="multi_device_switcher_options[userAgent_tablet]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_tablet'] ); ?></textarea></td>
1189 + </tr>
1190 + <tr><th scope="row"><?php esc_html_e( 'Mobile Phone', 'multi-device-switcher' ); ?></th>
1191 + <td><textarea name="multi_device_switcher_options[userAgent_mobile]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_mobile'] ); ?></textarea></td>
1192 + </tr>
1193 + <tr><th scope="row"><?php esc_html_e( 'Game Platforms', 'multi-device-switcher' ); ?></th>
1194 + <td><textarea name="multi_device_switcher_options[userAgent_game]" rows="4" cols="42"><?php echo esc_textarea( $options['userAgent_game'] ); ?></textarea></td>
1195 + </tr>
1196 + <tr><th></th>
1197 + <td><span class="submit"><input type="submit" name="multi_device_switcher_options[restore_UserAgent]" value="<?php esc_html_e( 'Reset Settings to Default UserAgent', 'multi-device-switcher' ); ?>" class="button"></span></td>
1198 + </tr>
568 1199
569 - <?php
570 - foreach ( $options as $key => $val ) {
571 - if ( ! preg_match( "/^custom_switcher_userAgent_/", $key ) )
572 - continue;
1200 +</table>
573 1201
574 - $custom_switcher_name = preg_replace("/^custom_switcher_userAgent_/", '', $key);
575 - $custom_switcher_option = $key;
576 - $custom_switcher_userAgent = $val;
1202 +<h3><?php esc_html_e( 'Custom Switcher UserAgent', 'multi-device-switcher' ); ?></h3>
1203 +<table class="form-table">
1204 + <?php
1205 + foreach ( (array) $options as $custom_switcher_option => $custom_switcher_user_agent ) {
1206 + if ( ! preg_match( '/^custom_switcher_userAgent_/', $custom_switcher_option ) ) {
1207 + continue;
1208 + }
1209 +
1210 + $custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $custom_switcher_option );
577 1211 ?>
578 1212
579 - <tr><th scope="row"><?php _e( $custom_switcher_name, 'multi-device-switcher' ); ?></th>
580 - <td><textarea name="multi_device_switcher_options[<?php echo $custom_switcher_option; ?>]" rows="4" cols="42"><?php echo $custom_switcher_userAgent; ?></textarea></td>
581 - </tr>
1213 + <tr><th scope="row"><?php echo esc_html( $custom_switcher_name ); ?></th>
1214 + <td><textarea name="multi_device_switcher_options[<?php echo esc_attr( $custom_switcher_option ); ?>]" rows="4" cols="42"><?php echo esc_textarea( $custom_switcher_user_agent ); ?></textarea></td>
1215 + </tr>
582 1216 <?php
583 - }
584 - ?>
1217 + }
1218 + ?>
585 1219
586 - </table>
587 - </fieldset>
588 - </div>
589 - <?php submit_button(); ?>
590 - </form>
591 - </div>
1220 +</table>
1221 +</fieldset>
592 1222
593 - <div id="donate" style="width: 36%; float: left; margin: 2em 0;padding: 0.4em;border: 1px solid #ddd;">
594 - <h2><?php _e( 'Donationware', 'multi-device-switcher' ); ?></h2>
595 - <p><?php _e( 'If you like this plugin, please donate to support development and maintenance.', 'multi-device-switcher' ); ?></p>
596 -<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
597 -<input type="hidden" name="cmd" value="_s-xclick">
598 -<input type="hidden" name="hosted_button_id" value="9L53NELFMHTWW">
599 -<table>
600 -<tr><td><input type="hidden" name="on0" value="Donationware">Donationware</td></tr><tr><td><select name="os0">
601 - <option value="1. Donate">1. Donate $3.00 USD</option>
602 - <option value="2. Donate">2. Donate $5.00 USD</option>
603 - <option value="3. Donate">3. Donate $7.00 USD</option>
604 - <option value="4. Donate" selected="selected">4. Donate $10.00 USD</option>
605 - <option value="5. Donate">5. Donate $20.00 USD</option>
606 - <option value="6. Donate">6. Donate $30.00 USD</option>
607 - <option value="7. Donate">7. Donate $40.00 USD</option>
608 - <option value="8. Donate">8. Donate $50.00 USD</option>
609 - <option value="9. Donate">9. Donate $60.00 USD</option>
610 - <option value="10. Donate">10. Donate $70.00 USD</option>
611 -</select> </td></tr>
1223 +<fieldset id="PC-Switcher" class="options">
1224 +<h3 class="label"><?php esc_html_e( 'PC Switcher', 'multi-device-switcher' ); ?></h3>
1225 +
1226 +<table class="form-table">
1227 + <tr><th scope="row"><?php esc_html_e( 'Add PC Switcher', 'multi-device-switcher' ); ?></th>
1228 + <td>
1229 + <legend class="screen-reader-text"><span><?php esc_html_e( 'Add PC Switcher', 'multi-device-switcher' ); ?></span></legend>
1230 + <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.', 'multi-device-switcher' ); ?></label>
1231 + </td>
1232 + </tr>
1233 + <tr><th scope="row"><?php esc_html_e( 'Add default CSS', 'multi-device-switcher' ); ?></th>
1234 + <td>
1235 + <legend class="screen-reader-text"><span><?php esc_html_e( 'Add default CSS', 'multi-device-switcher' ); ?></span></legend>
1236 + <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.', 'multi-device-switcher' ); ?></label>
1237 + </td>
1238 + </tr>
612 1239 </table>
613 -<input type="hidden" name="currency_code" value="USD">
614 -<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
615 -<img alt="" border="0" src="https://www.paypalobjects.com/ja_JP/i/scr/pixel.gif" width="1" height="1">
1240 +</fieldset>
1241 +
1242 +<fieldset id="Disable-Switcher" class="options">
1243 +<h3 class="label"><?php esc_html_e( 'Disable Switcher', 'multi-device-switcher' ); ?></h3>
1244 +
1245 +<table class="form-table">
1246 + <tr><th scope="row"><?php esc_html_e( 'Path', 'multi-device-switcher' ); ?></th>
1247 + <td>
1248 + <legend class="screen-reader-text"><span><?php esc_html_e( 'Path', 'multi-device-switcher' ); ?></span></legend>
1249 + <?php echo esc_html( home_url() ); ?><br>
1250 + <textarea name="multi_device_switcher_options[disable_path]" rows="16" cols="42" wrap="off"><?php echo esc_textarea( $options['disable_path'] ); ?></textarea>
1251 + </td>
1252 + </tr>
1253 + <tr><th scope="row"><?php esc_html_e( 'Regex mode', 'multi-device-switcher' ); ?></th>
1254 + <td>
1255 + <legend class="screen-reader-text"><span><?php esc_html_e( 'Regex mode', 'multi-device-switcher' ); ?></span></legend>
1256 + <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', 'multi-device-switcher' ); ?></label>
1257 + </td>
1258 + </tr>
1259 +</table>
1260 +</fieldset>
1261 +
1262 +</div>
1263 + <?php submit_button(); ?>
616 1264 </form>
617 - </div>
1265 +</div>
618 1266
619 - <?php
620 -}
1267 + <?php
1268 + }
621 1269
622 -/**
623 - * Sanitize and validate form input. Accepts an array, return a sanitized array.
624 - *
625 - * @see multi_device_switcher_init()
626 - *
627 - * @since 1.0
628 - */
629 -function multi_device_switcher_validate( $input ) {
630 - $output = $default_options = multi_device_switcher_get_default_options();
1270 + /**
1271 + * Validate options.
1272 + *
1273 + * @access public
1274 + *
1275 + * @param array $input
1276 + *
1277 + * @return array
1278 + */
1279 + public function validate_options( $input ) {
1280 + $output = $this->default_options;
631 1281
632 - if ( isset( $input['theme_smartphone'] ) )
633 - $output['theme_smartphone'] = $input['theme_smartphone'];
634 - if ( isset( $input['theme_tablet'] ) )
635 - $output['theme_tablet'] = $input['theme_tablet'];
636 - if ( isset( $input['theme_mobile'] ) )
637 - $output['theme_mobile'] = $input['theme_mobile'];
638 - if ( isset( $input['theme_game'] ) )
639 - $output['theme_game'] = $input['theme_game'];
1282 + if ( isset( $input['theme_smartphone'] ) ) {
1283 + $output['theme_smartphone'] = $input['theme_smartphone'];
1284 + }
1285 + if ( isset( $input['theme_tablet'] ) ) {
1286 + $output['theme_tablet'] = $input['theme_tablet'];
1287 + }
1288 + if ( isset( $input['theme_mobile'] ) ) {
1289 + $output['theme_mobile'] = $input['theme_mobile'];
1290 + }
1291 + if ( isset( $input['theme_game'] ) ) {
1292 + $output['theme_game'] = $input['theme_game'];
1293 + }
640 1294
641 - if (isset( $input['restore_UserAgent'] )) {
642 - $output['userAgent_smart'] = $default_options['userAgent_smart'];
643 - $output['userAgent_tablet'] = $default_options['userAgent_tablet'];
644 - $output['userAgent_mobile'] = $default_options['userAgent_mobile'];
645 - $output['userAgent_game'] = $default_options['userAgent_game'];
1295 + if ( isset( $input['restore_UserAgent'] ) ) {
1296 + $output['userAgent_smart'] = $this->default_options['userAgent_smart'];
1297 + $output['userAgent_tablet'] = $this->default_options['userAgent_tablet'];
1298 + $output['userAgent_mobile'] = $this->default_options['userAgent_mobile'];
1299 + $output['userAgent_game'] = $this->default_options['userAgent_game'];
1300 + }
1301 + else {
1302 + if ( isset( $input['userAgent_smart'] ) ) {
1303 + $output['userAgent_smart'] = $input['userAgent_smart'];
1304 + }
1305 + if ( isset( $input['userAgent_tablet'] ) ) {
1306 + $output['userAgent_tablet'] = $input['userAgent_tablet'];
1307 + }
1308 + if ( isset( $input['userAgent_mobile'] ) ) {
1309 + $output['userAgent_mobile'] = $input['userAgent_mobile'];
1310 + }
1311 + if ( isset( $input['userAgent_game'] ) ) {
1312 + $output['userAgent_game'] = $input['userAgent_game'];
1313 + }
1314 + }
1315 +
1316 + foreach ( $input as $key => $val ) {
1317 + if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
1318 + continue;
1319 + }
1320 +
1321 + $custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
1322 +
1323 + if ( isset( $input[ 'custom_switcher_theme_' . $custom_switcher_name ] ) ) {
1324 + $output[ 'custom_switcher_theme_' . $custom_switcher_name ] = $input[ 'custom_switcher_theme_' . $custom_switcher_name ];
1325 + }
1326 + if ( isset( $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ] ) ) {
1327 + $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] = $input[ 'custom_switcher_userAgent_' . $custom_switcher_name ];
1328 + }
1329 + }
1330 +
1331 + foreach ( $input as $key => $val ) {
1332 + if ( ! preg_match( '/^delete_custom_switcher_/', $key ) ) {
1333 + continue;
1334 + }
1335 +
1336 + $custom_switcher_name = preg_replace( '/^delete_custom_switcher_/', '', $key );
1337 +
1338 + unset( $output[ 'custom_switcher_theme_' . $custom_switcher_name ] );
1339 + unset( $output[ 'custom_switcher_userAgent_' . $custom_switcher_name ] );
1340 + }
1341 +
1342 + if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! isset( $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] ) ) {
1343 + if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true )
1344 + && preg_match( '/^[A-Za-z0-9]{1,20}$/', $input['custom_switcher'] ) ) {
1345 + $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] = 'None';
1346 + $output[ 'custom_switcher_userAgent_' . $input['custom_switcher'] ] = '';
1347 + }
1348 + }
1349 +
1350 + $output['pc_switcher'] = isset( $input['pc_switcher'] ) ? $input['pc_switcher'] : 0;
1351 + $output['default_css'] = isset( $input['default_css'] ) ? $input['default_css'] : 0;
1352 +
1353 + $output['disable_path'] = isset( $input['disable_path'] ) ? $input['disable_path'] : '';
1354 + $output['enable_regex'] = isset( $input['enable_regex'] ) ? $input['enable_regex'] : 0;
1355 +
1356 + /**
1357 + * Filter hook: multi_device_switcher/validate_options.
1358 + *
1359 + * @param array $options The options.
1360 + * @param array $input The options.
1361 + * @param array $default The options.
1362 + *
1363 + * @since 1.7.0
1364 + */
1365 + return apply_filters( 'multi_device_switcher/validate_options', $output, $input, $this->default_options );
646 1366 }
647 - else {
648 - if ( isset( $input['userAgent_smart'] ) )
649 - $output['userAgent_smart'] = $input['userAgent_smart'];
650 - if ( isset( $input['userAgent_tablet'] ) )
651 - $output['userAgent_tablet'] = $input['userAgent_tablet'];
652 - if ( isset( $input['userAgent_mobile'] ) )
653 - $output['userAgent_mobile'] = $input['userAgent_mobile'];
654 - if ( isset( $input['userAgent_game'] ) )
655 - $output['userAgent_game'] = $input['userAgent_game'];
1367 +
1368 + /**
1369 + * Register customize options to Customizer.
1370 + *
1371 + * @access public
1372 + *
1373 + * @param object $wp_customize
1374 + *
1375 + * @return void
1376 + *
1377 + * @since 1.3.1
1378 + */
1379 + public function customizer( $wp_customize ) {
1380 + $options = $this->get_options();
1381 + $default_theme_options = $this->default_options;
1382 + $default_theme = wp_get_theme()->get( 'Name' );
1383 + $themes = wp_get_themes();
1384 +
1385 + $theme_names = array();
1386 + $choices = array();
1387 +
1388 + if ( count( $themes ) ) {
1389 + foreach ( $themes as $t ) {
1390 + $theme_names[] = $t->get( 'Name' );
1391 + }
1392 + natcasesort( $theme_names );
1393 +
1394 + $choices['None'] = __( 'None', 'multi-device-switcher' );
1395 + foreach ( $theme_names as $theme_name ) {
1396 + if ( $default_theme === $theme_name ) {
1397 + continue;
1398 + }
1399 + $choices[ $theme_name ] = $theme_name;
1400 + }
1401 + }
1402 +
1403 + $switcher = array(
1404 + 'theme_smartphone' => __( 'Smart Phone Theme', 'multi-device-switcher' ),
1405 + 'theme_tablet' => __( 'Tablet PC Theme', 'multi-device-switcher' ),
1406 + 'theme_mobile' => __( 'Mobile Phone Theme', 'multi-device-switcher' ),
1407 + 'theme_game' => __( 'Game Platforms Theme', 'multi-device-switcher' ),
1408 + );
1409 +
1410 + $wp_customize->add_section(
1411 + 'multi_device_switcher',
1412 + array(
1413 + 'title' => __( 'Multi Device Switcher', 'multi-device-switcher' ),
1414 + 'priority' => 80,
1415 + )
1416 + );
1417 +
1418 + foreach ( $switcher as $name => $label ) {
1419 + $wp_customize->add_setting(
1420 + 'multi_device_switcher_options[' . $name . ']',
1421 + array(
1422 + 'default' => $default_theme_options[ $name ],
1423 + 'type' => 'option',
1424 + 'capability' => $this->capability,
1425 + )
1426 + );
1427 +
1428 + $wp_customize->add_control(
1429 + 'multi_device_switcher_options[' . $name . ']',
1430 + array(
1431 + 'label' => $label,
1432 + 'section' => 'multi_device_switcher',
1433 + 'type' => 'select',
1434 + 'choices' => $choices,
1435 + )
1436 + );
1437 + }
1438 +
1439 + foreach ( (array) $options as $custom_switcher_option => $custom_switcher_theme ) {
1440 + if ( ! preg_match( '/^custom_switcher_theme_/', $custom_switcher_option ) ) {
1441 + continue;
1442 + }
1443 +
1444 + $label = preg_replace( '/^custom_switcher_theme_/', '', $custom_switcher_option );
1445 +
1446 + $wp_customize->add_setting(
1447 + 'multi_device_switcher_options[' . $custom_switcher_option . ']',
1448 + array(
1449 + 'default' => __( 'None', 'multi-device-switcher' ),
1450 + 'type' => 'option',
1451 + 'capability' => $this->capability,
1452 + )
1453 + );
1454 +
1455 + $wp_customize->add_control(
1456 + 'multi_device_switcher_options[' . $custom_switcher_option . ']',
1457 + array(
1458 + 'label' => $label,
1459 + 'section' => 'multi_device_switcher',
1460 + 'type' => 'select',
1461 + 'choices' => $choices,
1462 + )
1463 + );
1464 +
1465 + }
656 1466 }
657 1467
658 - foreach ( $input as $key => $val ) {
659 - if ( ! preg_match( "/^custom_switcher_theme_/", $key ) )
660 - continue;
1468 + /**
1469 + * Load files.
1470 + *
1471 + * @access public
1472 + *
1473 + * @since 1.0.0
1474 + */
1475 + public function load_file() {
1476 + /**
1477 + * Include PC Switcher Widget.
1478 + *
1479 + * @since 1.2
1480 + */
1481 + require_once dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/pc-switcher-widget.php';
661 1482
662 - $custom_switcher_name = preg_replace("/^custom_switcher_theme_/", '', $key);
1483 + /**
1484 + * Include Multi Device Switcher Command
1485 + *
1486 + * @since 1.4
1487 + */
1488 + if ( defined( 'WP_CLI' ) && WP_CLI ) {
1489 + require_once dirname( __MULTI_DEVICE_SWITCHER_FILE__ ) . '/wp-cli.php';
1490 + }
1491 + }
1492 +}
663 1493
664 - if ( isset( $input['custom_switcher_theme_' . $custom_switcher_name] ) )
665 - $output['custom_switcher_theme_' . $custom_switcher_name] = $input['custom_switcher_theme_' . $custom_switcher_name];
666 - if ( isset( $input['custom_switcher_userAgent_' . $custom_switcher_name] ) )
667 - $output['custom_switcher_userAgent_' . $custom_switcher_name] = $input['custom_switcher_userAgent_' . $custom_switcher_name];
1494 +define( '__MULTI_DEVICE_SWITCHER_FILE__', __FILE__ );
1495 +
1496 +if ( class_exists( 'Multi_Device_Switcher' ) ) {
1497 + global $multi_device_switcher;
1498 + $multi_device_switcher = new Multi_Device_Switcher();
1499 +}
1500 +
1501 +/**
1502 + * Add PC Switcher.
1503 + *
1504 + * @since 1.2
1505 + */
1506 +function multi_device_switcher_add_pc_switcher() {
1507 + global $multi_device_switcher;
1508 + if ( is_object( $multi_device_switcher ) ) {
1509 + $multi_device_switcher->add_pc_switcher( 1 );
668 1510 }
1511 +}
669 1512
670 - foreach ( $input as $key => $val ) {
671 - if ( ! preg_match( "/^delete_custom_switcher_/", $key ) )
672 - continue;
1513 +/**
1514 + * Return boolean whether a particular device.
1515 + *
1516 + * @since 1.2.4
1517 + */
1518 +if ( ! function_exists( 'is_multi_device' ) ) :
673 1519
674 - $custom_switcher_name = preg_replace("/^delete_custom_switcher_/", '', $key);
1520 + function is_multi_device( $device = '' ) {
1521 + global $multi_device_switcher;
1522 + if ( is_object( $multi_device_switcher ) ) {
1523 + return $multi_device_switcher->is_multi_device( $device );
1524 + }
1525 + }
1526 +endif;
675 1527
676 - unset($output['custom_switcher_theme_' . $custom_switcher_name]);
677 - unset($output['custom_switcher_userAgent_' . $custom_switcher_name]);
1528 +/**
1529 + * Return the state of PC Switcher.
1530 + *
1531 + * @since 1.4.1
1532 + */
1533 +if ( ! function_exists( 'is_pc_switcher' ) ) :
1534 +
1535 + function is_pc_switcher() {
1536 + global $multi_device_switcher;
1537 + if ( is_object( $multi_device_switcher ) ) {
1538 + return $multi_device_switcher->is_pc_switcher();
1539 + }
678 1540 }
1541 +endif;
679 1542
680 - if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! $output['custom_switcher_theme_' . $input['custom_switcher']] ) {
681 - if ( preg_match( "/^[A-Za-z0-9]{1,20}$/", $input['custom_switcher'] ) ) {
682 - $output['custom_switcher_theme_' . $input['custom_switcher']] = 'None';
683 - $output['custom_switcher_userAgent_' . $input['custom_switcher']] = '';
1543 +/**
1544 + * Return the state of disabled.
1545 + *
1546 + * @since 1.4.1
1547 + */
1548 +if ( ! function_exists( 'is_disable_switcher' ) ) :
1549 +
1550 + function is_disable_switcher() {
1551 + global $multi_device_switcher;
1552 + if ( is_object( $multi_device_switcher ) ) {
1553 + return $multi_device_switcher->is_disable_switcher();
684 1554 }
685 1555 }
1556 +endif;
686 1557
687 - return apply_filters( 'multi_device_switcher_validate', $output, $input, $default_options );
688 -}
1558 +/**
1559 + * Returns the default options.
1560 + *
1561 + * @since 1.5.3
1562 + */
1563 +if ( ! function_exists( 'multi_device_switcher_get_default_options' ) ) :
689 1564
690 -?>
1565 + function multi_device_switcher_get_default_options() {
1566 + global $multi_device_switcher;
1567 + if ( is_object( $multi_device_switcher ) ) {
1568 + return $multi_device_switcher->get_default_options();
1569 + }
1570 + }
1571 +endif;