PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.13
Post Views Counter v1.3.13
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 3 years ago admin-post.js 5 years ago admin-quick-edit.js 5 years ago admin-settings.js 4 years ago admin-widgets.js 5 years ago block-editor.min.js 3 years ago counter.js 3 years ago counter.min.js 3 years ago frontend.js 3 years ago frontend.min.js 3 years ago
frontend.js
107 lines
1 document.addEventListener( 'DOMContentLoaded', function() {
2 PostViewsCounter = {
3 promise: null,
4
5 /**
6 * Initialize counter.
7 *
8 * @param {object} args
9 *
10 * @return {void}
11 */
12 init: function( args ) {
13 // rest api request
14 if ( args.mode === 'rest_api' ) {
15 this.promise = this.request( args.requestURL, {}, 'POST', {
16 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
17 'X-WP-Nonce': args.nonce
18 } );
19 // admin ajax request
20 } else {
21 this.promise = this.request( args.requestURL, {
22 action: 'pvc-check-post',
23 pvc_nonce: args.nonce,
24 id: args.postID
25 }, 'POST', {
26 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
27 } );
28 }
29 },
30
31 /**
32 * Handle fetch request.
33 *
34 * @param {string} url
35 * @param {object} params
36 * @param {string} method
37 * @param {object} headers
38 *
39 * @return {object}
40 */
41 request: function( url, params, method, headers ) {
42 let options = {
43 method: method,
44 mode: 'cors',
45 cache: 'no-cache',
46 credentials: 'same-origin',
47 headers: headers,
48 body: this.prepareData( params )
49 };
50
51 const _this = this;
52
53 return fetch( url, options ).then( function( response ) {
54 // invalid response?
55 if ( ! response.ok )
56 throw Error( response.statusText );
57
58 return response.json();
59 } ).then( function( response ) {
60 try {
61 if ( typeof response === 'object' && response !== null )
62 _this.triggerEvent( 'pvcCheckPost', response );
63 } catch( error ) {
64 console.log( 'Invalid JSON data' );
65 console.log( error );
66 }
67 } ).catch( function( error ) {
68 console.log( 'Invalid response' );
69 console.log( error );
70 } );
71 },
72
73 /**
74 * Prepare the data to be sent with the request.
75 *
76 * @param {object} data
77 *
78 * @return {string}
79 */
80 prepareData: function( data ) {
81 return Object.keys( data ).map( function( el ) {
82 // add extra "data" array
83 return encodeURIComponent( el ) + '=' + encodeURIComponent( data[el] );
84 } ).join( '&' ).replace( /%20/g, '+' );
85 },
86
87 /**
88 * Prepare the data to be sent with the request.
89 *
90 * @param {string} eventName
91 * @param {object} data
92 *
93 * @return {void}
94 */
95 triggerEvent: function( eventName, data ) {
96 const newEvent = new CustomEvent( eventName, {
97 bubbles: true,
98 detail: data
99 } );
100
101 // trigger event
102 document.dispatchEvent( newEvent );
103 }
104 }
105
106 PostViewsCounter.init( pvcArgsFrontend );
107 } );