PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.4.1
Post Views Counter v1.4.1
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 2 years ago admin-post.js 5 years ago admin-quick-edit.js 5 years ago admin-settings.js 2 years ago admin-widgets.js 5 years ago block-editor.min.js 2 years ago counter.js 2 years ago counter.min.js 2 years ago frontend.js 2 years ago frontend.min.js 2 years ago
frontend.js
261 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 ( _this.args.dataStorage === 'cookieless' )
94 _this.saveStorageData.call( _this, name, response.storage, response.type );
95 else
96 _this.saveCookieData( name, response.storage );
97
98 _this.triggerEvent( 'pvcCheckPost', response );
99 }
100 } catch( error ) {
101 console.log( 'Invalid JSON data' );
102 console.log( error );
103 }
104 } ).catch( function( error ) {
105 console.log( 'Invalid response' );
106 console.log( error );
107 } );
108 },
109
110 /**
111 * Prepare the data to be sent with the request.
112 *
113 * @param {object} data
114 *
115 * @return {string}
116 */
117 prepareRequestData: function( data ) {
118 return Object.keys( data ).map( function( el ) {
119 // add extra "data" array
120 return encodeURIComponent( el ) + '=' + encodeURIComponent( data[el] );
121 } ).join( '&' ).replace( /%20/g, '+' );
122 },
123
124 /**
125 * Prepare the data to be sent with the request.
126 *
127 * @param {string} eventName
128 * @param {object} data
129 *
130 * @return {void}
131 */
132 triggerEvent: function( eventName, data ) {
133 const newEvent = new CustomEvent( eventName, {
134 bubbles: true,
135 detail: data
136 } );
137
138 // trigger event
139 document.dispatchEvent( newEvent );
140 },
141
142 /**
143 * Save localStorage data.
144 *
145 * @param {string} name
146 * @param {object} data
147 * @param {string} type
148 *
149 * @return {void}
150 */
151 saveStorageData: function( name, data, type ) {
152 /* COUNT_POST_AS_AUTHOR_VIEW | removed setting localStorage user data
153 if ( 'countAuthor' in this.args && this.args.countAuthor === true && 'user' in data )
154 window.localStorage.setItem( 'pvc_visits_user' + ( this.args.multisite !== false ? '_' + parseInt( this.args.multisite ) : '' ), JSON.stringify( data['user'] ) );
155 */
156
157 window.localStorage.setItem( name, JSON.stringify( data[type] ) );
158 },
159
160 /**
161 * Read localStorage data.
162 *
163 * @param {string} name
164 *
165 * @return {string}
166 */
167 readStorageData: function( name ) {
168 let data = null;
169
170 // get data
171 data = window.localStorage.getItem( name );
172
173 // no data?
174 if ( data === null )
175 data = '';
176
177 return data;
178 },
179
180 /**
181 * Save cookies.
182 *
183 * @param {string} name
184 * @param {object} data
185 *
186 * @return {void}
187 */
188 saveCookieData: function( name, data ) {
189 var cookieSecure = '';
190
191 // ssl?
192 if ( document.location.protocol === 'https:' )
193 cookieSecure = ';secure';
194
195 for ( let i = 0; i < data.name.length; i++ ) {
196 var cookieDate = new Date();
197 var expiration = parseInt( data.expiry[i] );
198
199 // valid expiration timestamp?
200 if ( expiration )
201 expiration = expiration * 1000;
202 // add default 24 hours
203 else
204 expiration = cookieDate.getTime() + 86400000;
205
206 // set cookie date expiry
207 cookieDate.setTime( expiration );
208
209 // set cookie
210 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';
211 }
212 },
213
214 /**
215 * Read cookies.
216 *
217 * @param {string} name
218 *
219 * @return {string}
220 */
221 readCookieData: function( name ) {
222 var cookies = [];
223
224 document.cookie.split( ';' ).forEach( function( el ) {
225 var [key, value] = el.split( '=' );
226 var trimmedKey = key.trim();
227 var regex = new RegExp( name + '\\[\\d+\\]' );
228
229 // look all cookies starts with name parameter
230 if ( regex.test( trimmedKey ) )
231 cookies.push( value );
232 } );
233
234 return cookies.join( 'a' );
235 },
236
237 /**
238 * Check whether localStorage is available.
239 *
240 * @return {bool}
241 */
242 isLocalStorageAvailable: function() {
243 var storage;
244
245 try {
246 storage = window['localStorage'];
247
248 storage.setItem( '__pvcStorageTest', 0 );
249 storage.removeItem( '__pvcStorageTest' );
250
251 return true;
252 } catch( e ) {
253 return e instanceof DOMException && ( e.code === 22 || e.code === 1014 || e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED' ) && ( storage && storage.length !== 0 );
254 }
255 }
256 }
257
258 PostViewsCounter.init( pvcArgsFrontend );
259 }
260
261 document.addEventListener( 'DOMContentLoaded', initPostViewsCounter );