PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.19.1
Independent Analytics – WordPress Analytics Plugin v1.19.1
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 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 / Models / Visitor.php
independent-analytics / IAWP / Models Last commit date
Campaign.php 3 years ago Current_Traffic.php 3 years ago Geo.php 3 years ago Page.php 3 years ago Page_Author_Archive.php 3 years ago Page_Date_Archive.php 3 years ago Page_Home.php 3 years ago Page_Not_Found.php 3 years ago Page_Post_Type_Archive.php 3 years ago Page_Search.php 3 years ago Page_Singular.php 3 years ago Page_Term_Archive.php 3 years ago Referrer.php 3 years ago View_Stats.php 3 years ago Visitor.php 3 years ago WooCommerce_Stats.php 3 years ago
Visitor.php
148 lines
1 <?php
2
3 namespace IAWP\Models;
4
5 use IAWP\Geo_Database;
6 use IAWP\Query;
7 use IAWP\Utils\Salt;
8
9 /**
10 * How to use:
11 *
12 * Example IP from the Netherlands
13 * $visitor = new Visitor('92.111.145.208', 'some ua string');
14 *
15 * Example IP from the United States
16 * $visitor = new Visitor('98.111.145.208', 'some ua string');
17 *
18 * Access visitor token
19 * $visitor->id();
20 *
21 * Access geo data
22 * $visitor->country_code()
23 * $visitor->city()
24 * $visitor->subdivision()
25 * $visitor->country()
26 * $visitor->continent()
27 */
28 class Visitor
29 {
30 private $id;
31 private $geo;
32
33 /**
34 * New instances should be created with a string ip address
35 *
36 * @param string $ip
37 * @param string $user_agent
38 */
39 public function __construct(string $ip, string $user_agent)
40 {
41 $geo_database = new Geo_Database();
42 $this->id = self::calculate_id($ip, $user_agent);
43 $this->geo = $geo_database->ip_to_geo($ip);
44 $this->get_or_upsert_visitor();
45 }
46
47 private function get_or_upsert_visitor()
48 {
49 Query::query('create_visitor', [
50 'visitor_id' => $this->id(),
51 'country_code' => $this->country_code(),
52 'city' => $this->city(),
53 'subdivision' => $this->subdivision(),
54 'country' => $this->country(),
55 'continent' => $this->continent(),
56 ]);
57 }
58
59 /**
60 * @param string $ip
61 * @param string $user_agent
62 * @return string
63 */
64 private function calculate_id(string $ip, string $user_agent): string
65 {
66 $salt = Salt::visitor_token_salt();
67 $result = $salt . $ip . $user_agent;
68
69 return md5($result);
70 }
71
72 /**
73 * Return the database id for a visitor
74 *
75 * @return string
76 */
77 public function id(): string
78 {
79 return $this->id;
80 }
81
82 /**
83 * Return an ISO country code
84 *
85 * @return string|null
86 */
87 public function country_code(): ?string
88 {
89 if (isset($this->geo['country']['iso_code'])) {
90 return $this->geo['country']['iso_code'];
91 } else {
92 return null;
93 }
94 }
95
96 /**
97 * Return an English city name
98 *
99 * @return string|null
100 */
101 public function city(): ?string
102 {
103 if (isset($this->geo['city']['names']['en'])) {
104 return $this->geo['city']['names']['en'];
105 } else {
106 return null;
107 }
108 }
109
110 /**
111 * Return an English subdivision name
112 *
113 * @return string|null
114 */
115 public function subdivision(): ?string
116 {
117 if (isset($this->geo['subdivisions'][0]['names']['en'])) {
118 return $this->geo['subdivisions'][0]['names']['en'];
119 } else {
120 return null;
121 }
122 }
123
124 /**
125 * @return string|null
126 */
127 public function country(): ?string
128 {
129 if (isset($this->geo['country']['names']['en'])) {
130 return $this->geo['country']['names']['en'];
131 } else {
132 return null;
133 }
134 }
135
136 /**
137 * @return string|null
138 */
139 public function continent(): ?string
140 {
141 if (isset($this->geo['continent']['names']['en'])) {
142 return $this->geo['continent']['names']['en'];
143 } else {
144 return null;
145 }
146 }
147 }
148