Completed
Push — update/idc-endpoint-migration ( 5a8f44...65623a )
by
unknown
71:43 queued 60:12
created

REST_Endpoints::start_fresh_connection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
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.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',
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...
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',
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...
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',
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...
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() ) );
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...
122
	}
123
124
}
125