Skip to content

Commit

Permalink
Merge pull request #920 from WordPress/load/migration-script-in-setti…
Browse files Browse the repository at this point in the history
…ng-page

Load migration script in plugin setting page
  • Loading branch information
felixarntz authored Jan 8, 2024
2 parents a776953 + abc39aa commit 8c6d56d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 50 deletions.
50 changes: 50 additions & 0 deletions admin/js/perflab-module-migration-notice.js
Original file line number Diff line number Diff line change
@@ -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 );
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,4 @@
}, 1500 );
}
} );

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();
} );
}
} );
} );
} )( jQuery, document );
13 changes: 12 additions & 1 deletion admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,17 @@ function perflab_enqueue_modules_page_scripts() {

wp_enqueue_script( 'plugin-install' );

wp_enqueue_script(
'perflab-plugin-management',
plugin_dir_url( __FILE__ ) . 'js/perflab-plugin-management.js',
array(),
'1.0.0',
array(
'in_footer' => true,
'strategy' => 'defer',
)
);

// Bail early if module is not active.
$get_active_modules_with_standalone_plugins = perflab_get_active_modules_with_standalone_plugins();
if ( empty( $get_active_modules_with_standalone_plugins ) ) {
Expand All @@ -585,7 +596,7 @@ function perflab_enqueue_modules_page_scripts() {

wp_enqueue_script(
'perflab-module-migration-notice',
plugin_dir_url( __FILE__ ) . 'perflab-module-migration-notice.js',
plugin_dir_url( __FILE__ ) . 'js/perflab-module-migration-notice.js',
array( 'wp-i18n' ),
'1.0.0',
array(
Expand Down

0 comments on commit 8c6d56d

Please sign in to comment.