ld-license.php
5.19 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
210
211
<?php
/**
* LearnDash License utility functions.
*
* @since 4.3.1
*
* @package LearnDash\License
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
const LEARNDASH_HUB_LICENSE_CACHE_OPTION = 'learndash_hub_license_result';
const LEARNDASH_HUB_LICENSE_CACHE_TIMEOUT = 6 * HOUR_IN_SECONDS;
const LEARNDASH_LICENSE_KEY = 'nss_plugin_license_sfwd_lms';
const LEARNDASH_LICENSE_EMAIL_KEY = 'nss_plugin_license_email_sfwd_lms';
const LEARNDASH_HUB_PLUGIN_SLUG = 'learndash-hub/learndash-hub.php';
/**
* Updates the LearnDash Hub license cache when the license is verified.
*
* @since 4.5.0
*
* @param WP_Error|bool $license_response The license response.
*
* @return void
*/
add_action(
'learndash_licensing_management_license_verified',
function( $license_response ) {
update_option(
LEARNDASH_HUB_LICENSE_CACHE_OPTION,
array(
time(),
! is_wp_error( $license_response ),
)
);
}
);
/**
* Removes the license cache after the license logout.
*
* @since 4.5.0
*
* @return void
*/
add_action(
'learndash_licensing_management_license_logout',
function () {
delete_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION );
}
);
/**
* Redirects to the LearnDash Hub license page if the L&M plugin is installed and can be activated.
*
* @since 4.8.0
*
* @return void
*/
add_action(
'admin_init',
function () {
if (
! isset( $_GET['page'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|| $_GET['page'] !== 'nss_plugin_license-sfwd_lms-settings' // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) {
return;
}
if ( ! learndash_is_learndash_hub_installed() || ! learndash_activate_learndash_hub() ) {
return;
}
learndash_safe_redirect( admin_url( 'admin.php?page=learndash_hub_licensing' ) );
}
);
/**
* Activates the LearnDash Hub plugin (Licensing & Management).
*
* @since 4.8.0
*
* @return bool True if the plugin is activated. False otherwise.
*/
function learndash_activate_learndash_hub(): bool {
if ( learndash_is_learndash_hub_active() ) {
return true;
}
$activation_result = activate_plugin(
LEARNDASH_HUB_PLUGIN_SLUG,
'',
is_plugin_active_for_network( LEARNDASH_LMS_PLUGIN_KEY ),
true
);
if ( is_wp_error( $activation_result ) ) {
WP_DEBUG && error_log( 'Failed to activate the learndash licensing & management plugin: ' . $activation_result->get_error_message() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
return false;
}
return true;
}
/**
* Check if LearnDash Hub is installed.
*
* @since 4.8.0
*
* @return bool True if the LearnDash Hub is installed. False otherwise.
*/
function learndash_is_learndash_hub_installed() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return array_key_exists( LEARNDASH_HUB_PLUGIN_SLUG, get_plugins() );
}
/**
* Check if LearnDash Hub is installed and active.
*
* @since 4.3.1
*
* @return bool True if the LearnDash Hub is installed and active. False otherwise.
*/
function learndash_is_learndash_hub_active() {
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return function_exists( 'is_plugin_active' ) && is_plugin_active( LEARNDASH_HUB_PLUGIN_SLUG );
}
/**
* Validate a license key.
*
* @since 4.3.1
*
* @param string $email The email address of the license key.
* @param string $license_key The license key.
*
* @return bool True if the license key is valid. False otherwise.
*/
function learndash_validate_hub_license( string $email, string $license_key ) {
if ( ! learndash_is_learndash_hub_active() || ! class_exists( 'LearnDash\Hub\Component\API' ) ) {
delete_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION );
return false; // legacy license system is not supported.
}
if ( empty( $email ) || empty( $license_key ) ) {
delete_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION );
return false;
}
$hub_api = new LearnDash\Hub\Component\API();
$validation_result = $hub_api->verify_license( $email, $license_key );
$license_valid = ! is_wp_error( $validation_result ) && $validation_result === true;
update_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION, array( time(), $license_valid ) );
return $license_valid;
}
/**
* Check if the license is valid.
*
* @since 4.3.1
*
* @return bool True if the license is valid. False otherwise.
*/
function learndash_is_license_hub_valid() {
$license_valid = get_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION );
if (
! is_array( $license_valid ) ||
count( $license_valid ) !== 2 ||
$license_valid[0] < time() - LEARNDASH_HUB_LICENSE_CACHE_TIMEOUT
) {
// recheck the license.
return learndash_validate_hub_license(
get_option( LEARNDASH_LICENSE_EMAIL_KEY, '' ),
get_option( LEARNDASH_LICENSE_KEY, '' )
);
}
return $license_valid[1];
}
/**
* Get the last check time of the LearnDash Hub license status.
*
* @since 4.3.1
*
* @return int The last check time or 0 if never checked.
*/
function learndash_get_last_license_hub_check_time() {
$license_valid = get_option( LEARNDASH_HUB_LICENSE_CACHE_OPTION );
if (
! is_array( $license_valid ) ||
count( $license_valid ) !== 2
) {
return 0;
}
return intval( $license_valid[0] );
}