PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.10.4
Independent Analytics – WordPress Analytics Plugin v2.10.4
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 / REST_API.php
independent-analytics / IAWP Last commit date
AJAX 1 year ago Admin_Page 1 year ago Click_Tracking 1 year ago Custom_WordPress_Columns 2 years ago Data_Pruning 1 year ago Date_Picker 1 year ago Date_Range 1 year ago Ecommerce 1 year ago Email_Reports 1 year ago Filter_Lists 1 year ago Form_Submissions 1 year ago Interval 1 year ago Menu_Bar_Stats 1 year ago Migrations 1 year ago Models 1 year ago Public_API 1 year ago Rows 1 year ago Statistics 1 year ago Tables 1 year ago Utils 1 year ago Campaign_Builder.php 1 year ago Capability_Manager.php 1 year ago Chart.php 1 year ago Chart_Geo.php 1 year ago Click_Tracking.php 1 year ago Cron_Job.php 1 year ago Cron_Manager.php 1 year ago Current_Traffic_Finder.php 1 year ago Dashboard_Options.php 1 year ago Dashboard_Widget.php 2 years ago Database.php 1 year ago Database_Manager.php 2 years ago Empty_Report_Option.php 2 years ago Env.php 1 year ago Filters.php 1 year ago Geo_Database_Background_Job.php 2 years ago Geo_Database_Manager.php 1 year ago Geoposition.php 2 years ago Icon_Directory.php 2 years ago Icon_Directory_Factory.php 2 years ago Illuminate_Builder.php 1 year ago Independent_Analytics.php 1 year ago Interrupt.php 1 year ago Known_Referrers.php 2 years ago MainWP.php 1 year ago Patch.php 1 year ago Payload_Validator.php 1 year ago Plugin_Conflict_Detector.php 1 year ago Plugin_Group.php 1 year ago Plugin_Group_Option.php 2 years ago Query.php 1 year ago Query_Taps.php 1 year ago Quick_Stats.php 1 year ago REST_API.php 1 year ago Real_Time.php 1 year ago Report.php 1 year ago Report_Finder.php 1 year ago Report_Options_Parser.php 2 years ago Resource_Identifier.php 1 year ago Settings.php 1 year ago Sort_Configuration.php 1 year ago Tables.php 1 year ago Track_Resource_Changes.php 1 year ago View.php 1 year ago View_Counter.php 1 year ago Views_Over_Time_Finder.php 1 year ago WP_Option_Cache_Bust.php 2 years ago
REST_API.php
434 lines
1 <?php
2
3 namespace IAWP;
4
5 use IAWP\Click_Tracking\Link_Rule;
6 use IAWP\Click_Tracking\Link_Rule_Finder;
7 use IAWP\Models\Visitor;
8 use IAWP\Utils\Device;
9 use IAWP\Utils\Request;
10 use IAWP\Utils\Salt;
11 use IAWP\Utils\Security;
12 use IAWP\Utils\URL;
13 /** @internal */
14 class REST_API
15 {
16 public function __construct()
17 {
18 \add_action('wp_footer', [$this, 'echo_tracking_script']);
19 \add_action('rest_api_init', [$this, 'register_rest_api']);
20 // Support for PDF Viewer by Themencode (free and pro versions)
21 \add_action('tnc_pvfw_viewer_head', [$this, 'echo_tracking_script']);
22 \add_action('tnc_pvfw_head', [$this, 'echo_tracking_script']);
23 // Support for Coming Soon and Maintenance by Colorlib
24 \add_action('ccsm_header', [$this, 'echo_tracking_script']);
25 // Support for CMP - Coming Soon & Maintenance
26 \add_action('cmp_footer', [$this, 'echo_tracking_script']);
27 }
28 public function echo_tracking_script()
29 {
30 \IAWP\Migrations\Migrations::handle_migration_18_error();
31 \IAWP\Migrations\Migrations::handle_migration_22_error();
32 \IAWP\Migrations\Migrations::handle_migration_29_error();
33 \IAWP\Migrations\Migrations::create_or_migrate();
34 if (\IAWP\Migrations\Migrations::is_migrating()) {
35 return;
36 }
37 if (!\get_option('iawp_track_authenticated_users') && \is_user_logged_in()) {
38 return;
39 }
40 if ($this->block_user_role()) {
41 return;
42 }
43 // Don't track post or page previews
44 if (\is_preview()) {
45 return;
46 }
47 $payload = [];
48 $current_resource = \IAWP\Resource_Identifier::for_resource_being_viewed();
49 if (\is_null($current_resource)) {
50 return;
51 }
52 $payload['resource'] = $current_resource->type();
53 if ($current_resource->has_meta()) {
54 $payload[$current_resource->meta_key()] = $current_resource->meta_value();
55 }
56 $payload['page'] = \max(1, \get_query_var('paged'));
57 $data = ['payload' => $payload];
58 $data['signature'] = \md5(Salt::request_payload_salt() . \json_encode($data['payload']));
59 $track_view_url = \get_rest_url(null, '/iawp/search');
60 $track_click_url = \IAWPSCOPED\iawp_url_to('/iawp-click-endpoint.php');
61 $link_rules = Link_Rule_Finder::link_rules()->filter(function (Link_Rule $link_rule) {
62 return $link_rule->is_active();
63 })->map(function (Link_Rule $link_rule) {
64 return ['type' => $link_rule->type(), 'value' => $link_rule->value()];
65 })->values();
66 $link_rules_json = \json_encode($link_rules);
67 ?>
68 <script>
69 (function () {
70 const calculateParentDistance = (child, parent) => {
71 let count = 0;
72 let currentElement = child;
73
74 // Traverse up the DOM tree until we reach parent or the top of the DOM
75 while (currentElement && currentElement !== parent) {
76 currentElement = currentElement.parentNode;
77 count++;
78 }
79
80 // If parent was not found in the hierarchy, return -1
81 if (!currentElement) {
82 return -1; // Indicates parent is not an ancestor of element
83 }
84
85 return count; // Number of layers between element and parent
86 }
87 const isMatchingClass = (linkRule, href, classes) => {
88 return classes.includes(linkRule.value)
89 }
90 const isMatchingDomain = (linkRule, href, classes) => {
91 if(!URL.canParse(href)) {
92 return false
93 }
94
95 const url = new URL(href)
96
97 return linkRule.value === url.host
98 }
99 const isMatchingExtension = (linkRule, href, classes) => {
100 if(!URL.canParse(href)) {
101 return false
102 }
103
104 const url = new URL(href)
105
106 return url.pathname.endsWith('.' + linkRule.value)
107 }
108 const isMatchingSubdirectory = (linkRule, href, classes) => {
109 if(!URL.canParse(href)) {
110 return false
111 }
112
113 const url = new URL(href)
114
115 return url.pathname.startsWith('/' + linkRule.value + '/')
116 }
117 const isMatchingProtocol = (linkRule, href, classes) => {
118 if(!URL.canParse(href)) {
119 return false
120 }
121
122 const url = new URL(href)
123
124 return url.protocol === linkRule.value + ':'
125 }
126 const isMatchingExternal = (linkRule, href, classes) => {
127 if(!URL.canParse(href) || !URL.canParse(document.location.href)) {
128 return false
129 }
130
131 const matchingProtocols = ['http:', 'https:']
132 const siteUrl = new URL(document.location.href)
133 const linkUrl = new URL(href)
134
135 // Links to subdomains will appear to be external matches according to JavaScript,
136 // but the PHP rules will filter those events out.
137 return matchingProtocols.includes(linkUrl.protocol) && siteUrl.host !== linkUrl.host
138 }
139 const isMatch = (linkRule, href, classes) => {
140 switch (linkRule.type) {
141 case 'class':
142 return isMatchingClass(linkRule, href, classes)
143 case 'domain':
144 return isMatchingDomain(linkRule, href, classes)
145 case 'extension':
146 return isMatchingExtension(linkRule, href, classes)
147 case 'subdirectory':
148 return isMatchingSubdirectory(linkRule, href, classes)
149 case 'protocol':
150 return isMatchingProtocol(linkRule, href, classes)
151 case 'external':
152 return isMatchingExternal(linkRule, href, classes)
153 default:
154 return false;
155 }
156 }
157 const track = (element) => {
158 const href = element.href ?? null
159 const classes = Array.from(element.classList)
160 const linkRules = <?php
161 echo $link_rules_json;
162 ?>
163
164 if(linkRules.length === 0) {
165 return
166 }
167
168 // For link rules that target a class, we need to allow that class to appear
169 // in any ancestor up to the 7th ancestor. This loop looks for those matches
170 // and counts them.
171 linkRules.forEach((linkRule) => {
172 if(linkRule.type !== 'class') {
173 return;
174 }
175
176 const matchingAncestor = element.closest('.' + linkRule.value)
177
178 if(!matchingAncestor || matchingAncestor.matches('html, body')) {
179 return;
180 }
181
182 const depth = calculateParentDistance(element, matchingAncestor)
183
184 if(depth < 7) {
185 classes.push(linkRule.value)
186 }
187 });
188
189 const hasMatch = linkRules.some((linkRule) => {
190 return isMatch(linkRule, href, classes)
191 })
192
193 if(!hasMatch) {
194 return
195 }
196
197 const url = "<?php
198 echo $track_click_url;
199 ?>";
200 const body = {
201 href: href,
202 classes: classes.join(' '),
203 ...<?php
204 echo \json_encode($data);
205 ?>
206 };
207
208 if (navigator.sendBeacon) {
209 let blob = new Blob([JSON.stringify(body)], {
210 type: "application/json"
211 });
212 navigator.sendBeacon(url, blob);
213 } else {
214 const xhr = new XMLHttpRequest();
215 xhr.open("POST", url, true);
216 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
217 xhr.send(JSON.stringify(body))
218 }
219 }
220 document.addEventListener('mousedown', function (event) {
221 <?php
222 if (!\defined('IAWP_TESTING')) {
223 ?>
224 if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) {
225 return;
226 }
227 <?php
228 }
229 ?>
230
231 const element = event.target.closest('a')
232
233 if(!element) {
234 return
235 }
236
237 const isPro = <?php
238 echo \IAWPSCOPED\iawp_is_pro() ? 'true' : 'false';
239 ?>
240
241 if(!isPro) {
242 return
243 }
244
245 // Don't track left clicks with this event. The click event is used for that.
246 if(event.button === 0) {
247 return
248 }
249
250 track(element)
251 })
252 document.addEventListener('click', function (event) {
253 <?php
254 if (!\defined('IAWP_TESTING')) {
255 ?>
256 if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) {
257 return;
258 }
259 <?php
260 }
261 ?>
262
263 const element = event.target.closest('a, button, input[type="submit"], input[type="button"]')
264
265 if(!element) {
266 return
267 }
268
269 const isPro = <?php
270 echo \IAWPSCOPED\iawp_is_pro() ? 'true' : 'false';
271 ?>
272
273 if(!isPro) {
274 return
275 }
276
277 track(element)
278 })
279 document.addEventListener('play', function (event) {
280 <?php
281 if (!\defined('IAWP_TESTING')) {
282 ?>
283 if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) {
284 return;
285 }
286 <?php
287 }
288 ?>
289
290 const element = event.target.closest('audio, video')
291
292 if(!element) {
293 return
294 }
295
296 const isPro = <?php
297 echo \IAWPSCOPED\iawp_is_pro() ? 'true' : 'false';
298 ?>
299
300 if(!isPro) {
301 return
302 }
303
304 track(element)
305 }, true)
306 document.addEventListener("DOMContentLoaded", function (e) {
307 if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") {
308 return;
309 }
310
311 <?php
312 if (!\defined('IAWP_TESTING')) {
313 ?>
314 if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) {
315 return;
316 }
317 <?php
318 }
319 ?>
320
321 let referrer_url = null;
322
323 if (typeof document.referrer === 'string' && document.referrer.length > 0) {
324 referrer_url = document.referrer;
325 }
326
327 const params = location.search.slice(1).split('&').reduce((acc, s) => {
328 const [k, v] = s.split('=');
329 return Object.assign(acc, {[k]: v});
330 }, {});
331
332 const url = "<?php
333 echo $track_view_url;
334 ?>";
335 const body = {
336 referrer_url,
337 utm_source: params.utm_source,
338 utm_medium: params.utm_medium,
339 utm_campaign: params.utm_campaign,
340 utm_term: params.utm_term,
341 utm_content: params.utm_content,
342 gclid: params.gclid,
343 ...<?php
344 echo \json_encode($data);
345 ?>
346 };
347
348 if (navigator.sendBeacon) {
349 let blob = new Blob([JSON.stringify(body)], {
350 type: "application/json"
351 });
352 navigator.sendBeacon(url, blob);
353 } else {
354 const xhr = new XMLHttpRequest();
355 xhr.open("POST", url, true);
356 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
357 xhr.send(JSON.stringify(body))
358 }
359 });
360 })();
361 </script>
362 <?php
363 }
364 public function register_rest_api()
365 {
366 \register_rest_route('iawp', '/search', ['methods' => 'POST', 'callback' => [$this, 'track_view'], 'permission_callback' => function () {
367 return \true;
368 }]);
369 }
370 public function track_view($request)
371 {
372 if (Device::getInstance()->is_bot() && !\defined('IAWP_TESTING')) {
373 return;
374 }
375 \IAWP\Migrations\Migrations::handle_migration_18_error();
376 \IAWP\Migrations\Migrations::handle_migration_22_error();
377 \IAWP\Migrations\Migrations::handle_migration_29_error();
378 \IAWP\Migrations\Migrations::create_or_migrate();
379 if (\IAWP\Migrations\Migrations::is_migrating()) {
380 return;
381 }
382 if (Request::is_ip_address_blocked()) {
383 return;
384 }
385 $visitor = Visitor::fetch_current_visitor();
386 $signature = \md5(Salt::request_payload_salt() . \json_encode($request['payload']));
387 $campaign = [];
388 if (\IAWPSCOPED\iawp_is_pro()) {
389 $campaign = ['utm_source' => $this->decode_or_nullify($request['utm_source']), 'utm_medium' => $this->decode_or_nullify($request['utm_medium']), 'utm_campaign' => $this->decode_or_nullify($request['utm_campaign']), 'utm_term' => $this->decode_or_nullify($request['utm_term']), 'utm_content' => $this->decode_or_nullify($request['utm_content'])];
390 }
391 if ($signature == $request['signature']) {
392 new \IAWP\View($request['payload'], $this->calculate_referrer_url($request), $visitor, $campaign);
393 return new \WP_REST_Response(['success' => \true], 200, ['X-IAWP' => 'iawp']);
394 } else {
395 return new \WP_REST_Response(['success' => \false], 200, ['X-IAWP' => 'iawp']);
396 }
397 }
398 private function calculate_referrer_url($request) : ?string
399 {
400 $referrer_url = $request['referrer_url'];
401 $url = new URL($referrer_url ?? '');
402 if (!\is_null($this->decode_or_nullify($request['gclid'])) && $url->get_domain() !== 'googleads.g.doubleclick.net') {
403 $referrer_url = 'https://googleads.iawp';
404 }
405 if (\is_null($referrer_url)) {
406 return null;
407 }
408 return $referrer_url;
409 }
410 private function decode_or_nullify($string)
411 {
412 if (!isset($string)) {
413 return null;
414 }
415 $safe_string = \trim(\urldecode($string));
416 $safe_string = \str_replace('+', ' ', $safe_string);
417 $safe_string = Security::string($safe_string);
418 if (\strlen($safe_string) === 0) {
419 return null;
420 }
421 return $safe_string;
422 }
423 private function block_user_role() : bool
424 {
425 $blocked_roles = \IAWPSCOPED\iawp()->get_option('iawp_blocked_roles', ['administrator']);
426 foreach (\wp_get_current_user()->roles as $visitor_role) {
427 if (\in_array($visitor_role, $blocked_roles)) {
428 return \true;
429 }
430 }
431 return \false;
432 }
433 }
434