Completed
Push — renovate/major-eslint-and-plug... ( 32931a...ea602c )
by Jeremy
41:27 queued 30:19
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-backup
6
 */
7
8
use Automattic\Jetpack\Connection\Manager;
9
use Automattic\Jetpack\Connection\REST_Connector;
10
use Automattic\Jetpack\Constants;
11
use Automattic\Jetpack\Device_Detection\User_Agent_Info;
12
13
/**
14
 * The React initial state.
15
 */
16 View Code Duplication
class Initial_State {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
18
	/**
19
	 * The connection manager object.
20
	 *
21
	 * @var Manager
22
	 */
23
	private $manager;
24
25
	/**
26
	 * The constructor.
27
	 */
28
	public function __construct() {
29
		$this->manager = new Manager();
30
	}
31
32
	/**
33
	 * Get the initial state data.
34
	 *
35
	 * @return array
36
	 */
37
	private function get_data() {
38
		return array(
39
			'connectionStatus' => REST_Connector::connection_status( false ),
40
			'API'              => array(
41
				'WP_API_root'       => esc_url_raw( rest_url() ),
42
				'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
43
				'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
44
			),
45
			'connectionData'   => array(
46
				'doNotUseConnectionIframe' => ! $this->can_use_connection_iframe(),
47
				'authorizationUrl'         => ( $this->manager->is_connected() && ! $this->manager->is_user_connected() )
48
					? $this->manager->get_authorization_url( null, admin_url( 'admin.php?page=jetpack-backup' ) )
49
					: null,
50
			),
51
		);
52
	}
53
54
	/**
55
	 * Whether we can the connection iframe.
56
	 *
57
	 * @return bool
58
	 */
59
	private function can_use_connection_iframe() {
60
		global $is_safari;
61
62
		/**
63
		 * Filters whether the connection manager should use the iframe authorization
64
		 * flow instead of the regular redirect-based flow.
65
		 *
66
		 * @since 8.3.0
67
		 *
68
		 * @param Boolean $is_iframe_flow_used should the iframe flow be used, defaults to false.
69
		 */
70
		$iframe_flow = apply_filters( 'jetpack_use_iframe_authorization_flow', false );
71
72
		if ( ! $iframe_flow ) {
73
			return false;
74
		}
75
76
		return ! $is_safari && ! User_Agent_Info::is_opera_desktop() && ! Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' );
77
	}
78
79
	/**
80
	 * Render the initial state into a JavaScript variable.
81
	 *
82
	 * @return string
83
	 */
84
	public function render() {
85
		add_action( 'jetpack_use_iframe_authorization_flow', '__return_true' );
86
87
		return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));';
88
	}
89
90
}
91