Completed
Push — update/backup-plugin-add-conne... ( 17c076 )
by
unknown
145:05 queued 134:28
created

Initial_State::get_data()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 0
dl 18
loc 18
rs 9.3554
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
		global $is_safari;
39
40
		return array(
41
			'connectionStatus' => REST_Connector::connection_status( false ),
42
			'API'              => array(
43
				'WP_API_root'       => esc_url_raw( rest_url() ),
44
				'WP_API_nonce'      => wp_create_nonce( 'wp_rest' ),
45
				'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
46
			),
47
			'connectionData'   => array(
48
				'doNotUseConnectionIframe' => $is_safari || User_Agent_Info::is_opera_desktop() || Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ),
49
				'authorizationUrl'         => ( $this->manager->is_connected() && ! $this->manager->is_user_connected() )
50
					? $this->manager->get_authorization_url( null, admin_url( 'tools.php?page=wpcom-connection-manager' ) )
51
					: null,
52
			),
53
		);
54
	}
55
56
	/**
57
	 * Render the initial state into a JavaScript variable.
58
	 *
59
	 * @return string
60
	 */
61
	public function render() {
62
		return 'var JPBACKUP_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));';
63
	}
64
65
}
66