Completed
Push — core-compat/site-env ( c8b38d...6bb0d4 )
by
unknown
166:41 queued 159:04
created

REST_Connector::connection_status()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 1
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
1
<?php
2
/**
3
 * Sets up the Connection REST API endpoints.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Status;
11
use Jetpack_XMLRPC_Server;
12
use WP_Error;
13
use WP_REST_Request;
14
use WP_REST_Response;
15
use WP_REST_Server;
16
17
/**
18
 * Registers the REST routes for Connections.
19
 */
20
class REST_Connector {
21
	/**
22
	 * The Connection Manager.
23
	 *
24
	 * @var Manager
25
	 */
26
	private $connection;
27
28
	/**
29
	 * This property stores the localized "Insufficient Permissions" error message.
30
	 *
31
	 * @var string Generic error message when user is not allowed to perform an action.
32
	 */
33
	private static $user_permissions_error_msg;
34
35
	/**
36
	 * Constructor.
37
	 *
38
	 * @param Manager $connection The Connection Manager.
39
	 */
40
	public function __construct( Manager $connection ) {
41
		$this->connection = $connection;
42
43
		self::$user_permissions_error_msg = esc_html__(
44
			'You do not have the correct user permissions to perform this action.
45
			Please contact your site admin if you think this is a mistake.',
46
			'jetpack'
47
		);
48
49
		if ( ! $this->connection->is_active() ) {
50
			// Register a site.
51
			register_rest_route(
52
				'jetpack/v4',
53
				'/verify_registration',
54
				array(
55
					'methods'             => WP_REST_Server::EDITABLE,
56
					'callback'            => array( $this, 'verify_registration' ),
57
					'permission_callback' => '__return_true',
58
				)
59
			);
60
		}
61
62
		// Authorize a remote user.
63
		register_rest_route(
64
			'jetpack/v4',
65
			'/remote_authorize',
66
			array(
67
				'methods'             => WP_REST_Server::EDITABLE,
68
				'callback'            => __CLASS__ . '::remote_authorize',
69
				'permission_callback' => '__return_true',
70
			)
71
		);
72
73
		// Get current connection status of Jetpack.
74
		register_rest_route(
75
			'jetpack/v4',
76
			'/connection',
77
			array(
78
				'methods'             => WP_REST_Server::READABLE,
79
				'callback'            => __CLASS__ . '::connection_status',
80
				'permission_callback' => '__return_true',
81
			)
82
		);
83
84
		// Get list of plugins that use the Jetpack connection.
85
		register_rest_route(
86
			'jetpack/v4',
87
			'/connection/plugins',
88
			array(
89
				'methods'             => WP_REST_Server::READABLE,
90
				'callback'            => array( $this, 'get_connection_plugins' ),
91
				'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
92
			)
93
		);
94
	}
95
96
	/**
97
	 * Handles verification that a site is registered.
98
	 *
99
	 * @since 5.4.0
100
	 *
101
	 * @param \WP_REST_Request $request The request sent to the WP REST API.
102
	 *
103
	 * @return string|WP_Error
104
	 */
105
	public function verify_registration( \WP_REST_Request $request ) {
106
		$registration_data = array( $request['secret_1'], $request['state'] );
107
108
		return $this->connection->handle_registration( $registration_data );
109
	}
110
111
	/**
112
	 * Handles verification that a site is registered
113
	 *
114
	 * @since 5.4.0
115
	 *
116
	 * @param WP_REST_Request $request The request sent to the WP REST API.
117
	 *
118
	 * @return array|wp-error
0 ignored issues
show
Documentation introduced by
The doc-type array|wp-error could not be parsed: Unknown type name "wp-error" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
119
	 */
120
	public static function remote_authorize( $request ) {
121
		$xmlrpc_server = new Jetpack_XMLRPC_Server();
122
		$result        = $xmlrpc_server->remote_authorize( $request );
123
124
		if ( is_a( $result, 'IXR_Error' ) ) {
125
			$result = new WP_Error( $result->code, $result->message );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $result->code.

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...
126
		}
127
128
		return $result;
129
	}
130
131
	/**
132
	 * Get connection status for this Jetpack site.
133
	 *
134
	 * @since 4.3.0
135
	 *
136
	 * @param bool $rest_response Should we return a rest response or a simple array. Default to rest response.
137
	 *
138
	 * @return WP_REST_Response|array Connection information.
139
	 */
140
	public static function connection_status( $rest_response = true ) {
141
		$status     = new Status();
142
		$connection = new Manager();
143
144
		$connection_status = array(
145
			'isActive'     => $connection->is_active(),
146
			'isStaging'    => $status->is_staging_site(),
147
			'isRegistered' => $connection->is_registered(),
148
			'offlineMode'  => array(
149
				'isActive'        => $status->is_offline_mode(),
150
				'constant'        => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
151
				'url'             => $status->is_local_site(),
152
				/** This filter is documented in packages/status/src/class-status.php */
153
				'filter'          => ( apply_filters( 'jetpack_development_mode', false ) || apply_filters( 'jetpack_offline_mode', false ) ), // jetpack_development_mode is deprecated.
154
				'wpLocalConstant' => defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV,
155
			),
156
			'isPublic'     => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
157
		);
158
159
		if ( $rest_response ) {
160
			return rest_ensure_response(
161
				$connection_status
162
			);
163
		} else {
164
			return $connection_status;
165
		}
166
	}
167
168
169
	/**
170
	 * Get plugins connected to the Jetpack.
171
	 *
172
	 * @since 8.6.0
173
	 *
174
	 * @return WP_REST_Response|WP_Error Response or error object, depending on the request result.
175
	 */
176
	public function get_connection_plugins() {
177
		$plugins = $this->connection->get_connected_plugins();
178
179
		if ( is_wp_error( $plugins ) ) {
180
			return $plugins;
181
		}
182
183
		array_walk(
184
			$plugins,
185
			function( &$data, $slug ) {
186
				$data['slug'] = $slug;
187
			}
188
		);
189
190
		return rest_ensure_response( array_values( $plugins ) );
191
	}
192
193
	/**
194
	 * Verify that user can view Jetpack admin page and can activate plugins.
195
	 *
196
	 * @since 8.8.0
197
	 *
198
	 * @return bool|WP_Error Whether user has the capability 'jetpack_admin_page' and 'activate_plugins'.
199
	 */
200
	public static function activate_plugins_permission_check() {
201
		if ( current_user_can( 'activate_plugins' ) ) {
202
			return true;
203
		}
204
205
		return new WP_Error( 'invalid_user_permission_activate_plugins', self::get_user_permissions_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_activate_plugins'.

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...
206
	}
207
208
	/**
209
	 * Returns generic error message when user is not allowed to perform an action.
210
	 *
211
	 * @return string The error message.
212
	 */
213
	public static function get_user_permissions_error_msg() {
214
		return self::$user_permissions_error_msg;
215
	}
216
217
}
218