Completed
Push — feature/jetpack-packages ( 47fdbc...0e3ac7 )
by
unknown
163:01 queued 155:46
created

Manager::get_option_manager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Jetpack Connection manager class file.
4
 *
5
 * @package jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Connection\Manager_Interface;
11
12
/**
13
 * The Jetpack Connection Manager class that is used as a single gateway between WordPress.com
14
 * and Jetpack.
15
 */
16
class Manager implements Manager_Interface {
17
18
	const SECRETS_MISSING = 'secrets_missing';
19
	const SECRETS_EXPIRED = 'secrets_expired';
20
21
	/**
22
	 * The object for managing options.
23
	 *
24
	 * @var \Automattic\Jetpack\Options\Manager
25
	 */
26
	protected $option_manager;
27
28
	/**
29
	 * The procedure that should be run to generate secrets.
30
	 *
31
	 * @var Callable
32
	 */
33
	protected $secret_callable;
34
35
	/**
36
	 * Initializes all needed hooks and request handlers. Handles API calls, upload
37
	 * requests, authentication requests. Also XMLRPC options requests.
38
	 * Fallback XMLRPC is also a bridge, but probably can be a class that inherits
39
	 * this one. Among other things it should strip existing methods.
40
	 *
41
	 * @param Array $methods an array of API method names for the Connection to accept and
42
	 *                       pass on to existing callables. It's possible to specify whether
43
	 *                       each method should be available for unauthenticated calls or not.
44
	 * @see Jetpack::__construct
45
	 */
46
	public function initialize( $methods ) {
47
		$methods;
48
	}
49
50
	/**
51
	 * Returns true if the current site is connected to WordPress.com.
52
	 *
53
	 * @return Boolean is the site connected?
54
	 */
55
	public function is_active() {
56
		return false;
57
	}
58
59
	/**
60
	 * Returns true if the user with the specified identifier is connected to
61
	 * WordPress.com.
62
	 *
63
	 * @param Integer $user_id the user identifier.
64
	 * @return Boolean is the user connected?
65
	 */
66
	public function is_user_connected( $user_id ) {
67
		return $user_id;
68
	}
69
70
	/**
71
	 * Get the wpcom user data of the current|specified connected user.
72
	 *
73
	 * @param Integer $user_id the user identifier.
74
	 * @return Object the user object.
75
	 */
76
	public function get_connected_user_data( $user_id ) {
77
		return $user_id;
78
	}
79
80
	/**
81
	 * Is the user the connection owner.
82
	 *
83
	 * @param Integer $user_id the user identifier.
84
	 * @return Boolean is the user the connection owner?
85
	 */
86
	public function is_connection_owner( $user_id ) {
87
		return $user_id;
88
	}
89
90
	/**
91
	 * Unlinks the current user from the linked WordPress.com user
92
	 *
93
	 * @param Integer $user_id the user identifier.
94
	 */
95
	public static function disconnect_user( $user_id ) {
96
		return $user_id;
97
	}
98
99
	/**
100
	 * Initializes a transport server, whatever it may be, saves into the object property.
101
	 * Should be changed to be protected.
102
	 */
103
	public function initialize_server() {
104
105
	}
106
107
	/**
108
	 * Checks if the current request is properly authenticated, bails if not.
109
	 * Should be changed to be protected.
110
	 */
111
	public function require_authentication() {
112
113
	}
114
115
	/**
116
	 * Verifies the correctness of the request signature.
117
	 * Should be changed to be protected.
118
	 */
119
	public function verify_signature() {
120
121
	}
122
123
	/**
124
	 * Attempts Jetpack registration which sets up the site for connection. Should
125
	 * remain public because the call to action comes from the current site, not from
126
	 * WordPress.com.
127
	 *
128
	 * @return Integer zero on success, or a bitmask on failure.
129
	 */
130
	public function register() {
131
		return 0;
132
	}
133
134
	/**
135
	 * Returns the callable that would be used to generate secrets.
136
	 *
137
	 * @return Callable a function that returns a secure string to be used as a secret.
138
	 */
139
	protected function get_secret_callable() {
140
		if ( ! isset( $this->secret_callable ) ) {
141
			/**
142
			 * Allows modification of the callable that is used to generate connection secrets.
143
			 *
144
			 * @param Callable a function or method that returns a secret string.
145
			 */
146
			$this->secret_callable = apply_filters( 'jetpack_connection_secret_generator', 'wp_generate_password' );
147
		}
148
149
		return $this->secret_callable;
150
	}
151
152
	/**
153
	 * Generates two secret tokens and the end of life timestamp for them.
154
	 *
155
	 * @param String  $action  The action name.
156
	 * @param Integer $user_id The user identifier.
157
	 * @param Integer $exp     Expiration time in seconds.
158
	 */
159
	public function generate_secrets( $action, $user_id, $exp ) {
160
		$callable = $this->get_secret_callable();
161
162
		$secret_name = 'jetpack_' . $action . '_' . $user_id;
163
		$secrets     = \Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
164
165
		if (
166
			isset( $secrets[ $secret_name ] ) &&
167
			$secrets[ $secret_name ]['exp'] > time()
168
		) {
169
			return $secrets[ $secret_name ];
170
		}
171
172
		$secret_value = array(
173
			'secret_1' => call_user_func( $callable ),
174
			'secret_2' => call_user_func( $callable ),
175
			'exp'      => time() + $exp,
176
		);
177
178
		$secrets[ $secret_name ] = $secret_value;
179
180
		\Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
181
		return $secrets[ $secret_name ];
182
	}
183
184
	/**
185
	 * Returns two secret tokens and the end of life timestamp for them.
186
	 *
187
	 * @param String  $action  The action name.
188
	 * @param Integer $user_id The user identifier.
189
	 * @return string|array an array of secrets or an error string.
190
	 */
191
	public function get_secrets( $action, $user_id ) {
192
		$secret_name = 'jetpack_' . $action . '_' . $user_id;
193
		$secrets     = \Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
194
195
		if ( ! isset( $secrets[ $secret_name ] ) ) {
196
			return self::SECRETS_MISSING;
197
		}
198
199
		if ( $secrets[ $secret_name ]['exp'] < time() ) {
200
			$this->delete_secrets( $action, $user_id );
201
			return self::SECRETS_EXPIRED;
202
		}
203
204
		return $secrets[ $secret_name ];
205
	}
206
207
	/**
208
	 * Deletes secret tokens in case they, for example, have expired.
209
	 *
210
	 * @param String  $action  The action name.
211
	 * @param Integer $user_id The user identifier.
212
	 */
213
	public function delete_secrets( $action, $user_id ) {
214
		$secret_name = 'jetpack_' . $action . '_' . $user_id;
215
		$secrets     = \Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
216
		if ( isset( $secrets[ $secret_name ] ) ) {
217
			unset( $secrets[ $secret_name ] );
218
			\Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
219
		}
220
	}
221
222
	/**
223
	 * Responds to a WordPress.com call to register the current site.
224
	 * Should be changed to protected.
225
	 */
226
	public function handle_registration() {
227
228
	}
229
230
	/**
231
	 * Responds to a WordPress.com call to authorize the current user.
232
	 * Should be changed to protected.
233
	 */
234
	public function handle_authorization() {
235
236
	}
237
238
	/**
239
	 * Builds a URL to the Jetpack connection auth page.
240
	 * This needs rethinking.
241
	 *
242
	 * @param bool        $raw If true, URL will not be escaped.
243
	 * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
244
	 *                              If string, will be a custom redirect.
245
	 * @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
246
	 * @param bool        $register If true, will generate a register URL regardless of the existing token, since 4.9.0.
247
	 *
248
	 * @return string Connect URL
249
	 */
250
	public function build_connect_url( $raw, $redirect, $from, $register ) {
251
		return array( $raw, $redirect, $from, $register );
252
	}
253
254
	/**
255
	 * Disconnects from the Jetpack servers.
256
	 * Forgets all connection details and tells the Jetpack servers to do the same.
257
	 */
258
	public function disconnect_site() {
259
260
	}
261
}
262