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