PluginProbe
Performance Lab / trunk
Performance Lab vtrunk
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / includes / admin / plugin-activate-ajax.js

plugin-activate-ajax.js in Performance Lab trunk, at includes/admin/plugin-activate-ajax.js

118 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Handles activation of Performance Features (Plugins) using AJAX.
3 */
4
5 ( function () {
6 // @ts-ignore
7 const { i18n, a11y, apiFetch } = wp;
8 const { __ } = i18n;
9
10 // Queue to hold pending activation requests.
11 /** @type {{target: HTMLElement, pluginSlug: string}[]} */
12 const activationQueue = [];
13 let isProcessingActivation = false;
14
15 /**
16 * Enqueues plugin activation requests and starts processing if not already in progress.
17 *
18 * @param {MouseEvent} event - The click event object.
19 */
20 function enqueuePluginActivation( event ) {
21 // Prevent the default link behavior.
22 event.preventDefault();
23
24 const target = /** @type {HTMLElement} */ ( event.target );
25
26 if (
27 target.classList.contains( 'updating-message' ) ||
28 target.classList.contains( 'disabled' )
29 ) {
30 return;
31 }
32
33 target.classList.add( 'updating-message' );
34 target.textContent = __( 'Waiting…', 'performance-lab' );
35
36 const pluginSlug = /** @type {string} */ ( target.dataset.pluginSlug );
37 activationQueue.push( { target, pluginSlug } );
38
39 // Start processing the queue if not already doing so.
40 if ( ! isProcessingActivation ) {
41 handlePluginActivation();
42 }
43 }
44
45 /**
46 * Handles activation of feature/plugin using queue based approach.
47 *
48 * @return {Promise<void>} The asynchronous function returns a promise that resolves to void.
49 */
50 async function handlePluginActivation() {
51 const activationItem = activationQueue.shift();
52 if ( ! activationItem ) {
53 isProcessingActivation = false;
54 return;
55 }
56
57 isProcessingActivation = true;
58
59 const { target, pluginSlug } = activationItem;
60
61 target.textContent = __( 'Activating…', 'performance-lab' );
62
63 a11y.speak( __( 'Activating…', 'performance-lab' ) );
64
65 try {
66 // Activate the plugin/feature via the REST API.
67 await apiFetch( {
68 path: `/performance-lab/v1/features/${ pluginSlug }:activate`,
69 method: 'POST',
70 } );
71
72 // Fetch the plugin/feature information via the REST API.
73 /** @type {{settingsUrl: string|null}} */
74 const featureInfo = await apiFetch( {
75 path: `/performance-lab/v1/features/${ pluginSlug }`,
76 method: 'GET',
77 } );
78
79 if ( featureInfo.settingsUrl ) {
80 const actionButtonList = document.querySelector(
81 `.plugin-card-${ pluginSlug } .plugin-action-buttons`
82 );
83
84 const listItem = document.createElement( 'li' );
85 const anchor = document.createElement( 'a' );
86
87 anchor.href = featureInfo.settingsUrl;
88 anchor.textContent = __( 'Settings', 'performance-lab' );
89
90 listItem.appendChild( anchor );
91 actionButtonList?.appendChild( listItem );
92 }
93
94 a11y.speak( __( 'Plugin activated.', 'performance-lab' ) );
95
96 target.textContent = __( 'Active', 'performance-lab' );
97 target.classList.remove( 'updating-message' );
98 target.classList.add( 'disabled' );
99 } catch {
100 a11y.speak( __( 'Plugin failed to activate.', 'performance-lab' ) );
101
102 target.classList.remove( 'updating-message' );
103 target.textContent = __( 'Activate', 'performance-lab' );
104 } finally {
105 handlePluginActivation();
106 }
107 }
108
109 // Attach the event listeners.
110 document
111 .querySelectorAll( '.perflab-install-active-plugin[data-plugin-slug]' )
112 .forEach( ( item ) => {
113 item.addEventListener( 'click', ( event ) =>
114 enqueuePluginActivation( /** @type {MouseEvent} */ ( event ) )
115 );
116 } );
117 } )();
118