Passed
Push — 19-feature/export-logs-in-batc... ( 47f618...645032 )
by Sudar
07:04 queued 01:42
created

AddonList::is_addon_installed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
				$json_string = $this->get_addon_data_from_local_file();
144
			} else {
145
				$json_string = wp_remote_retrieve_body( $response );
146
			}
147
148
			$json = json_decode( $json_string, true );
149
150
			if ( ! is_array( $json ) ) {
151
				return array();
152
			}
153
154
			set_transient( self::CACHE_KEY, $json, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS );
155
		}
156
157
		return $this->parse_response( $json );
158
	}
159
160
	/**
161
	 * Parse the response and get the list of add-on.
162
	 *
163
	 * @param array $data JSON Data array.
164
	 *
165
	 * @return array List of Add-ons.
166
	 */
167
	protected function parse_response( $data ) {
168
		if ( ! array_key_exists( 'products', $data ) ) {
169
			return array();
170
		}
171
172
		return $this->build_addon_list( $data['products'] );
173
	}
174
175
	/**
176
	 * Build a list of Addon objects from products data array.
177
	 *
178
	 * @param array $products Products data array.
179
	 *
180
	 * @return Addon[] List of Addons.
181
	 */
182
	protected function build_addon_list( $products ) {
183
		$addons = array();
184
185
		foreach ( $products as $product ) {
186
			$addon                  = new Addon( $product );
187
			$addons[ $addon->name ] = $addon;
188
		}
189
190
		return $addons;
191
	}
192
193
	/**
194
	 * Render the add-on list or display an error if the list can't be retrieved.
195
	 */
196
	protected function render_addons() {
197
		if ( empty( $this->addons ) ) {
198
			$this->render_empty_list();
199
		}
200
201
		foreach ( $this->addons as $addon ) {
202
			$addon->render();
203
		}
204
	}
205
206
	/**
207
	 * Display a notice if the list of add-on can't be retrieved.
208
	 */
209
	protected function render_empty_list() {
210
		?>
211
		<span class="el-addon-empty">
212
			<?php
213
				printf(
214
					__( '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
215
					'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid-failed'
216
				);
217
			?>
218
		</span>
219
		<?php
220
	}
221
222
	/**
223
	 * Get API URL.
224
	 *
225
	 * @return string API URL.
226
	 */
227
	protected function get_api_url() {
228
		return $this->store_url . '/json/products.json';
229
	}
230
231
	/**
232
	 * Read the add-on data from the local data file.
233
	 *
234
	 * @since 2.1
235
	 *
236
	 * @return false|string JSON file content, False on failure.
237
	 */
238
	private function get_addon_data_from_local_file() {
239
		$email_log = email_log();
240
241
		$local_json_file_path = $email_log->get_plugin_path() . 'data/products.json';
242
243
		return file_get_contents( $local_json_file_path );
244
	}
245
}
246