admin.js
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
jQuery( document ).ready( () => {
// Don't run on versions of WordPress too old for the block editor and the translation methods it brings.
// All the install / activate options are plain links with meaningful destinations anyway.
if ( ! window.wp || ! window.wp.i18n ) {
return;
}
const { __, sprintf } = window.wp.i18n;
const ajaxurl = window.ajaxurl;
const wpscAdmin = window.wpscAdmin;
const link = jQuery( '.wpsc-install-action-button' );
const label = link.find( 'label' );
const spinner = link.find( '.spinner' );
// Dismiss Boost banner.
jQuery( '.wpsc-boost-dismiss' ).on( 'click', function () {
jQuery( '.wpsc-boost-banner' ).fadeOut( 'slow' );
jQuery.post( ajaxurl, {
action: 'wpsc-hide-boost-banner',
nonce: wpscAdmin.boostDismissNonce,
} );
} );
// One-click install for Boost.
jQuery( '#wpsc-install-boost-button' ).on( 'click', event => {
event.preventDefault();
showBoostBannerBusy( __( 'Installing…', 'wp-super-cache' ) );
jQuery
.post( ajaxurl, {
action: 'wpsc_install_plugin',
_ajax_nonce: wpscAdmin.boostInstallNonce,
slug: 'jetpack-boost',
} )
.done( response => {
if ( response.success ) {
activateBoost();
} else {
showBoostBannerError( response.data );
}
} )
.fail( response => {
showBoostBannerError(
sprintf(
/* translators: %d is an HTTP error code */
__( 'Failed to install Jetpack Boost: HTTP %d error received', 'wp-super-cache' ),
response.status
)
);
} );
} );
// Handle activate button click.
jQuery( '#wpsc-activate-boost-button' ).on( 'click', event => {
event.preventDefault();
activateBoost();
} );
// Helper function to show Boost Banner work in progress.
const showBoostBannerBusy = action => {
link.attr( 'disabled', true );
label.text( action );
spinner.addClass( 'is-active' ).show();
};
// Helper function to reset Boost Banner button.
const resetBoostBannerButton = () => {
link.attr( 'disabled', false );
jQuery( '#wpsc-activate-boost-button' )
.find( 'label' )
.text( __( 'Activate Jetpack Boost', 'wp-super-cache' ) );
jQuery( '#wpsc-install-boost-button' )
.find( 'label' )
.text( __( 'Install Jetpack Boost', 'wp-super-cache' ) );
spinner.removeClass( 'is-active' ).hide();
};
// Helper function to show an error.
const showBoostBannerError = err => {
resetBoostBannerButton();
jQuery( '#wpsc-boost-banner-error' )
.text(
err || __( 'An error occurred while trying to activate Jetpack Boost', 'wp-super-cache' )
)
.show();
};
// Activate Jetpack Boost.
const activateBoost = () => {
showBoostBannerBusy( __( 'Activating…', 'wp-super-cache' ) );
jQuery
.post( ajaxurl, {
action: 'wpsc_activate_boost',
_ajax_nonce: wpscAdmin.boostActivateNonce,
} )
.done( response => {
if ( response.success ) {
label.text( 'Success! Sending you to Jetpack Boost...' );
spinner.hide();
window.location.href = 'admin.php?page=jetpack-boost';
} else {
showBoostBannerError( response.data );
}
} )
.fail( response => {
showBoostBannerError(
sprintf(
/* translators: %d is an HTTP error code */
__( 'Failed to activate Jetpack Boost: HTTP %d error received', 'wp-super-cache' ),
response.status
)
);
} );
};
} );