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 | } |