From 47f3938e197596310b39fdbc829be0cae8dae414 Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Thu, 7 Nov 2024 18:21:00 +0530 Subject: [PATCH 01/14] Update print plugin progress indicator script function to support ajax handling, add ajax callback for activation of plugin --- .../performance-lab/includes/admin/load.php | 90 +++++++++++++++++-- .../includes/admin/plugins.php | 3 +- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 819c116e2..0d4c067a6 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -290,6 +290,36 @@ function perflab_install_activate_plugin_callback(): void { } add_action( 'admin_action_perflab_install_activate_plugin', 'perflab_install_activate_plugin_callback' ); +/** + * Callback for handling installation/activation of plugin using ajax. + */ +function perflab_install_activate_plugin_ajax_callback(): void { + check_ajax_referer( 'perflab_install_activate_plugin_ajax', '_ajax_nonce' ); + + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; + require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; + + if ( ! isset( $_POST['slug'] ) ) { + wp_send_json_error( __( 'Missing required parameter.', 'performance-lab' ), 400 ); + } + + $plugin_slug = perflab_sanitize_plugin_slug( wp_unslash( $_POST['slug'] ) ); + if ( null === $plugin_slug ) { + wp_send_json_error( __( 'Invalid plugin.', 'performance-lab' ), 400 ); + } + + // Install and activate the plugin and its dependencies. + $result = perflab_install_and_activate_plugin( $plugin_slug ); + if ( $result instanceof WP_Error ) { + wp_send_json_error( $result->get_error_message() ); + } + + wp_send_json_success(); +} +add_action( 'wp_ajax_perflab_install_activate_plugin', 'perflab_install_activate_plugin_ajax_callback' ); + /** * Callback function to handle admin inline style. * @@ -402,8 +432,18 @@ static function ( $name ) { * @since 3.1.0 */ function perflab_print_plugin_progress_indicator_script(): void { + $nonce = wp_create_nonce( 'perflab_install_activate_plugin_ajax' ); + + $script_data = array( + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'nonce' => $nonce, + 'activatingText' => __( 'Activating...', 'performance-lab' ), + 'activateText' => __( 'Activate', 'performance-lab' ), + 'activatedText' => __( 'Active', 'performance-lab' ), + ); + $js_function = << response.json()) + .then(response => { + if (response.success) { + const newButton = document.createElement('button'); + newButton.type = 'button'; + newButton.className = 'button button-disabled'; + newButton.disabled = true; + newButton.textContent = data.activatedText; + target.parentNode.replaceChild(newButton, target); + } else { + console.error('Error:', response); + // TODO: Show error using admin notice. + target.classList.remove('updating-message'); + target.textContent = data.activateText; + } + }) + .catch(error => { + console.error('Error:', error); + // TODO: Show error using admin notice. + target.classList.remove('updating-message'); + target.textContent = data.activateText; + }); } } ); } ); @@ -426,7 +506,7 @@ function addPluginProgressIndicator( message ) { sprintf( '( %s )( %s );', $js_function, - wp_json_encode( __( 'Activating...', 'default' ) ) + wp_json_encode( $script_data ) ), array( 'type' => 'module' ) ); diff --git a/plugins/performance-lab/includes/admin/plugins.php b/plugins/performance-lab/includes/admin/plugins.php index 709300061..95b43d1e5 100644 --- a/plugins/performance-lab/includes/admin/plugins.php +++ b/plugins/performance-lab/includes/admin/plugins.php @@ -426,8 +426,9 @@ function perflab_render_plugin_card( array $plugin_data ): void { ); $action_links[] = sprintf( - '%s', + '%s', esc_url( $url ), + esc_attr( $plugin_data['slug'] ), esc_html__( 'Activate', 'default' ) ); } else { From 9d97f597501a618fb17fa872a050fa73ad411d2f Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Fri, 8 Nov 2024 15:23:39 +0530 Subject: [PATCH 02/14] Add admin notice dynamically, Update to use async/await --- .../performance-lab/includes/admin/load.php | 148 ++++++++++++++---- 1 file changed, 115 insertions(+), 33 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 0d4c067a6..502c12a68 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -316,6 +316,15 @@ function perflab_install_activate_plugin_ajax_callback(): void { wp_send_json_error( $result->get_error_message() ); } + $plugin_settings_url = perflab_get_plugin_settings_url( $plugin_slug ); + if ( null !== $plugin_settings_url ) { + wp_send_json_success( + array( + 'pluginSettingsURL' => esc_url( $plugin_settings_url ), + ) + ); + } + wp_send_json_success(); } add_action( 'wp_ajax_perflab_install_activate_plugin', 'perflab_install_activate_plugin_ajax_callback' ); @@ -440,12 +449,18 @@ function perflab_print_plugin_progress_indicator_script(): void { 'activatingText' => __( 'Activating...', 'performance-lab' ), 'activateText' => __( 'Activate', 'performance-lab' ), 'activatedText' => __( 'Active', 'performance-lab' ), + 'dismissText' => __( 'Dismiss this notice.', 'performance-lab' ), + 'successText' => __( 'Feature activated.', 'performance-lab' ), + 'reviewText' => __( ' Review ', 'performance-lab' ), + 'settingsText' => __( 'settings', 'performance-lab' ), + 'endText' => _x( '.', 'Punctuation mark', 'performance-lab' ), + 'errorText' => __( 'There was an error activating the plugin. Please try again.', 'performance-lab' ), ); $js_function = << response.json()) - .then(response => { - if (response.success) { - const newButton = document.createElement('button'); - newButton.type = 'button'; - newButton.className = 'button button-disabled'; - newButton.disabled = true; - newButton.textContent = data.activatedText; - target.parentNode.replaceChild(newButton, target); - } else { - console.error('Error:', response); - // TODO: Show error using admin notice. + const pluginSlug = target.getAttribute( 'data-plugin-slug' ); + + try { + const response = await fetch( data.ajaxUrl, { + method: 'POST', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams( { + action: 'perflab_install_activate_plugin', + slug: pluginSlug, + _ajax_nonce: data.nonce, + } ), + } ); + + const responseData = await response.json(); + + if ( !responseData.success ) { + showAdminNotice( data.errorText ); + target.classList.remove('updating-message'); target.textContent = data.activateText; + + return; } - }) - .catch(error => { - console.error('Error:', error); - // TODO: Show error using admin notice. - target.classList.remove('updating-message'); + + const newButton = document.createElement( 'button' ); + + newButton.type = 'button'; + newButton.className = 'button button-disabled'; + newButton.disabled = true; + newButton.textContent = data.activatedText; + + target.parentNode.replaceChild( newButton, target ); + console.log(responseData) + showAdminNotice( data.successText, 'success', responseData?.data?.pluginSettingsURL ); + } catch (error) { + showAdminNotice( data.errorText ); + + target.classList.remove( 'updating-message' ); target.textContent = data.activateText; - }); + } } } ); } ); + + function showAdminNotice( message, type = 'error', pluginSettingsURL = undefined ) { + // Create the notice container elements. + const notice = document.createElement( 'div' ); + const para = document.createElement( 'p' ); + + notice.className = 'notice is-dismissible notice-' + type; + para.textContent = message; + + if ( pluginSettingsURL ) { + para.textContent = para.textContent + data.reviewText; + + const anchor = document.createElement( 'a' ); + anchor.setAttribute( 'href', pluginSettingsURL ); + anchor.textContent = data.settingsText; + + para.appendChild( anchor ); + para.appendChild( document.createTextNode( data.endText ) ); + } + + notice.appendChild( para ); + + const dismissButton = document.createElement( 'button' ); + const dismissButtonTextWrap = document.createElement( 'span' ); + + dismissButton.type = 'button'; + dismissButton.className = 'notice-dismiss'; + + dismissButtonTextWrap.className = 'screen-reader-text'; + dismissButtonTextWrap.textContent = data.dismissText; + + dismissButton.appendChild( dismissButtonTextWrap ); + + // Add event listener to remove the notice when dismissed. + dismissButton.addEventListener( 'click', () => { + notice.remove(); + } ); + + notice.appendChild( dismissButton ); + + // Insert the notice at the top of the admin notices area. + const noticeContainer = document.querySelector( '.wrap.plugin-install-php' ) || document.body; + + if ( !noticeContainer ) { + // Fallback append to body if no suitable container is found. + document.body.prepend( notice ); + + return; + } + + if ( noticeContainer.children.length >= 1 ) { + // Insert as the second child. + noticeContainer.insertBefore( notice, noticeContainer.children[1] ); + + return; + } + + // If there's only one child or none, append as the last child. + noticeContainer.appendChild( notice ); + } } JS; From 6f172adaf5ea10d4227993707a79cd38abd2e07c Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Mon, 11 Nov 2024 13:33:30 +0530 Subject: [PATCH 03/14] Add settings URL of plugin in admin notice --- .../performance-lab/includes/admin/load.php | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 502c12a68..cd9f007ab 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -444,17 +444,18 @@ function perflab_print_plugin_progress_indicator_script(): void { $nonce = wp_create_nonce( 'perflab_install_activate_plugin_ajax' ); $script_data = array( - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), - 'nonce' => $nonce, - 'activatingText' => __( 'Activating...', 'performance-lab' ), - 'activateText' => __( 'Activate', 'performance-lab' ), - 'activatedText' => __( 'Active', 'performance-lab' ), - 'dismissText' => __( 'Dismiss this notice.', 'performance-lab' ), - 'successText' => __( 'Feature activated.', 'performance-lab' ), - 'reviewText' => __( ' Review ', 'performance-lab' ), - 'settingsText' => __( 'settings', 'performance-lab' ), - 'endText' => _x( '.', 'Punctuation mark', 'performance-lab' ), - 'errorText' => __( 'There was an error activating the plugin. Please try again.', 'performance-lab' ), + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'nonce' => $nonce, + 'activatingText' => __( 'Activating...', 'performance-lab' ), + 'activateText' => __( 'Activate', 'performance-lab' ), + 'activatedText' => __( 'Active', 'performance-lab' ), + 'dismissText' => __( 'Dismiss this notice.', 'performance-lab' ), + 'successText' => __( 'Feature activated.', 'performance-lab' ), + 'reviewText' => __( ' Review ', 'performance-lab' ), + 'settingsPrimaryText' => __( 'settings', 'performance-lab' ), + 'settingsSecondaryText' => __( 'Settings', 'performance-lab' ), + 'endText' => _x( '.', 'Punctuation mark', 'performance-lab' ), + 'errorText' => __( 'There was an error activating the plugin. Please try again.', 'performance-lab' ), ); $js_function = << Date: Mon, 11 Nov 2024 15:02:37 +0530 Subject: [PATCH 04/14] Move inline JavaScript to separate file --- .../performance-lab/includes/admin/load.php | 199 ++---------------- .../includes/admin/plugin-activate-ajax.js | 193 +++++++++++++++++ plugins/performance-lab/load.php | 1 + 3 files changed, 215 insertions(+), 178 deletions(-) create mode 100644 plugins/performance-lab/includes/admin/plugin-activate-ajax.js diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index cd9f007ab..862f83fce 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -39,6 +39,8 @@ function perflab_add_features_page(): void { * @since 1.0.0 * @since 3.0.0 Renamed to perflab_load_features_page(), and the * $module and $hook_suffix parameters were removed. + * @since n.e.x.t Removed added action for perflab_print_plugin_progress_indicator_script + * as this function is removed. */ function perflab_load_features_page(): void { // Handle script enqueuing for settings page. @@ -49,9 +51,6 @@ function perflab_load_features_page(): void { // Handle style for settings page. add_action( 'admin_head', 'perflab_print_features_page_style' ); - - // Handle script for settings page. - add_action( 'admin_footer', 'perflab_print_plugin_progress_indicator_script' ); } /** @@ -221,6 +220,7 @@ function perflab_dismiss_wp_pointer_wrapper(): void { * * @since 2.8.0 * @since 3.0.0 Renamed to perflab_enqueue_features_page_scripts(). + * @since n.e.x.t Added enqueue plugin activate AJAX script. */ function perflab_enqueue_features_page_scripts(): void { // These assets are needed for the "Learn more" popover. @@ -230,6 +230,24 @@ function perflab_enqueue_features_page_scripts(): void { // Enqueue the a11y script. wp_enqueue_script( 'wp-a11y' ); + + // Enqueue plugin activate AJAX script and localize script data. + wp_enqueue_script( + 'perflab-plugin-activate-ajax', + PERFLAB_PLUGIN_DIR_URL . 'includes/admin/plugin-activate-ajax.js', + array( 'jquery', 'wp-i18n', 'wp-a11y' ), + (string) filemtime( PERFLAB_PLUGIN_DIR_PATH . 'includes/admin/plugin-activate-ajax.js' ), + true + ); + + wp_localize_script( + 'perflab-plugin-activate-ajax', + 'perflabPluginActivateAjaxData', + array( + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'nonce' => wp_create_nonce( 'perflab_install_activate_plugin_ajax' ), + ) + ); } /** @@ -435,181 +453,6 @@ static function ( $name ) { } } -/** - * Callback function that print plugin progress indicator script. - * - * @since 3.1.0 - */ -function perflab_print_plugin_progress_indicator_script(): void { - $nonce = wp_create_nonce( 'perflab_install_activate_plugin_ajax' ); - - $script_data = array( - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), - 'nonce' => $nonce, - 'activatingText' => __( 'Activating...', 'performance-lab' ), - 'activateText' => __( 'Activate', 'performance-lab' ), - 'activatedText' => __( 'Active', 'performance-lab' ), - 'dismissText' => __( 'Dismiss this notice.', 'performance-lab' ), - 'successText' => __( 'Feature activated.', 'performance-lab' ), - 'reviewText' => __( ' Review ', 'performance-lab' ), - 'settingsPrimaryText' => __( 'settings', 'performance-lab' ), - 'settingsSecondaryText' => __( 'Settings', 'performance-lab' ), - 'endText' => _x( '.', 'Punctuation mark', 'performance-lab' ), - 'errorText' => __( 'There was an error activating the plugin. Please try again.', 'performance-lab' ), - ); - - $js_function = << { - notice.remove(); - } ); - - notice.appendChild( dismissButton ); - - // Insert the notice at the top of the admin notices area. - const noticeContainer = document.querySelector( '.wrap.plugin-install-php' ) || document.body; - - if ( !noticeContainer ) { - // Fallback append to body if no suitable container is found. - document.body.prepend( notice ); - - return; - } - - if ( noticeContainer.children.length >= 1 ) { - // Insert as the second child. - noticeContainer.insertBefore( notice, noticeContainer.children[1] ); - - return; - } - - // If there's only one child or none, append as the last child. - noticeContainer.appendChild( notice ); - } - } -JS; - - wp_print_inline_script_tag( - sprintf( - '( %s )( %s );', - $js_function, - wp_json_encode( $script_data ) - ), - array( 'type' => 'module' ) - ); -} - /** * Gets the URL to the plugin settings screen if one exists. * diff --git a/plugins/performance-lab/includes/admin/plugin-activate-ajax.js b/plugins/performance-lab/includes/admin/plugin-activate-ajax.js new file mode 100644 index 000000000..81e225aee --- /dev/null +++ b/plugins/performance-lab/includes/admin/plugin-activate-ajax.js @@ -0,0 +1,193 @@ +/** + * Handles activation of Performance Features (Plugins) using AJAX. + */ + +/* global perflabPluginActivateAjaxData */ +document.addEventListener( 'DOMContentLoaded', function () { + // @ts-ignore + const { i18n, a11y } = wp; + const { __, _x } = i18n; + + /** + * Adds a click event listener to the document. + * + * This asynchronous function listens for click events on the document and executes + * the provided callback function if triggered. + * + * @param {MouseEvent} event - The click event object that is triggered when the user clicks on the document. + * + * @return {Promise} - The asynchronous function returns a promise that resolves to void. + */ + document.addEventListener( 'click', async function ( event ) { + const target = /** @type {HTMLElement} */ ( event.target ); + + if ( target.classList.contains( 'perflab-install-active-plugin' ) ) { + // Prevent the default link behavior. + event.preventDefault(); + + target.classList.add( 'updating-message' ); + target.textContent = __( 'Activating…', 'performance-lab' ); + + a11y.speak( __( 'Activating…', 'performance-lab' ) ); + + const pluginSlug = target.getAttribute( 'data-plugin-slug' ).trim(); + + try { + const response = await fetch( + // @ts-ignore + perflabPluginActivateAjaxData.ajaxUrl, + { + method: 'POST', + credentials: 'same-origin', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams( { + action: 'perflab_install_activate_plugin', + slug: pluginSlug, + // @ts-ignore + _ajax_nonce: perflabPluginActivateAjaxData.nonce, + } ), + } + ); + + const responseData = await response.json(); + + if ( ! responseData.success ) { + showAdminNotice( + __( + 'There was an error activating the plugin. Please try again.', + 'performance-lab' + ) + ); + + target.classList.remove( 'updating-message' ); + target.textContent = __( 'Activate', 'performance-lab' ); + + return; + } + + const newButton = document.createElement( 'button' ); + + newButton.type = 'button'; + newButton.className = 'button button-disabled'; + newButton.disabled = true; + newButton.textContent = __( 'Active', 'performance-lab' ); + + target.parentNode.replaceChild( newButton, target ); + + const pluginSettingsURL = responseData?.data?.pluginSettingsURL; + + const actionButtonList = document.querySelector( + '.plugin-card-' + pluginSlug + ' .plugin-action-buttons' + ); + + if ( pluginSettingsURL && actionButtonList ) { + const listItem = document.createElement( 'li' ); + const anchor = document.createElement( 'a' ); + + anchor.setAttribute( 'href', pluginSettingsURL ); + anchor.textContent = __( 'Settings', 'performance-lab' ); + + listItem.appendChild( anchor ); + actionButtonList.appendChild( listItem ); + } + + showAdminNotice( + __( 'Feature activated.', 'performance-lab' ), + 'success', + pluginSettingsURL + ); + } catch ( error ) { + showAdminNotice( + __( + 'There was an error activating the plugin. Please try again.', + 'performance-lab' + ) + ); + + target.classList.remove( 'updating-message' ); + target.textContent = __( 'Activate', 'performance-lab' ); + } + } + } ); + + function showAdminNotice( + message, + type = 'error', + pluginSettingsURL = undefined + ) { + // Create the notice container elements. + const notice = document.createElement( 'div' ); + const para = document.createElement( 'p' ); + + notice.className = 'notice is-dismissible notice-' + type; + para.textContent = message; + + if ( pluginSettingsURL ) { + para.textContent = `${ para.textContent } ${ __( + 'Review', + 'performance-lab' + ) } `; + + const anchor = document.createElement( 'a' ); + anchor.setAttribute( 'href', pluginSettingsURL ); + anchor.textContent = __( 'settings', 'performance-lab' ); + + para.appendChild( anchor ); + para.appendChild( + document.createTextNode( + _x( '.', 'Punctuation mark', 'performance-lab' ) + ) + ); + } + + notice.appendChild( para ); + + const dismissButton = document.createElement( 'button' ); + const dismissButtonTextWrap = document.createElement( 'span' ); + + dismissButton.type = 'button'; + dismissButton.className = 'notice-dismiss'; + + dismissButtonTextWrap.className = 'screen-reader-text'; + dismissButtonTextWrap.textContent = __( + 'Dismiss this notice.', + 'performance-lab' + ); + + dismissButton.appendChild( dismissButtonTextWrap ); + + // Add event listener to remove the notice when dismissed. + dismissButton.addEventListener( 'click', () => { + notice.remove(); + } ); + + notice.appendChild( dismissButton ); + + // Insert the notice at the top of the admin notices area. + const noticeContainer = + document.querySelector( '.wrap.plugin-install-php' ) || + document.body; + + if ( ! noticeContainer ) { + // Fallback append to body if no suitable container is found. + document.body.prepend( notice ); + + return; + } + + if ( noticeContainer.children.length >= 1 ) { + // Insert as the second child. + noticeContainer.insertBefore( + notice, + noticeContainer.children[ 1 ] + ); + + return; + } + + // If there's only one child or none, append as the last child. + noticeContainer.appendChild( notice ); + } +} ); diff --git a/plugins/performance-lab/load.php b/plugins/performance-lab/load.php index 304089d13..2a80a2906 100644 --- a/plugins/performance-lab/load.php +++ b/plugins/performance-lab/load.php @@ -22,6 +22,7 @@ define( 'PERFLAB_VERSION', '3.5.1' ); define( 'PERFLAB_MAIN_FILE', __FILE__ ); define( 'PERFLAB_PLUGIN_DIR_PATH', plugin_dir_path( PERFLAB_MAIN_FILE ) ); +define( 'PERFLAB_PLUGIN_DIR_URL', plugin_dir_url( PERFLAB_MAIN_FILE ) ); define( 'PERFLAB_SCREEN', 'performance-lab' ); // If the constant isn't defined yet, it means the Performance Lab object cache file is not loaded. From 58c37d9cfd6aa1600297872aa1cc42f6c0380fd0 Mon Sep 17 00:00:00 2001 From: b1ink0 Date: Mon, 11 Nov 2024 17:10:46 +0530 Subject: [PATCH 05/14] Refactor JavaScript code to use jQuery --- .../performance-lab/includes/admin/load.php | 2 + .../includes/admin/plugin-activate-ajax.js | 202 ++++++++---------- .../includes/admin/plugins.php | 1 + 3 files changed, 94 insertions(+), 111 deletions(-) diff --git a/plugins/performance-lab/includes/admin/load.php b/plugins/performance-lab/includes/admin/load.php index 862f83fce..84037958a 100644 --- a/plugins/performance-lab/includes/admin/load.php +++ b/plugins/performance-lab/includes/admin/load.php @@ -310,6 +310,8 @@ function perflab_install_activate_plugin_callback(): void { /** * Callback for handling installation/activation of plugin using ajax. + * + * @since n.e.x.t */ function perflab_install_activate_plugin_ajax_callback(): void { check_ajax_referer( 'perflab_install_activate_plugin_ajax', '_ajax_nonce' ); diff --git a/plugins/performance-lab/includes/admin/plugin-activate-ajax.js b/plugins/performance-lab/includes/admin/plugin-activate-ajax.js index 81e225aee..f56db4ed2 100644 --- a/plugins/performance-lab/includes/admin/plugin-activate-ajax.js +++ b/plugins/performance-lab/includes/admin/plugin-activate-ajax.js @@ -3,56 +3,54 @@ */ /* global perflabPluginActivateAjaxData */ -document.addEventListener( 'DOMContentLoaded', function () { +/* global jQuery */ +// @ts-ignore +jQuery( document ).ready( ( $ ) => { // @ts-ignore const { i18n, a11y } = wp; const { __, _x } = i18n; /** - * Adds a click event listener to the document. - * - * This asynchronous function listens for click events on the document and executes - * the provided callback function if triggered. + * Adds a click event listener to the plugin activate buttons. * * @param {MouseEvent} event - The click event object that is triggered when the user clicks on the document. * - * @return {Promise} - The asynchronous function returns a promise that resolves to void. + * @return {Promise} - The asynchronous function returns a promise. */ - document.addEventListener( 'click', async function ( event ) { - const target = /** @type {HTMLElement} */ ( event.target ); - - if ( target.classList.contains( 'perflab-install-active-plugin' ) ) { + $( document ).on( + 'click', + '.perflab-install-active-plugin', + async function ( event ) { // Prevent the default link behavior. event.preventDefault(); - target.classList.add( 'updating-message' ); - target.textContent = __( 'Activating…', 'performance-lab' ); + // Get the clicked element as a jQuery object. + const target = $( this ); + + target + .addClass( 'updating-message' ) + .text( __( 'Activating…', 'performance-lab' ) ); a11y.speak( __( 'Activating…', 'performance-lab' ) ); - const pluginSlug = target.getAttribute( 'data-plugin-slug' ).trim(); + // Retrieve the plugin slug from the data attribute. + const pluginSlug = $.trim( target.attr( 'data-plugin-slug' ) ); try { - const response = await fetch( + // Send an AJAX POST request to activate the plugin. + const responseData = await $.post( // @ts-ignore perflabPluginActivateAjaxData.ajaxUrl, { - method: 'POST', - credentials: 'same-origin', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: new URLSearchParams( { - action: 'perflab_install_activate_plugin', - slug: pluginSlug, - // @ts-ignore - _ajax_nonce: perflabPluginActivateAjaxData.nonce, - } ), - } + action: 'perflab_install_activate_plugin', + slug: pluginSlug, + // @ts-ignore + _ajax_nonce: perflabPluginActivateAjaxData.nonce, + }, + null, + 'json' ); - const responseData = await response.json(); - if ( ! responseData.success ) { showAdminNotice( __( @@ -61,36 +59,40 @@ document.addEventListener( 'DOMContentLoaded', function () { ) ); - target.classList.remove( 'updating-message' ); - target.textContent = __( 'Activate', 'performance-lab' ); + target + .removeClass( 'updating-message' ) + .text( __( 'Activate', 'performance-lab' ) ); return; } - const newButton = document.createElement( 'button' ); - - newButton.type = 'button'; - newButton.className = 'button button-disabled'; - newButton.disabled = true; - newButton.textContent = __( 'Active', 'performance-lab' ); - - target.parentNode.replaceChild( newButton, target ); + // Replace the 'Activate' button with a disabled 'Active' button. + target.replaceWith( + $( '