| 1 |
/** |
| 2 |
* Script handle admin notices. |
| 3 |
* |
| 4 |
* @since 4.1.7.3.2 |
| 5 |
* @version 1.0.3 |
| 6 |
*/ |
| 7 |
import * as lpUtils from 'lpAssetsJsPath/utils.js'; |
| 8 |
import * as lpToastify from 'lpAssetsJsPath/lpToastify.js'; |
| 9 |
|
| 10 |
class AdminNotices { |
| 11 |
static selectors = { |
| 12 |
elAddonsNewVersionTotals: '.lp-addons-new-version-totals', |
| 13 |
elBtnDismiss: '.btn-lp-notice-dismiss', |
| 14 |
elAdminMenu: '#adminmenu', |
| 15 |
elTabLP: '#toplevel_page_learn_press', |
| 16 |
elTabLPName: '.wp-menu-name', |
| 17 |
elTabLPNotice: '.tab-lp-admin-notice', |
| 18 |
elTabLPAddons: 'a[href="admin.php?page=learn-press-addons"]', |
| 19 |
elMenuAddons: '.lp-menu-addons', |
| 20 |
elUpdatePlugins: '.update-plugins', |
| 21 |
elMessage: '.learn-press-message', |
| 22 |
}; |
| 23 |
|
| 24 |
init() { |
| 25 |
lpUtils.lpOnElementReady( |
| 26 |
AdminNotices.selectors.elAddonsNewVersionTotals, |
| 27 |
() => { |
| 28 |
this.notifyAddonsNewVersion(); |
| 29 |
} |
| 30 |
); |
| 31 |
|
| 32 |
this.events(); |
| 33 |
} |
| 34 |
|
| 35 |
notifyAddonsNewVersion() { |
| 36 |
try { |
| 37 |
const elAdminMenu = document.querySelector( |
| 38 |
AdminNotices.selectors.elAdminMenu |
| 39 |
); |
| 40 |
if ( ! elAdminMenu ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
const elTabLP = elAdminMenu.querySelector( |
| 45 |
AdminNotices.selectors.elTabLP |
| 46 |
); |
| 47 |
if ( ! elTabLP ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
const elTabLPName = elTabLP.querySelector( |
| 51 |
AdminNotices.selectors.elTabLPName |
| 52 |
); |
| 53 |
if ( ! elTabLPName ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
const elAddonsNewVerTotal = document.querySelector( |
| 57 |
AdminNotices.selectors.elAddonsNewVersionTotals |
| 58 |
); |
| 59 |
if ( ! elAddonsNewVerTotal ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
const htmlNotifyLP = `<span class="tab-lp-admin-notice"></span>`; |
| 63 |
if ( |
| 64 |
! elTabLPName.querySelector( |
| 65 |
AdminNotices.selectors.elTabLPNotice |
| 66 |
) |
| 67 |
) { |
| 68 |
elTabLPName.insertAdjacentHTML( 'beforeend', htmlNotifyLP ); |
| 69 |
} |
| 70 |
const elTabLPAddons = elTabLP.querySelector( |
| 71 |
AdminNotices.selectors.elTabLPAddons |
| 72 |
); |
| 73 |
if ( ! elTabLPAddons ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
const total = elAddonsNewVerTotal.dataset.total; |
| 78 |
const elMenuAddons = elTabLPAddons.querySelector( |
| 79 |
AdminNotices.selectors.elMenuAddons |
| 80 |
); |
| 81 |
if ( ! elMenuAddons ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
elTabLPAddons.setAttribute( |
| 86 |
'href', |
| 87 |
'admin.php?page=learn-press-addons&tab=update' |
| 88 |
); |
| 89 |
elMenuAddons.textContent = total; |
| 90 |
elMenuAddons.classList.remove( lpUtils.lpClassName.hidden ); |
| 91 |
} catch ( e ) { |
| 92 |
console.log( e ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/*** Events ***/ |
| 97 |
events() { |
| 98 |
if ( AdminNotices._loadedEvents ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
AdminNotices._loadedEvents = true; |
| 102 |
|
| 103 |
lpUtils.eventHandlers( 'click', [ |
| 104 |
{ |
| 105 |
selector: AdminNotices.selectors.elBtnDismiss, |
| 106 |
class: this, |
| 107 |
callBack: this.dismissNotice.name, |
| 108 |
}, |
| 109 |
] ); |
| 110 |
} |
| 111 |
|
| 112 |
dismissNotice( args ) { |
| 113 |
const { e, target } = args; |
| 114 |
const el = target.closest( AdminNotices.selectors.elBtnDismiss ); |
| 115 |
if ( ! el ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
e.preventDefault(); |
| 120 |
|
| 121 |
// eslint-disable-next-line no-alert |
| 122 |
if ( confirm( el.dataset.message ) ) { |
| 123 |
const parent = el.closest( AdminNotices.selectors.elMessage ); |
| 124 |
const dataSend = JSON.parse( el.dataset.send ); |
| 125 |
window.lpAJAXG.fetchAJAX( dataSend, { |
| 126 |
success: ( response ) => { |
| 127 |
const { message, status } = response; |
| 128 |
lpToastify.show( message, status ); |
| 129 |
if ( status === 'success' ) { |
| 130 |
parent?.remove(); |
| 131 |
} |
| 132 |
}, |
| 133 |
error: ( error ) => { |
| 134 |
lpToastify.show( error, 'error' ); |
| 135 |
}, |
| 136 |
} ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
const adminNotices = new AdminNotices(); |
| 142 |
adminNotices.init(); |
| 143 |
|