PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.5.0
Post Views Counter v1.5.0
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / js / frontend.js
post-views-counter / js Last commit date
admin-dashboard.js 1 year ago admin-post.js 5 years ago admin-quick-edit.js 1 year ago admin-settings.js 1 year ago admin-widgets.js 5 years ago block-editor.min.js 1 year ago counter.js 1 year ago counter.min.js 1 year ago dummy.js 1 year ago frontend.js 1 year ago frontend.min.js 1 year ago
frontend.js
274 lines
1 var initPostViewsCounter = function() {
2 PostViewsCounter = {
3 promise: null,
4 args: {},
5
6 /**
7 * Initialize counter.
8 *
9 * @param {object} args
10 *
11 * @return {void}
12 */
13 init: function( args ) {
14 this.args = args;
15
16 // default parameters
17 let params = {};
18
19 // set cookie/storage name
20 let name = 'pvc_visits' + ( args.multisite !== false ? '_' + parseInt( args.multisite ) : '' );
21
22 // cookieless data storage?
23 if ( args.dataStorage === 'cookieless' && this.isLocalStorageAvailable() ) {
24 params.storage_type = 'cookieless';
25 params.storage_data = this.readStorageData( name );
26
27 /* COUNT_POST_AS_AUTHOR_VIEW | removed parameter from request
28 if ( 'countAuthor' in args && args.countAuthor === true ) {
29 params.storage_data_author = this.readStorageData( 'pvc_visits_user' + ( args.multisite !== false ? '_' + parseInt( args.multisite ) : '' ) );
30 }
31 */
32 } else {
33 params.storage_type = 'cookies';
34 params.storage_data = this.readCookieData( name );
35
36 /* COUNT_POST_AS_AUTHOR_VIEW | removed parameter from request
37 if ( 'countAuthor' in args && args.countAuthor === true ) {
38 params.storage_data_author = this.readCookieData( 'pvc_visits_user' + ( args.multisite !== false ? '_' + parseInt( args.multisite ) : '' ) );
39 }
40 */
41 }
42
43 // rest api request
44 if ( args.mode === 'rest_api' ) {
45 this.promise = this.request( args.requestURL, params, 'POST', {
46 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
47 'X-WP-Nonce': args.nonce
48 }, name );
49 // admin ajax request
50 } else {
51 params.action = 'pvc-check-post';
52 params.pvc_nonce = args.nonce;
53 params.id = args.postID;
54
55 this.promise = this.request( args.requestURL, params, 'POST', {
56 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
57 }, name );
58 }
59 },
60
61 /**
62 * Handle fetch request.
63 *
64 * @param {string} url
65 * @param {object} params
66 * @param {string} method
67 * @param {object} headers
68 * @param {string} name
69 *
70 * @return {object}
71 */
72 request: function( url, params, method, headers, name = '' ) {
73 let options = {
74 method: method,
75 mode: 'cors',
76 cache: 'no-cache',
77 credentials: 'same-origin',
78 headers: headers,
79 body: this.prepareRequestData( params )
80 };
81
82 const _this = this;
83
84 return fetch( url, options ).then( function( response ) {
85 // invalid response?
86 if ( ! response.ok )
87 throw Error( response.statusText );
88
89 return response.json();
90 } ).then( function( response ) {
91 try {
92 if ( typeof response === 'object' && response !== null ) {
93 if ( 'success' in response && response.success === false ) {
94 console.log( 'Request error' );
95 console.log( response.data );
96 } else {
97 if ( _this.args.dataStorage === 'cookieless' )
98 _this.saveStorageData.call( _this, name, response.storage, response.type );
99 else
100 _this.saveCookieData( name, response.storage );
101
102 _this.triggerEvent( 'pvcCheckPost', response );
103 }
104 } else {
105 console.log( 'Invalid object' );
106 console.log( response );
107 }
108 } catch( error ) {
109 console.log( 'Invalid JSON data' );
110 console.log( error );
111 }
112 } ).catch( function( error ) {
113 console.log( 'Invalid response' );
114 console.log( error );
115 } );
116 },
117
118 /**
119 * Prepare the data to be sent with the request.
120 *
121 * @param {object} data
122 *
123 * @return {string}
124 */
125 prepareRequestData: function( data ) {
126 return Object.keys( data ).map( function( el ) {
127 // add extra "data" array
128 return encodeURIComponent( el ) + '=' + encodeURIComponent( data[el] );
129 } ).join( '&' ).replace( /%20/g, '+' );
130 },
131
132 /**
133 * Prepare the data to be sent with the request.
134 *
135 * @param {string} eventName
136 * @param {object} data
137 *
138 * @return {void}
139 */
140 triggerEvent: function( eventName, data ) {
141 const newEvent = new CustomEvent( eventName, {
142 bubbles: true,
143 detail: data
144 } );
145
146 // trigger event
147 document.dispatchEvent( newEvent );
148 },
149
150 /**
151 * Save localStorage data.
152 *
153 * @param {string} name
154 * @param {object} data
155 * @param {string} type
156 *
157 * @return {void}
158 */
159 saveStorageData: function( name, data, type ) {
160 /* COUNT_POST_AS_AUTHOR_VIEW | removed setting localStorage user data
161 if ( 'countAuthor' in this.args && this.args.countAuthor === true && 'user' in data )
162 window.localStorage.setItem( 'pvc_visits_user' + ( this.args.multisite !== false ? '_' + parseInt( this.args.multisite ) : '' ), JSON.stringify( data['user'] ) );
163 */
164
165 window.localStorage.setItem( name, JSON.stringify( data[type] ) );
166 },
167
168 /**
169 * Read localStorage data.
170 *
171 * @param {string} name
172 *
173 * @return {string}
174 */
175 readStorageData: function( name ) {
176 let data = null;
177
178 // get data
179 data = window.localStorage.getItem( name );
180
181 // no data?
182 if ( data === null )
183 data = '';
184
185 return data;
186 },
187
188 /**
189 * Save cookies.
190 *
191 * @param {string} name
192 * @param {object} data
193 *
194 * @return {void}
195 */
196 saveCookieData: function( name, data ) {
197 var cookieSecure = '';
198
199 // ssl?
200 if ( document.location.protocol === 'https:' )
201 cookieSecure = ';secure';
202
203 if ( data.hasOwnProperty( 'name' ) === false ) {
204 console.log( 'Error saveCookieData' );
205 return;
206 }
207
208 for ( let i = 0; i < data.name.length; i++ ) {
209 var cookieDate = new Date();
210 var expiration = parseInt( data.expiry[i] );
211
212 // valid expiration timestamp?
213 if ( expiration )
214 expiration = expiration * 1000;
215 // add default 24 hours
216 else
217 expiration = cookieDate.getTime() + 86400000;
218
219 // set cookie date expiry
220 cookieDate.setTime( expiration );
221
222 // set cookie
223 document.cookie = data.name[i] + '=' + data.value[i] + ';expires=' + cookieDate.toUTCString() + ';path=/' + ( this.args.path === '/' ? '' : this.args.path ) + ';domain=' + this.args.domain + cookieSecure + ';SameSite=Lax';
224 }
225 },
226
227 /**
228 * Read cookies.
229 *
230 * @param {string} name
231 *
232 * @return {string}
233 */
234 readCookieData: function( name ) {
235 var cookies = [];
236
237 document.cookie.split( ';' ).forEach( function( el ) {
238 var [key, value] = el.split( '=' );
239 var trimmedKey = key.trim();
240 var regex = new RegExp( name + '\\[\\d+\\]' );
241
242 // look all cookies starts with name parameter
243 if ( regex.test( trimmedKey ) )
244 cookies.push( value );
245 } );
246
247 return cookies.join( 'a' );
248 },
249
250 /**
251 * Check whether localStorage is available.
252 *
253 * @return {bool}
254 */
255 isLocalStorageAvailable: function() {
256 var storage;
257
258 try {
259 storage = window['localStorage'];
260
261 storage.setItem( '__pvcStorageTest', 0 );
262 storage.removeItem( '__pvcStorageTest' );
263
264 return true;
265 } catch( e ) {
266 return e instanceof DOMException && ( e.code === 22 || e.code === 1014 || e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED' ) && ( storage && storage.length !== 0 );
267 }
268 }
269 }
270
271 PostViewsCounter.init( pvcArgsFrontend );
272 }
273
274 document.addEventListener( 'DOMContentLoaded', initPostViewsCounter );