blocks
2 years ago
vendor
2 years ago
admin-notices.js
2 years ago
admin.js
2 years ago
chart.js
2 years ago
wpp.js
2 years ago
wpp.min.js
2 years ago
wpp.js
199 lines
| 1 | const wpp_params = document.currentScript.dataset; |
| 2 | const WordPressPopularPosts = (function() { |
| 3 | |
| 4 | "use strict"; |
| 5 | |
| 6 | const noop = function(){}; |
| 7 | |
| 8 | const get = function( url, params, callback, additional_headers ){ |
| 9 | callback = ( 'function' === typeof callback ) ? callback : noop; |
| 10 | ajax( "GET", url, params, callback, additional_headers ); |
| 11 | }; |
| 12 | |
| 13 | const post = function( url, params, callback, additional_headers ){ |
| 14 | callback = ( 'function' === typeof callback ) ? callback : noop; |
| 15 | ajax( "POST", url, params, callback, additional_headers ); |
| 16 | }; |
| 17 | |
| 18 | const ajax = function( method, url, params, callback, additional_headers ){ |
| 19 | /* Create XMLHttpRequest object and set variables */ |
| 20 | let xhr = new XMLHttpRequest(), |
| 21 | target = url, |
| 22 | args = params, |
| 23 | valid_methods = ["GET", "POST"], |
| 24 | headers = { |
| 25 | 'X-Requested-With': 'XMLHttpRequest' |
| 26 | }; |
| 27 | |
| 28 | method = -1 != valid_methods.indexOf( method ) ? method : "GET"; |
| 29 | |
| 30 | /* Set request headers */ |
| 31 | if ( 'POST' == method ) { |
| 32 | headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 33 | } |
| 34 | |
| 35 | if ( 'object' == typeof additional_headers && Object.keys(additional_headers).length ) { |
| 36 | headers = Object.assign({}, headers, additional_headers); |
| 37 | } |
| 38 | |
| 39 | /* Set request method and target URL */ |
| 40 | xhr.open( method, target + ( 'GET' == method ? '?' + args : '' ), true ); |
| 41 | |
| 42 | for (const key in headers) { |
| 43 | if ( headers.hasOwnProperty(key) ) { |
| 44 | xhr.setRequestHeader( key, headers[key] ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /* Hook into onreadystatechange */ |
| 49 | xhr.onreadystatechange = function() { |
| 50 | if ( 4 === xhr.readyState && 200 <= xhr.status && 300 > xhr.status ) { |
| 51 | if ( 'function' === typeof callback ) { |
| 52 | callback.call( undefined, xhr.response ); |
| 53 | } |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | /* Send request */ |
| 58 | xhr.send( ( 'POST' == method ? args : null ) ); |
| 59 | }; |
| 60 | |
| 61 | const theme = function(wpp_list) { |
| 62 | let base_styles = document.createElement('style'), |
| 63 | dummy_list = document.createElement('ul'); |
| 64 | |
| 65 | dummy_list.innerHTML = '<li><a href="#"></a></li>'; |
| 66 | wpp_list.parentNode.appendChild(dummy_list); |
| 67 | |
| 68 | let dummy_list_item_styles = getComputedStyle(dummy_list.querySelector('li')), |
| 69 | dummy_link_item_styles = getComputedStyle(dummy_list.querySelector('li a')); |
| 70 | |
| 71 | base_styles.innerHTML = '.wpp-list li {font-size: '+ dummy_list_item_styles.fontSize +'}'; |
| 72 | base_styles.innerHTML += '.wpp-list li a {color: '+ dummy_link_item_styles.color +'}'; |
| 73 | |
| 74 | wpp_list.parentNode.removeChild(dummy_list); |
| 75 | |
| 76 | let wpp_list_sr = wpp_list.attachShadow({mode: "open"}); |
| 77 | |
| 78 | wpp_list_sr.append(base_styles); |
| 79 | |
| 80 | while(wpp_list.firstElementChild) { |
| 81 | wpp_list_sr.append(wpp_list.firstElementChild); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | return { |
| 86 | get: get, |
| 87 | post: post, |
| 88 | ajax: ajax, |
| 89 | theme: theme |
| 90 | }; |
| 91 | |
| 92 | })(); |
| 93 | |
| 94 | (function(){ |
| 95 | if ( ! Object.keys(wpp_params).length ) { |
| 96 | console.error('WPP params not found, if you are using a JS minifier tool please add wpp.min.js to its exclusion list'); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | const post_id = Number(wpp_params.postId); |
| 101 | let do_request = true; |
| 102 | |
| 103 | if ( post_id ) { |
| 104 | if ( '1' == wpp_params.sampling ) { |
| 105 | let num = Math.floor(Math.random() * wpp_params.samplingRate) + 1; |
| 106 | do_request = ( 1 === num ); |
| 107 | } |
| 108 | |
| 109 | if ( do_request ) { |
| 110 | WordPressPopularPosts.post( |
| 111 | wpp_params.apiUrl + '/v2/views/' + post_id, |
| 112 | "_wpnonce=" + wpp_params.token + "&sampling=" + wpp_params.sampling + "&sampling_rate=" + wpp_params.samplingRate, |
| 113 | function( response ) { |
| 114 | wpp_params.debug&&window.console&&window.console.log&&window.console.log(JSON.parse(response)); |
| 115 | } |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | })(); |
| 120 | |
| 121 | document.addEventListener('DOMContentLoaded', function() { |
| 122 | if ( ! Object.keys(wpp_params).length ) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | const widget_placeholders = document.querySelectorAll('.wpp-widget-placeholder, .wpp-widget-block-placeholder, .wpp-shortcode-placeholder'); |
| 127 | let w = 0; |
| 128 | |
| 129 | while ( w < widget_placeholders.length ) { |
| 130 | fetchWidget(widget_placeholders[w]); |
| 131 | w++; |
| 132 | } |
| 133 | |
| 134 | const sr = document.querySelectorAll('.popular-posts-sr'); |
| 135 | |
| 136 | if ( sr.length ) { |
| 137 | for( let s = 0; s < sr.length; s++ ) { |
| 138 | WordPressPopularPosts.theme(sr[s]); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | function fetchWidget(widget_placeholder) { |
| 143 | let widget_id_attr = widget_placeholder.getAttribute('data-widget-id'), |
| 144 | method = 'GET', |
| 145 | url = '', |
| 146 | headers = { |
| 147 | 'X-WP-Nonce': wpp_params.token |
| 148 | }, |
| 149 | params = ''; |
| 150 | |
| 151 | if ( widget_id_attr ) { |
| 152 | url = wpp_params.apiUrl + '/v1/popular-posts/widget/' + widget_id_attr.split('-')[1]; |
| 153 | params = 'is_single=' + wpp_params.postId + ( wpp_params.lang ? '&lang=' + wpp_params.lang : '' ); |
| 154 | } else { |
| 155 | method = 'POST'; |
| 156 | url = wpp_params.apiUrl + '/v2/widget?is_single=' + wpp_params.postId + ( wpp_params.lang ? '&lang=' + wpp_params.lang : '' ); |
| 157 | headers['Content-Type'] = 'application/json'; |
| 158 | |
| 159 | let json_tag = widget_placeholder.parentNode.querySelector('script[type="application/json"]'); |
| 160 | |
| 161 | if ( json_tag ) { |
| 162 | let args = JSON.parse(json_tag.textContent.replace(/[\n\r]/g,'')); |
| 163 | params = JSON.stringify(args); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | WordPressPopularPosts.ajax( |
| 168 | method, |
| 169 | url, |
| 170 | params, |
| 171 | function(response) { |
| 172 | renderWidget(response, widget_placeholder); |
| 173 | }, |
| 174 | headers |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | function renderWidget(response, widget_placeholder) { |
| 179 | widget_placeholder.insertAdjacentHTML('afterend', JSON.parse(response).widget); |
| 180 | |
| 181 | let parent = widget_placeholder.parentNode, |
| 182 | sr = parent.querySelector('.popular-posts-sr'), |
| 183 | json_tag = parent.querySelector('script[type="application/json"]'); |
| 184 | |
| 185 | if ( json_tag ) |
| 186 | parent.removeChild(json_tag); |
| 187 | |
| 188 | parent.removeChild(widget_placeholder); |
| 189 | parent.classList.add('wpp-ajax'); |
| 190 | |
| 191 | if ( sr ) { |
| 192 | WordPressPopularPosts.theme(sr); |
| 193 | } |
| 194 | |
| 195 | let event = new Event("wpp-onload", {"bubbles": true, "cancelable": false}); |
| 196 | parent.dispatchEvent(event); |
| 197 | } |
| 198 | }); |
| 199 |