Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Manager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Manager implements Manager_Interface { |
||
18 | |||
19 | const SECRETS_MISSING = 'secrets_missing'; |
||
20 | const SECRETS_EXPIRED = 'secrets_expired'; |
||
21 | const SECRETS_OPTION_NAME = 'jetpack_secrets'; |
||
22 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
23 | const JETPACK_MASTER_USER = true; |
||
24 | |||
25 | /** |
||
26 | * The procedure that should be run to generate secrets. |
||
27 | * |
||
28 | * @var Callable |
||
29 | */ |
||
30 | protected $secret_callable; |
||
31 | |||
32 | /** |
||
33 | * A copy of the raw POST data for signature verification purposes. |
||
34 | * |
||
35 | * @var String |
||
36 | */ |
||
37 | protected $raw_post_data; |
||
38 | |||
39 | /** |
||
40 | * Verification data needs to be stored to properly verify everything. |
||
41 | * |
||
42 | * @var Object |
||
43 | */ |
||
44 | private $xmlrpc_verification = null; |
||
45 | |||
46 | /** |
||
47 | * Initializes required listeners. This is done separately from the constructors |
||
48 | * because some objects sometimes need to instantiate separate objects of this class. |
||
49 | * |
||
50 | * @todo Implement a proper nonce verification. |
||
51 | */ |
||
52 | public function init() { |
||
82 | |||
83 | /** |
||
84 | * Sets up the XMLRPC request handlers. |
||
85 | * |
||
86 | * @param Array $request_params incoming request parameters. |
||
87 | * @param Boolean $is_active whether the connection is currently active. |
||
88 | * @param Boolean $is_signed whether the signature check has been successful. |
||
89 | * @param \Jetpack_XMLRPC_Server $xmlrpc_server (optional) an instance of the server to use instead of instantiating a new one. |
||
90 | */ |
||
91 | public function setup_xmlrpc_handlers( |
||
169 | |||
170 | /** |
||
171 | * Initializes the REST API connector on the init hook. |
||
172 | */ |
||
173 | public function initialize_rest_api_registration_connector() { |
||
176 | |||
177 | /** |
||
178 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
179 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
180 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
181 | * which is accessible via a different URI. Most of the below is copied directly |
||
182 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
183 | * |
||
184 | * @todo Tighten $wp_xmlrpc_server_class a bit to make sure it doesn't do bad things. |
||
185 | */ |
||
186 | public function alternate_xmlrpc() { |
||
187 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
188 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
||
189 | global $HTTP_RAW_POST_DATA; |
||
190 | |||
191 | // Some browser-embedded clients send cookies. We don't want them. |
||
192 | $_COOKIE = array(); |
||
193 | |||
194 | // A fix for mozBlog and other cases where '<?xml' isn't on the very first line. |
||
195 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
196 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
197 | } |
||
198 | |||
199 | // phpcs:enable |
||
200 | |||
201 | include_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
202 | include_once ABSPATH . WPINC . '/class-IXR.php'; |
||
203 | include_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'; |
||
204 | |||
205 | /** |
||
206 | * Filters the class used for handling XML-RPC requests. |
||
207 | * |
||
208 | * @since 3.1.0 |
||
209 | * |
||
210 | * @param string $class The name of the XML-RPC server class. |
||
211 | */ |
||
212 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
213 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class(); |
||
214 | |||
215 | // Fire off the request. |
||
216 | nocache_headers(); |
||
217 | $wp_xmlrpc_server->serve_request(); |
||
218 | |||
219 | exit; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
224 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
225 | * ensure that Core and other plugins' methods are not exposed. |
||
226 | * |
||
227 | * @param array $methods a list of registered WordPress XMLRPC methods. |
||
228 | * @return array filtered $methods |
||
229 | */ |
||
230 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
231 | $jetpack_methods = array(); |
||
232 | |||
233 | foreach ( $methods as $method => $callback ) { |
||
234 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
235 | $jetpack_methods[ $method ] = $callback; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | return $jetpack_methods; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Removes all other authentication methods not to allow other |
||
244 | * methods to validate unauthenticated requests. |
||
245 | */ |
||
246 | public function require_jetpack_authentication() { |
||
247 | // Don't let anyone authenticate. |
||
248 | $_COOKIE = array(); |
||
249 | remove_all_filters( 'authenticate' ); |
||
250 | remove_all_actions( 'wp_login_failed' ); |
||
251 | |||
252 | if ( $this->is_active() ) { |
||
253 | // Allow Jetpack authentication. |
||
254 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
260 | * |
||
261 | * @param WP_User|Mixed $user user object if authenticated. |
||
262 | * @param String $username username. |
||
263 | * @param String $password password string. |
||
264 | * @return WP_User|Mixed authenticated user or error. |
||
265 | */ |
||
266 | public function authenticate_jetpack( $user, $username, $password ) { |
||
267 | if ( is_a( $user, '\\WP_User' ) ) { |
||
268 | return $user; |
||
269 | } |
||
270 | |||
271 | $token_details = $this->verify_xml_rpc_signature(); |
||
272 | |||
273 | if ( ! $token_details ) { |
||
274 | return $user; |
||
275 | } |
||
276 | |||
277 | if ( 'user' !== $token_details['type'] ) { |
||
278 | return $user; |
||
279 | } |
||
280 | |||
281 | if ( ! $token_details['user_id'] ) { |
||
282 | return $user; |
||
283 | } |
||
284 | |||
285 | nocache_headers(); |
||
286 | |||
287 | return new \WP_User( $token_details['user_id'] ); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Verifies the signature of the current request. |
||
292 | * |
||
293 | * @return false|array |
||
294 | */ |
||
295 | public function verify_xml_rpc_signature() { |
||
296 | if ( is_null( $this->xmlrpc_verification ) ) { |
||
297 | $this->xmlrpc_verification = $this->internal_verify_xml_rpc_signature(); |
||
298 | |||
299 | if ( is_wp_error( $this->xmlrpc_verification ) ) { |
||
300 | /** |
||
301 | * Action for logging XMLRPC signature verification errors. This data is sensitive. |
||
302 | * |
||
303 | * Error codes: |
||
304 | * - malformed_token |
||
305 | * - malformed_user_id |
||
306 | * - unknown_token |
||
307 | * - could_not_sign |
||
308 | * - invalid_nonce |
||
309 | * - signature_mismatch |
||
310 | * |
||
311 | * @since 7.5.0 |
||
312 | * |
||
313 | * @param WP_Error $signature_verification_error The verification error |
||
314 | */ |
||
315 | do_action( 'jetpack_verify_signature_error', $this->xmlrpc_verification ); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return is_wp_error( $this->xmlrpc_verification ) ? false : $this->xmlrpc_verification; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Verifies the signature of the current request. |
||
324 | * |
||
325 | * This function has side effects and should not be used. Instead, |
||
326 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
327 | * |
||
328 | * @internal |
||
329 | */ |
||
330 | private function internal_verify_xml_rpc_signature() { |
||
331 | // It's not for us. |
||
332 | if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
||
333 | return false; |
||
334 | } |
||
335 | |||
336 | $signature_details = array( |
||
337 | 'token' => isset( $_GET['token'] ) ? wp_unslash( $_GET['token'] ) : '', |
||
338 | 'timestamp' => isset( $_GET['timestamp'] ) ? wp_unslash( $_GET['timestamp'] ) : '', |
||
339 | 'nonce' => isset( $_GET['nonce'] ) ? wp_unslash( $_GET['nonce'] ) : '', |
||
340 | 'body_hash' => isset( $_GET['body-hash'] ) ? wp_unslash( $_GET['body-hash'] ) : '', |
||
341 | 'method' => wp_unslash( $_SERVER['REQUEST_METHOD'] ), |
||
342 | 'url' => wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ), // Temp - will get real signature URL later. |
||
343 | 'signature' => isset( $_GET['signature'] ) ? wp_unslash( $_GET['signature'] ) : '', |
||
344 | ); |
||
345 | |||
346 | @list( $token_key, $version, $user_id ) = explode( ':', wp_unslash( $_GET['token'] ) ); |
||
347 | if ( |
||
348 | empty( $token_key ) |
||
349 | || |
||
350 | empty( $version ) || strval( JETPACK__API_VERSION ) !== $version |
||
351 | ) { |
||
352 | return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details' ) ); |
||
353 | } |
||
354 | |||
355 | if ( '0' === $user_id ) { |
||
356 | $token_type = 'blog'; |
||
357 | $user_id = 0; |
||
358 | } else { |
||
359 | $token_type = 'user'; |
||
360 | if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
||
361 | return new \WP_Error( |
||
362 | 'malformed_user_id', |
||
363 | 'Malformed user_id in request', |
||
364 | compact( 'signature_details' ) |
||
365 | ); |
||
366 | } |
||
367 | $user_id = (int) $user_id; |
||
368 | |||
369 | $user = new \WP_User( $user_id ); |
||
370 | if ( ! $user || ! $user->exists() ) { |
||
371 | return new \WP_Error( |
||
372 | 'unknown_user', |
||
373 | sprintf( 'User %d does not exist', $user_id ), |
||
374 | compact( 'signature_details' ) |
||
375 | ); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | $token = $this->get_access_token( $user_id, $token_key, false ); |
||
380 | if ( is_wp_error( $token ) ) { |
||
381 | $token->add_data( compact( 'signature_details' ) ); |
||
382 | return $token; |
||
383 | } elseif ( ! $token ) { |
||
384 | return new \WP_Error( |
||
385 | 'unknown_token', |
||
386 | sprintf( 'Token %s:%s:%d does not exist', $token_key, $version, $user_id ), |
||
387 | compact( 'signature_details' ) |
||
388 | ); |
||
389 | } |
||
390 | |||
391 | $jetpack_signature = new \Jetpack_Signature( $token->secret, (int) \Jetpack_Options::get_option( 'time_diff' ) ); |
||
392 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
||
393 | if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
||
394 | $post_data = $_POST; |
||
395 | $file_hashes = array(); |
||
396 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
397 | if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
||
398 | continue; |
||
399 | } |
||
400 | $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
||
401 | $file_hashes[ $post_data_key ] = $post_data_value; |
||
402 | } |
||
403 | |||
404 | foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
||
405 | unset( $post_data[ "_jetpack_file_hmac_{$post_data_key}" ] ); |
||
406 | $post_data[ $post_data_key ] = $post_data_value; |
||
407 | } |
||
408 | |||
409 | ksort( $post_data ); |
||
410 | |||
411 | $body = http_build_query( stripslashes_deep( $post_data ) ); |
||
412 | } elseif ( is_null( $this->raw_post_data ) ) { |
||
413 | $body = file_get_contents( 'php://input' ); |
||
414 | } else { |
||
415 | $body = null; |
||
416 | } |
||
417 | // phpcs:enable |
||
418 | |||
419 | $signature = $jetpack_signature->sign_current_request( |
||
420 | array( 'body' => is_null( $body ) ? $this->raw_post_data : $body ) |
||
421 | ); |
||
422 | |||
423 | $signature_details['url'] = $jetpack_signature->current_request_url; |
||
424 | |||
425 | if ( ! $signature ) { |
||
426 | return new \WP_Error( |
||
427 | 'could_not_sign', |
||
428 | 'Unknown signature error', |
||
429 | compact( 'signature_details' ) |
||
430 | ); |
||
431 | } elseif ( is_wp_error( $signature ) ) { |
||
432 | return $signature; |
||
433 | } |
||
434 | |||
435 | $timestamp = (int) $_GET['timestamp']; |
||
436 | $nonce = stripslashes( (string) $_GET['nonce'] ); |
||
437 | |||
438 | // Use up the nonce regardless of whether the signature matches. |
||
439 | if ( ! $this->add_nonce( $timestamp, $nonce ) ) { |
||
440 | return new \WP_Error( |
||
441 | 'invalid_nonce', |
||
442 | 'Could not add nonce', |
||
443 | compact( 'signature_details' ) |
||
444 | ); |
||
445 | } |
||
446 | |||
447 | // Be careful about what you do with this debugging data. |
||
448 | // If a malicious requester has access to the expected signature, |
||
449 | // bad things might be possible. |
||
450 | $signature_details['expected'] = $signature; |
||
451 | |||
452 | if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
||
453 | return new \WP_Error( |
||
454 | 'signature_mismatch', |
||
455 | 'Signature mismatch', |
||
456 | compact( 'signature_details' ) |
||
457 | ); |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Action for additional token checking. |
||
462 | * |
||
463 | * @since 7.7.0 |
||
464 | * |
||
465 | * @param Array $post_data request data. |
||
466 | * @param Array $token_data token data. |
||
467 | */ |
||
468 | return apply_filters( |
||
469 | 'jetpack_signature_check_token', |
||
470 | array( |
||
471 | 'type' => $token_type, |
||
472 | 'token_key' => $token_key, |
||
473 | 'user_id' => $token->external_user_id, |
||
474 | ), |
||
475 | $token, |
||
476 | $this->raw_post_data |
||
477 | ); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Returns true if the current site is connected to WordPress.com. |
||
482 | * |
||
483 | * @return Boolean is the site connected? |
||
484 | */ |
||
485 | public function is_active() { |
||
486 | return (bool) $this->get_access_token( self::JETPACK_MASTER_USER ); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Returns true if the site has both a token and a blog id, which indicates a site has been registered. |
||
491 | * |
||
492 | * @access public |
||
493 | * |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function is_registered() { |
||
497 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
||
498 | $has_token = $this->is_active(); |
||
499 | return $blog_id && $has_token; |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Returns true if the user with the specified identifier is connected to |
||
504 | * WordPress.com. |
||
505 | * |
||
506 | * @param Integer|Boolean $user_id the user identifier. |
||
507 | * @return Boolean is the user connected? |
||
508 | */ |
||
509 | public function is_user_connected( $user_id = false ) { |
||
517 | |||
518 | /** |
||
519 | * Get the wpcom user data of the current|specified connected user. |
||
520 | * |
||
521 | * @param Integer $user_id the user identifier. |
||
522 | * @return Object the user object. |
||
523 | */ |
||
524 | View Code Duplication | public function get_connected_user_data( $user_id = null ) { |
|
551 | |||
552 | /** |
||
553 | * Returns true if the provided user is the Jetpack connection owner. |
||
554 | * If user ID is not specified, the current user will be used. |
||
555 | * |
||
556 | * @param Integer|Boolean $user_id the user identifier. False for current user. |
||
557 | * @return Boolean True the user the connection owner, false otherwise. |
||
558 | */ |
||
559 | public function is_connection_owner( $user_id = false ) { |
||
568 | |||
569 | /** |
||
570 | * Unlinks the current user from the linked WordPress.com user |
||
571 | * |
||
572 | * @param Integer $user_id the user identifier. |
||
573 | */ |
||
574 | public static function disconnect_user( $user_id ) { |
||
577 | |||
578 | /** |
||
579 | * Returns the requested Jetpack API URL. |
||
580 | * |
||
581 | * @param String $relative_url the relative API path. |
||
582 | * @return String API URL. |
||
583 | */ |
||
584 | public function api_url( $relative_url ) { |
||
593 | |||
594 | /** |
||
595 | * Attempts Jetpack registration which sets up the site for connection. Should |
||
596 | * remain public because the call to action comes from the current site, not from |
||
597 | * WordPress.com. |
||
598 | * |
||
599 | * @param String $api_endpoint (optional) an API endpoint to use, defaults to 'register'. |
||
600 | * @return Integer zero on success, or a bitmask on failure. |
||
601 | */ |
||
602 | public function register( $api_endpoint = 'register' ) { |
||
737 | |||
738 | /** |
||
739 | * Takes the response from the Jetpack register new site endpoint and |
||
740 | * verifies it worked properly. |
||
741 | * |
||
742 | * @since 2.6 |
||
743 | * |
||
744 | * @param Mixed $response the response object, or the error object. |
||
745 | * @return string|WP_Error A JSON object on success or Jetpack_Error on failures |
||
746 | **/ |
||
747 | protected function validate_remote_register_response( $response ) { |
||
816 | |||
817 | /** |
||
818 | * Adds a used nonce to a list of known nonces. |
||
819 | * |
||
820 | * @param int $timestamp the current request timestamp. |
||
821 | * @param string $nonce the nonce value. |
||
822 | * @return bool whether the nonce is unique or not. |
||
823 | */ |
||
824 | public function add_nonce( $timestamp, $nonce ) { |
||
862 | |||
863 | /** |
||
864 | * Cleans nonces that were saved when calling ::add_nonce. |
||
865 | * |
||
866 | * @todo Properly prepare the query before executing it. |
||
867 | * |
||
868 | * @param bool $all whether to clean even non-expired nonces. |
||
869 | */ |
||
870 | public function clean_nonces( $all = false ) { |
||
891 | |||
892 | /** |
||
893 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
894 | * |
||
895 | * Based on local php max_execution_time in php.ini |
||
896 | * |
||
897 | * @since 5.4 |
||
898 | * @return int |
||
899 | **/ |
||
900 | public function get_max_execution_time() { |
||
909 | |||
910 | /** |
||
911 | * Sets a minimum request timeout, and returns the current timeout |
||
912 | * |
||
913 | * @since 5.4 |
||
914 | * @param Integer $min_timeout the minimum timeout value. |
||
915 | **/ |
||
916 | View Code Duplication | public function set_min_time_limit( $min_timeout ) { |
|
924 | |||
925 | /** |
||
926 | * Get our assumed site creation date. |
||
927 | * Calculated based on the earlier date of either: |
||
928 | * - Earliest admin user registration date. |
||
929 | * - Earliest date of post of any post type. |
||
930 | * |
||
931 | * @since 7.2.0 |
||
932 | * |
||
933 | * @return string Assumed site creation date and time. |
||
934 | */ |
||
935 | View Code Duplication | public function get_assumed_site_creation_date() { |
|
966 | |||
967 | /** |
||
968 | * Adds the activation source string as a parameter to passed arguments. |
||
969 | * |
||
970 | * @param Array $args arguments that need to have the source added. |
||
971 | * @return Array $amended arguments. |
||
972 | */ |
||
973 | View Code Duplication | public static function apply_activation_source_to_args( $args ) { |
|
986 | |||
987 | /** |
||
988 | * Returns the callable that would be used to generate secrets. |
||
989 | * |
||
990 | * @return Callable a function that returns a secure string to be used as a secret. |
||
991 | */ |
||
992 | protected function get_secret_callable() { |
||
1004 | |||
1005 | /** |
||
1006 | * Generates two secret tokens and the end of life timestamp for them. |
||
1007 | * |
||
1008 | * @param String $action The action name. |
||
1009 | * @param Integer $user_id The user identifier. |
||
1010 | * @param Integer $exp Expiration time in seconds. |
||
1011 | */ |
||
1012 | public function generate_secrets( $action, $user_id, $exp ) { |
||
1040 | |||
1041 | /** |
||
1042 | * Returns two secret tokens and the end of life timestamp for them. |
||
1043 | * |
||
1044 | * @param String $action The action name. |
||
1045 | * @param Integer $user_id The user identifier. |
||
1046 | * @return string|array an array of secrets or an error string. |
||
1047 | */ |
||
1048 | public function get_secrets( $action, $user_id ) { |
||
1066 | |||
1067 | /** |
||
1068 | * Deletes secret tokens in case they, for example, have expired. |
||
1069 | * |
||
1070 | * @param String $action The action name. |
||
1071 | * @param Integer $user_id The user identifier. |
||
1072 | */ |
||
1073 | public function delete_secrets( $action, $user_id ) { |
||
1084 | |||
1085 | /** |
||
1086 | * Responds to a WordPress.com call to register the current site. |
||
1087 | * Should be changed to protected. |
||
1088 | * |
||
1089 | * @param array $registration_data Array of [ secret_1, user_id ]. |
||
1090 | */ |
||
1091 | public function handle_registration( array $registration_data ) { |
||
1099 | |||
1100 | /** |
||
1101 | * Verify a Previously Generated Secret. |
||
1102 | * |
||
1103 | * @param string $action The type of secret to verify. |
||
1104 | * @param string $secret_1 The secret string to compare to what is stored. |
||
1105 | * @param int $user_id The user ID of the owner of the secret. |
||
1106 | */ |
||
1107 | protected function verify_secrets( $action, $secret_1, $user_id ) { |
||
1223 | |||
1224 | /** |
||
1225 | * Responds to a WordPress.com call to authorize the current user. |
||
1226 | * Should be changed to protected. |
||
1227 | */ |
||
1228 | public function handle_authorization() { |
||
1231 | |||
1232 | /** |
||
1233 | * Builds a URL to the Jetpack connection auth page. |
||
1234 | * This needs rethinking. |
||
1235 | * |
||
1236 | * @param bool $raw If true, URL will not be escaped. |
||
1237 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
1238 | * If string, will be a custom redirect. |
||
1239 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
1240 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0. |
||
1241 | * |
||
1242 | * @return string Connect URL |
||
1243 | */ |
||
1244 | public function build_connect_url( $raw, $redirect, $from, $register ) { |
||
1247 | |||
1248 | /** |
||
1249 | * Disconnects from the Jetpack servers. |
||
1250 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
1251 | */ |
||
1252 | public function disconnect_site() { |
||
1255 | |||
1256 | /** |
||
1257 | * The Base64 Encoding of the SHA1 Hash of the Input. |
||
1258 | * |
||
1259 | * @param string $text The string to hash. |
||
1260 | * @return string |
||
1261 | */ |
||
1262 | public function sha1_base64( $text ) { |
||
1265 | |||
1266 | /** |
||
1267 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
1268 | * |
||
1269 | * @param string $domain The domain to check. |
||
1270 | * |
||
1271 | * @return bool|WP_Error |
||
1272 | */ |
||
1273 | public function is_usable_domain( $domain ) { |
||
1360 | |||
1361 | /** |
||
1362 | * Gets the requested token. |
||
1363 | * |
||
1364 | * Tokens are one of two types: |
||
1365 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
1366 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
1367 | * are not associated with a user account. They represent the site's connection with |
||
1368 | * the Jetpack servers. |
||
1369 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
1370 | * |
||
1371 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
1372 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
1373 | * over the network; it's used only for signing things. |
||
1374 | * |
||
1375 | * Blog Tokens can be "Normal" or "Special". |
||
1376 | * * Normal: The result of a normal connection flow. They look like |
||
1377 | * "{$random_string_1}.{$random_string_2}" |
||
1378 | * That is, $token_key and $private are both random strings. |
||
1379 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
1380 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
1381 | * constant (rare). |
||
1382 | * * Special: A connection token for sites that have gone through an alternative |
||
1383 | * connection flow. They look like: |
||
1384 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
1385 | * That is, $private is a random string and $token_key has a special structure with |
||
1386 | * lots of semicolons. |
||
1387 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
1388 | * JETPACK_BLOG_TOKEN constant. |
||
1389 | * |
||
1390 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
1391 | * Special Blog Tokens always do. |
||
1392 | * |
||
1393 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
1394 | * order: |
||
1395 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
1396 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
1397 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
1398 | * |
||
1399 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
1400 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
1401 | * @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found. |
||
1402 | * |
||
1403 | * @return object|false |
||
1404 | */ |
||
1405 | public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) { |
||
1491 | |||
1492 | /** |
||
1493 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths |
||
1494 | * since it is passed by reference to various methods. |
||
1495 | * Capture it here so we can verify the signature later. |
||
1496 | * |
||
1497 | * @param Array $methods an array of available XMLRPC methods. |
||
1498 | * @return Array the same array, since this method doesn't add or remove anything. |
||
1499 | */ |
||
1500 | public function xmlrpc_methods( $methods ) { |
||
1504 | |||
1505 | /** |
||
1506 | * Resets the raw post data parameter for testing purposes. |
||
1507 | */ |
||
1508 | public function reset_raw_post_data() { |
||
1511 | |||
1512 | /** |
||
1513 | * Registering an additional method. |
||
1514 | * |
||
1515 | * @param Array $methods an array of available XMLRPC methods. |
||
1516 | * @return Array the amended array in case the method is added. |
||
1517 | */ |
||
1518 | public function public_xmlrpc_methods( $methods ) { |
||
1524 | |||
1525 | /** |
||
1526 | * Handles a getOptions XMLRPC method call. |
||
1527 | * |
||
1528 | * @todo Audit whether we really need to use strings without textdomains. |
||
1529 | * |
||
1530 | * @param Array $args method call arguments. |
||
1531 | * @return an amended XMLRPC server options array. |
||
1532 | */ |
||
1533 | public function jetpack_getOptions( $args ) { |
||
1574 | |||
1575 | /** |
||
1576 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
1577 | * |
||
1578 | * @todo Audit whether we really need to use strings without textdomains. |
||
1579 | * |
||
1580 | * @param Array $options standard Core options. |
||
1581 | * @return Array amended options. |
||
1582 | */ |
||
1583 | public function xmlrpc_options( $options ) { |
||
1601 | |||
1602 | /** |
||
1603 | * Resets the saved authentication state in between testing requests. |
||
1604 | */ |
||
1605 | public function reset_saved_auth_state() { |
||
1608 | } |
||
1609 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.