PUE.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Promoter__PUE |
| 5 | * |
| 6 | * @since 4.9 |
| 7 | */ |
| 8 | class Tribe__Promoter__PUE { |
| 9 | |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | private $slug = 'promoter'; |
| 14 | |
| 15 | /** |
| 16 | * @var Tribe__PUE__Checker |
| 17 | */ |
| 18 | private $pue_checker; |
| 19 | |
| 20 | /** |
| 21 | * Setup the PUE Checker. |
| 22 | * |
| 23 | * @since 4.9 |
| 24 | */ |
| 25 | public function load() { |
| 26 | $this->pue_checker = new Tribe__PUE__Checker( 'http://tri.be/', $this->slug, [ |
| 27 | 'context' => 'service', |
| 28 | 'plugin_name' => __( 'Promoter', 'tribe-common' ), |
| 29 | ] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get whether service has a license and if the license is activated on network. |
| 34 | * |
| 35 | * @return array|false License information or false if not set. |
| 36 | * |
| 37 | * @since 4.9 |
| 38 | */ |
| 39 | public function get_license_info() { |
| 40 | $option_name = 'pue_install_key_' . $this->slug; |
| 41 | |
| 42 | $key = get_option( $option_name ); |
| 43 | |
| 44 | $is_network_key = false; |
| 45 | |
| 46 | if ( is_multisite() ) { |
| 47 | $network_key = get_network_option( null, $option_name ); |
| 48 | |
| 49 | if ( empty( $key ) ) { |
| 50 | $key = $network_key; |
| 51 | |
| 52 | $is_network_key = true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if ( empty( $key ) ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return [ |
| 61 | 'key' => $key, |
| 62 | 'is_network_key' => $is_network_key, |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Check whether service has a license key set or not. |
| 68 | * |
| 69 | * @return bool Whether service has a license key set. |
| 70 | * |
| 71 | * @since 4.9 |
| 72 | */ |
| 73 | public function has_license_key() { |
| 74 | return ! empty( $this->get_license_info() ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check whether service has a valid license key or not. |
| 79 | * |
| 80 | * @return bool Whether service has a valid license key. |
| 81 | * |
| 82 | * @since 4.9 |
| 83 | */ |
| 84 | public function has_valid_license() { |
| 85 | $license_info = $this->get_license_info(); |
| 86 | |
| 87 | if ( ! $license_info ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | $response = $this->pue_checker->validate_key( $license_info['key'], $license_info['is_network_key'] ); |
| 92 | |
| 93 | return isset( $response['status'] ) && 1 === (int) $response['status']; |
| 94 | } |
| 95 | |
| 96 | } |
| 97 |