PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.2
Independent Analytics – WordPress Analytics Plugin v2.14.2
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Utils / Device.php
independent-analytics / IAWP / Utils Last commit date
BladeOne.php 2 years ago CSV.php 9 months ago Calculations.php 2 years ago Currency.php 8 months ago Device.php 1 year ago Device_Cache.php 2 years ago Dir.php 5 months ago Format.php 5 months ago Link_Validator.php 6 months ago Number_Formatter.php 5 months ago Obj.php 6 months ago Request.php 11 months ago Salt.php 1 year ago Security.php 1 year ago Server.php 2 years ago Singleton.php 2 years ago String_Util.php 2 years ago Timezone.php 5 months ago URL.php 6 months ago WP_Async_Request.php 1 year ago
Device.php
119 lines
1 <?php
2
3 namespace IAWP\Utils;
4
5 use IAWPSCOPED\DeviceDetector\DeviceDetector;
6 use IAWPSCOPED\DeviceDetector\Parser\Client\Browser;
7 use IAWP\Illuminate_Builder;
8 use IAWP\Query;
9 use Throwable;
10 /** @internal */
11 class Device
12 {
13 use \IAWP\Utils\Singleton;
14 private $detector;
15 private $type;
16 private $os;
17 private $browser;
18 private function __construct()
19 {
20 $this->detector = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
21 $cache = new \IAWP\Utils\Device_Cache();
22 $cache->load_from_file();
23 $this->detector->setCache($cache);
24 try {
25 @$this->detector->parse();
26 $this->type = $this->detect_type($this->detector);
27 $this->os = $this->detect_os($this->detector);
28 $this->browser = $this->detect_browser($this->detector);
29 } catch (Throwable $e) {
30 $this->type = null;
31 $this->os = null;
32 $this->browser = null;
33 }
34 $cache->save_to_file();
35 }
36 public function type_id() : ?int
37 {
38 if (!\is_string($this->type) || $this->type === '') {
39 return null;
40 }
41 $device_types_table = Query::get_table_name(Query::DEVICE_TYPES);
42 $id = Illuminate_Builder::new()->select('device_type_id')->from($device_types_table)->where('device_type', '=', $this->type)->value('device_type_id');
43 if (\is_null($id)) {
44 $id = Illuminate_Builder::new()->from($device_types_table)->insertGetId(['device_type' => $this->type], 'device_type_id');
45 }
46 return $id;
47 }
48 public function os_id() : ?int
49 {
50 if (!\is_string($this->os) || $this->os === '') {
51 return null;
52 }
53 $device_oss_table = Query::get_table_name(Query::DEVICE_OSS);
54 $id = Illuminate_Builder::new()->select('device_os_id')->from($device_oss_table)->where('device_os', '=', $this->os)->value('device_os_id');
55 if (\is_null($id)) {
56 $id = Illuminate_Builder::new()->from($device_oss_table)->insertGetId(['device_os' => $this->os], 'device_os_id');
57 }
58 return $id;
59 }
60 public function browser_id() : ?int
61 {
62 if (!\is_string($this->browser) || $this->browser === '') {
63 return null;
64 }
65 $device_browsers_table = Query::get_table_name(Query::DEVICE_BROWSERS);
66 $id = Illuminate_Builder::new()->select('device_browser_id')->from($device_browsers_table)->where('device_browser', '=', $this->browser)->value('device_browser_id');
67 if (\is_null($id)) {
68 $id = Illuminate_Builder::new()->from($device_browsers_table)->insertGetId(['device_browser' => $this->browser], 'device_browser_id');
69 }
70 return $id;
71 }
72 public function is_bot() : bool
73 {
74 return $this->detector->isBot();
75 }
76 private function detect_os(DeviceDetector $detector) : ?string
77 {
78 $os = $detector->getOs('name');
79 if ($os === "UNK") {
80 return null;
81 }
82 return $os;
83 }
84 private function detect_type(DeviceDetector $detector) : ?string
85 {
86 $detected_type = $detector->getDeviceName();
87 $type_mapping = ['Mobile' => ['smartphone', 'phablet'], 'Tablet' => ['tablet'], 'Desktop' => ['desktop'], 'Car' => ['car browser'], 'Console' => ['console'], 'TV' => ['tv'], 'Wearable' => ['wearable']];
88 foreach ($type_mapping as $type => $types) {
89 if (\in_array($detected_type, $types)) {
90 return $type;
91 }
92 }
93 return null;
94 }
95 private function detect_browser(DeviceDetector $detector) : ?string
96 {
97 $name = $detector->getClient('name');
98 if ($name === 'UNK' || \is_array($name)) {
99 return null;
100 }
101 if ($this->should_use_browser_name($name)) {
102 return $name;
103 }
104 return \ucwords(Browser::getBrowserFamily($name) ?? $name);
105 }
106 /**
107 * Should the browsers name be used instead of the browsers family name?
108 *
109 * @param string $name
110 *
111 * @return bool
112 */
113 private function should_use_browser_name(string $name) : bool
114 {
115 $exceptions = ['Microsoft Edge'];
116 return \in_array($name, $exceptions);
117 }
118 }
119