| 1 |
document.addEventListener( 'DOMContentLoaded', function () { |
| 2 |
const validTemplates = [ |
| 3 |
'single-lp_course', |
| 4 |
'archive-lp_course', |
| 5 |
'taxonomy-course_tag', |
| 6 |
'taxonomy-course_category', |
| 7 |
]; |
| 8 |
|
| 9 |
const getParamP = () => { |
| 10 |
const urlParams = new URLSearchParams( window.location.search ); |
| 11 |
const paramP = urlParams.get( 'p' )?.replace( /^\/|\/$/g, '' ); |
| 12 |
return paramP === 'template' ? paramP : null; |
| 13 |
}; |
| 14 |
|
| 15 |
const debounce = ( func, wait ) => { |
| 16 |
let timeout; |
| 17 |
return ( ...args ) => { |
| 18 |
clearTimeout( timeout ); |
| 19 |
timeout = setTimeout( () => func( ...args ), wait ); |
| 20 |
}; |
| 21 |
}; |
| 22 |
|
| 23 |
let previousTemplate = null; |
| 24 |
const checkAndReload = () => { |
| 25 |
const currentTemplate = |
| 26 |
wp?.data?.select( 'core/editor' )?.getEditedPostAttribute( 'slug' ) || |
| 27 |
wp?.data?.select( 'core/editor' )?.getCurrentPostId() || |
| 28 |
getParamP(); |
| 29 |
|
| 30 |
if ( ! currentTemplate || currentTemplate === previousTemplate ) return; |
| 31 |
|
| 32 |
if ( |
| 33 |
currentTemplate !== 'home' && |
| 34 |
previousTemplate && |
| 35 |
previousTemplate !== currentTemplate && |
| 36 |
( validTemplates.includes( currentTemplate ) || validTemplates.includes( previousTemplate ) ) |
| 37 |
) { |
| 38 |
window.location.reload(); |
| 39 |
} |
| 40 |
|
| 41 |
previousTemplate = currentTemplate; |
| 42 |
}; |
| 43 |
|
| 44 |
const debouncedCheckAndReload = debounce( checkAndReload, 200 ); |
| 45 |
|
| 46 |
wp?.data?.subscribe( () => { |
| 47 |
debouncedCheckAndReload(); |
| 48 |
} ); |
| 49 |
} ); |
| 50 |
|