Completed
Push — update/refactor-rna-connection ( baa613...32e2c6 )
by
unknown
50:09 queued 39:03
created

Initial_State::can_use_connection_iframe()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * The React initial state.
4
 *
5
 * @package automattic/jetpack-connection-ui
6
 */
7
8
namespace Automattic\Jetpack\ConnectionUI;
9
10
use Automattic\Jetpack\Connection\REST_Connector;
11
use Automattic\Jetpack\Constants;
12
use Automattic\Jetpack\Device_Detection\User_Agent_Info;
13
14
/**
15
 * The React initial state.
16
 */
17
class Initial_State {
18
19
	/**
20
	 * Get the initial state data.
21
	 *
22
	 * @return array
23
	 */
24
	private function get_data() {
25
		return array(
26
			'connectionStatus' => REST_Connector::connection_status( false ),
27
			'API'              => array(
28
				'WP_API_root'       => esc_url_raw( rest_url() ),
29
				'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
30
				'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
31
			),
32
			'connectionData'   => array(
33
				'doNotUseConnectionIframe' => ! $this->can_use_connection_iframe(),
34
			),
35
		);
36
	}
37
38
	/**
39
	 * Whether we can the connection iframe.
40
	 *
41
	 * @return bool
42
	 */
43
	private function can_use_connection_iframe() {
44
		global $is_safari;
45
46
		/**
47
		 * Filters whether the connection manager should use the iframe authorization
48
		 * flow instead of the regular redirect-based flow.
49
		 *
50
		 * @since 8.3.0
51
		 *
52
		 * @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false.
53
		 */
54
		$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false );
55
56
		if ( ! $iframe_flow ) {
57
			return false;
58
		}
59
60
		return ! $is_safari && ! User_Agent_Info::is_opera_desktop() && ! Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' );
61
	}
62
63
	/**
64
	 * Render the initial state into a JavaScript variable.
65
	 *
66
	 * @return string
67
	 */
68
	public function render() {
69
		add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' );
70
71
		return 'var CUI_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));';
72
	}
73
74
}
75