| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Promotions\InPluginUpsells; |
| 4 |
|
| 5 |
/** |
| 6 |
* @since 2.17.0 |
| 7 |
*/ |
| 8 |
class AddonsRepository |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var string |
| 12 |
*/ |
| 13 |
private $endpoint = 'https://givewp.com/downloads/upsells/addons.json'; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $transient = 'give-in-plugin-upsells'; |
| 19 |
|
| 20 |
/** |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
private function fetchAddons() |
| 24 |
{ |
| 25 |
$request = wp_remote_get($this->endpoint, [ |
| 26 |
'headers' => [ |
| 27 |
'Content-Type' => 'application/json', |
| 28 |
], |
| 29 |
]); |
| 30 |
|
| 31 |
if (is_wp_error($request)) { |
| 32 |
return []; |
| 33 |
} |
| 34 |
|
| 35 |
$body = wp_remote_retrieve_body($request); |
| 36 |
|
| 37 |
if (empty($body)) { |
| 38 |
return []; |
| 39 |
} |
| 40 |
|
| 41 |
$json = json_decode($body, true); |
| 42 |
|
| 43 |
// Sanitize JSON |
| 44 |
array_walk_recursive($json, function (&$item) { |
| 45 |
$item = wp_kses($item, [ |
| 46 |
'strong' => [], |
| 47 |
]); |
| 48 |
}); |
| 49 |
|
| 50 |
return $json; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function getAddons() |
| 57 |
{ |
| 58 |
$cache = get_transient($this->transient); |
| 59 |
|
| 60 |
if (false === $cache) { |
| 61 |
$addons = $this->fetchAddons(); |
| 62 |
|
| 63 |
set_transient( |
| 64 |
$this->transient, |
| 65 |
serialize($addons), |
| 66 |
DAY_IN_SECONDS |
| 67 |
); |
| 68 |
|
| 69 |
return $addons; |
| 70 |
} |
| 71 |
|
| 72 |
return unserialize($cache); |
| 73 |
} |
| 74 |
} |
| 75 |
|