1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Identity_Crisis package. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-identity-crisis |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\IdentityCrisis; |
9
|
|
|
|
10
|
|
|
use Jetpack_Options; |
11
|
|
|
use WP_Error; |
12
|
|
|
use WP_REST_Server; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class will handle Identity Crisis Endpoints |
16
|
|
|
* |
17
|
|
|
* @since 9.8.0 |
18
|
|
|
*/ |
19
|
|
|
class REST_Endpoints { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Initialize REST routes. |
23
|
|
|
*/ |
24
|
|
|
public static function initialize_rest_api() { |
25
|
|
|
|
26
|
|
|
// Confirm that a site in identity crisis should be in staging mode. |
27
|
|
|
register_rest_route( |
28
|
|
|
'jetpack/v4', |
29
|
|
|
'/identity-crisis/confirm-safe-mode', |
30
|
|
|
array( |
31
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
32
|
|
|
'callback' => __CLASS__ . '::confirm_safe_mode', |
33
|
|
|
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check', |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// Handles the request to migrate stats and subscribers during an identity crisis. |
38
|
|
|
register_rest_route( |
39
|
|
|
'jetpack/v4', |
40
|
|
|
'identity-crisis/migrate', |
41
|
|
|
array( |
42
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
43
|
|
|
'callback' => __CLASS__ . '::migrate_stats_and_subscribers', |
44
|
|
|
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check', |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
// IDC resolve: create an entirely new shadow site for this URL. |
49
|
|
|
register_rest_route( |
50
|
|
|
'jetpack/v4', |
51
|
|
|
'/identity-crisis/start-fresh', |
52
|
|
|
array( |
53
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
54
|
|
|
'callback' => __CLASS__ . '::start_fresh_connection', |
55
|
|
|
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check', |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Handles identity crisis mitigation, confirming safe mode for this site. |
63
|
|
|
* |
64
|
|
|
* @since 4.4.0 |
65
|
|
|
* |
66
|
|
|
* @return bool | WP_Error True if option is properly set. |
67
|
|
|
*/ |
68
|
|
|
public static function confirm_safe_mode() { |
69
|
|
|
$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true ); |
70
|
|
|
if ( $updated ) { |
71
|
|
|
return rest_ensure_response( |
72
|
|
|
array( |
73
|
|
|
'code' => 'success', |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return new WP_Error( |
79
|
|
|
'error_setting_jetpack_safe_mode', |
|
|
|
|
80
|
|
|
esc_html__( 'Could not confirm safe mode.', 'jetpack' ), |
81
|
|
|
array( 'status' => 500 ) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url. |
87
|
|
|
* |
88
|
|
|
* @since 4.4.0 |
89
|
|
|
* |
90
|
|
|
* @return bool | WP_Error True if option is properly set. |
91
|
|
|
*/ |
92
|
|
|
public static function migrate_stats_and_subscribers() { |
93
|
|
|
if ( Jetpack_Options::get_option( 'sync_error_idc' ) && ! Jetpack_Options::delete_option( 'sync_error_idc' ) ) { |
94
|
|
|
return new WP_Error( |
95
|
|
|
'error_deleting_sync_error_idc', |
|
|
|
|
96
|
|
|
esc_html__( 'Could not delete sync error option.', 'jetpack' ), |
97
|
|
|
array( 'status' => 500 ) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ( Jetpack_Options::get_option( 'migrate_for_idc' ) || Jetpack_Options::update_option( 'migrate_for_idc', true ) ) { |
102
|
|
|
return rest_ensure_response( |
103
|
|
|
array( |
104
|
|
|
'code' => 'success', |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
return new WP_Error( |
109
|
|
|
'error_setting_jetpack_migrate', |
|
|
|
|
110
|
|
|
esc_html__( 'Could not confirm migration.', 'jetpack' ), |
111
|
|
|
array( 'status' => 500 ) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* This IDC resolution will disconnect the site and re-connect to a completely new |
117
|
|
|
* and separate shadow site than the original. |
118
|
|
|
* |
119
|
|
|
* It will first will disconnect the site without phoning home as to not disturb the production site. |
120
|
|
|
* It then builds a fresh connection URL and sends it back along with the response. |
121
|
|
|
* |
122
|
|
|
* @since 4.4.0 |
123
|
|
|
* @return bool|WP_Error |
124
|
|
|
*/ |
125
|
|
|
public static function start_fresh_connection() { |
126
|
|
|
// First clear the options / disconnect. |
127
|
|
|
do_action( 'jetpack_idc_disconnect' ); |
128
|
|
|
return apply_filters( 'jetpack_idc_build_connect_url', '' ); // ToDo what is the default url? |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Verify that user can mitigate an identity crisis. |
133
|
|
|
* |
134
|
|
|
* @since 4.4.0 |
135
|
|
|
* |
136
|
|
|
* @return bool Whether user has capability 'jetpack_disconnect'. |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public static function identity_crisis_mitigation_permission_check() { |
139
|
|
|
if ( current_user_can( 'jetpack_disconnect' ) ) { |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
$error_msg = esc_html__( |
143
|
|
|
'You do not have the correct user permissions to perform this action. |
144
|
|
|
Please contact your site admin if you think this is a mistake.', |
145
|
|
|
'jetpack' |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
return new WP_Error( 'invalid_user_permission_identity_crisis', $error_msg, array( 'status' => rest_authorization_required_code() ) ); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.