PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.0.1
WP Popular Posts v7.0.1
7.4.1 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / assets / js / wpp.js
wordpress-popular-posts / assets / js Last commit date
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