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-container"> |
126
|
|
|
<?php $this->render_addons(); ?> |
127
|
|
|
<div class="clear"></div> |
128
|
|
|
</div> <!-- .el-container --> |
129
|
|
|
<?php |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieve the list of add-ons by calling the store API. |
134
|
|
|
* If the store API is down, then read the data from the local JSON file. |
135
|
|
|
* |
136
|
|
|
* @return Addon[] List of add-ons, empty array if API call fails. |
137
|
|
|
*/ |
138
|
|
|
public function get_addons() { |
139
|
|
|
$json = get_transient( self::CACHE_KEY ); |
140
|
|
|
if ( false === $json ) { |
141
|
|
|
$response = wp_remote_get( $this->get_api_url() ); |
142
|
|
|
|
143
|
|
|
if ( is_wp_error( $response ) || ! is_array( $response ) ) { |
144
|
|
|
$json_string = $this->get_addon_data_from_local_file(); |
145
|
|
|
} else { |
146
|
|
|
$json_string = wp_remote_retrieve_body( $response ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$json = json_decode( $json_string, true ); |
150
|
|
|
|
151
|
|
|
if ( ! is_array( $json ) ) { |
152
|
|
|
return array(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
set_transient( self::CACHE_KEY, $json, self::CACHE_EXPIRY_IN_HRS * HOUR_IN_SECONDS ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->parse_response( $json ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Parse the response and get the list of add-on. |
163
|
|
|
* |
164
|
|
|
* @param array $data JSON Data array. |
165
|
|
|
* |
166
|
|
|
* @return array List of Add-ons. |
167
|
|
|
*/ |
168
|
|
|
protected function parse_response( $data ) { |
169
|
|
|
if ( ! array_key_exists( 'products', $data ) ) { |
170
|
|
|
return array(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->build_addon_list( $data['products'] ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Build a list of Addon objects from products data array. |
178
|
|
|
* |
179
|
|
|
* @param array $products Products data array. |
180
|
|
|
* |
181
|
|
|
* @return Addon[] List of Addons. |
182
|
|
|
*/ |
183
|
|
|
protected function build_addon_list( $products ) { |
184
|
|
|
$addons = array(); |
185
|
|
|
|
186
|
|
|
foreach ( $products as $product ) { |
187
|
|
|
$addon = new Addon( $product ); |
188
|
|
|
$addons[ $addon->name ] = $addon; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $addons; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Render the add-on list or display an error if the list can't be retrieved. |
196
|
|
|
*/ |
197
|
|
|
protected function render_addons() { |
198
|
|
|
if ( empty( $this->addons ) ) { |
199
|
|
|
$this->render_empty_list(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
foreach ( $this->addons as $addon ) { |
203
|
|
|
$addon->render(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Display a notice if the list of add-on can't be retrieved. |
209
|
|
|
*/ |
210
|
|
|
protected function render_empty_list() { |
211
|
|
|
?> |
212
|
|
|
<span class="el-addon-empty"> |
213
|
|
|
<?php |
214
|
|
|
printf( |
215
|
|
|
__( '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 |
216
|
|
|
'https://wpemaillog.com/store/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=addon-grid-failed' |
217
|
|
|
); |
218
|
|
|
?> |
219
|
|
|
</span> |
220
|
|
|
<?php |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get API URL. |
225
|
|
|
* |
226
|
|
|
* @return string API URL. |
227
|
|
|
*/ |
228
|
|
|
protected function get_api_url() { |
229
|
|
|
return $this->store_url . '/json/products.json'; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Read the add-on data from the local data file. |
234
|
|
|
* |
235
|
|
|
* @since 2.1 |
236
|
|
|
* |
237
|
|
|
* @return false|string JSON file content, False on failure. |
238
|
|
|
*/ |
239
|
|
|
private function get_addon_data_from_local_file() { |
240
|
|
|
$email_log = email_log(); |
241
|
|
|
|
242
|
|
|
$local_json_file_path = $email_log->get_plugin_path() . 'data/products.json'; |
243
|
|
|
|
244
|
|
|
return file_get_contents( $local_json_file_path ); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|