PremiumAddonsListManager.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\License; |
| 4 | |
| 5 | /** |
| 6 | * Class PremiumAddonManager |
| 7 | * @package Give\License |
| 8 | * |
| 9 | * this class we use to manager premium addons list for licensing. |
| 10 | * |
| 11 | * @since 2.9.2 |
| 12 | */ |
| 13 | class PremiumAddonsListManager |
| 14 | { |
| 15 | /** |
| 16 | * Products list api url. |
| 17 | * |
| 18 | * @since 2.9.2 |
| 19 | */ |
| 20 | const PRODUCTS_LIST_API_URL = 'https://givewp.com/edd-api/products'; |
| 21 | |
| 22 | /** |
| 23 | * Cached addon values for the same request, prevents subsequent transient queries |
| 24 | * |
| 25 | * @since 2.11.3 |
| 26 | */ |
| 27 | private $addonIds; |
| 28 | |
| 29 | /** |
| 30 | * Get premium addons slugs as addons ids. |
| 31 | * |
| 32 | * @since 2.11.3 Always cache to avoid timeouts on givewp.com |
| 33 | * @since 2.9.2 |
| 34 | * |
| 35 | * @return array |
| 36 | */ |
| 37 | private function getAddonsIds() |
| 38 | { |
| 39 | if (isset($this->addonIds)) { |
| 40 | return $this->addonIds; |
| 41 | } |
| 42 | |
| 43 | $optionName = 'give_premium_addons_ids'; |
| 44 | $cachedResult = get_transient($optionName); |
| 45 | if ($cachedResult !== false) { |
| 46 | return $this->addonIds = $cachedResult; |
| 47 | } |
| 48 | |
| 49 | $response = wp_remote_get( |
| 50 | self::PRODUCTS_LIST_API_URL . '?number=-1', |
| 51 | [ |
| 52 | 'timeout' => 3, |
| 53 | ] |
| 54 | ); |
| 55 | $productsInformation = wp_remote_retrieve_body($response); |
| 56 | $responseCode = wp_remote_retrieve_response_code($response); |
| 57 | if ( ! $productsInformation || $responseCode < 200 || $responseCode > 299) { |
| 58 | set_transient($optionName, [], HOUR_IN_SECONDS); |
| 59 | |
| 60 | return $this->addonIds = []; |
| 61 | } |
| 62 | |
| 63 | $productsInformation = json_decode($productsInformation, true); |
| 64 | $productsIds = []; |
| 65 | foreach ($productsInformation['products'] as $product) { |
| 66 | $productsIds[] = $product['info']['slug']; |
| 67 | } |
| 68 | |
| 69 | if ($productsIds) { |
| 70 | set_transient($optionName, $productsIds, DAY_IN_SECONDS); |
| 71 | } |
| 72 | |
| 73 | return $this->addonIds = $productsIds; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Return whether or not addon is premium addon. |
| 78 | * |
| 79 | * @since 2.9.2 |
| 80 | * |
| 81 | * @param string $pluginURI |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function isPremiumAddons($pluginURI) |
| 86 | { |
| 87 | $addonId = basename($pluginURI); |
| 88 | $addonsIds = $this->getAddonsIds(); |
| 89 | |
| 90 | return in_array($addonId, $addonsIds, true); |
| 91 | } |
| 92 | } |
| 93 |