Completed
Push — renovate/slack-web-api-6.x ( 1d54b7...bfe495 )
by
unknown
193:23 queued 183:13
created

Webhooks::handle_authorize()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 0
dl 0
loc 49
rs 8.4905
c 0
b 0
f 0
1
<?php
2
/**
3
 * Connection Webhooks class.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
use Automattic\Jetpack\Roles;
11
use Automattic\Jetpack\Tracking;
12
use Jetpack_Options;
13
14
/**
15
 * Connection Webhooks class.
16
 */
17
class Webhooks {
18
19
	/**
20
	 * The Connection Manager object.
21
	 *
22
	 * @var Manager
23
	 */
24
	private $connection;
25
26
	/**
27
	 * Webhooks constructor.
28
	 *
29
	 * @param Manager $connection The Connection Manager object.
30
	 */
31
	public function __construct( $connection ) {
32
		$this->connection = $connection;
33
	}
34
35
	/**
36
	 * Initialize the webhooks.
37
	 *
38
	 * @param Manager $connection The Connection Manager object.
39
	 */
40
	public static function init( $connection ) {
41
		$webhooks = new static( $connection );
42
43
		add_action( 'init', array( $webhooks, 'controller' ) );
44
	}
45
46
	/**
47
	 * The "controller" decides which handler we need to run.
48
	 */
49
	public function controller() {
50
		// The nonce is verified in specific handlers.
51
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
52
		if ( empty( $_GET['handler'] ) || empty( $_GET['action'] ) || 'jetpack-connection-webhooks' !== $_GET['handler'] ) {
53
			return;
54
		}
55
56
		// The nonce is verified in specific handlers.
57
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
58
		switch ( $_GET['action'] ) {
59
			case 'authorize':
60
				$this->handle_authorize();
61
				break;
62
		}
63
64
		$this->do_exit();
65
	}
66
67
	/**
68
	 * Perform the authorization action.
69
	 */
70
	public function handle_authorize() {
71
		if ( $this->connection->is_active() && $this->connection->is_user_connected() ) {
72
			$redirect_url = apply_filters( 'jetpack_client_authorize_already_authorized_url', admin_url() );
73
			wp_safe_redirect( $redirect_url );
74
75
			return;
76
		}
77
		do_action( 'jetpack_client_authorize_processing' );
78
79
		$data              = stripslashes_deep( $_GET );
80
		$data['auth_type'] = 'client';
81
		$roles             = new Roles();
82
		$role              = $roles->translate_current_user_to_role();
83
		$redirect          = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
84
85
		check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" );
86
87
		$tracking = new Tracking();
88
89
		$result = $this->connection->authorize( $data );
90
91
		if ( is_wp_error( $result ) ) {
92
			do_action( 'jetpack_client_authorize_error', $result );
93
94
			$tracking->record_user_event(
95
				'jpc_client_authorize_fail',
96
				array(
97
					'error_code'    => $result->get_error_code(),
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
					'error_message' => $result->get_error_message(),
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
				)
100
			);
101
		} else {
102
			/**
103
			 * Fires after the Jetpack client is authorized to communicate with WordPress.com.
104
			 *
105
			 * @param int Jetpack Blog ID.
106
			 *
107
			 * @since 4.2.0
108
			 */
109
			do_action( 'jetpack_client_authorized', Jetpack_Options::get_option( 'id' ) );
110
111
			$tracking->record_user_event( 'jpc_client_authorize_success' );
112
		}
113
114
		$fallback_redirect = apply_filters( 'jetpack_client_authorize_fallback_url', admin_url() );
115
		$redirect          = wp_validate_redirect( $redirect ) ? $redirect : $fallback_redirect;
116
117
		wp_safe_redirect( $redirect );
118
	}
119
120
	/**
121
	 * The `exit` is wrapped into a method so we could mock it.
122
	 */
123
	protected function do_exit() {
124
		exit;
125
	}
126
}
127