LicenseActivator.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\App\Service\License; |
| 4 | |
| 5 | |
| 6 | use \PMXE_Plugin; |
| 7 | |
| 8 | class LicenseActivator |
| 9 | { |
| 10 | const CONTEXT_PMXE = 1; |
| 11 | const CONTEXT_SCHEDULING = 2; |
| 12 | |
| 13 | public function activateLicense($productName, $context = self::CONTEXT_PMXE) |
| 14 | { |
| 15 | if($context == self::CONTEXT_PMXE) { |
| 16 | $licenseField = 'license'; |
| 17 | $licenseStatusField = 'license_status'; |
| 18 | } else { |
| 19 | $licenseField = 'scheduling_license'; |
| 20 | $licenseStatusField = 'scheduling_license_status'; |
| 21 | } |
| 22 | |
| 23 | $options = PMXE_Plugin::getInstance()->getOption(); |
| 24 | |
| 25 | if ($productName !== false) { |
| 26 | // data to send in our API request |
| 27 | $api_params = array( |
| 28 | 'edd_action' => 'activate_license', |
| 29 | 'license' => PMXE_Plugin::decode($options[$licenseField]), |
| 30 | 'item_name' => urlencode($productName), // the name of our product in EDD |
| 31 | 'url' => home_url() |
| 32 | ); |
| 33 | |
| 34 | // Call the custom API. |
| 35 | $response = wp_remote_get(add_query_arg($api_params, $options['info_api_url']), array('timeout' => 15, 'sslverify' => false)); |
| 36 | |
| 37 | // make sure the response came back okay |
| 38 | if (is_wp_error($response)) |
| 39 | return false; |
| 40 | |
| 41 | // decode the license data |
| 42 | $license_data = json_decode(wp_remote_retrieve_body($response)); |
| 43 | |
| 44 | // $license_data->license will be either "active" or "inactive" |
| 45 | |
| 46 | $options[$licenseStatusField] = $license_data->license; |
| 47 | |
| 48 | PMXE_Plugin::getInstance()->updateOption($options); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param $productName |
| 54 | * @param $options |
| 55 | * @param $context |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function checkLicense($productName, $options, $context) |
| 59 | { |
| 60 | if($context == self::CONTEXT_PMXE) { |
| 61 | $licenseField = 'license'; |
| 62 | } else { |
| 63 | $licenseField = 'scheduling_license'; |
| 64 | } |
| 65 | |
| 66 | if (!empty($options[$licenseField])) { |
| 67 | |
| 68 | if ($productName !== false) { |
| 69 | |
| 70 | $api_params = array( |
| 71 | 'edd_action' => 'check_license', |
| 72 | 'license' => PMXE_Plugin::decode($options[$licenseField]), |
| 73 | 'item_name' => urlencode($productName) |
| 74 | ); |
| 75 | |
| 76 | // Call the custom API. |
| 77 | $response = wp_remote_get(add_query_arg($api_params, $options['info_api_url']), array('timeout' => 15, 'sslverify' => false)); |
| 78 | |
| 79 | if (is_wp_error($response)) |
| 80 | return false; |
| 81 | |
| 82 | $license_data = json_decode(wp_remote_retrieve_body($response)); |
| 83 | |
| 84 | return $license_data->license; |
| 85 | |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | } |