models
4 years ago
queries
4 years ago
utils
4 years ago
chart.php
4 years ago
db.php
4 years ago
filters-ajax.php
4 years ago
filters.php
4 years ago
rest-api.php
4 years ago
settings-ajax.php
4 years ago
settings.php
4 years ago
super-secret-content-generator.php
4 years ago
table-referrers.php
4 years ago
table-views.php
4 years ago
table.php
4 years ago
track-resource-changes.php
4 years ago
rest-api.php
143 lines
| 1 | <?php |
| 2 | if (!class_exists('IAWP_REST_API')) { |
| 3 | class IAWP_REST_API |
| 4 | { |
| 5 | public function __construct() |
| 6 | { |
| 7 | // Todo - Def don't want this function running multiple times... |
| 8 | add_action('wp_footer', [$this, 'echo_tracking_script']); |
| 9 | add_action('rest_api_init', [$this, 'register_rest_api']); |
| 10 | } |
| 11 | |
| 12 | private function get_date_archive_date() |
| 13 | { |
| 14 | $str = get_query_var('year'); |
| 15 | |
| 16 | if (is_month() || is_day()) { |
| 17 | $month = get_query_var('monthnum'); |
| 18 | $str = $str . '-' . str_pad($month, 2, '0', STR_PAD_LEFT); |
| 19 | } |
| 20 | |
| 21 | if (is_day()) { |
| 22 | $day = get_query_var('day'); |
| 23 | $str = $str . '-' . str_pad($day, 2, '0', STR_PAD_LEFT); |
| 24 | } |
| 25 | |
| 26 | return $str; |
| 27 | } |
| 28 | |
| 29 | public function echo_tracking_script() |
| 30 | { |
| 31 | if (!get_option('iawp_track_authenticated_users') && is_user_logged_in()) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | $payload = []; |
| 36 | |
| 37 | if (is_singular()) { |
| 38 | $payload['resource'] = 'singular'; |
| 39 | $payload['singular_id'] = get_queried_object_id(); |
| 40 | } elseif (is_author()) { |
| 41 | $payload['resource'] = 'author_archive'; |
| 42 | $payload['author_id'] = get_queried_object_id(); |
| 43 | } elseif (is_date()) { |
| 44 | $payload['resource'] = 'date_archive'; |
| 45 | $payload['date_archive'] = $this->get_date_archive_date(); |
| 46 | } elseif (is_post_type_archive()) { |
| 47 | $payload['resource'] = 'post_type_archive'; |
| 48 | $payload['post_type'] = get_queried_object()->name; |
| 49 | } elseif (is_category() || is_tag() || is_tax()) { |
| 50 | $payload['resource'] = 'term_archive'; |
| 51 | $payload['term_id'] = get_queried_object_id(); |
| 52 | } elseif (is_search()) { |
| 53 | $payload['resource'] = 'search'; |
| 54 | $payload['search_query'] = get_search_query(); |
| 55 | } elseif (is_home()) { |
| 56 | $payload['resource'] = 'home'; |
| 57 | } elseif (is_404()) { |
| 58 | $path = IAWP_Request::path_relative_to_site_url(); |
| 59 | |
| 60 | if (is_null($path)) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $payload['resource'] = '404'; |
| 65 | $payload['not_found_url'] = $path; |
| 66 | } |
| 67 | |
| 68 | $payload['page'] = max(1, get_query_var('paged')); |
| 69 | |
| 70 | $valid_data = !empty($payload); |
| 71 | if ($valid_data) { |
| 72 | $data = [ |
| 73 | 'payload' => $payload, |
| 74 | ]; |
| 75 | $data['signature'] = md5(json_encode($data['payload']) . 'mysupersecret'); |
| 76 | if (empty(get_option('permalink_structure'))) { |
| 77 | $url = get_site_url() . '/?rest_route=/iawp/search'; |
| 78 | } else { |
| 79 | $url = get_site_url() . '/wp-json/iawp/search'; |
| 80 | } ?> |
| 81 | <script> |
| 82 | (function () { |
| 83 | document.addEventListener("DOMContentLoaded", function (e) { |
| 84 | if (document.hasOwnProperty("visibilityState") && document.visibilityState === "prerender") { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (navigator.webdriver || /bot|crawler|spider|crawling/i.test(navigator.userAgent)) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | let referrer_url = null; |
| 93 | |
| 94 | if (typeof document.referrer === 'string' && document.referrer.length > 0 && document.referrer.indexOf(window.location.origin) === -1) { |
| 95 | referrer_url = document.referrer; |
| 96 | } |
| 97 | |
| 98 | const url = "<?= $url; ?>" |
| 99 | const body = { |
| 100 | referrer_url, |
| 101 | ...<?= json_encode($data); ?> |
| 102 | } |
| 103 | const xhr = new XMLHttpRequest() |
| 104 | xhr.open("POST", url, true) |
| 105 | xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8") |
| 106 | xhr.send(JSON.stringify(body)) |
| 107 | }) |
| 108 | })(); |
| 109 | </script> |
| 110 | <?php |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function register_rest_api() |
| 115 | { |
| 116 | register_rest_route('iawp', '/search', [ |
| 117 | 'methods' => 'POST', |
| 118 | 'callback' => [$this, 'track_view'], |
| 119 | 'permission_callback' => function () { |
| 120 | return true; |
| 121 | }, |
| 122 | ]); |
| 123 | } |
| 124 | |
| 125 | public function track_view($request) |
| 126 | { |
| 127 | if (is_user_logged_in()) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $signature = md5(json_encode($request['payload']) . 'mysupersecret'); |
| 132 | |
| 133 | if ($signature == $request['signature']) { |
| 134 | IAWP()->db->record_view($request['payload'], $request['referrer_url']); |
| 135 | |
| 136 | return ['success' => true]; |
| 137 | } else { |
| 138 | return ['success' => false]; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | ?> |