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 | } ); |