PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.8.5
Independent Analytics – WordPress Analytics Plugin v2.8.5
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 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 All 120 releases
independent-analytics / IAWP / Utils / Device.php
Device.php
119 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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::get_builder()->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::get_builder()->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::get_builder()->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::get_builder()->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::get_builder()->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::get_builder()->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