AddonList   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
eloc 66
dl 0
loc 217
ccs 0
cts 91
cp 0
rs 10
c 5
b 0
f 4
wmc 27

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A get_addon_by_name() 0 6 2
A is_addon_active() 0 8 2
A is_addon_installed() 0 8 2
A get_inactive_addons() 0 10 3
A render() 0 6 1
A parse_response() 0 6 2
A get_api_url() 0 2 1
A render_addons() 0 7 3
A render_empty_list() 0 10 1
A build_addon_list() 0 9 2
A get_addons() 0 19 5
1
<?php namespace EmailLog\Addon;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Retrieve the list of add-ons and render them.
7
 *
8
 * @since 2.0.0
9
 */
10
class AddonList {
11
12
	const CACHE_EXPIRY_IN_HRS = 12;
13
	const CACHE_KEY           = 'el_addon_data';
14
15
	/**
16
	 * Add-on list.
17
	 *
18
	 * @var Addon[]
19
	 */
20
	protected $addons;
21
22
	/**
23
	 * Store URL.
24
	 *
25
	 * @var string
26
	 */
27
	protected $store_url;
28
29
	/**
30
	 * Create a list of add-ons.
31
	 *
32
	 * @param Addon[]|null $addons    List of Add-ons. If not passed, they will be automatically loaded.
33
	 * @param string|null  $store_url Store url.
34
	 */
35
	public function __construct( $addons = null, $store_url = null ) {
36
		if ( null === $store_url ) {
37
			$email_log = email_log();
38
			$store_url = $email_log->get_store_url();
39
		}
40
		$this->store_url = $store_url;
41
42
		if ( null === $addons ) {
43
			$addons = $this->get_addons();
44
		}
45
46
		$this->addons = $addons;
47
	}
48
49
	/**
50
	 * Get an add-on by name.
51
	 *
52
	 * @param string $name Add-on name.
53
	 *
54
	 * @return \EmailLog\Addon\Addon|false Add-on if found, False otherwise.
55
	 */
56
	public function get_addon_by_name( $name ) {
57
		if ( array_key_exists( $name, $this->addons ) ) {
58
			return $this->addons[ $name ];
59
		}
60
61
		return false;
62
	}
63
64
	/**
65
	 * Is an add-on active?
66
	 *
67
	 * @since 2.4.0
68
	 *
69
	 * @param string $name Add-on name.
70
	 *
71
	 * @return bool True if add-on is present and is active, false otherwise.
72
	 */
73
	public function is_addon_active( $name ) {
74
		$addon = $this->get_addon_by_name( $name );
75
76
		if ( ! $addon instanceof Addon ) {
77
			return false;
78
		}
79
80
		return $addon->is_active();
81
	}
82
83
	/**
84
	 * Is an add-on installed?
85
	 *
86
	 * @since 2.4.0
87
	 *
88
	 * @param string $name Add-on name.
89
	 *
90
	 * @return bool True if add-on is present and is installed, false otherwise.
91
	 */
92
	public function is_addon_installed( $name ) {
93
		$addon = $this->get_addon_by_name( $name );
94
95
		if ( ! $addon instanceof Addon ) {
96
			return false;
97
		}
98
99
		return $addon->is_installed();
100
	}
101
102
	/**
103
	 * Get all add-ons that are not active (either not installed or not activated).
104
	 *
105
	 * @return \EmailLog\Addon\Addon[] List of inactive add-ons.
106
	 */
107
	public function get_inactive_addons() {
108
		$inactive_addons = array();
109
110
		foreach ( $this->addons as $addon ) {
111
			if ( ! $addon->is_active() ) {
112
				$inactive_addons[] = $addon;
113
			}
114
		}
115
116
		return $inactive_addons;
117
	}
118
119
	/**
120
	 * Setup page to render the list of add-ons.
121
	 */
122
	public function render() {
123
		?>
124
125
		<div class="el-addon-list">
126
			<?php $this->render_addons(); ?>
127
		</div> <!-- .el-container -->
128
		<?php
129
	}
130
131
	/**
132
	 * Retrieve the list of add-ons by calling the store API.
133
	 * If the store API is down, then read the data from the local JSON file.
134
	 *
135
	 * @return Addon[] List of add-ons, empty array if API call fails.
136
	 */
137
	public function get_addons() {
138
		$json = get_transient( self::CACHE_KEY );
139
		if ( false === $json ) {
140
			$response = wp_remote_get( $this->get_api_url() );
141
142
			if ( is_wp_error( $response ) || ! is_array( $response ) ) {
143
				return [];
144
			}
145
146
			$json = json_decode( wp_remote_retrieve_body( $response ), true );
147
148
			if ( ! is_array( $json ) ) {
149
				return array();
150
			}
151
152
			set_transient( self::CACHE_KEY, $json, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
153
		}
154
155
		return $this->parse_response( $json );
156
	}
157
158
	/**
159
	 * Parse the response and get the list of add-on.
160
	 *
161
	 * @param array $data JSON Data array.
162
	 *
163
	 * @return array List of Add-ons.
164
	 */
165
	protected function parse_response( $data ) {
166
		if ( ! array_key_exists( 'products', $data ) ) {
167
			return array();
168
		}
169
170
		return $this->build_addon_list( $data['products'] );
171
	}
172
173
	/**
174
	 * Build a list of Addon objects from products data array.
175
	 *
176
	 * @param array $products Products data array.
177
	 *
178
	 * @return Addon[] List of Addons.
179
	 */
180
	protected function build_addon_list( $products ) {
181
		$addons = array();
182
183
		foreach ( $products as $product ) {
184
			$addon                  = new Addon( $product );
185
			$addons[ $addon->name ] = $addon;
186
		}
187
188
		return $addons;
189
	}
190
191
	/**
192
	 * Render the add-on list or display an error if the list can't be retrieved.
193
	 */
194
	protected function render_addons() {
195
		if ( empty( $this->addons ) ) {
196
			$this->render_empty_list();
197
		}
198
199
		foreach ( $this->addons as $addon ) {
200
			$addon->render();
201
		}
202
	}
203
204
	/**
205
	 * Display a notice if the list of add-on can't be retrieved.
206
	 */
207
	protected function render_empty_list() {
208
		?>
209
		<span class="el-addon-empty">
210
			<?php
211
				printf(
212
					__( 'We are not able to retrieve the add-on list now. Please visit the <a target="_blank" rel="noopener" href="%s">add-on page</a> to view the add-ons.', 'email-log' ), // @codingStandardsIgnoreLine
213
					'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid-failed'
214
				);
215
			?>
216
		</span>
217
		<?php
218
	}
219
220
	/**
221
	 * Get API URL.
222
	 *
223
	 * @return string API URL.
224
	 */
225
	protected function get_api_url() {
226
		return $this->store_url . '/json/products.json';
227
	}
228
}
229