-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-aihrus-licensing.php
188 lines (130 loc) · 3.83 KB
/
class-aihrus-licensing.php
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
Copyright 2014 Michael Cannon (email: mc@aihr.us)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( class_exists( 'Aihrus_Licensing' ) )
return;
require_once 'interface-aihrus-licensing.php';
abstract class Aihrus_Licensing implements Aihrus_Licensing_Interface {
public $author;
public $item_name;
public $slug;
public $store_url;
public function __construct( $slug, $item_name, $author = 'Michael Cannon', $store_url = 'http://aihr.us' ) {
$this->author = $author;
$this->item_name = urlencode( $item_name );
$this->slug = $slug;
$this->store_url = $store_url;
}
public function license_key() {
$key = $this->slug . 'license_key';
return $key;
}
public function get_license() {
$key = $this->license_key();
$license = get_transient( $key );
return $license;
}
public function update_license( $value = null ) {
$license = $this->get_license();
if ( $license === $value )
return $value;
if ( $this->valid_hash( $value ) ) {
$this->set_license( $value );
$value = $this->activate_license();
$this->set_license( $value );
return $value;
} else {
$this->deactivate_license();
$this->delete_license();
return '';
}
}
public function set_license( $value = null ) {
$key = $this->license_key();
delete_transient( $key );
if ( ! is_null( $value ) )
set_transient( $key, $value, WEEK_IN_SECONDS );
}
public function valid_license() {
$license = $this->get_license();
if ( $this->valid_hash( $license ) )
return true;
else
return false;
}
public function get_api_call( $action ) {
$license = $this->get_license();
$api_params = array(
'edd_action' => $action,
'item_name' => $this->item_name,
'license' => $license,
);
$api_call = add_query_arg(
$api_params,
$this->store_url
);
return $api_call;
}
public function get_remote_get( $api_call ) {
$response = wp_remote_get(
$api_call,
array(
'timeout' => 10,
'sslverify' => false,
)
);
return $response;
}
public function activate_license() {
$license_data = $this->get_license_data( 'activate_license' );
if ( false !== $license_data ) {
if ( 'valid' == $license_data->license ) {
$license = $this->get_license();
return $license;
}
return $license_data->license;
}
return false;
}
public function get_license_data( $action = 'check_license' ) {
$api_call = $this->get_api_call( $action );
$response = $this->get_remote_get( $api_call );
if ( is_wp_error( $response ) )
return false;
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
return $license_data;
}
public function deactivate_license() {
$license_data = $this->get_license_data( 'deactivate_license' );
if ( false !== $license_data ) {
if ( 'deactivated' == $license_data->license )
return true;
return $license_data->license;
}
return false;
}
public function delete_license() {
$this->set_license();
}
public function license_notice() {
$result = '<p><em>' . esc_html( 'Premium features require licensing to function.' ) . '</em></p>';
return $result;
}
public function valid_hash( $value = null ) {
if ( is_string( $value ) && preg_match( '#^[0-9a-f]{32}$#i', $value ) )
return true;
return false;
}
}
?>