diff --git a/admin/js/perflab-module-migration-notice.js b/admin/js/perflab-module-migration-notice.js new file mode 100644 index 0000000000..12844e801f --- /dev/null +++ b/admin/js/perflab-module-migration-notice.js @@ -0,0 +1,50 @@ +( function ( document ) { + document.addEventListener( 'DOMContentLoaded', function () { + document.addEventListener( 'click', function ( event ) { + if ( + event.target.classList.contains( + 'perflab-install-active-plugin' + ) + ) { + const target = event.target; + target.parentElement + .querySelector( 'span' ) + .classList.remove( 'hidden' ); + + const data = new FormData(); + data.append( + 'action', + 'perflab_install_activate_standalone_plugins' + ); + data.append( 'nonce', perflab_module_migration_notice.nonce ); + + fetch( perflab_module_migration_notice.ajaxurl, { + method: 'POST', + credentials: 'same-origin', + body: data, + } ) + .then( function ( response ) { + if ( ! response.ok ) { + throw new Error( + wp.i18n.__( 'Network response was not ok.', 'performance-lab' ) + ); + } + return response.json(); + } ) + .then( function ( result ) { + target.parentElement + .querySelector( 'span' ) + .classList.add( 'hidden' ); + if ( ! result.success ) { + alert( result.data.errorMessage ); + } + window.location.reload(); + } ) + .catch( function ( error ) { + alert( error.errorMessage ); + window.location.reload(); + } ); + } + } ); + } ); +} )( document ); diff --git a/admin/js/perflab-plugin-management.js b/admin/js/perflab-plugin-management.js new file mode 100644 index 0000000000..f36626204a --- /dev/null +++ b/admin/js/perflab-plugin-management.js @@ -0,0 +1,38 @@ +( function ( $, document ) { + $( document ).ajaxComplete( function( event, xhr, settings ) { + // Check if this is the 'install-plugin' request. + if ( settings.data && typeof settings.data === 'string' && settings.data.includes( 'action=install-plugin' ) ) { + var params = new URLSearchParams( settings.data ); + var slug = params.get('slug'); + + // Check if 'slug' was found and output the value. + if ( ! slug ) { + return; + } + + var target_element = $( '.wpp-standalone-plugins a[data-slug="' + slug + '"]' ); + if ( ! target_element ) { + return; + } + + /* + * WordPress core uses a 1s timeout for updating the activation link, + * so we set a 1.5 timeout here to ensure our changes get updated after + * the core changes have taken place. + */ + setTimeout( function() { + var plugin_url = target_element.attr( 'href' ); + if ( ! plugin_url ) { + return; + } + var nonce = target_element.attr( 'data-plugin-activation-nonce' ); + var plugin_slug = target_element.attr( 'data-slug' ); + var url = new URL( plugin_url ); + url.searchParams.set( 'action', 'perflab_activate_plugin' ); + url.searchParams.set( '_wpnonce', nonce ); + url.searchParams.set( 'plugin', plugin_slug ); + target_element.attr( 'href', url.href ); + }, 1500 ); + } + } ); +} )( jQuery, document ); diff --git a/admin/load.php b/admin/load.php index 36bf0ade8b..2e04628959 100644 --- a/admin/load.php +++ b/admin/load.php @@ -53,6 +53,15 @@ function perflab_add_modules_page() { function perflab_load_modules_page( $modules = null, $focus_areas = null ) { global $wp_settings_sections; + // Handle script enqueuing for settings page. + add_action( 'admin_enqueue_scripts', 'perflab_enqueue_modules_page_scripts' ); + + // Handle style for settings page. + add_action( 'admin_head', 'perflab_print_modules_page_style' ); + + // Handle admin notices for settings page. + add_action( 'admin_notices', 'perflab_plugin_admin_notices' ); + // Register sections for all focus areas, plus 'Other'. if ( ! is_array( $focus_areas ) ) { $focus_areas = perflab_get_focus_areas(); @@ -107,9 +116,11 @@ static function () use ( $module_slug, $module_data, $module_settings ) { function perflab_render_modules_page() { ?>