PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.20.0
Independent Analytics – WordPress Analytics Plugin v1.20.0
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 / Queries / View.php
independent-analytics / IAWP / Queries Last commit date
Campaigns.php 3 years ago City_Statistics.php 3 years ago Country_Statistics.php 3 years ago Current_Traffic_Finder.php 3 years ago Interval.php 3 years ago Minute_Interval.php 3 years ago Range_Query.php 3 years ago Referrers.php 3 years ago Reset_Database.php 3 years ago Resources.php 3 years ago Ten_Second_Interval.php 3 years ago View.php 3 years ago Views.php 3 years ago Visitors_Over_Time_Finder.php 3 years ago
View.php
209 lines
1 <?php
2
3 namespace IAWP_SCOPED\IAWP\Queries;
4
5 use IAWP_SCOPED\IAWP\Models\Page_Author_Archive;
6 use IAWP_SCOPED\IAWP\Models\Page_Date_Archive;
7 use IAWP_SCOPED\IAWP\Models\Page_Home;
8 use IAWP_SCOPED\IAWP\Models\Page_Not_Found;
9 use IAWP_SCOPED\IAWP\Models\Page_Post_Type_Archive;
10 use IAWP_SCOPED\IAWP\Models\Page_Search;
11 use IAWP_SCOPED\IAWP\Models\Page_Singular;
12 use IAWP_SCOPED\IAWP\Models\Page_Term_Archive;
13 use IAWP_SCOPED\IAWP\Models\Visitor;
14 use IAWP_SCOPED\IAWP\Query;
15 use IAWP_SCOPED\IAWP\Utils\String_Util;
16 use IAWP_SCOPED\IAWP\Utils\URL;
17 class View
18 {
19 private $payload;
20 private $referrer_url;
21 private $visitor;
22 private $campaign_fields;
23 private $viewed_at;
24 private $resource;
25 private $session;
26 /**
27 * @param array $payload
28 * @param string|null $referrer_url
29 * @param Visitor $visitor
30 * @param array $campaign_fields
31 * @param null $viewed_at
32 */
33 public function __construct(array $payload, ?string $referrer_url, Visitor $visitor, array $campaign_fields, $viewed_at = null)
34 {
35 $this->payload = $payload;
36 $this->referrer_url = \trim($referrer_url);
37 $this->visitor = $visitor;
38 $this->campaign_fields = $campaign_fields;
39 $this->viewed_at = $viewed_at;
40 $this->resource = $this->get_resource();
41 $this->session = $this->get_session();
42 $view_id = $this->create_view();
43 $this->set_sessions_initial_view($view_id);
44 }
45 private function set_sessions_initial_view(int $view_id)
46 {
47 global $wpdb;
48 $sessions_table = Query::get_table_name(Query::SESSIONS);
49 $wpdb->query($wpdb->prepare("UPDATE {$sessions_table} SET initial_view_id = %d WHERE session_id = %d AND initial_view_id IS NULL", $view_id, $this->session));
50 }
51 private function create_view()
52 {
53 $date = \is_null($this->viewed_at) ? new \DateTime() : $this->viewed_at;
54 $viewed_at = $date->format('Y-m-d H:i:s');
55 return Query::query('create_view', ['resource_id' => $this->resource, 'viewed_at' => $viewed_at, 'page' => $this->payload['page'], 'session_id' => $this->session])->last_inserted_id();
56 }
57 private function get_resource() : ?int
58 {
59 global $wpdb;
60 $resources_table = Query::get_table_name(Query::RESOURCES);
61 $query = '';
62 $payload = \array_merge($this->payload);
63 unset($payload['page']);
64 switch ($payload['resource']) {
65 case 'singular':
66 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND singular_id = %d", $payload['resource'], $payload['singular_id']);
67 break;
68 case 'author_archive':
69 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND author_id = %d", $payload['resource'], $payload['author_id']);
70 break;
71 case 'date_archive':
72 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND date_archive = %s", $payload['resource'], $payload['date_archive']);
73 break;
74 case 'post_type_archive':
75 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND post_type = %s", $payload['resource'], $payload['post_type']);
76 break;
77 case 'term_archive':
78 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND term_id = %s", $payload['resource'], $payload['term_id']);
79 break;
80 case 'search':
81 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND search_query = %s", $payload['resource'], $payload['search_query']);
82 break;
83 case 'home':
84 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s ", $payload['resource']);
85 break;
86 case '404':
87 $query = $wpdb->prepare("SELECT * FROM {$resources_table} WHERE resource = %s AND not_found_url = %s", $payload['resource'], $payload['not_found_url']);
88 break;
89 }
90 $resource = $wpdb->get_row($query);
91 if (\is_null($resource)) {
92 $wpdb->insert($resources_table, $payload);
93 $resource = $wpdb->get_row($query);
94 }
95 // Todo - This probably shouldn't happen here... A call should be make to the page and then the page should
96 // know if it's a page type that needs to be cached or not.
97 switch ($resource->resource) {
98 case 'singular':
99 (new Page_Singular($resource))->update_cache();
100 break;
101 case 'author_archive':
102 (new Page_Author_Archive($resource))->update_cache();
103 break;
104 case 'post_type_archive':
105 (new Page_Post_Type_Archive($resource))->update_cache();
106 break;
107 case 'term_archive':
108 (new Page_Term_Archive($resource))->update_cache();
109 break;
110 case 'home':
111 (new Page_Home($resource))->update_cache();
112 break;
113 case 'date_archive':
114 (new Page_Date_Archive($resource))->update_cache();
115 break;
116 case '404':
117 (new Page_Not_Found($resource))->update_cache();
118 break;
119 case 'search':
120 (new Page_Search($resource))->update_cache();
121 break;
122 }
123 return $resource->id;
124 }
125 private function get_session() : int
126 {
127 $is_internal_referrer = $this->is_internal_referrer($this->referrer_url);
128 $current_session = $this->get_current_session();
129 if (isset($current_session) && $is_internal_referrer) {
130 return $current_session;
131 } else {
132 return $this->create_session();
133 }
134 }
135 /**
136 * @param string|null $referrer_url
137 * @return bool
138 */
139 private function is_internal_referrer(?string $referrer_url) : bool
140 {
141 return !empty($referrer_url) && String_Util::str_starts_with(\strtolower($referrer_url), \strtolower(\site_url()));
142 }
143 /**
144 * Gets the current session not including any that might get created by the current view
145 *
146 * @return int|null
147 */
148 private function get_current_session() : ?int
149 {
150 $session = Query::query('get_current_session', ['visitor_id' => $this->visitor->id()])->row();
151 if (isset($session)) {
152 return $session->session_id;
153 } else {
154 return null;
155 }
156 }
157 public function create_session() : int
158 {
159 $date = \is_null($this->viewed_at) ? new \DateTime() : $this->viewed_at;
160 $created_at = $date->format('Y-m-d H:i:s');
161 return Query::query('create_session', ['visitor_id' => $this->visitor->id(), 'referrer_id' => $this->get_referrer(), 'campaign_id' => $this->get_campaign(), 'created_at' => $created_at])->last_inserted_id();
162 }
163 private function get_referrer() : ?int
164 {
165 if (!isset($this->referrer_url) || \strlen($this->referrer_url) === 0) {
166 return null;
167 }
168 $url = new URL($this->referrer_url);
169 if (!$url->is_valid_url()) {
170 return null;
171 }
172 if ($this->is_internal_referrer($this->referrer_url)) {
173 return null;
174 }
175 Query::query('create_referrer', ['domain' => $url->get_domain()]);
176 $referrer = Query::query('get_referrer', ['domain' => $url->get_domain()])->row();
177 if (isset($referrer)) {
178 return $referrer->id;
179 } else {
180 return null;
181 }
182 }
183 private function get_campaign() : ?int
184 {
185 global $wpdb;
186 $required_fields = ['utm_source', 'utm_medium', 'utm_campaign'];
187 $valid = \true;
188 foreach ($required_fields as $field) {
189 if (!isset($this->campaign_fields[$field])) {
190 $valid = \false;
191 }
192 }
193 if (!$valid) {
194 return null;
195 }
196 $campaigns_table = Query::get_table_name(Query::CAMPAIGNS);
197 $campaign = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$campaigns_table} WHERE utm_source = %s AND utm_medium = %s AND utm_campaign = %s AND (utm_term = %s OR (%d = 0 AND utm_term IS NULL)) AND (utm_content = %s OR (%d = 0 AND utm_content IS NULL))", $this->campaign_fields['utm_source'], $this->campaign_fields['utm_medium'], $this->campaign_fields['utm_campaign'], $this->campaign_fields['utm_term'], isset($this->campaign_fields['utm_term']) ? 1 : 0, $this->campaign_fields['utm_content'], isset($this->campaign_fields['utm_content']) ? 1 : 0));
198 if (!\is_null($campaign)) {
199 return $campaign->campaign_id;
200 }
201 $wpdb->insert($campaigns_table, ['utm_source' => $this->campaign_fields['utm_source'], 'utm_medium' => $this->campaign_fields['utm_medium'], 'utm_campaign' => $this->campaign_fields['utm_campaign'], 'utm_term' => $this->campaign_fields['utm_term'], 'utm_content' => $this->campaign_fields['utm_content']]);
202 $campaign = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$campaigns_table} WHERE utm_source = %s AND utm_medium = %s AND utm_campaign = %s AND (utm_term = %s OR (%d = 0 AND utm_term IS NULL)) AND (utm_content = %s OR (%d = 0 AND utm_content IS NULL))", $this->campaign_fields['utm_source'], $this->campaign_fields['utm_medium'], $this->campaign_fields['utm_campaign'], $this->campaign_fields['utm_term'], isset($this->campaign_fields['utm_term']) ? 1 : 0, $this->campaign_fields['utm_content'], isset($this->campaign_fields['utm_content']) ? 1 : 0));
203 if (!\is_null($campaign)) {
204 return $campaign->campaign_id;
205 }
206 return null;
207 }
208 }
209