admin-dashboard.js
7 months ago
admin-post.js
7 months ago
admin-quick-edit.js
7 months ago
admin-settings.js
7 months ago
admin-widgets.js
7 months ago
block-editor.min.js
7 months ago
column-modal.js
7 months ago
counter.js
7 months ago
counter.min.js
7 months ago
dummy.js
7 months ago
frontend.js
7 months ago
frontend.min.js
7 months ago
frontend.js
186 lines
| 1 | var initPostViewsCounter = function() { |
| 2 | PostViewsCounter = { |
| 3 | promise: null, |
| 4 | args: {}, |
| 5 | |
| 6 | /** |
| 7 | * Initialize counter. |
| 8 | * |
| 9 | * @param {Object} args |
| 10 | * @return {void} |
| 11 | */ |
| 12 | init: function( args ) { |
| 13 | this.args = args; |
| 14 | |
| 15 | // default parameters |
| 16 | var params = {}; |
| 17 | |
| 18 | // data storage |
| 19 | params.storage_type = 'cookies'; |
| 20 | params.storage_data = this.readCookieData( 'pvc_visits' + ( args.multisite !== false ? '_' + parseInt( args.multisite ) : '' ) ); |
| 21 | |
| 22 | // rest api request |
| 23 | if ( args.mode === 'rest_api' ) { |
| 24 | this.promise = this.request( args.requestURL, params, 'POST', { |
| 25 | 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', |
| 26 | 'X-WP-Nonce': args.nonce |
| 27 | } ); |
| 28 | // ajax request |
| 29 | } else { |
| 30 | params.action = 'pvc-check-post'; |
| 31 | params.pvc_nonce = args.nonce; |
| 32 | params.id = args.postID; |
| 33 | |
| 34 | this.promise = this.request( args.requestURL, params, 'POST', { |
| 35 | 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' |
| 36 | } ); |
| 37 | } |
| 38 | }, |
| 39 | |
| 40 | /** |
| 41 | * Handle fetch request. |
| 42 | * |
| 43 | * @param {string} url |
| 44 | * @param {Object} params |
| 45 | * @param {string} method |
| 46 | * @param {Object} headers |
| 47 | * @return {Promise} |
| 48 | */ |
| 49 | request: function( url, params, method, headers ) { |
| 50 | var options = { |
| 51 | method: method, |
| 52 | mode: 'cors', |
| 53 | cache: 'no-cache', |
| 54 | credentials: 'same-origin', |
| 55 | headers: headers, |
| 56 | body: this.prepareRequestData( params ) |
| 57 | }; |
| 58 | |
| 59 | var _this = this; |
| 60 | |
| 61 | return fetch( url, options ).then( function( response ) { |
| 62 | // invalid response? |
| 63 | if ( ! response.ok ) |
| 64 | throw Error( response.statusText ); |
| 65 | |
| 66 | return response.json(); |
| 67 | } ).then( function( response ) { |
| 68 | try { |
| 69 | if ( typeof response === 'object' && response !== null ) { |
| 70 | if ( 'success' in response && response.success === false ) { |
| 71 | console.log( 'PVC: Request error.' ); |
| 72 | console.log( response.data ); |
| 73 | } else { |
| 74 | _this.saveCookieData( response.storage ); |
| 75 | |
| 76 | _this.triggerEvent( 'pvcCheckPost', response ); |
| 77 | } |
| 78 | } else { |
| 79 | console.log( 'PVC: Invalid object.' ); |
| 80 | console.log( response ); |
| 81 | } |
| 82 | } catch( error ) { |
| 83 | console.log( 'PVC: Invalid JSON data.' ); |
| 84 | console.log( error ); |
| 85 | } |
| 86 | } ).catch( function( error ) { |
| 87 | console.log( 'PVC: Invalid response.' ); |
| 88 | console.log( error ); |
| 89 | } ); |
| 90 | }, |
| 91 | |
| 92 | /** |
| 93 | * Prepare the data to be sent with the request. |
| 94 | * |
| 95 | * @param {Object} data |
| 96 | * @return {string} |
| 97 | */ |
| 98 | prepareRequestData: function( data ) { |
| 99 | return Object.keys( data ).map( function( el ) { |
| 100 | // add extra "data" array |
| 101 | return encodeURIComponent( el ) + '=' + encodeURIComponent( data[el] ); |
| 102 | } ).join( '&' ).replace( /%20/g, '+' ); |
| 103 | }, |
| 104 | |
| 105 | /** |
| 106 | * Trigger a custom DOM event. |
| 107 | * |
| 108 | * @param {string} eventName |
| 109 | * @param {Object} data |
| 110 | * @return {void} |
| 111 | */ |
| 112 | triggerEvent: function( eventName, data ) { |
| 113 | var newEvent = new CustomEvent( eventName, { |
| 114 | bubbles: true, |
| 115 | detail: data |
| 116 | } ); |
| 117 | |
| 118 | // trigger event |
| 119 | document.dispatchEvent( newEvent ); |
| 120 | }, |
| 121 | |
| 122 | /** |
| 123 | * Save cookies. |
| 124 | * |
| 125 | * @param {Object} data |
| 126 | * @return {void} |
| 127 | */ |
| 128 | saveCookieData: function( data ) { |
| 129 | // empty storage? nothing to save |
| 130 | if ( ! data.hasOwnProperty( 'name' ) ) |
| 131 | return; |
| 132 | |
| 133 | var cookieSecure = ''; |
| 134 | |
| 135 | // ssl? |
| 136 | if ( document.location.protocol === 'https:' ) |
| 137 | cookieSecure = ';secure'; |
| 138 | |
| 139 | for ( var i = 0; i < data.name.length; i++ ) { |
| 140 | var cookieDate = new Date(); |
| 141 | var expiration = parseInt( data.expiry[i] ); |
| 142 | |
| 143 | // valid expiration timestamp? |
| 144 | if ( expiration ) |
| 145 | expiration = expiration * 1000; |
| 146 | // add default 24 hours |
| 147 | else |
| 148 | expiration = cookieDate.getTime() + 86400000; |
| 149 | |
| 150 | // set cookie date expiry |
| 151 | cookieDate.setTime( expiration ); |
| 152 | |
| 153 | // set cookie |
| 154 | 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'; |
| 155 | } |
| 156 | }, |
| 157 | |
| 158 | /** |
| 159 | * Read cookies. |
| 160 | * |
| 161 | * @param {string} name |
| 162 | * @return {string} |
| 163 | */ |
| 164 | readCookieData: function( name ) { |
| 165 | var cookies = []; |
| 166 | |
| 167 | document.cookie.split( ';' ).forEach( function( el ) { |
| 168 | var parts = el.split( '=' ); |
| 169 | var key = parts[0]; |
| 170 | var value = parts[1]; |
| 171 | var trimmedKey = key.trim(); |
| 172 | var regex = new RegExp( name + '\\[\\d+\\]' ); |
| 173 | |
| 174 | // look all cookies starts with name parameter |
| 175 | if ( regex.test( trimmedKey ) ) |
| 176 | cookies.push( value ); |
| 177 | } ); |
| 178 | |
| 179 | return cookies.join( 'a' ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | PostViewsCounter.init( pvcArgsFrontend ); |
| 184 | } |
| 185 | |
| 186 | document.addEventListener( 'DOMContentLoaded', initPostViewsCounter ); |