Skip to content

Commit a639901

Browse files
committed
Jetpack IDC notice
- 'Confirm Safe Mode' button now hits the proper API endpoint - IDC notice is conditional on jetpack option 'safe_mode_confirmed'
1 parent 40dac13 commit a639901

4 files changed

Lines changed: 44 additions & 30 deletions

File tree

‎_inc/idc-notice.js‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
/* global idcL10n, jQuery, alert, JSON, console */
1+
/* global idcL10n, jQuery */
22

33
( function( $ ) {
44
var restNonce = idcL10n.nonce,
55
restRoot = idcL10n.apiRoot,
6-
notice = $( '.jp-idc-notice' );
6+
notice = $( '.jp-idc-notice' ),
7+
idcButtons = $( '.jp-idc-notice .dops-button' );
78

89
// Confirm Safe Mode
910
$( '#jp-idc-confirm-safe-mode-action' ).click( function() {
@@ -15,21 +16,29 @@
1516
fixJetpackConnection();
1617
} );
1718

19+
function disableDopsButtons() {
20+
idcButtons.prop( 'disabled', true );
21+
}
22+
23+
function enableDopsButtons() {
24+
idcButtons.prop( 'disabled', false );
25+
}
26+
1827
function confirmSafeMode() {
19-
var route = restRoot + 'jetpack/v4/site';
28+
var route = restRoot + 'jetpack/v4/identity-crisis/confirm-safe-mode';
29+
disableDopsButtons();
2030
$.ajax( {
21-
method: 'GET',
31+
method: 'POST',
2232
beforeSend : function ( xhr ) {
2333
xhr.setRequestHeader( 'X-WP-Nonce', restNonce );
2434
},
2535
url: route,
2636
data: {},
27-
success: function( response ){
37+
success: function(){
2838
$( '.jp-idc-notice' ).hide();
29-
alert( JSON.stringify( response, null, 4 ) );
3039
},
31-
error: function( response ) {
32-
console.log( response.responseText );
40+
error: function() {
41+
enableDopsButtons();
3342
}
3443
} );
3544
}

‎_inc/lib/class.core-rest-api-endpoints.php‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public static function register_endpoints() {
9898
) );
9999

100100
// Confirm that a site in identity crisis should be in staging mode
101-
register_rest_route( 'jetpack/v4', '/identity-crisis/confirm-staging-mode', array(
101+
register_rest_route( 'jetpack/v4', '/identity-crisis/confirm-safe-mode', array(
102102
'methods' => WP_REST_Server::EDITABLE,
103-
'callback' => __CLASS__ . '::confirm_staging_mode',
103+
'callback' => __CLASS__ . '::confirm_safe_mode',
104104
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
105105
) );
106106

@@ -668,19 +668,22 @@ public static function get_site_data() {
668668
}
669669

670670
/**
671-
* Sets a flag
671+
* Sets a flag to confirm safe mode.
672672
*
673673
* @since 4.4.0
674674
*
675675
* @return bool True
676676
*/
677-
public static function confirm_staging_mode() {
678-
Jetpack_Options::update_option( 'auto_staging_confirmed', true );
679-
return rest_ensure_response(
680-
array(
681-
'code' => 'success'
682-
)
683-
);
677+
public static function confirm_safe_mode() {
678+
$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true );
679+
if ( $updated ) {
680+
return rest_ensure_response(
681+
array(
682+
'code' => 'success'
683+
)
684+
);
685+
}
686+
return new WP_Error( 'error_confirming_safe_mode', esc_html__( 'Jetpack could not confirm safe mode.', 'jetpack' ), array( 'status' => 500 ) );
684687
}
685688

686689
/**

‎class.jetpack-idc.php‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,33 @@ private function __construct() {
3636
if ( false === $urls_in_crisis = Jetpack::check_identity_crisis() ) {
3737
return;
3838
}
39-
4039
self::$wpcom_home_url = $urls_in_crisis['wpcom_home'];
40+
add_action( 'init', array( $this, 'wordpress_init' ) );
41+
}
4142

43+
function wordpress_init() {
44+
if ( ! $this->should_show_idc_notice() ) {
45+
return;
46+
}
4247
add_action( 'admin_notices', array( $this, 'display_idc_notice' ) );
4348
add_action( 'admin_enqueue_scripts', array( $this,'enqueue_idc_notice_files' ) );
4449
}
4550

4651
function should_show_idc_notice() {
47-
return current_user_can( 'jetpack_disconnect' ) && Jetpack::is_active() && ! Jetpack::is_development_mode();
52+
return (
53+
current_user_can( 'jetpack_disconnect' )
54+
&& Jetpack::is_active()
55+
&& ! Jetpack::is_development_mode()
56+
&& ! Jetpack_Options::get_option( 'safe_mode_confirmed', false )
57+
);
4858
}
4959

5060
/**
5161
* First "step" of the IDC mitigation. Will provide some messaging and two options/buttons.
5262
* "Confirm Staging" - Dismiss the notice and continue on with our lives in staging mode.
5363
* "Fix Jetpack Connection" - Will disconnect the site and start the mitigation...
5464
*/
55-
function display_idc_notice() {
56-
if ( ! $this->should_show_idc_notice() ) {
57-
return;
58-
}
59-
60-
?>
65+
function display_idc_notice() { ?>
6166
<div class="jp-idc-notice notice notice-warning">
6267
<div class="jp-idc-notice__header">
6368
<div class="jp-idc-notice__header__emblem">
@@ -77,9 +82,6 @@ function display_idc_notice() {
7782
* Enqueue scripts for the notice
7883
*/
7984
function enqueue_idc_notice_files() {
80-
if ( ! $this->should_show_idc_notice() ) {
81-
return;
82-
}
8385

8486
wp_enqueue_script(
8587
'jetpack-idc-js',

‎class.jetpack-options.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function get_option_names( $type = 'compact' ) {
3535
'unique_connection', // (array) A flag to determine a unique connection to wordpress.com two values "connected" and "disconnected" with values for how many times each has occured
3636
'protect_whitelist', // (array) IP Address for the Protect module to ignore
3737
'sync_error_idc', // (bool|array) false or array containing the site's home and siteurl at time of IDC error
38-
'auto_staging_confirmed' // (bool) True if someone confirms that a site was correctly put into staging mode automatically after an identity crisis is discovered.
38+
'safe_mode_confirmed' // (bool) True if someone confirms that a site was correctly put into safe mode automatically after an identity crisis is discovered.
3939
);
4040

4141
case 'private' :

0 commit comments

Comments
 (0)