PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.15
Independent Analytics – WordPress Analytics Plugin v1.15
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 / inc / rest_api.php
independent-analytics / inc Last commit date
ajax 3 years ago migrations 3 years ago models 3 years ago queries 3 years ago sql 3 years ago tables 3 years ago utils 3 years ago campaign_builder.php 3 years ago capability_manager.php 3 years ago chart.php 3 years ago chart_geo.php 3 years ago dashboard_options.php 3 years ago dashboard_widget.php 3 years ago db.php 3 years ago filters.php 3 years ago freemius.php 3 years ago geo_database.php 3 years ago geo_database_download_job.php 3 years ago independent_analytics.php 3 years ago known_referrers.php 3 years ago quick_stats.php 3 years ago real_time.php 3 years ago rest_api.php 3 years ago settings.php 3 years ago track_resource_changes.php 3 years ago view_counter.php 3 years ago
rest_api.php
238 lines
1 <?php
2
3 namespace IAWP;
4
5 class REST_API
6 {
7 public function __construct()
8 {
9 add_action( 'wp_footer', [ $this, 'echo_tracking_script' ] );
10 add_action( 'rest_api_init', [ $this, 'register_rest_api' ] );
11 }
12
13 private function get_date_archive_date()
14 {
15 $str = get_query_var( 'year' );
16
17 if ( is_month() || is_day() ) {
18 $month = get_query_var( 'monthnum' );
19 $str = $str . '-' . str_pad(
20 $month,
21 2,
22 '0',
23 STR_PAD_LEFT
24 );
25 }
26
27
28 if ( is_day() ) {
29 $day = get_query_var( 'day' );
30 $str = $str . '-' . str_pad(
31 $day,
32 2,
33 '0',
34 STR_PAD_LEFT
35 );
36 }
37
38 return $str;
39 }
40
41 public function echo_tracking_script()
42 {
43 if ( !get_option( 'iawp_track_authenticated_users' ) && is_user_logged_in() ) {
44 // Todo - Can we clear the cache plugins to make sure that the pages so tracking code as user logs in & out?
45 return;
46 }
47 // Don't track post or page previews
48 if ( is_preview() ) {
49 return;
50 }
51 $payload = [];
52
53 if ( is_singular() ) {
54 $payload['resource'] = 'singular';
55 $payload['singular_id'] = get_queried_object_id();
56 } elseif ( is_author() ) {
57 $payload['resource'] = 'author_archive';
58 $payload['author_id'] = get_queried_object_id();
59 } elseif ( is_date() ) {
60 $payload['resource'] = 'date_archive';
61 $payload['date_archive'] = $this->get_date_archive_date();
62 } elseif ( is_post_type_archive() ) {
63 $payload['resource'] = 'post_type_archive';
64 $payload['post_type'] = get_queried_object()->name;
65 } elseif ( is_category() || is_tag() || is_tax() ) {
66 $payload['resource'] = 'term_archive';
67 $payload['term_id'] = get_queried_object_id();
68 } elseif ( is_search() ) {
69 $payload['resource'] = 'search';
70 $payload['search_query'] = get_search_query();
71 } elseif ( is_home() ) {
72 $payload['resource'] = 'home';
73 } elseif ( is_404() ) {
74 $path = Request::path_relative_to_site_url();
75 if ( is_null( $path ) ) {
76 return;
77 }
78 $payload['resource'] = '404';
79 $payload['not_found_url'] = $path;
80 }
81
82 $payload['page'] = max( 1, get_query_var( 'paged' ) );
83 $valid_data = !empty($payload);
84
85 if ( $valid_data ) {
86 $data = [
87 'payload' => $payload,
88 ];
89 $data['signature'] = md5( Salt::request_payload_salt() . json_encode( $data['payload'] ) );
90 $url = get_rest_url() . 'iawp/search';
91 ?>
92 <script>
93 (function () {
94 document.addEventListener("DOMContentLoaded", function (e) {
95 if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") {
96 return;
97 }
98
99 if (navigator.webdriver || /bot|crawler|spider|crawling/i.test(navigator.userAgent)) {
100 return;
101 }
102
103 let referrer_url = null;
104
105 if (typeof document.referrer === 'string' && document.referrer.length > 0 && document.referrer.indexOf(window.location.origin) === -1) {
106 referrer_url = document.referrer;
107 }
108
109 const params = location.search.slice(1).split('&').reduce((acc, s) => {
110 const [k, v] = s.split('=')
111 return Object.assign(acc, {[k]: v})
112 }, {})
113
114 const url = "<?php
115 echo $url ;
116 ?>"
117 const body = {
118 referrer_url,
119 utm_source: params.utm_source,
120 utm_medium: params.utm_medium,
121 utm_campaign: params.utm_campaign,
122 utm_term: params.utm_term,
123 utm_content: params.utm_content,
124 ...<?php
125 echo json_encode( $data ) ;
126 ?>
127 }
128 const xhr = new XMLHttpRequest()
129 xhr.open("POST", url, true)
130 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8")
131 xhr.send(JSON.stringify(body))
132 })
133 })();
134 </script>
135 <?php
136 }
137
138 }
139
140 public function register_rest_api()
141 {
142 register_rest_route( 'iawp', '/search', [
143 'methods' => 'POST',
144 'callback' => [ $this, 'track_view' ],
145 'permission_callback' => function () {
146 return true;
147 },
148 ] );
149 }
150
151 public function track_view( $request )
152 {
153 Migration::create_or_migrate();
154 if ( Migration::is_migrating() ) {
155 return;
156 }
157 if ( $this->blocked_ip( Request::ip() ) ) {
158 return;
159 }
160 $visitor = new Visitor( Request::ip(), Request::user_agent() );
161 $signature = md5( Salt::request_payload_salt() . json_encode( $request['payload'] ) );
162 $campaign = null;
163
164 if ( $signature == $request['signature'] ) {
165 new View(
166 $request['payload'],
167 $request['referrer_url'],
168 $visitor,
169 $campaign
170 );
171 return [
172 'success' => true,
173 ];
174 } else {
175 return [
176 'success' => false,
177 ];
178 }
179
180 }
181
182 private function decode_or_nullify( $string )
183 {
184 if ( !isset( $string ) ) {
185 return null;
186 }
187 $safe_string = trim( urldecode( $string ) );
188 $safe_string = str_replace( '+', ' ', $safe_string );
189 $safe_string = Security::string( $safe_string );
190 if ( strlen( $safe_string ) === 0 ) {
191 return null;
192 }
193 return $safe_string;
194 }
195
196 private function blocked_ip( $visitor_ip )
197 {
198 $blocked_ips = IAWP()->get_option( 'iawp_blocked_ips', [] );
199 if ( count( $blocked_ips ) == 0 ) {
200 return false;
201 }
202 if ( in_array( $visitor_ip, $blocked_ips ) ) {
203 return true;
204 }
205 $wildcard_ips = [];
206 foreach ( $blocked_ips as $blocked_ip ) {
207 if ( StringUtil::str_contains( $blocked_ip, '*' ) ) {
208 $wildcard_ips[] = $blocked_ip;
209 }
210 }
211 if ( count( $wildcard_ips ) == 0 ) {
212 return false;
213 }
214 $visitor_parts = explode( '.', $visitor_ip );
215 $goal = count( $visitor_parts );
216 foreach ( $wildcard_ips as $blocked_ip ) {
217 $blocked_parts = explode( '.', $blocked_ip );
218 $matches = 0;
219 for ( $i = 0 ; $i < count( $visitor_parts ) ; $i++ ) {
220
221 if ( !array_key_exists( $i, $blocked_parts ) ) {
222 $matches++;
223 } elseif ( $visitor_parts[$i] == $blocked_parts[$i] || $blocked_parts[$i] == '*' ) {
224 $matches++;
225 continue;
226 } else {
227 break;
228 }
229
230 }
231 if ( $matches == $goal ) {
232 return true;
233 }
234 }
235 return false;
236 }
237
238 }