Completed
Push — add/connection-ui ( 776f55...bf4f6b )
by
unknown
184:54 queued 174:43
created

Initial_State   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_data() 0 8 1
A get_plugins() 0 22 4
A render() 0 3 1
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\Manager;
11
use Automattic\Jetpack\Connection\Plugin_Storage;
12
use Automattic\Jetpack\Connection\REST_Connector;
13
14
/**
15
 * The React initial state.
16
 */
17
class Initial_State {
18
19
	/**
20
	 * The connection manager object.
21
	 *
22
	 * @var Manager
23
	 */
24
	private $manager;
25
26
	/**
27
	 * The constructor.
28
	 */
29
	public function __construct() {
30
		$this->manager = new Manager();
31
	}
32
33
	/**
34
	 * Get the initial state data.
35
	 *
36
	 * @return array
37
	 */
38
	private function get_data() {
39
		return array(
40
			'connectionStatus' => REST_Connector::connection_status( false ) + array( 'isRefreshing' => false ),
41
			'plugins'          => array(
42
				'all' => $this->get_plugins(),
43
			),
44
		);
45
	}
46
47
	/**
48
	 * Retrieve the list of plugins, and mark them as connected/disconnected.
49
	 *
50
	 * @return array
51
	 */
52
	private function get_plugins() {
53
		$plugins          = Plugin_Storage::get_all();
54
		$disabled_plugins = Plugin_Storage::get_all_disabled_plugins();
55
56
		if ( is_wp_error( $plugins ) ) {
57
			$plugins = array();
58
		}
59
60
		array_walk(
61
			$plugins,
62
			function ( &$plugin, $slug ) use ( $disabled_plugins ) {
63
				$plugin = array(
64
					'slug'        => $slug,
65
					'name'        => empty( $plugin['name'] ) ? $slug : $plugin['name'],
66
					'urlInfo'     => empty( $plugin['url_info'] ) ? null : $plugin['url_info'],
67
					'isConnected' => ! in_array( $slug, $disabled_plugins, true ),
68
				);
69
			}
70
		);
71
72
		return array_values( $plugins );
73
	}
74
75
	/**
76
	 * Render the initial state into a JavaScript variable.
77
	 *
78
	 * @return string
79
	 */
80
	public function render() {
81
		return 'var CUI_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_data() ) ) . '"));';
82
	}
83
84
}
85