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