blocks
1 week ago
vendor
4 years ago
admin-notices.js
1 year ago
admin.js
1 week ago
chart.js
1 week ago
wpp.js
1 week ago
wpp.min.js
1 week ago
wpp.js
160 lines
| 1 | 'use strict'; |
| 2 | |
| 3 | const wpp_params = document.currentScript.dataset; |
| 4 | |
| 5 | if ( ! Object.keys(wpp_params).length ) { |
| 6 | throw new Error('WPP params not found, if you are using a JavaScript minifier tool please add wpp.min.js to its exclusion list'); |
| 7 | } |
| 8 | |
| 9 | const WordPressPopularPosts = (() => { |
| 10 | const noop = () => {}; |
| 11 | |
| 12 | const get = (url, params, callback = noop, additional_headers) => { |
| 13 | ajax('GET', url, params, callback, additional_headers); |
| 14 | }; |
| 15 | |
| 16 | const post = (url, params, callback = noop, additional_headers) => { |
| 17 | ajax('POST', url, params, callback, additional_headers); |
| 18 | }; |
| 19 | |
| 20 | const ajax = (method, url, params, callback, additional_headers) => { |
| 21 | const valid_methods = ['GET', 'POST']; |
| 22 | const headers = { |
| 23 | 'X-Requested-With': 'XMLHttpRequest', |
| 24 | ...additional_headers |
| 25 | }; |
| 26 | |
| 27 | if ( ! valid_methods.includes(method) ) { |
| 28 | method = 'GET'; |
| 29 | } |
| 30 | |
| 31 | fetch(url + (method === 'GET' ? '?' + params : ''), { |
| 32 | method, |
| 33 | headers, |
| 34 | body: method === 'POST' ? params : null |
| 35 | }) |
| 36 | .then(response => { |
| 37 | if ( ! response.ok ) { |
| 38 | throw new Error('Network response was not ok'); |
| 39 | } |
| 40 | return response.text(); |
| 41 | }) |
| 42 | .then(data => callback(data)) |
| 43 | .catch(error => console.error('Fetch error:', error)); |
| 44 | }; |
| 45 | |
| 46 | const theme = (wpp_list) => { |
| 47 | const base_styles = document.createElement('style'), |
| 48 | dummy_list = document.createElement('ul'); |
| 49 | |
| 50 | dummy_list.innerHTML = '<li><a href="#"></a></li>'; |
| 51 | wpp_list.parentNode.appendChild(dummy_list); |
| 52 | |
| 53 | const dummy_list_item_styles = getComputedStyle(dummy_list.querySelector('li')), |
| 54 | dummy_link_item_styles = getComputedStyle(dummy_list.querySelector('li a')); |
| 55 | |
| 56 | base_styles.innerHTML = `.wpp-list li {font-size: ${dummy_list_item_styles.fontSize}}`; |
| 57 | base_styles.innerHTML += `.wpp-list li a {color: ${dummy_link_item_styles.color}}`; |
| 58 | |
| 59 | wpp_list.parentNode.removeChild(dummy_list); |
| 60 | |
| 61 | const wpp_list_sr = wpp_list.attachShadow({mode: "open"}); |
| 62 | |
| 63 | wpp_list_sr.append(base_styles); |
| 64 | |
| 65 | while (wpp_list.firstElementChild) { |
| 66 | wpp_list_sr.append(wpp_list.firstElementChild); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return { |
| 71 | get, |
| 72 | post, |
| 73 | ajax, |
| 74 | theme |
| 75 | }; |
| 76 | |
| 77 | })(); |
| 78 | |
| 79 | (() => { |
| 80 | const post_id = Number(wpp_params.post); |
| 81 | let do_request = true; |
| 82 | |
| 83 | if ( post_id ) { |
| 84 | if ( '1' == wpp_params.sampling ) { |
| 85 | const num = Math.floor(Math.random() * wpp_params.samplingRate) + 1; |
| 86 | do_request = ( 1 === num ); |
| 87 | } |
| 88 | |
| 89 | if ( 'boolean' === typeof window.wpp_do_request ) { |
| 90 | do_request = window.wpp_do_request; |
| 91 | } |
| 92 | |
| 93 | if ( do_request ) { |
| 94 | WordPressPopularPosts.post( |
| 95 | `${wpp_params.apiUrl}/v2/views/${post_id}`, |
| 96 | `sampling=${wpp_params.sampling}&sampling_rate=${wpp_params.samplingRate}`, |
| 97 | ( response ) => { |
| 98 | if ( wpp_params.debug && window.console ) { |
| 99 | console.log(JSON.parse(response)); |
| 100 | } |
| 101 | }, |
| 102 | { |
| 103 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 104 | 'X-WP-Nonce': wpp_params.token |
| 105 | } |
| 106 | ); |
| 107 | } |
| 108 | } |
| 109 | })(); |
| 110 | |
| 111 | document.addEventListener('DOMContentLoaded', () => { |
| 112 | const widget_placeholders = document.querySelectorAll('.wpp-widget-block-placeholder, .wpp-shortcode-placeholder'); |
| 113 | widget_placeholders.forEach((widget_placeholder) => fetchWidget(widget_placeholder)); |
| 114 | |
| 115 | const sr = document.querySelectorAll('.popular-posts-sr'); |
| 116 | sr.forEach((s) => WordPressPopularPosts.theme(s)); |
| 117 | |
| 118 | function fetchWidget(widget_placeholder) { |
| 119 | let params = ''; |
| 120 | const json_tag = widget_placeholder.parentNode.querySelector('script[type="application/json"]'); |
| 121 | |
| 122 | if ( json_tag ) { |
| 123 | const args = JSON.parse(json_tag.textContent.replace(/[\n\r]/g, '')); |
| 124 | params = JSON.stringify(args); |
| 125 | } |
| 126 | |
| 127 | WordPressPopularPosts.post( |
| 128 | `${wpp_params.apiUrl}/v2/widget?is_single=${wpp_params.postId}${wpp_params.lang ? `&lang=${wpp_params.lang}` : ''}`, |
| 129 | params, |
| 130 | response => renderWidget(response, widget_placeholder), |
| 131 | { |
| 132 | 'Content-Type': 'application/json', |
| 133 | 'X-WP-Nonce': wpp_params.token |
| 134 | } |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | function renderWidget(response, widget_placeholder) { |
| 139 | widget_placeholder.insertAdjacentHTML('afterend', JSON.parse(response).widget); |
| 140 | |
| 141 | const parent = widget_placeholder.parentNode, |
| 142 | sr = parent.querySelector('.popular-posts-sr'), |
| 143 | json_tag = parent.querySelector('script[type="application/json"]'); |
| 144 | |
| 145 | if ( json_tag ) { |
| 146 | parent.removeChild(json_tag); |
| 147 | } |
| 148 | |
| 149 | parent.removeChild(widget_placeholder); |
| 150 | parent.classList.add('wpp-ajax'); |
| 151 | |
| 152 | if ( sr ) { |
| 153 | WordPressPopularPosts.theme(sr); |
| 154 | } |
| 155 | |
| 156 | const event = new Event('wpp-onload', { bubbles: true, cancelable: false }); |
| 157 | parent.dispatchEvent(event); |
| 158 | } |
| 159 | }); |
| 160 |