blocks
1 month ago
vendor
4 years ago
admin-notices.js
1 year ago
admin.js
1 month ago
chart.js
5 months ago
wpp.js
1 year ago
wpp.min.js
1 year ago
admin.js
407 lines
| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Stats screen |
| 5 | */ |
| 6 | const wppChartContainer = document.getElementById('wpp-chart'); |
| 7 | |
| 8 | if ( wppChartContainer && WPPChart.canRender() ) { |
| 9 | WPPChart.init('wpp-chart'); |
| 10 | |
| 11 | let updatingStats = false; |
| 12 | |
| 13 | const wppBtnStatsConfig = document.getElementById('wpp-stats-config-btn'); |
| 14 | const wppModals = document.querySelectorAll('.wpp-lightbox'); |
| 15 | const wppStatsModal = [...wppModals].filter((modal) => modal.id === 'wpp-stats-config')[0]; |
| 16 | const wppTimeRangeModal = [...wppModals].filter((modal) => modal.id === 'wpp-stats-range')[0]; |
| 17 | const wppTimeRangeOptions = document.getElementById('wpp-time-ranges'); |
| 18 | const wppTimeRangeUnit = document.getElementById('stats_range_time_unit'); |
| 19 | const wppTimeRangeQty = document.getElementById('stats_range_time_quantity'); |
| 20 | const wppListsContainer = document.getElementById('wpp-listing'); |
| 21 | const wppTabComponents = document.querySelectorAll('.wpp-tabs'); |
| 22 | |
| 23 | /** Event listeners */ |
| 24 | if ( wppBtnStatsConfig && wppStatsModal ) { |
| 25 | wppBtnStatsConfig.addEventListener('click', () => { |
| 26 | closeAllModals(); |
| 27 | wppStatsModal.style.display = 'block'; |
| 28 | wppStatsModal.querySelector('input[id="stats_type"]').focus(); |
| 29 | }); |
| 30 | |
| 31 | wppStatsModal.querySelector('.button-secondary').addEventListener('click', () => { |
| 32 | closeAllModals(); |
| 33 | wppBtnStatsConfig.focus(); |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | wppTimeRangeOptions.querySelectorAll('li button').forEach((timerRangeBtn) => { |
| 38 | timerRangeBtn.addEventListener('click', (e) => { |
| 39 | const me = e.target, |
| 40 | range = me.dataset.range; |
| 41 | |
| 42 | closeAllModals(); |
| 43 | |
| 44 | if ( ! updatingStats && 'custom' !== range ) { |
| 45 | updatingStats = true; |
| 46 | |
| 47 | wppTimeRangeModal.querySelector('#stats_range_start_date').readonly = true; |
| 48 | wppTimeRangeModal.querySelector('#stats_range_end_date').readonly = true; |
| 49 | |
| 50 | getChartData(range) |
| 51 | .then((json) => { |
| 52 | updatingStats = false; |
| 53 | |
| 54 | if ( json ) { |
| 55 | const data = json.data; |
| 56 | |
| 57 | resetTimeRangeBtns(); |
| 58 | me.parentNode.classList.add('current'); |
| 59 | me.classList.add('button-primary'); |
| 60 | |
| 61 | updateChart(data); |
| 62 | updateLists(); |
| 63 | } |
| 64 | }); |
| 65 | } else { |
| 66 | wppTimeRangeModal.style.display = 'block'; |
| 67 | wppTimeRangeModal.querySelector('.wpp-tabs-container button[aria-selected="true"]').focus(); |
| 68 | } |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | wppTimeRangeModal.querySelectorAll('.wpp-lightbox-tabs button').forEach((btn) => { |
| 73 | btn.addEventListener('click', (e) => { |
| 74 | const me = e.target; |
| 75 | |
| 76 | wppTimeRangeModal.querySelector('#stats_range_start_date').readonly = ( 'custom-time-range' === me.getAttribute('aria-controls') ); |
| 77 | wppTimeRangeModal.querySelector('#stats_range_end_date').readonly = ( 'custom-time-range' === me.getAttribute('aria-controls') ); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | if ( wppTabComponents.length ) { |
| 82 | wppTabComponents.forEach((tabComponent) => { |
| 83 | tabComponent.querySelectorAll('.wpp-tabs-container button').forEach((btn) => { |
| 84 | btn.addEventListener('click', (e) => { |
| 85 | const me = e.target, |
| 86 | panelID = me.getAttribute('aria-controls'); |
| 87 | |
| 88 | me.setAttribute('aria-selected', 'true'); |
| 89 | me.setAttribute('tabindex', '0'); |
| 90 | me.closest('.wpp-tabs').querySelector(`#${panelID}`).classList.add('active'); |
| 91 | |
| 92 | [...me.parentNode.children] |
| 93 | .filter((child) => child !== me) |
| 94 | .forEach((sibling) => { |
| 95 | sibling.setAttribute('aria-selected', 'false'); |
| 96 | sibling.setAttribute('tabindex', '-1'); |
| 97 | me.closest('.wpp-tabs').querySelector(`#${sibling.getAttribute('aria-controls')}`).classList.remove('active'); |
| 98 | }); |
| 99 | }); |
| 100 | }); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | wppTimeRangeModal.querySelector('#stats_range_start_date').addEventListener('change', (e) => { |
| 105 | if ( e.target.value ) { |
| 106 | wppTimeRangeModal.querySelector('#stats_range_end_date').setAttribute('min', e.target.value); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | wppTimeRangeModal.querySelector('form').addEventListener('submit', (e) => { |
| 111 | e.preventDefault(); |
| 112 | |
| 113 | const submitBtn = wppTimeRangeModal.querySelector('form .button-primary'); |
| 114 | |
| 115 | submitBtn.disabled = true; |
| 116 | |
| 117 | getChartData('custom') |
| 118 | .then((json) => { |
| 119 | submitBtn.disabled = false; |
| 120 | |
| 121 | if ( json ) { |
| 122 | const data = json.data; |
| 123 | |
| 124 | closeAllModals(); |
| 125 | |
| 126 | resetTimeRangeBtns(); |
| 127 | |
| 128 | const customTimeRangeBtn = wppTimeRangeOptions.querySelector('li button[data-range="custom"]'); |
| 129 | customTimeRangeBtn.parentNode.classList.add('current'); |
| 130 | customTimeRangeBtn.classList.remove('button-secondary'); |
| 131 | customTimeRangeBtn.classList.add('button-primary'); |
| 132 | customTimeRangeBtn.focus(); |
| 133 | |
| 134 | updateChart(data); |
| 135 | updateLists(); |
| 136 | } |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | wppTimeRangeModal.querySelector('form .button-secondary').addEventListener('click', () => { |
| 141 | closeAllModals(); |
| 142 | wppTimeRangeOptions.querySelector('li.current button').focus(); |
| 143 | }); |
| 144 | |
| 145 | /** Functions */ |
| 146 | const resetTimeRangeBtns = () => { |
| 147 | wppTimeRangeOptions.querySelectorAll('li button').forEach((timerRangeBtn) => { |
| 148 | timerRangeBtn.parentNode.classList.remove('current'); |
| 149 | timerRangeBtn.classList.remove('button-primary'); |
| 150 | timerRangeBtn.classList.add('button-secondary'); |
| 151 | }); |
| 152 | }; |
| 153 | |
| 154 | const closeAllModals = () => { |
| 155 | wppModals.forEach((modal) => { modal.style.display = 'none'; }); |
| 156 | }; |
| 157 | |
| 158 | const getChartData = async (range) => { |
| 159 | const args = { |
| 160 | action: 'wpp_update_chart', |
| 161 | nonce: wpp_admin_params.nonce, |
| 162 | range: range, |
| 163 | time_quantity: wppTimeRangeQty.value, |
| 164 | time_unit: wppTimeRangeUnit.value |
| 165 | }; |
| 166 | |
| 167 | if ( |
| 168 | 'custom' === range |
| 169 | && ! wppTimeRangeModal.querySelector('#stats_range_start_date').readonly |
| 170 | && ! wppTimeRangeModal.querySelector('#stats_range_end_date').readonly |
| 171 | && wppTimeRangeModal.querySelector('#stats_range_start_date').value |
| 172 | && wppTimeRangeModal.querySelector('#stats_range_end_date').value |
| 173 | ) { |
| 174 | args.start_date = wppTimeRangeModal.querySelector('#stats_range_start_date').value; |
| 175 | args.end_date = wppTimeRangeModal.querySelector('#stats_range_end_date').value; |
| 176 | } |
| 177 | |
| 178 | const url = ajaxurl + '?' + new URLSearchParams(args).toString(); |
| 179 | |
| 180 | try { |
| 181 | const response = await fetch(url); |
| 182 | |
| 183 | if ( ! response.ok ) { |
| 184 | throw new Error(`Response status: ${response.status}`); |
| 185 | } |
| 186 | |
| 187 | const json = await response.json(); |
| 188 | |
| 189 | return json; |
| 190 | } catch (error) { |
| 191 | console.error(error.message); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | const updateChart = (data) => { |
| 196 | // Update titles |
| 197 | wppChartContainer.parentNode.querySelector('p:nth-of-type(1)').innerHTML = data.totals.label_summary; |
| 198 | wppChartContainer.parentNode.querySelector('p:nth-of-type(2)').innerHTML = data.totals.label_date_range; |
| 199 | // Update chart |
| 200 | WPPChart.populate(data); |
| 201 | }; |
| 202 | |
| 203 | const updateList = (listIndex, action, items) => { |
| 204 | const args = { |
| 205 | action: action, |
| 206 | nonce: wpp_admin_params.nonce, |
| 207 | items: items |
| 208 | }; |
| 209 | |
| 210 | if ( |
| 211 | ! wppTimeRangeModal.querySelector('#stats_range_start_date').readonly |
| 212 | && ! wppTimeRangeModal.querySelector('#stats_range_end_date').readonly |
| 213 | && wppTimeRangeModal.querySelector('#stats_range_start_date').value |
| 214 | && wppTimeRangeModal.querySelector('#stats_range_end_date').value |
| 215 | ) { |
| 216 | args.start_date = wppTimeRangeModal.querySelector('#stats_range_start_date').value; |
| 217 | args.end_date = wppTimeRangeModal.querySelector('#stats_range_end_date').value; |
| 218 | } |
| 219 | |
| 220 | const url = ajaxurl + '?' + new URLSearchParams(args).toString(); |
| 221 | |
| 222 | wppListsContainer.querySelector(`.wpp-tabs-panel:nth-of-type(${listIndex + 1})`).innerHTML = '<span class="spinner"></span>'; |
| 223 | |
| 224 | fetch(url) |
| 225 | .then((response) => { |
| 226 | return response.text(); |
| 227 | }) |
| 228 | .then((response) => { |
| 229 | wppListsContainer.querySelector(`.wpp-tabs-panel:nth-of-type(${listIndex + 1})`).innerHTML = response; |
| 230 | }) |
| 231 | .catch(() => { |
| 232 | // handle the error |
| 233 | wppListsContainer.querySelector(`.wpp-tabs-panel:nth-of-type(${listIndex + 1})`).innerHTML = '<p>Error: Could not fetch data.</p>'; |
| 234 | }); |
| 235 | }; |
| 236 | |
| 237 | const updateLists = () => { |
| 238 | updateList(1, 'wpp_get_most_viewed', 'most-viewed'); |
| 239 | updateList(2, 'wpp_get_most_commented', 'most-commented'); |
| 240 | updateList(3, 'wpp_get_trending', 'trending'); |
| 241 | }; |
| 242 | |
| 243 | /** Calls / Triggers */ |
| 244 | // Load chart and lists for the current time range |
| 245 | wppTimeRangeOptions.querySelector('li.current button').click(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tools screen |
| 250 | */ |
| 251 | const wppThumbnailSrc = document.getElementById('thumb_source'); |
| 252 | |
| 253 | if ( wppThumbnailSrc ) { |
| 254 | const wppThumbnailCustomFieldNameRow = document.getElementById('row_custom_field'); |
| 255 | const wppThumbnailCustomFieldImgResizeRow = document.getElementById('row_custom_field_resize'); |
| 256 | const wppThumbnailUploadBtn = document.getElementById('upload_thumb_button'); |
| 257 | const wppThumbnailUploadSrc = document.getElementById('upload_thumb_src'); |
| 258 | const wppThumbnailReview = document.getElementById('thumb-review'); |
| 259 | const wppThumbnailResetBtn = document.getElementById('reset_thumb_button'); |
| 260 | const wppThumbnailCacheDeleteBtn = document.getElementById('wpp-reset-image-cache'); |
| 261 | const wppLogLimitDropdown = document.getElementById('log_limit'); |
| 262 | const wppEnableDataCacheDropdown = document.getElementById('cache'); |
| 263 | const wppCacheOptionsRow = document.getElementById('cache_refresh_interval'); |
| 264 | const wppDataSampling = document.getElementById('sampling'); |
| 265 | const wppSamplingRateRow = document.getElementById('sampling_rate'); |
| 266 | const wppAdminListViewsColumn = document.getElementById('views_column'); |
| 267 | const wppAdminListViewsColumnForPostTypesRow = document.getElementById('views_for_post_types'); |
| 268 | |
| 269 | /** Event listeners */ |
| 270 | wppThumbnailSrc.addEventListener('change', (e) => { |
| 271 | wppThumbnailCustomFieldNameRow.style.display = ( 'custom_field' === e.target.value ) ? 'table-row' : 'none'; |
| 272 | wppThumbnailCustomFieldImgResizeRow.style.display = ( 'custom_field' === e.target.value ) ? 'table-row' : 'none'; |
| 273 | }); |
| 274 | |
| 275 | wppThumbnailUploadBtn.addEventListener('click', () => { |
| 276 | const custom_uploader = wp.media({ |
| 277 | title: 'WP Popular Posts', |
| 278 | library: { type: 'image' }, |
| 279 | button: { text: wpp_admin_params.label_media_upload_button }, |
| 280 | id: 'library-' + (Math.floor(Math.random() * 10) + 1), |
| 281 | multiple: false |
| 282 | }).on('select', () => { |
| 283 | const attachment = custom_uploader.state().get('selection').first().toJSON(); |
| 284 | wppThumbnailUploadSrc.value = attachment.url; |
| 285 | |
| 286 | const img = new Image(); |
| 287 | img.onload = function() { |
| 288 | wppThumbnailReview.innerHTML = ''; |
| 289 | wppThumbnailReview.append(this); |
| 290 | } |
| 291 | img.src = attachment.url; |
| 292 | }) |
| 293 | .open(); |
| 294 | }); |
| 295 | |
| 296 | wppThumbnailResetBtn.addEventListener('click', () => resetDefaultThumbnail()); |
| 297 | |
| 298 | wppThumbnailCacheDeleteBtn.addEventListener('click', () => clearThumbnailsCache()); |
| 299 | |
| 300 | wppLogLimitDropdown.addEventListener('change', (e) => { |
| 301 | const me = e.target, |
| 302 | siblings = me.parentNode.querySelectorAll('label, .description'), |
| 303 | brTags = me.parentNode.querySelectorAll('br'); |
| 304 | |
| 305 | siblings.forEach((elem) => { |
| 306 | elem.style.display = ( 1 == me.value ) ? ( elem.classList.contains('description') ? 'block' : 'inline-block') : 'none'; |
| 307 | }); |
| 308 | brTags.forEach((elem) => { |
| 309 | elem.style.display = ( 1 == me.value ) ? 'none' : 'block'; |
| 310 | }); |
| 311 | }); |
| 312 | |
| 313 | wppEnableDataCacheDropdown.addEventListener('change', (e) => { |
| 314 | wppCacheOptionsRow.querySelector('#cache_too_long').style.display = 'none'; |
| 315 | wppCacheOptionsRow.style.display = ( 1 == e.target.value ) ? 'table-row' : 'none'; |
| 316 | }); |
| 317 | |
| 318 | wppCacheOptionsRow.querySelector('#cache_interval_value').addEventListener('input', (e) => { |
| 319 | if ( isNaN(e.target.value) ) { |
| 320 | e.target.value = 1; |
| 321 | } |
| 322 | |
| 323 | if ( e.target.reportValidity() ) { |
| 324 | isCachingDataForTooLong(); |
| 325 | } |
| 326 | }); |
| 327 | |
| 328 | wppCacheOptionsRow.querySelector('#cache_interval_time').addEventListener('change', () => isCachingDataForTooLong()); |
| 329 | |
| 330 | wppDataSampling.addEventListener('change', (e) => { |
| 331 | wppSamplingRateRow.style.display = ( '1' === e.target.value ) ? 'table-row' : 'none'; |
| 332 | }); |
| 333 | |
| 334 | wppAdminListViewsColumn.addEventListener('change', (e) => { |
| 335 | wppAdminListViewsColumnForPostTypesRow.style.display = ( '1' === e.target.value ) ? 'table-row' : 'none'; |
| 336 | }); |
| 337 | |
| 338 | /** Functions */ |
| 339 | const resetDefaultThumbnail = () => { |
| 340 | fetch(ajaxurl + '?action=wpp_reset_thumbnail') |
| 341 | .then((response) => { |
| 342 | return response.text(); |
| 343 | }) |
| 344 | .then((thumb_url) => { |
| 345 | const img = new Image(); |
| 346 | img.onload = function() { |
| 347 | wppThumbnailReview.innerHTML = ''; |
| 348 | wppThumbnailReview.append(this); |
| 349 | } |
| 350 | img.src = thumb_url; |
| 351 | }) |
| 352 | .catch(() => { |
| 353 | // handle the error |
| 354 | }); |
| 355 | }; |
| 356 | |
| 357 | const clearThumbnailsCache = () => { |
| 358 | if ( confirm(wpp_admin_params.text_confirm_image_cache_reset + " \n\n" + wpp_admin_params.text_continue) ) { |
| 359 | fetch(ajaxurl, { |
| 360 | method: 'POST', |
| 361 | body: new URLSearchParams({ |
| 362 | action: 'wpp_clear_thumbnail', |
| 363 | token: wpp_admin_params.nonce_reset_thumbnails |
| 364 | }) |
| 365 | }) |
| 366 | .then((response) => response.text()) |
| 367 | .then((response) => { |
| 368 | let msg = ''; |
| 369 | |
| 370 | switch(response) { |
| 371 | case '1': |
| 372 | msg = wpp_admin_params.text_image_cache_cleared; |
| 373 | break; |
| 374 | case '2': |
| 375 | msg = wpp_admin_params.text_image_cache_already_empty; |
| 376 | break; |
| 377 | case '4': |
| 378 | msg = wpp_admin_params.text_insufficient_permissions; |
| 379 | break; |
| 380 | default: |
| 381 | msg = wpp_admin_params.text_invalid_action; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | alert(msg); |
| 386 | }); |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | const isCachingDataForTooLong = () => { |
| 391 | const cacheInternalValue = parseInt(wppCacheOptionsRow.querySelector('#cache_interval_value').value, 10); |
| 392 | const cacheIntervalTimeUnit = wppCacheOptionsRow.querySelector('#cache_interval_time').value; |
| 393 | |
| 394 | if ( |
| 395 | ('hour' === cacheIntervalTimeUnit && cacheInternalValue > 72 ) |
| 396 | || ('day' === cacheIntervalTimeUnit && cacheInternalValue > 3 ) |
| 397 | || ('week' === cacheIntervalTimeUnit && cacheInternalValue > 1 ) |
| 398 | || ('month' === cacheIntervalTimeUnit && cacheInternalValue >= 1 ) |
| 399 | || ('year' === cacheIntervalTimeUnit && cacheInternalValue >= 1 ) |
| 400 | ) { |
| 401 | wppCacheOptionsRow.querySelector('#cache_too_long').style.display = 'block'; |
| 402 | } else { |
| 403 | wppCacheOptionsRow.querySelector('#cache_too_long').style.display = 'none'; |
| 404 | } |
| 405 | }; |
| 406 | } |
| 407 |