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