PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | assets/src/js/admin/admin-notices.js +119 -90 4.4.3 → 4.4.9 View file →
@@ -1,113 +1,142 @@
1 1 /**
2 2 * Script handle admin notices.
3 3 *
4 4 * @since 4.1.7.3.2
5 - * @version 1.0.2
5 + * @version 1.0.3
6 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' );
7 +import * as lpUtils from 'lpAssetsJsPath/utils.js';
8 +import * as lpToastify from 'lpAssetsJsPath/lpToastify.js';
14 9
15 -const notifyAddonsNewVersion = () => {
16 - try {
17 - const elAdminMenu = document.querySelector( '#adminmenu' );
18 - if ( ! elAdminMenu ) {
19 - return;
20 - }
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 + };
21 23
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 - }
24 + init() {
25 + lpUtils.lpOnElementReady(
26 + AdminNotices.selectors.elAddonsNewVersionTotals,
27 + () => {
28 + this.notifyAddonsNewVersion();
29 + }
30 + );
40 31
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 );
32 + this.events();
47 33 }
48 -};
49 34
50 -const callAdminNotices = ( set = '' ) => {
51 - if ( ! lpGlobalSettings.is_admin ) {
52 - return;
53 - }
35 + notifyAddonsNewVersion() {
36 + try {
37 + const elAdminMenu = document.querySelector(
38 + AdminNotices.selectors.elAdminMenu
39 + );
40 + if ( ! elAdminMenu ) {
41 + return;
42 + }
54 43
55 - let params = tab ? `?tab=${ tab }` : '';
56 - params += set ? ( tab ? '&' : '?' ) + `${ set }` : '';
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 + }
57 76
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);
77 + const total = elAddonsNewVerTotal.dataset.total;
78 + const elMenuAddons = elTabLPAddons.querySelector(
79 + AdminNotices.selectors.elMenuAddons
80 + );
81 + if ( ! elMenuAddons ) {
82 + return;
83 + }
67 84
68 - const { status, message, data } = res;
69 - if ( status === 'success' ) {
70 - if ( 'Dismissed!' !== message ) {
71 - dataHtml = data.content;
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 + }
72 95
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;
96 + /*** Events ***/
97 + events() {
98 + if ( AdminNotices._loadedEvents ) {
99 + return;
84 100 }
85 - } ).catch( ( err ) => {
86 - console.log( err );
87 - } );
88 -};
101 + AdminNotices._loadedEvents = true;
89 102
90 -// Listen element ready
91 -lpOnElementReady( '.lp-admin-notices', () => {
92 - elLPAdminNotices = document.querySelector( '.lp-admin-notices' );
93 - callAdminNotices();
94 -} );
103 + lpUtils.eventHandlers( 'click', [
104 + {
105 + selector: AdminNotices.selectors.elBtnDismiss,
106 + class: this,
107 + callBack: this.dismissNotice.name,
108 + },
109 + ] );
110 + }
95 111
96 -/*** Events ***/
97 -document.addEventListener( 'click', ( e ) => {
98 - const el = e.target;
112 + dismissNotice( args ) {
113 + const { e, target } = args;
114 + const el = target.closest( AdminNotices.selectors.elBtnDismiss );
115 + if ( ! el ) {
116 + return;
117 + }
99 118
100 - if ( el.classList.contains( 'btn-lp-notice-dismiss' ) ) {
101 119 e.preventDefault();
102 120
103 121 // 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 - }
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 + } );
111 137 }
112 138 }
113 -} );
139 +}
140 +
141 +const adminNotices = new AdminNotices();
142 +adminNotices.init();