Completed
Push — update/use-identity-crisis-pac... ( 441071...0442d9 )
by
unknown
233:14 queued 223:11
created

Initial_State::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

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