This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
abc-core.php
317 lines (286 loc) · 10.1 KB
/
abc-core.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
<?php
class ABC_Core
{
const UNKNOWN = 'unknown';
const PLATFORM_ANDROID = 'android';
const PLATFORM_IOS = 'iOS';
const PLATFORM_LINUX = 'linux';
const PLATFORM_MAC = 'mac';
const PLATFORM_WINDOWS = 'windows';
static $this;
/**
* ABC_Core constructor.
*/
public function __construct()
{
self::$this;
add_action( 'init', array( $this, 'default_setting_values' ) ); // Default settings
add_action( 'wp_footer', array( $this, 'content_wrapper' ) ); // HTML wrapper
add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) ); // Needed scripts
add_action( 'wp_enqueue_scripts', array( $this, 'styles' ) ); // Needed stylesheets
}
/**
* Get users browser details
*
* @return array
*/
public function get_browser()
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$platform = $this->get_platform( $user_agent );
$browser = $this->get_browser_name( $user_agent );
// finally get the correct version number
$known = array( 'Version', $browser['name'], 'rv' );
if ($browser['name'] === 'Opera') {
$known = array( 'Version', $browser['name'], 'OPR', 'rv' );
}
$pattern = '#(?<browser>' . implode( '|', $known ) . ')[/ |:]+(?<version>[0-9.|a-zA-Z.]*)#';
preg_match_all( $pattern, $user_agent, $matches );
// see how many we have
$i = count( $matches['browser'] );
if ( $i != 1 )
{
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if ( strripos( $user_agent, 'Version' ) < strripos( $user_agent, $browser['name'] ) )
{
$version = $matches['version'][0];
}
else
{
$version = $matches['version'][1];
}
}
else
{
$version = $matches['version'][0];
}
// check if we have a number
if ( $version === null || $version === '' || $version === 0 )
{
$version = self::UNKNOWN;
}
return array(
'user_agent' => $user_agent,
'name' => $browser['full_name'],
'short_name' => $browser['short_name'],
'version' => floor($version),
'platform' => $platform,
'pattern' => $pattern
);
}
/**
* The wrapper, added to the site footer, that the popup will be placed in after the ajax load
*
* @echo html
*/
function content_wrapper()
{
echo "<div class='advanced-browser-check' style='display:none;' data-url='".json_encode(
array( "abc_url" => admin_url( 'admin-ajax.php' ) )
)."'></div>";
}
/**
* Default settings and settings array used trough out the plugin
*
* @return array
*/
public function default_setting_values()
{
// Default settings values
add_option( 'abc_title', __( 'You are using a web browser not supported by this website!', 'advanced-browser-check' ) );
add_option( 'abc_message', __( 'You are using a web browser that is not supported by this website. This means that some functionality may not work as intended. This may result in strange behaviors when browsing around. Use or upgrade/install one of the following browsers to take full advantage of this website. - Thank you!', 'advanced-browser-check' ) );
add_option( 'abc_hide', NULL );
add_option( 'abc_show', array(
'ie' => '',
'edge' => '',
'ff' => 'http://www.mozilla.com/en-US/firefox/all.html',
'safari' => '',
'opera' => '',
'chrome' => 'https://www.google.com/chrome'
) );
add_option( 'abc_check', array(
'ie' => '11',
'ff' => '0',
'safari' => '4',
'opera' => '27',
'chrome' => '0',
'edge' => '0',
) );
add_option( 'abc_debug', 'off' );
// Add new settings option introduced in version 4.4.0 if it does not exists
$check = get_option( 'abc_check' );
if ( ! isset( $check['edge'] ) )
{
$check['edge'] = '';
update_option( 'abc_check', $check );
}
// Update plugin version
$this->update();
return array(
'title' => get_option( 'abc_title' ),
'msg' => get_option( 'abc_message' ),
'hide' => get_option( 'abc_hide' ),
'show_browser' => get_option( 'abc_show' ),
'check_browser' => get_option( 'abc_check' ),
'debug' => get_option( 'abc_debug' )
);
}
/**
* Default browsers settings. This builds the browser drop downs on the admin page
*
* @return array
*/
public function default_browsers()
{
// Included version numbers is current stable (since latest plugin update)
// and 5 future versions as most
// and 8 older versions as most
return array(
'safari' => array(0,3,4,5,6,7,8,9,10,11,12,13),
'opera' => array(0,27,28,29,30,31,32,33,34,35,36,37,38,39,40),
'ff' => array(0,38,39,40,41,42,43,44,45,46,47,48,49,51,52),
'chrome' => array(0,43,44,45,46,47,48,49,50,51,52,53,54,55,56),
'ie' => array(0,7,8,9,10,11),
'edge' => array(0,12,13,14,15,16)
);
}
/**
* Add scripts we need
**/
public function scripts()
{
// Load jQuery
wp_enqueue_script( "jquery" );
// jQuery cookie, used to add a cookie so visitors can hide the popup
wp_enqueue_script( "apc_jquery_cookie", plugins_url( '/js/jquery.cookie.js', __FILE__ ), array( 'jquery' ) );
// The ajax request so the plugin works with caching plugins
wp_enqueue_script( "abc_script", plugins_url( '/js/script.js', __FILE__ ), array( 'jquery' ) );
}
/**
* Add styles we need
**/
public function styles()
{
// Stylesheet for the popup
wp_enqueue_style( "abc_style", plugins_url( '/css/style.css', __FILE__ ) );
}
/**
* Get browser name details
*
* @param $user_agent
*
* @return array
*/
protected function get_browser_name( $user_agent )
{
// Next get the name of the useragent yes seperately and for good reason
if ( preg_match( '/Opera/i',$user_agent ) || preg_match( '/OPR/i',$user_agent ))
{
return array(
'full_name' => 'Opera',
'name' => 'Opera',
'short_name' => 'opera'
);
}
elseif ( preg_match( '/Edge/i',$user_agent ) )
{
return array(
'full_name' => 'Microsoft Edge',
'name' => 'Edge',
'short_name' => 'edge'
);
} elseif ( preg_match( '/Firefox/i',$user_agent ) )
{
return array(
'full_name' => 'Mozilla Firefox',
'name' => 'Firefox',
'short_name' => 'ff'
);
}
elseif ( preg_match( '/Chrome/i',$user_agent ) )
{
return array(
'full_name' => 'Google Chrome',
'name' => 'Chrome',
'short_name' => 'chrome'
);
}
elseif ( preg_match( '/Safari/i',$user_agent ) )
{
return array(
'full_name' => 'Apple Safari',
'name' => 'Safari',
'short_name' => 'safari'
);
}
elseif ( preg_match( '/MSIE/i',$user_agent ) || preg_match( '/Windows NT/i',$user_agent ) )
{
return array(
'full_name' => 'Internet Explorer',
'name' => 'MSIE',
'short_name' => 'ie'
);
}
return array(
'full_name' => self::UNKNOWN,
'name' => self::UNKNOWN,
'short_name' => self::UNKNOWN
);
}
/**
* Get users platform (OS)
*
* @param $user_agent
*
* @return string
*/
protected function get_platform( $user_agent )
{
if ( preg_match( '/android/i', $user_agent ) )
{
return self::PLATFORM_ANDROID;
}
elseif ( preg_match( '/iphone/i', $user_agent ) || preg_match( '/ipad/i', $user_agent ) || preg_match( '/ipod/i', $user_agent ) )
{
return self::PLATFORM_IOS;
}
elseif ( preg_match( '/linux/i', $user_agent ) )
{
return self::PLATFORM_LINUX;
}
elseif ( preg_match( '/macintosh|mac os x/i', $user_agent ) )
{
return self::PLATFORM_MAC;
}
elseif ( preg_match( '/windows|win32/i', $user_agent ) )
{
return self::PLATFORM_WINDOWS;
}
return self::UNKNOWN;
}
/**
* Sets plugin version and there update functions will be added when larger updates is made
**/
private function update()
{
// If no version number exists, we need to update the settings to v2.0.0 and above
if ( ! get_option( 'abc_version' ) )
{
// Get old settings array
$old_settings = get_option( 'advanced-browser-check' );
if ( $old_settings )
{
// Add the old settings to the new once
update_option( 'abc_title', $old_settings['title'] );
update_option( 'abc_message', $old_settings['msg'] );
update_option( 'abc_show', $old_settings['show_browser'] );
update_option( 'abc_check', $old_settings['check_browser'] );
update_option( 'abc_hide', $old_settings['hide'] );
}
// Delete the old settings array from the DB
delete_option( 'advanced-browser-check' );
}
update_option( 'abc_version', ABC_VERSION );
}
}