AJAX
1 year ago
Admin_Page
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
2 years ago
Form_Submissions
1 year ago
Interval
2 years ago
Menu_Bar_Stats
1 year ago
Migrations
1 year ago
Models
1 year ago
Public_API
2 years ago
Rows
1 year ago
Statistics
1 year ago
Tables
1 year ago
Utils
1 year ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
1 year ago
Chart.php
1 year ago
Chart_Geo.php
1 year ago
Cron_Job.php
1 year ago
Cron_Job_Autoloader.php
1 year ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years 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
Patch.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
2 years ago
REST_API.php
1 year ago
Real_Time.php
1 year ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
1 year ago
Resource_Identifier.php
1 year ago
Settings.php
1 year ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
1 year ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
REST_API.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Models\Visitor; |
| 6 | use IAWP\Utils\Device; |
| 7 | use IAWP\Utils\Request; |
| 8 | use IAWP\Utils\Salt; |
| 9 | use IAWP\Utils\Security; |
| 10 | use IAWP\Utils\URL; |
| 11 | /** @internal */ |
| 12 | class REST_API |
| 13 | { |
| 14 | public function __construct() |
| 15 | { |
| 16 | \add_action('wp_footer', [$this, 'echo_tracking_script']); |
| 17 | \add_action('rest_api_init', [$this, 'register_rest_api']); |
| 18 | // Support for PDF Viewer by Themencode (free and pro versions) |
| 19 | \add_action('tnc_pvfw_viewer_head', [$this, 'echo_tracking_script']); |
| 20 | \add_action('tnc_pvfw_head', [$this, 'echo_tracking_script']); |
| 21 | // Support for Coming Soon and Maintenance by Colorlib |
| 22 | \add_action('ccsm_header', [$this, 'echo_tracking_script']); |
| 23 | } |
| 24 | public function echo_tracking_script() |
| 25 | { |
| 26 | if (!\get_option('iawp_track_authenticated_users') && \is_user_logged_in()) { |
| 27 | // Todo - Can we clear the cache plugins to make sure that the pages so tracking code as user logs in & out? |
| 28 | return; |
| 29 | } |
| 30 | if ($this->block_user_role()) { |
| 31 | return; |
| 32 | } |
| 33 | // Don't track post or page previews |
| 34 | if (\is_preview()) { |
| 35 | return; |
| 36 | } |
| 37 | $payload = []; |
| 38 | $current_resource = \IAWP\Resource_Identifier::for_resource_being_viewed(); |
| 39 | if (\is_null($current_resource)) { |
| 40 | return; |
| 41 | } |
| 42 | $payload['resource'] = $current_resource->type(); |
| 43 | if ($current_resource->has_meta()) { |
| 44 | $payload[$current_resource->meta_key()] = $current_resource->meta_value(); |
| 45 | } |
| 46 | $payload['page'] = \max(1, \get_query_var('paged')); |
| 47 | $data = ['payload' => $payload]; |
| 48 | $data['signature'] = \md5(Salt::request_payload_salt() . \json_encode($data['payload'])); |
| 49 | $url = \get_rest_url() . 'iawp/search'; |
| 50 | ?> |
| 51 | <script> |
| 52 | (function () { |
| 53 | document.addEventListener("DOMContentLoaded", function (e) { |
| 54 | if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | <?php |
| 59 | if (!\defined('IAWP_TESTING')) { |
| 60 | ?> |
| 61 | if (navigator.webdriver || /bot|crawler|spider|crawling|semrushbot|chrome-lighthouse/i.test(navigator.userAgent)) { |
| 62 | return; |
| 63 | } |
| 64 | <?php |
| 65 | } |
| 66 | ?> |
| 67 | |
| 68 | let referrer_url = null; |
| 69 | |
| 70 | if (typeof document.referrer === 'string' && document.referrer.length > 0) { |
| 71 | referrer_url = document.referrer; |
| 72 | } |
| 73 | |
| 74 | const params = location.search.slice(1).split('&').reduce((acc, s) => { |
| 75 | const [k, v] = s.split('='); |
| 76 | return Object.assign(acc, {[k]: v}); |
| 77 | }, {}); |
| 78 | |
| 79 | const url = "<?php |
| 80 | echo $url; |
| 81 | ?>"; |
| 82 | const body = { |
| 83 | referrer_url, |
| 84 | utm_source: params.utm_source, |
| 85 | utm_medium: params.utm_medium, |
| 86 | utm_campaign: params.utm_campaign, |
| 87 | utm_term: params.utm_term, |
| 88 | utm_content: params.utm_content, |
| 89 | gclid: params.gclid, |
| 90 | ...<?php |
| 91 | echo \json_encode($data); |
| 92 | ?> |
| 93 | }; |
| 94 | const xhr = new XMLHttpRequest(); |
| 95 | xhr.open("POST", url, true); |
| 96 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| 97 | xhr.send(JSON.stringify(body)); |
| 98 | }); |
| 99 | })(); |
| 100 | </script> |
| 101 | <?php |
| 102 | } |
| 103 | public function register_rest_api() |
| 104 | { |
| 105 | \register_rest_route('iawp', '/search', ['methods' => 'POST', 'callback' => [$this, 'track_view'], 'permission_callback' => function () { |
| 106 | return \true; |
| 107 | }]); |
| 108 | } |
| 109 | public function track_view($request) |
| 110 | { |
| 111 | if (Device::getInstance()->is_bot() && !\defined('IAWP_TESTING')) { |
| 112 | return; |
| 113 | } |
| 114 | \IAWP\Migrations\Migrations::handle_migration_18_error(); |
| 115 | \IAWP\Migrations\Migrations::handle_migration_22_error(); |
| 116 | \IAWP\Migrations\Migrations::handle_migration_29_error(); |
| 117 | \IAWP\Migrations\Migrations::create_or_migrate(); |
| 118 | if (\IAWP\Migrations\Migrations::is_migrating()) { |
| 119 | return; |
| 120 | } |
| 121 | if ($this->maybe_block_ip_address()) { |
| 122 | return; |
| 123 | } |
| 124 | $visitor = Visitor::fetch_current_visitor(); |
| 125 | $signature = \md5(Salt::request_payload_salt() . \json_encode($request['payload'])); |
| 126 | $campaign = []; |
| 127 | if (\IAWPSCOPED\iawp_is_pro()) { |
| 128 | $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'])]; |
| 129 | } |
| 130 | if ($signature == $request['signature']) { |
| 131 | new \IAWP\View($request['payload'], $this->calculate_referrer_url($request), $visitor, $campaign); |
| 132 | return new \WP_REST_Response(['success' => \true], 200, ['X-IAWP' => 'iawp']); |
| 133 | } else { |
| 134 | return new \WP_REST_Response(['success' => \false], 200, ['X-IAWP' => 'iawp']); |
| 135 | } |
| 136 | } |
| 137 | private function calculate_referrer_url($request) : ?string |
| 138 | { |
| 139 | $referrer_url = $request['referrer_url']; |
| 140 | $url = new URL($referrer_url ?? ''); |
| 141 | if (!\is_null($this->decode_or_nullify($request['gclid'])) && $url->get_domain() !== 'googleads.g.doubleclick.net') { |
| 142 | $referrer_url = 'https://googleads.iawp'; |
| 143 | } |
| 144 | if (\is_null($referrer_url)) { |
| 145 | return null; |
| 146 | } |
| 147 | return $referrer_url; |
| 148 | } |
| 149 | private function decode_or_nullify($string) |
| 150 | { |
| 151 | if (!isset($string)) { |
| 152 | return null; |
| 153 | } |
| 154 | $safe_string = \trim(\urldecode($string)); |
| 155 | $safe_string = \str_replace('+', ' ', $safe_string); |
| 156 | $safe_string = Security::string($safe_string); |
| 157 | if (\strlen($safe_string) === 0) { |
| 158 | return null; |
| 159 | } |
| 160 | return $safe_string; |
| 161 | } |
| 162 | private function maybe_block_ip_address() |
| 163 | { |
| 164 | $blocked_ips = \IAWPSCOPED\iawp()->get_option('iawp_blocked_ips', []); |
| 165 | if (\count($blocked_ips) == 0) { |
| 166 | return \false; |
| 167 | } |
| 168 | return Request::is_ip_address_blocked($blocked_ips); |
| 169 | } |
| 170 | private function block_user_role() : bool |
| 171 | { |
| 172 | $blocked_roles = \IAWPSCOPED\iawp()->get_option('iawp_blocked_roles', []); |
| 173 | foreach (\wp_get_current_user()->roles as $visitor_role) { |
| 174 | if (\in_array($visitor_role, $blocked_roles)) { |
| 175 | return \true; |
| 176 | } |
| 177 | } |
| 178 | return \false; |
| 179 | } |
| 180 | } |
| 181 |