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
|
|
View Code Duplication |
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
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handles identity crisis mitigation, confirming safe mode for this site. |
52
|
|
|
* |
53
|
|
|
* @since 4.4.0 |
54
|
|
|
* |
55
|
|
|
* @return bool | WP_Error True if option is properly set. |
56
|
|
|
*/ |
57
|
|
|
public static function confirm_safe_mode() { |
58
|
|
|
$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true ); |
59
|
|
|
if ( $updated ) { |
60
|
|
|
return rest_ensure_response( |
61
|
|
|
array( |
62
|
|
|
'code' => 'success', |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new WP_Error( |
68
|
|
|
'error_setting_jetpack_safe_mode', |
|
|
|
|
69
|
|
|
esc_html__( 'Could not confirm safe mode.', 'jetpack' ), |
70
|
|
|
array( 'status' => 500 ) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url. |
76
|
|
|
* |
77
|
|
|
* @since 4.4.0 |
78
|
|
|
* |
79
|
|
|
* @return bool | WP_Error True if option is properly set. |
80
|
|
|
*/ |
81
|
|
|
public static function migrate_stats_and_subscribers() { |
82
|
|
|
if ( Jetpack_Options::get_option( 'sync_error_idc' ) && ! Jetpack_Options::delete_option( 'sync_error_idc' ) ) { |
83
|
|
|
return new WP_Error( |
84
|
|
|
'error_deleting_sync_error_idc', |
|
|
|
|
85
|
|
|
esc_html__( 'Could not delete sync error option.', 'jetpack' ), |
86
|
|
|
array( 'status' => 500 ) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ( Jetpack_Options::get_option( 'migrate_for_idc' ) || Jetpack_Options::update_option( 'migrate_for_idc', true ) ) { |
91
|
|
|
return rest_ensure_response( |
92
|
|
|
array( |
93
|
|
|
'code' => 'success', |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
return new WP_Error( |
98
|
|
|
'error_setting_jetpack_migrate', |
|
|
|
|
99
|
|
|
esc_html__( 'Could not confirm migration.', 'jetpack' ), |
100
|
|
|
array( 'status' => 500 ) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Verify that user can mitigate an identity crisis. |
106
|
|
|
* |
107
|
|
|
* @since 4.4.0 |
108
|
|
|
* |
109
|
|
|
* @return bool Whether user has capability 'jetpack_disconnect'. |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public static function identity_crisis_mitigation_permission_check() { |
112
|
|
|
if ( current_user_can( 'jetpack_disconnect' ) ) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
$error_msg = esc_html__( |
116
|
|
|
'You do not have the correct user permissions to perform this action. |
117
|
|
|
Please contact your site admin if you think this is a mistake.', |
118
|
|
|
'jetpack' |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return new WP_Error( 'invalid_user_permission_identity_crisis', $error_msg, array( 'status' => rest_authorization_required_code() ) ); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
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.