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