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 |