-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
multi-device-switcher.php
1570 lines (1389 loc) · 42.2 KB
/
multi-device-switcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Multi Device Switcher
* Plugin URI: https://github.com/thingsym/multi-device-switcher
* Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).
* Version: 1.8.5
* Author: thingsym
* Author URI: https://www.thingslabo.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: multi-device-switcher
* Domain Path: /languages/
*
* @package Multi_Device_Switcher
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Core class Multi_Device_Switcher
*
* @since 1.0.0
*/
class Multi_Device_Switcher {
/**
* Public variable.
*
* @access public
*
* @var string $option_group The group name of option
*/
public $option_group = 'multi_device_switcher';
/**
* Public variable.
*
* @access public
*
* @var string $option_name The option name
*/
public $option_name = 'multi_device_switcher_options';
/**
* Public variable.
*
* @access public
*
* @var string $capability The types of capability
*/
public $capability = 'switch_themes';
/**
* Public variable.
*
* @access public
*
* @var string $cookie_name_multi_device_switcher
*/
public $cookie_name_multi_device_switcher = 'multi-device-switcher';
/**
* Public variable.
*
* @access public
*
* @var string $cookie_name_disable_switcher
*/
public $cookie_name_disable_switcher = 'disable-switcher';
/**
* Public variable.
*
* @access public
*
* @var string $cookie_name_pc_switcher
*/
public $cookie_name_pc_switcher = 'pc-switcher';
/**
* Public variable.
*
* @access public
*
* @var array $default_options {
* default options
*
* @type bool pc_switcher
* @type bool default_css
* @type string theme_smartphone
* @type string theme_tablet
* @type string theme_mobile
* @type string theme_game
* @type string userAgent_smart
* @type string userAgent_tablet
* @type string userAgent_mobile
* @type string userAgent_game
* @type string disable_path
* @type bool enable_regex
* }
*
* @since 1.7.0
*/
public $default_options = array(
'pc_switcher' => 1,
'default_css' => 1,
'theme_smartphone' => 'None',
'theme_tablet' => 'None',
'theme_mobile' => 'None',
'theme_game' => 'None',
'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko',
'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko',
'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia',
'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox',
'disable_path' => '',
'enable_regex' => 0,
);
/**
* Public variable.
*
* @access public
*
* @var string $device
*/
public $device = '';
/**
* Public variable.
*
* @access public
*
* @var array|null $plugin_data
*/
public $plugin_data = array();
/**
* Constructor
*
* @access public
*
* @since 1.0.0
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'load_plugin_data' ) );
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
add_action( 'plugins_loaded', array( $this, 'init' ) );
if ( ! is_admin() ) {
add_filter( 'wp_headers', array( $this, 'add_header_vary' ) );
add_action( 'plugins_loaded', array( $this, 'detect_device' ) );
add_action( 'plugins_loaded', array( $this, 'switch_theme' ) );
}
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_menu', array( $this, 'add_option_page' ) );
add_action( 'customize_register', array( $this, 'customizer' ) );
add_action( 'plugins_loaded', array( $this, 'load_file' ) );
}
/**
* Initialize.
*
* Hooks to plugins_loaded
*
* @access public
*
* @since 1.6.0
*/
public function init() {
add_filter( 'option_page_capability_' . $this->option_group, array( $this, 'option_page_capability' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ), array( $this, 'plugin_action_links' ) );
add_filter( 'plugin_row_meta', array( $this, 'plugin_metadata_links' ), 10, 2 );
add_shortcode( 'multi', array( $this, 'shortcode_display_switcher' ) );
}
/**
* Detect device.
*
* @access public
*
* @return void
*
* @since 1.7.0
*/
public function detect_device() {
if ( isset( $_COOKIE[ $this->cookie_name_disable_switcher ] ) ) {
add_action( 'wp_headers', array( $this, 'set_cookie_rest_disable_switcher' ) );
}
if ( $this->is_disable_switcher() ) {
add_action( 'wp_headers', array( $this, 'set_cookie_enable_disable_switcher' ) );
return;
}
add_action( 'init', array( $this, 'session' ) );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$server_ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
$user_agent = $this->get_options_user_agent();
foreach ( array_reverse( $user_agent ) as $key => $val ) {
if ( ! preg_match( '/^custom_switcher_/', $key ) ) {
continue;
}
if ( $user_agent[ $key ] && preg_match( '/' . implode( '|', $user_agent[ $key ] ) . '/i', $server_ua ) ) {
$this->device = $key;
break;
}
}
if ( ! $this->device ) {
if ( $user_agent['game'] && preg_match( '/' . implode( '|', $user_agent['game'] ) . '/i', $server_ua ) ) {
$this->device = 'game';
}
elseif ( $user_agent['tablet'] && preg_match( '/' . implode( '|', $user_agent['tablet'] ) . '/i', $server_ua ) ) {
$this->device = 'tablet';
}
elseif ( $user_agent['smart'] && preg_match( '/' . implode( '|', $user_agent['smart'] ) . '/i', $server_ua ) ) {
$this->device = 'smart';
}
elseif ( $user_agent['mobile'] && preg_match( '/' . implode( '|', $user_agent['mobile'] ) . '/i', $server_ua ) ) {
$this->device = 'mobile';
}
}
/**
* Action hook: multi_device_switcher/detect_device.
*
* @since 1.7.0
*/
do_action( 'multi_device_switcher/detect_device' );
}
/**
* Switch theme.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function switch_theme() {
if ( $this->device ) {
add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
add_filter( 'template', array( $this, 'get_template' ) );
add_action( 'wp_footer', array( $this, 'add_pc_switcher' ) );
add_action( 'wp_headers', array( $this, 'set_cookie_switch_theme' ) );
}
else {
add_action( 'wp_headers', array( $this, 'set_cookie_normal_theme' ) );
}
if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
remove_filter( 'template', array( $this, 'get_template' ) );
}
}
/**
* Gets UserAgents.
*
* @access public
*
* @return array
*
* @since 1.0.0
*/
public function get_options_user_agent() {
$options = $this->get_options();
$user_agent['smart'] = empty( $options['userAgent_smart'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_smart'], -1, PREG_SPLIT_NO_EMPTY );
$user_agent['tablet'] = empty( $options['userAgent_tablet'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_tablet'], -1, PREG_SPLIT_NO_EMPTY );
$user_agent['mobile'] = empty( $options['userAgent_mobile'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_mobile'], -1, PREG_SPLIT_NO_EMPTY );
$user_agent['game'] = empty( $options['userAgent_game'] ) ? '' : preg_split( '/,\s*/', $options['userAgent_game'], -1, PREG_SPLIT_NO_EMPTY );
foreach ( (array) $options as $key => $val ) {
if ( ! preg_match( '/^custom_switcher_userAgent_/', $key ) ) {
continue;
}
$custom_switcher_name = preg_replace( '/^custom_switcher_userAgent_/', '', $key );
$user_agent[ 'custom_switcher_' . $custom_switcher_name ] = empty( $val ) ? '' : preg_split( '/,\s*/', $val, -1, PREG_SPLIT_NO_EMPTY );
}
return $user_agent;
}
/**
* Gets stylesheet.
*
* @access public
*
* @param string $stylesheet
*
* @return string
*
* @since 1.0.0
*/
public function get_stylesheet( $stylesheet = '' ) {
$name = $this->get_device_theme();
if ( empty( $name ) ) {
return $stylesheet;
}
$themes = wp_get_themes();
foreach ( $themes as $t ) {
if ( $name === $t->get( 'Name' ) ) {
$theme = $t;
break;
}
}
if ( empty( $theme ) ) {
return $stylesheet;
}
if ( 'publish' !== $theme->get( 'Status' ) ) {
return $stylesheet;
}
return $theme['Stylesheet'];
}
/**
* Gets template.
*
* @access public
*
* @param string $template
*
* @return string
*
* @since 1.0.0
*/
public function get_template( $template = '' ) {
$name = $this->get_device_theme();
if ( empty( $name ) ) {
return $template;
}
$themes = wp_get_themes();
foreach ( $themes as $t ) {
if ( $name === $t->get( 'Name' ) ) {
$theme = $t;
break;
}
}
if ( empty( $theme ) ) {
return $template;
}
if ( 'publish' !== $theme->get( 'Status' ) ) {
return $template;
}
return $theme['Template'];
}
/**
* Gets template by device.
*
* @access public
*
* @return string
*
* @since 1.0.0
*/
public function get_device_theme() {
$options = $this->get_options();
if ( 'smart' === $this->device ) {
return $options['theme_smartphone'];
}
elseif ( 'tablet' === $this->device ) {
return $options['theme_tablet'];
}
elseif ( 'mobile' === $this->device ) {
return $options['theme_mobile'];
}
elseif ( 'game' === $this->device ) {
return $options['theme_game'];
}
else {
foreach ( (array) $options as $key => $val ) {
if ( ! preg_match( '/^custom_switcher_theme_/', $key ) ) {
continue;
}
$custom_switcher_name = preg_replace( '/^custom_switcher_theme_/', '', $key );
if ( 'custom_switcher_' . $custom_switcher_name === $this->device ) {
return $options[ $key ];
}
}
}
return '';
}
/**
* Reset cookie for disable_switcher.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function set_cookie_rest_disable_switcher() {
setcookie( $this->cookie_name_disable_switcher, '', time() - 3600, '/', '', is_ssl(), false );
}
/**
* Set enable to cookie for disable_switcher.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function set_cookie_enable_disable_switcher() {
setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
setcookie( $this->cookie_name_disable_switcher, '1', 0, '/', '', is_ssl(), false );
}
/**
* Set switched theme to cookie.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function set_cookie_switch_theme() {
$device = preg_replace( '/^custom_switcher_/', '', $this->device );
setcookie( $this->cookie_name_multi_device_switcher, (string) $device, 0, '/', '', is_ssl(), false );
}
/**
* Reset cookie for normal theme.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function set_cookie_normal_theme() {
setcookie( $this->cookie_name_multi_device_switcher, '', time() - 3600, '/', '', is_ssl(), false );
}
/**
* Set session to cookie for pc switcher.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function session() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['pc-switcher'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
setcookie( $this->cookie_name_pc_switcher, sanitize_text_field( wp_unslash( $_GET['pc-switcher'] ) ) ? '1' : '', 0, '/', '', is_ssl(), false );
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] );
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
unset( $_GET['pc-switcher'] );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$uri = $uri . '?' . http_build_query( $_GET );
}
wp_safe_redirect( esc_url( $uri ) );
exit;
}
}
/**
* Enqueue styles for default_css
*
* @access public
*
* @return void
*
* @since 1.8.5
*/
public function enqueue_styles() {
wp_enqueue_style(
'pc-switcher-options',
plugins_url() . '/multi-device-switcher/pc-switcher.css',
array(),
$this->plugin_data['Version'],
'all'
);
}
/**
* Add pc switcher button.
*
* @access public
*
* @param bool $pc_switcher
*
* @return void
*
* @since 1.0.0
*/
public function add_pc_switcher( $pc_switcher = 0 ) {
$options = $this->get_options();
$name = $this->get_device_theme();
if ( $options['pc_switcher'] ) {
$pc_switcher = 1;
}
if ( $pc_switcher && $name && 'None' !== $name ) {
if ( $options['default_css'] ) {
$this->enqueue_styles();
}
$uri = is_ssl() ? 'https://' : 'http://';
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$uri .= $_SERVER['HTTP_HOST'];
}
if ( isset( $_COOKIE[ $this->cookie_name_pc_switcher ] ) ) {
$uri .= add_query_arg( 'pc-switcher', 0 );
?>
<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>
<?php
}
else {
$uri .= add_query_arg( 'pc-switcher', 1 );
?>
<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>
<?php
}
}
}
/**
* Check device.
*
* @access public
*
* @param string $device
*
* @return bool
*
* @since 1.0.0
*/
public function is_multi_device( $device = '' ) {
if ( $device === $this->device ) {
return true;
}
if ( 'custom_switcher_' . $device === $this->device ) {
return true;
}
return false;
}
/**
* Whether pc switcher enabled.
*
* @access public
*
* @return bool
*
* @since 1.0.0
*/
public function is_pc_switcher() {
return isset( $_COOKIE[ $this->cookie_name_pc_switcher ] );
}
/**
* Whether theme switch disabled.
*
* @access public
*
* @param bool $disable
*
* @return bool
*
* @since 1.0.0
*/
public function is_disable_switcher( $disable = false ) {
$options = $this->get_options();
$disable_path = preg_split( '/\R/', $options['disable_path'], -1, PREG_SPLIT_NO_EMPTY );
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$request_uri = $_SERVER['REQUEST_URI'];
}
foreach ( (array) $disable_path as $path ) {
if ( $options['enable_regex'] ) {
if ( preg_match( '/' . $path . '/i', $request_uri ) ) {
$disable = true;
break;
}
}
else {
if ( preg_match( '/^' . preg_quote( (string) $path, '/' ) . '$/i', $request_uri ) ) {
$disable = true;
break;
}
}
}
return $disable;
}
/**
* Shortcode.
*
* @access public
*
* @param array $atts
* @param string $content
*
* @return string
*
* @since 1.0.0
*/
public function shortcode_display_switcher( $atts, $content = '' ) {
$atts = shortcode_atts(
array(
'device' => '',
),
$atts
);
if ( empty( $atts['device'] ) && ( $this->is_multi_device( $atts['device'] ) || $this->is_pc_switcher() ) ) {
return $content;
}
elseif ( ! empty( $atts['device'] ) && $this->is_multi_device( $atts['device'] ) && ! $this->is_pc_switcher() ) {
return $content;
}
return '';
}
/**
* Add HTTP Vary header.
*
* @access public
*
* @param string $headers
*
* @return string
*
* @since 1.1.1
*/
public function add_header_vary( $headers ) {
/**
* Filter hook: multi_device_switcher/add_header_vary.
*
* @param string 'User-Agent' header name.
*
* @since 1.6.2
*/
$headers['Vary'] = apply_filters( 'multi_device_switcher/add_header_vary', 'User-Agent' );
return $headers;
}
/**
* Enqueue scripts.
*
* Hooks to admin_enqueue_scripts.
*
* @access public
*
* @since 1.0.0
*/
public function admin_enqueue_scripts( $hook_suffix = '' ) {
wp_enqueue_script(
'multi-device-switcher-options',
plugins_url() . '/multi-device-switcher/multi-device-switcher.js',
array( 'jquery', 'jquery-ui-tabs' ),
$this->plugin_data['Version'],
false
);
}
/**
* Enqueue styles.
*
* Hooks to admin_enqueue_styles.
*
* @access public
*
* @since 1.0.0
*/
public function admin_enqueue_styles( $hook_suffix = '' ) {
wp_enqueue_style(
'multi-device-switcher-options',
plugins_url() . '/multi-device-switcher/multi-device-switcher.css',
array(),
$this->plugin_data['Version'],
'all'
);
}
/**
* Register the form setting.
*
* Hooks to admin_init.
*
* @access public
*
* @return void
*
* @since 1.0
*/
public function register_settings() {
if ( is_null( $this->get_options() ) ) {
add_option( $this->option_name );
}
register_setting(
$this->option_group,
$this->option_name,
array( $this, 'validate_options' )
);
}
/**
* Returns capability.
*
* @access public
*
* @return string
*
* @since 1.0.0
*/
public function option_page_capability() {
return $this->capability;
}
/**
* Adds option page.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function add_option_page() {
$page_hook = add_theme_page(
__( 'Multi Device Switcher', 'multi-device-switcher' ),
__( 'Multi Device Switcher', 'multi-device-switcher' ),
$this->option_page_capability(),
'multi-device-switcher',
array( $this, 'render_option_page' )
);
if ( ! $page_hook ) {
return;
}
add_action( 'load-' . $page_hook, array( $this, 'page_hook_suffix' ) );
}
/**
* Page Hook Suffix.
*
* Hooks to load-{$page_hook}.
*
* @access public
*
* @return void
*
* @since 1.2.4
*/
public function page_hook_suffix() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
}
/**
* Set link to customizer section on the plugins page.
*
* Hooks to plugin_action_links_{$plugin_file}
*
* @see https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
*
* @access public
*
* @param array $links An array of plugin action links.
*
* @return array $links
*
* @since 1.6.0
*/
public function plugin_action_links( $links = array() ) {
$settings_link = '<a href="themes.php?page=multi-device-switcher">' . __( 'Settings', 'multi-device-switcher' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Set links below a plugin on the Plugins page.
*
* Hooks to plugin_row_meta
*
* @see https://developer.wordpress.org/reference/hooks/plugin_row_meta/
*
* @access public
*
* @param array $links An array of the plugin's metadata.
* @param string $file Path to the plugin file relative to the plugins directory.
*
* @return array $links
*
* @since 1.8.1
*/
public function plugin_metadata_links( $links, $file ) {
if ( $file === plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) {
$links[] = '<a href="https://github.com/sponsors/thingsym">' . __( 'Become a sponsor', 'multi-device-switcher' ) . '</a>';
}
return $links;
}
/**
* Returns the default options.
*
* @access public
*
* @return array|null
*
* @since 1.0.0
*/
public function get_default_options() {
return $this->default_options;
}
/**
* Returns the options array or value.
*
* @access public
*
* @param string $option_name Optional. The option name.
*
* @return array|null
*
* @since 1.0.0
*/
public function get_options( $option_name = null ) {
$options = get_option( $this->option_name, $this->default_options );
$options = array_merge( $this->default_options, $options );
if ( is_null( $option_name ) ) {
/**
* Filter hook: multi_device_switcher/get_options.
*
* @param array $options The options.
*
* @since 1.7.0
*/
return apply_filters( 'multi_device_switcher/get_options', $options );
}
if ( array_key_exists( $option_name, $options ) ) {
/**
* Filter hook: multi_device_switcher/get_option.
*
* @param mixed $option The value of option.
* @param string $option_name The option name via argument.
*
* @since 1.7.0
*/
return apply_filters( 'multi_device_switcher/get_option', $options[ $option_name ], $option_name );
}
return null;
}
/**
* Load textdomain
*
* @access public
*
* @return boolean
*
* @since 1.6.0
*/
public function load_textdomain() {
return load_plugin_textdomain(
'multi-device-switcher',
false,
plugin_dir_path( __MULTI_DEVICE_SWITCHER_FILE__ ) . 'languages'
);
}
/**
* Load plugin data
*
* @access public
*
* @return boolean
*
* @since 1.8.1
*/
public function load_plugin_data() {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$this->plugin_data = get_plugin_data( __MULTI_DEVICE_SWITCHER_FILE__ );
if ( ! $this->plugin_data ) {
return false;
}
return true;
}
/**
* Display option page.
*
* @access public
*
* @return void
*
* @since 1.0.0
*/
public function render_option_page() {
?>
<div class="wrap">
<div id="icon-themes" class="icon32"><br></div>
<h2><?php esc_html_e( 'Multi Device Switcher', 'multi-device-switcher' ); ?></h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'multi_device_switcher' );
$options = $this->get_options();
$default_theme = wp_get_theme()->get( 'Name' );
$themes = wp_get_themes();
$theme_names = array();
if ( count( $themes ) ) {
foreach ( $themes as $t ) {
$theme_names[] = $t->get( 'Name' );
}
natcasesort( $theme_names );
}
?>
<div id="admin-tabs">
<fieldset id="Theme" class="options">
<h3 class="label"><?php esc_html_e( 'Theme', 'multi-device-switcher' ); ?></h3>
<table class="form-table">
<tr><th scope="row"><?php esc_html_e( 'Smart Phone Theme', 'multi-device-switcher' ); ?></th>
<td>
<?php
$html = '';
if ( count( $theme_names ) ) {
$html = '<select name="multi_device_switcher_options[theme_smartphone]">';
if ( ( 'None' === $options['theme_smartphone'] ) || ( '' === $options['theme_smartphone'] ) ) {
$html .= '<option value="None" selected="selected">' . __( 'None', 'multi-device-switcher' ) . '</option>';
}
else {
$html .= '<option value="None">' . __( 'None', 'multi-device-switcher' ) . '</option>';
}
foreach ( $theme_names as $theme_name ) {
if ( $default_theme === $theme_name ) {
continue;
}
if ( $options['theme_smartphone'] === $theme_name ) {
$html .= '<option value="' . esc_attr( $theme_name ) . '" selected="selected">' . esc_html( $theme_name ) . '</option>';