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
counter.js
69 lines
| 1 | PostViewsCounterManual = { |
| 2 | /** |
| 3 | * Initialize counter. |
| 4 | * |
| 5 | * @param {object} args |
| 6 | * |
| 7 | * @return {void} |
| 8 | */ |
| 9 | init: function( args ) { |
| 10 | let params = { |
| 11 | action: 'pvc-view-posts', |
| 12 | pvc_nonce: args.nonce, |
| 13 | ids: args.ids |
| 14 | }; |
| 15 | |
| 16 | let newParams = Object.keys( params ).map( function( el ) { |
| 17 | // add extra "data" array |
| 18 | return encodeURIComponent( el ) + '=' + encodeURIComponent( params[el] ); |
| 19 | } ).join( '&' ).replace( /%20/g, '+' ); |
| 20 | |
| 21 | const _this = this; |
| 22 | |
| 23 | return fetch( args.url, { |
| 24 | method: 'POST', |
| 25 | mode: 'cors', |
| 26 | cache: 'no-cache', |
| 27 | credentials: 'same-origin', |
| 28 | headers: { |
| 29 | 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' |
| 30 | }, |
| 31 | body: newParams |
| 32 | } ).then( function( response ) { |
| 33 | // invalid response? |
| 34 | if ( ! response.ok ) |
| 35 | throw Error( response.statusText ); |
| 36 | |
| 37 | return response.json(); |
| 38 | } ).then( function( response ) { |
| 39 | try { |
| 40 | if ( typeof response === 'object' && response !== null ) |
| 41 | _this.triggerEvent( 'pvcCheckPost', response ); |
| 42 | } catch( error ) { |
| 43 | console.log( 'Invalid JSON data' ); |
| 44 | console.log( error ); |
| 45 | } |
| 46 | } ).catch( function( error ) { |
| 47 | console.log( 'Invalid response' ); |
| 48 | console.log( error ); |
| 49 | } ); |
| 50 | }, |
| 51 | |
| 52 | /** |
| 53 | * Prepare the data to be sent with the request. |
| 54 | * |
| 55 | * @param {string} eventName |
| 56 | * @param {object} data |
| 57 | * |
| 58 | * @return {void} |
| 59 | */ |
| 60 | triggerEvent: function( eventName, data ) { |
| 61 | const newEvent = new CustomEvent( eventName, { |
| 62 | bubbles: true, |
| 63 | detail: data |
| 64 | } ); |
| 65 | |
| 66 | // trigger event |
| 67 | document.dispatchEvent( newEvent ); |
| 68 | } |
| 69 | } |