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