-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_select.php
270 lines (233 loc) · 7.33 KB
/
device_select.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
<?php
/*
Plugin Name: Device Select
Plugin URI: http://github.com/weldstudio/device-select
Description: Adds the ability to have desktop, touchscreen and mobile friendly versions of the site that the user can easily choose between.
Author: The Weld Studio
Author URI: http://www.theweldstudio.com
Version: 0.1
*/
if (!class_exists('DeviceSelect')) {
class DeviceSelect extends WP_Widget {
protected static $options;
protected $cookie = 'device_select_type';
function __construct() {
parent::__construct('device_select',
__('Device Select', 'device_select'),
array( 'description' => __('Allows the user to switch between '
. 'the mobile, touch-friendly or desktop version of the site',
'device_select')));
$this->options = array(
'title' => array(
'l' => __('Widget Title', 'device_select'),
'e' => __('Leave blank for no title', 'device_select'),
'd' => ''
),
'desktop_label' =>array(
'l' => __('Desktop-friendly Link Label', 'device_select'),
'e' => __('Leave blank to hide link', 'device_select'),
'd' => __('Desktop', 'device_select')
),
'desktop_class' =>array(
'l' => __('Desktop-friendly Class', 'device_select'),
'd' => 'desktop',
),
'touch_label' =>array(
'l' => __('Touch-friendly Link Label', 'device_select'),
'e' => __('Leave blank to hide link', 'device_select'),
'd' => __('Touch', 'device_select')
),
'touch_class' =>array(
'l' => __('Touch-friendly Class', 'device_select'),
'd' => 'touch',
),
'mobile_label' =>array(
'l' => __('Mobile-friendly Link Label', 'device_select'),
'e' => __('Leave blank to hide link', 'device_select'),
'd' => __('Mobile', 'device_select')
),
'mobile_class' =>array(
'l' => __('Mobile-friendly Class', 'device_select'),
'd' => 'mobile',
),
'separator' => array(
'l' => __('Separator', 'device_select'),
'e' => __('This will be placed between each link', 'device_select'),
'd' => ' | ',
)
);
add_filter('wp_head', array(&$this, 'print_javascript'));
add_filter('body_class', array(&$this, 'body_classes'));
add_filter('send_headers', array(&$this, 'send_cookie'));
}
/**
* Print the widget
*
*/
function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if (!empty($title))
echo $args['before_title'] . $title . $args['after_title'];
$links = array();
foreach(array('desktop', 'touch', 'mobile') as $l) {
if (!empty($instance[$l . '_label'])) {
$links[] = '<a class="' . $instance[$l . '_class'] . '" onclick="device_select.change(\'' . $instance[$l . '_class'] . '\')">' . __($instance[$l . '_label'], 'device_select') . '</a>';
}
}
echo join ($instance['separator'], $links);
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
foreach ($this->options as $o => &$opt) {
if (!isset($instance[$o]) && $opt['d']) {
$instance[$o] = $opt['d'];
}
?>
<p>
<label for="<?php echo $this->get_field_id($o); ?>"><?php echo $opt['l']; ?></label>
<?php echo (isset($opt['e']) ? '<span>' . $opt['e'] . '</span>' : ''); ?>
<input class="widefat" id="<?php echo $this->get_field_id($o); ?>" name="<?php echo $this->get_field_name($o ); ?>" type="text" value="<?php echo esc_attr($instance[$o]); ?>">
</p>
<?php
}
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance) {
$instance = array();
foreach ($this->options as $o => $opt) {
#$instance[$o] = (!empty($new_instance[$o]) ? strip_tags($new_instance[$o]) : '');
if (!empty($new_instance[$o])) {
$instance[$o] = strip_tags($new_instance[$o]);
} else {
$instance[$o] = '';
}
}
return $instance;
}
/**
*/
function body_classes($classes) {
if (is_active_widget(false, false, $this->id_base, true)) {
$class = null;
/* Check if forcing they way the page is displayed */
if (isset($_COOKIE[$this->cookie])) {
$class = $_COOKIE[$this->cookie];
}
if (is_null($class)) {
/* Set the settings cookie so we don't have to continuously check type of
* browser.
*/
$class = $this->get_device_class();
}
if (!is_null($class)) {
$classes[] = $class;
}
}
return $classes;
}
protected function &_getSettings() {
if (!isset($this->_settings)) {
$this->_settings = $this->get_settings();
$this->_settings = $this->_settings[$this->number];
}
return $this->_settings;
}
function print_javascript() {
print '<script>' . $this->generate_javascript()
. '</script>';
}
protected function generate_javascript($settings = null) {
if (is_null($settings)) {
$settings = $this->_getSettings();
}
$classes = array();
if (!empty($settings['touch_label'])) {
$classes[] = $settings['touch_class'];
}
if (!empty($settings['mobile_label'])) {
$classes[] = $settings['mobile_class'];
}
if (!empty($settings['desktop_class'])) {
$classes[] = $settings['desktop_class'];
}
return "
device_select = {
classes: ['" . join("', '", $classes) . "'],
change: function(style) {
if (jQuery.inArray(style, this.classes) != -1) {
for (s in this.classes) {
if(jQuery('body').hasClass(this.classes[s]))
jQuery('body').removeClass(this.classes[s]);
}
jQuery('body').addClass(style);
document.cookie = '" . $this->cookie . "=' + style;
}
}
};
";
}
function send_cookie() {
if (is_active_widget(false, false, $this->id_base, true)) {
if (isset($_COOKIE[$this->cookie])) {
$settings = $this->_getSettings();
if (in_array($_COOKIE[$this->cookie], array($settings['desktop_class'], $settings['touch_class'], $settings['mobile_class']))) {
return;
}
}
/* Set the settings cookie so we don't have to continuously check type of
* browser.
*/
$class = $this->get_device_class();
setcookie($this->cookie, $class);
$_COOKIE[$this->cookie] = $class;
}
}
/**
* @todo Add ability to detect request desktop version
*/
function get_device_class() {
$settings = $this->_getSettings();
require_once('lib/Mobile-Detect/Mobile_Detect.php');
if (class_exists(Mobile_Detect)) {
$detect = new Mobile_Detect;
/* Determine what type of browser we have */
if ($detect->isTablet()) {
if (!empty($settings['touch_label'])) {
return $settings['touch_class'];
}
}
if ($detect->isMobile()) {
if (!empty($settings['mobile_label'])) {
return $settings['mobile_class'];
}
}
if (!empty($settings['desktop_class'])) {
return $settings['desktop_class'];
}
}
return null;
}
}
function device_select_register_widget() {
register_widget('DeviceSelect');
}
add_action('widgets_init', 'device_select_register_widget');
}
?>