class-license.php
5 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Enable and validate Pro version licensing.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Admin;
use Block_Lab\Component_Abstract;
/**
* Class License
*/
class License extends Component_Abstract {
/**
* Option name of the license key.
*
* @var string
*/
const LICENSE_KEY_OPTION_NAME = 'block_lab_license_key';
/**
* URL of the Block Lab store.
*
* @var string
*/
public $store_url;
/**
* Product slug of the Pro version on the Block Lab store.
*
* @var string
*/
public $product_slug;
/**
* The name of the license key transient.
*
* @var string
*/
const TRANSIENT_NAME = 'block_lab_license';
/**
* The transient 'license' value for when the request to validate the Pro license failed.
*
* This is for when the actual POST request fails,
* not for when it returns that the license is invalid.
*
* @var string
*/
const REQUEST_FAILED = 'request_failed';
/**
* Initialise the Pro component.
*/
public function init() {
$this->store_url = 'https://getblocklab.com';
$this->product_slug = 'block-lab-pro';
}
/**
* Register any hooks that this component needs.
*/
public function register_hooks() {
add_filter( 'pre_update_option_block_lab_license_key', [ $this, 'save_license_key' ] );
}
/**
* Check that the license key is valid before saving.
*
* @param string $key The license key that was submitted.
*
* @return string
*/
public function save_license_key( $key ) {
$this->activate_license( $key );
$license = get_transient( self::TRANSIENT_NAME );
if ( ! $this->is_valid() ) {
$key = '';
if ( isset( $license['license'] ) && self::REQUEST_FAILED === $license['license'] ) {
block_lab()->admin->settings->prepare_notice( $this->license_request_failed_message() );
} else {
block_lab()->admin->settings->prepare_notice( $this->license_invalid_message() );
}
} else {
block_lab()->admin->settings->prepare_notice( $this->license_success_message() );
}
return $key;
}
/**
* Check if the license if valid.
*
* @return bool
*/
public function is_valid() {
$license = $this->get_license();
if ( isset( $license['license'] ) && 'valid' === $license['license'] ) {
if ( isset( $license['expires'] ) && time() < strtotime( $license['expires'] ) ) {
return true;
}
}
return false;
}
/**
* Retrieve the license data.
*
* @return mixed
*/
public function get_license() {
$license = get_transient( self::TRANSIENT_NAME );
if ( ! $license ) {
$key = get_option( self::LICENSE_KEY_OPTION_NAME );
if ( ! empty( $key ) ) {
$this->activate_license( $key );
$license = get_transient( self::TRANSIENT_NAME );
}
}
return $license;
}
/**
* Try to activate the license.
*
* @param string $key The license key to activate.
*/
public function activate_license( $key ) {
// Data to send in our API request.
$api_params = [
'edd_action' => 'activate_license',
'license' => $key,
'item_name' => rawurlencode( $this->product_slug ),
'url' => home_url(),
];
// Call the Block Lab store's API.
$response = wp_remote_post(
$this->store_url,
[
'timeout' => 10,
'sslverify' => true,
'body' => $api_params,
]
);
if ( is_wp_error( $response ) ) {
$license = [ 'license' => self::REQUEST_FAILED ];
} else {
$license = json_decode( wp_remote_retrieve_body( $response ), true );
}
$expiration = DAY_IN_SECONDS;
set_transient( self::TRANSIENT_NAME, $license, $expiration );
}
/**
* Admin notice for correct license details.
*
* @return string
*/
public function license_success_message() {
$message = __( 'Your Block Lab license was successfully activated!', 'block-lab' );
return sprintf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( $message ) );
}
/**
* Admin notice for the license request failing.
*
* This is for when the validation request fails entirely, like with a 404.
* Not for when it returns that the license is invalid.
*
* @return string
*/
public function license_request_failed_message() {
$message = sprintf(
/* translators: %s is an HTML link to contact support */
__( 'There was a problem activating the license, but it may not be invalid. If the problem persists, please %s.', 'block-lab' ),
sprintf(
'<a href="%1$s">%2$s</a>',
'mailto:hi@getblocklab.com?subject=There was a problem activating my Block Lab Pro license',
esc_html__( 'contact support', 'block-lab' )
)
);
return sprintf( '<div class="notice notice-error"><p>%s</p></div>', wp_kses_post( $message ) );
}
/**
* Admin notice for incorrect license details.
*
* @return string
*/
public function license_invalid_message() {
$message = __( 'There was a problem activating your Block Lab license.', 'block-lab' );
return sprintf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $message ) );
}
}