| @@ -1,113 +1,113 @@ | ||
| 1 | -/** | |
| 2 | - * Script handle admin notices. | |
| 3 | - * | |
| 4 | - * @since 4.1.7.3.2 | |
| 5 | - * @version 1.0.2 | |
| 6 | - */ | |
| 7 | -import { lpOnElementReady } from '../utils.js'; | |
| 8 | -import API from '../api.js'; | |
| 9 | -let elLPAdminNotices = null; | |
| 10 | -let dataHtml = null; | |
| 11 | -const queryString = window.location.search; | |
| 12 | -const urlParams = new URLSearchParams( queryString ); | |
| 13 | -const tab = urlParams.get( 'tab' ); | |
| 14 | - | |
| 15 | -const notifyAddonsNewVersion = () => { | |
| 16 | - try { | |
| 17 | - const elAdminMenu = document.querySelector( '#adminmenu' ); | |
| 18 | - if ( ! elAdminMenu ) { | |
| 19 | - return; | |
| 20 | - } | |
| 21 | - | |
| 22 | - const elTabLP = elAdminMenu.querySelector( '#toplevel_page_learn_press' ); | |
| 23 | - if ( ! elTabLP ) { | |
| 24 | - return; | |
| 25 | - } | |
| 26 | - const elTabLPName = elTabLP.querySelector( '.wp-menu-name' ); | |
| 27 | - if ( ! elTabLPName ) { | |
| 28 | - return; | |
| 29 | - } | |
| 30 | - const elAddonsNewVerTotal = document.querySelector( 'input[name=lp-addons-new-version-totals]' ); | |
| 31 | - if ( ! elAddonsNewVerTotal ) { | |
| 32 | - return; | |
| 33 | - } | |
| 34 | - const htmlNotifyLP = `<span class="tab-lp-admin-notice"></span>`; | |
| 35 | - elTabLPName.insertAdjacentHTML( 'beforeend', htmlNotifyLP ); | |
| 36 | - const elTabLPAddons = elTabLP.querySelector( 'a[href="admin.php?page=learn-press-addons"]' ); | |
| 37 | - if ( ! elTabLPAddons ) { | |
| 38 | - return; | |
| 39 | - } | |
| 40 | - | |
| 41 | - const total = elAddonsNewVerTotal.value; | |
| 42 | - const html = `<span style="margin-left: 5px" class="update-plugins">${ total }</span>`; | |
| 43 | - elTabLPAddons.setAttribute( 'href', 'admin.php?page=learn-press-addons&tab=update' ); | |
| 44 | - elTabLPAddons.insertAdjacentHTML( 'beforeend', html ); | |
| 45 | - } catch ( e ) { | |
| 46 | - console.log( e ); | |
| 47 | - } | |
| 48 | -}; | |
| 49 | - | |
| 50 | -const callAdminNotices = ( set = '' ) => { | |
| 51 | - if ( ! lpGlobalSettings.is_admin ) { | |
| 52 | - return; | |
| 53 | - } | |
| 54 | - | |
| 55 | - let params = tab ? `?tab=${ tab }` : ''; | |
| 56 | - params += set ? ( tab ? '&' : '?' ) + `${ set }` : ''; | |
| 57 | - | |
| 58 | - fetch( API.admin.apiAdminNotice + params, { | |
| 59 | - method: 'POST', | |
| 60 | - headers: { | |
| 61 | - 'X-WP-Nonce': lpGlobalSettings.nonce, | |
| 62 | - }, | |
| 63 | - } ).then( ( res ) => | |
| 64 | - res.json() | |
| 65 | - ).then( ( res ) => { | |
| 66 | - // console.log(data); | |
| 67 | - | |
| 68 | - const { status, message, data } = res; | |
| 69 | - if ( status === 'success' ) { | |
| 70 | - if ( 'Dismissed!' !== message ) { | |
| 71 | - dataHtml = data.content; | |
| 72 | - | |
| 73 | - if ( dataHtml.length === 0 ) { | |
| 74 | - elLPAdminNotices.style.display = 'none'; | |
| 75 | - } else { | |
| 76 | - elLPAdminNotices.innerHTML = dataHtml; | |
| 77 | - elLPAdminNotices.style.display = 'block'; | |
| 78 | - // Handle notify addons new version. | |
| 79 | - notifyAddonsNewVersion(); | |
| 80 | - } | |
| 81 | - } | |
| 82 | - } else { | |
| 83 | - dataHtml = message; | |
| 84 | - } | |
| 85 | - } ).catch( ( err ) => { | |
| 86 | - console.log( err ); | |
| 87 | - } ); | |
| 88 | -}; | |
| 89 | - | |
| 90 | -// Listen element ready | |
| 91 | -lpOnElementReady( '.lp-admin-notices', () => { | |
| 92 | - elLPAdminNotices = document.querySelector( '.lp-admin-notices' ); | |
| 93 | - callAdminNotices(); | |
| 94 | -} ); | |
| 95 | - | |
| 96 | -/*** Events ***/ | |
| 97 | -document.addEventListener( 'click', ( e ) => { | |
| 98 | - const el = e.target; | |
| 99 | - | |
| 100 | - if ( el.classList.contains( 'btn-lp-notice-dismiss' ) ) { | |
| 101 | - e.preventDefault(); | |
| 102 | - | |
| 103 | - // eslint-disable-next-line no-alert | |
| 104 | - if ( confirm( 'Are you sure you want to dismiss this notice?' ) ) { | |
| 105 | - const parent = el.closest( '.lp-notice' ); | |
| 106 | - callAdminNotices( `dismiss=${ el.getAttribute( 'data-dismiss' ) }` ); | |
| 107 | - parent.remove(); | |
| 108 | - if ( elLPAdminNotices.querySelectorAll( '.lp-notice' ).length === 0 ) { | |
| 109 | - elLPAdminNotices.style.display = 'none'; | |
| 110 | - } | |
| 111 | - } | |
| 112 | - } | |
| 113 | -} ); | |
| 1 | +/** | |
| 2 | + * Script handle admin notices. | |
| 3 | + * | |
| 4 | + * @since 4.1.7.3.2 | |
| 5 | + * @version 1.0.2 | |
| 6 | + */ | |
| 7 | +import { lpOnElementReady } from '../utils.js'; | |
| 8 | +import API from '../api.js'; | |
| 9 | +let elLPAdminNotices = null; | |
| 10 | +let dataHtml = null; | |
| 11 | +const queryString = window.location.search; | |
| 12 | +const urlParams = new URLSearchParams( queryString ); | |
| 13 | +const tab = urlParams.get( 'tab' ); | |
| 14 | + | |
| 15 | +const notifyAddonsNewVersion = () => { | |
| 16 | + try { | |
| 17 | + const elAdminMenu = document.querySelector( '#adminmenu' ); | |
| 18 | + if ( ! elAdminMenu ) { | |
| 19 | + return; | |
| 20 | + } | |
| 21 | + | |
| 22 | + const elTabLP = elAdminMenu.querySelector( '#toplevel_page_learn_press' ); | |
| 23 | + if ( ! elTabLP ) { | |
| 24 | + return; | |
| 25 | + } | |
| 26 | + const elTabLPName = elTabLP.querySelector( '.wp-menu-name' ); | |
| 27 | + if ( ! elTabLPName ) { | |
| 28 | + return; | |
| 29 | + } | |
| 30 | + const elAddonsNewVerTotal = document.querySelector( 'input[name=lp-addons-new-version-totals]' ); | |
| 31 | + if ( ! elAddonsNewVerTotal ) { | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + const htmlNotifyLP = `<span class="tab-lp-admin-notice"></span>`; | |
| 35 | + elTabLPName.insertAdjacentHTML( 'beforeend', htmlNotifyLP ); | |
| 36 | + const elTabLPAddons = elTabLP.querySelector( 'a[href="admin.php?page=learn-press-addons"]' ); | |
| 37 | + if ( ! elTabLPAddons ) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + | |
| 41 | + const total = elAddonsNewVerTotal.value; | |
| 42 | + const html = `<span style="margin-left: 5px" class="update-plugins">${ total }</span>`; | |
| 43 | + elTabLPAddons.setAttribute( 'href', 'admin.php?page=learn-press-addons&tab=update' ); | |
| 44 | + elTabLPAddons.insertAdjacentHTML( 'beforeend', html ); | |
| 45 | + } catch ( e ) { | |
| 46 | + console.log( e ); | |
| 47 | + } | |
| 48 | +}; | |
| 49 | + | |
| 50 | +const callAdminNotices = ( set = '' ) => { | |
| 51 | + if ( ! lpGlobalSettings.is_admin ) { | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + let params = tab ? `?tab=${ tab }` : ''; | |
| 56 | + params += set ? ( tab ? '&' : '?' ) + `${ set }` : ''; | |
| 57 | + | |
| 58 | + fetch( API.admin.apiAdminNotice + params, { | |
| 59 | + method: 'POST', | |
| 60 | + headers: { | |
| 61 | + 'X-WP-Nonce': lpGlobalSettings.nonce, | |
| 62 | + }, | |
| 63 | + } ).then( ( res ) => | |
| 64 | + res.json() | |
| 65 | + ).then( ( res ) => { | |
| 66 | + // console.log(data); | |
| 67 | + | |
| 68 | + const { status, message, data } = res; | |
| 69 | + if ( status === 'success' ) { | |
| 70 | + if ( 'Dismissed!' !== message ) { | |
| 71 | + dataHtml = data.content; | |
| 72 | + | |
| 73 | + if ( dataHtml.length === 0 ) { | |
| 74 | + elLPAdminNotices.style.display = 'none'; | |
| 75 | + } else { | |
| 76 | + elLPAdminNotices.innerHTML = dataHtml; | |
| 77 | + elLPAdminNotices.style.display = 'block'; | |
| 78 | + // Handle notify addons new version. | |
| 79 | + notifyAddonsNewVersion(); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + } else { | |
| 83 | + dataHtml = message; | |
| 84 | + } | |
| 85 | + } ).catch( ( err ) => { | |
| 86 | + console.log( err ); | |
| 87 | + } ); | |
| 88 | +}; | |
| 89 | + | |
| 90 | +// Listen element ready | |
| 91 | +lpOnElementReady( '.lp-admin-notices', () => { | |
| 92 | + elLPAdminNotices = document.querySelector( '.lp-admin-notices' ); | |
| 93 | + callAdminNotices(); | |
| 94 | +} ); | |
| 95 | + | |
| 96 | +/*** Events ***/ | |
| 97 | +document.addEventListener( 'click', ( e ) => { | |
| 98 | + const el = e.target; | |
| 99 | + | |
| 100 | + if ( el.classList.contains( 'btn-lp-notice-dismiss' ) ) { | |
| 101 | + e.preventDefault(); | |
| 102 | + | |
| 103 | + // eslint-disable-next-line no-alert | |
| 104 | + if ( confirm( 'Are you sure you want to dismiss this notice?' ) ) { | |
| 105 | + const parent = el.closest( '.lp-notice' ); | |
| 106 | + callAdminNotices( `dismiss=${ el.getAttribute( 'data-dismiss' ) }` ); | |
| 107 | + parent.remove(); | |
| 108 | + if ( elLPAdminNotices.querySelectorAll( '.lp-notice' ).length === 0 ) { | |
| 109 | + elLPAdminNotices.style.display = 'none'; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } | |
| 113 | +} ); | |