class-license.php
141 lines
| 1 | <?php |
| 2 | /** |
| 3 | * License class. |
| 4 | * Handles license management for Advanced Ads add-ons. |
| 5 | * |
| 6 | * @since 2.0.17 |
| 7 | * @package AdvancedAds |
| 8 | * @author Advanced Ads <info@wpadvancedads.com> |
| 9 | */ |
| 10 | |
| 11 | namespace AdvancedAds\License; |
| 12 | |
| 13 | use AdvancedAds\Utilities\Data; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * License class. |
| 19 | */ |
| 20 | class License { |
| 21 | |
| 22 | /** |
| 23 | * Option name for licenses. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private const OPTION_NAME = 'advanced-ads-licenses'; |
| 28 | |
| 29 | /** |
| 30 | * Add-on slugs. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | public const ADDONS_SLUGS = [ |
| 35 | 'pro', |
| 36 | 'responsive', |
| 37 | 'gam', |
| 38 | 'layer', |
| 39 | 'selling', |
| 40 | 'sticky', |
| 41 | 'tracking', |
| 42 | 'slider-ads', |
| 43 | ]; |
| 44 | |
| 45 | /** |
| 46 | * Main instance |
| 47 | * |
| 48 | * Ensure only one instance is loaded or can be loaded. |
| 49 | * |
| 50 | * @return License |
| 51 | */ |
| 52 | public static function get() { |
| 53 | static $instance; |
| 54 | |
| 55 | if ( null === $instance ) { |
| 56 | $instance = new License(); |
| 57 | } |
| 58 | |
| 59 | return $instance; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get license |
| 64 | * |
| 65 | * @param string $slug Add-on slug. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | public static function get_license_details( $slug ): array { |
| 70 | $licenses = get_option( self::OPTION_NAME, [] ); |
| 71 | $license = $licenses[ $slug ] ?? []; |
| 72 | |
| 73 | if ( ! empty( $license ) ) { |
| 74 | $license['status'] = get_option( 'advanced-ads-' . $slug . '-license-status', 'invalid' ); |
| 75 | $license['expires'] = get_option( 'advanced-ads-' . $slug . '-license-expires', false ); |
| 76 | } |
| 77 | |
| 78 | return $license; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Save license |
| 83 | * |
| 84 | * @param string $slug Add-on slug. |
| 85 | * @param string $license_key License key. |
| 86 | * @param string $status License status. |
| 87 | * @param string $expires License expires. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public static function update_license_details( $slug, $license_key, $status = 'valid', $expires = false ): void { |
| 92 | $licenses = get_option( self::OPTION_NAME, [] ); |
| 93 | |
| 94 | if ( 'lifetime' === $expires ) { |
| 95 | $expires = time() + YEAR_IN_SECONDS * 200; |
| 96 | } |
| 97 | |
| 98 | $licenses[ $slug ] = [ |
| 99 | 'license' => $license_key, |
| 100 | 'status' => $status, |
| 101 | 'expires' => $expires, |
| 102 | ]; |
| 103 | |
| 104 | update_option( self::OPTION_NAME, $licenses ); |
| 105 | update_option( 'advanced-ads-' . $slug . '-license-status', $status ); |
| 106 | update_option( 'advanced-ads-' . $slug . '-license-expires', $expires ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get license key |
| 111 | * |
| 112 | * @param string $slug Add-on slug. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | public static function has_valid_license( $slug ): bool { |
| 117 | $license = self::get_license_details( $slug ); |
| 118 | |
| 119 | if ( empty( $license ) ) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return 'valid' === $license['status'] && $license['expires'] > time(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check if any license is valid |
| 128 | * |
| 129 | * @return bool |
| 130 | */ |
| 131 | public static function has_any_valid_license(): bool { |
| 132 | foreach ( self::ADDONS_SLUGS as $add_on ) { |
| 133 | if ( self::has_valid_license( $add_on ) ) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 |