Completed
Push — update/idc-disconnect ( 1405a6 )
by
unknown
81:54 queued 71:03
created

REST_Endpoints::initialize_rest_api()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
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.9.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',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_setting_jetpack_safe_mode'.

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.

Loading history...
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',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_deleting_sync_error_idc'.

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.

Loading history...
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',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'error_setting_jetpack_migrate'.

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.

Loading history...
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 WP_REST_Response|WP_Error
124
	 */
125
	public static function start_fresh_connection() {
126
		/**
127
		 * Fires when Users have requested through Identity Crisis for the connection to be reset.
128
		 * Should be used to disconnect any connections and reset options.
129
		 *
130
		 * @since 9.9.0
131
		 */
132
		do_action( 'jetpack_idc_disconnect' );
133
134
		/**
135
		 * Filters the connection url that users should be redirected to for re-establishing their connection.
136
		 *
137
		 * @since 9.9.0
138
		 *
139
		 * @param WP_REST_Response|WP_Error    $connection_url Connection URL user should be redirected to.
140
		 */
141
		return apply_filters( 'jetpack_idc_build_connect_url', '' ); // ToDo what is the default url?
142
	}
143
144
	/**
145
	 * Verify that user can mitigate an identity crisis.
146
	 *
147
	 * @since 4.4.0
148
	 *
149
	 * @return bool Whether user has capability 'jetpack_disconnect'.
150
	 */
151 View Code Duplication
	public static function identity_crisis_mitigation_permission_check() {
152
		if ( current_user_can( 'jetpack_disconnect' ) ) {
153
			return true;
154
		}
155
		$error_msg = esc_html__(
156
			'You do not have the correct user permissions to perform this action.
157
			Please contact your site admin if you think this is a mistake.',
158
			'jetpack'
159
		);
160
161
		return new WP_Error( 'invalid_user_permission_identity_crisis', $error_msg, array( 'status' => rest_authorization_required_code() ) );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_user_permission_identity_crisis'.

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.

Loading history...
162
	}
163
164
}
165