class-api.php
2.89 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
<?php
declare( strict_types=1 );
namespace LearnDash\Hub\Component;
use LearnDash\Hub\Framework\Base;
use LearnDash\Hub\Traits\Formats;
use LearnDash\Hub\Traits\License;
/**
* This class handle all stuffs relate to API.
*/
class API extends Base {
use License;
use Formats;
/**
* The API base URL.
*
* @var string
*/
public $base = LICENSING_SITE . '/wp-json/' . BASE_REST;
/**
* Trigger a license verification.
*
* @param string $email The email that registered with LearnDash.
* @param string $license_key The license key provided when registered.
* @param bool $force_check Force check the license status.
*
* @return \WP_Error|bool
*/
public function verify_license( string $email, string $license_key, bool $force_check = false ) {
if ( ! $force_check ) {
$license_status = $this->get_license_status();
if ( '' !== $license_status ) {
return ! is_wp_error( $license_status ) ? true : $license_status;
}
}
$response = $this->do_api_request(
'/site/auth',
'POST',
array(
'site_url' => site_url(),
'license_key' => $license_key,
'email' => $email,
)
);
/**
* Fires after the license verification.
*
* @since 1.1.5
*
* @param \WP_Error|bool $license_response `WP_Error` on failure, `true` on success.
* @param string $license_email License email.
* @param string $license_key License key.
*/
do_action(
'learndash_licensing_management_license_verified',
! is_wp_error( $response ) ? true : $response,
$email,
$license_key
);
$this->update_license_status( $response, $email, $license_key );
return ! is_wp_error( $response ) ? true : $response;
}
/**
* Return all the projects, and cache it.
*/
public function get_projects() {
if ( defined( 'LEARNDASH_HUB_FETCH_ERROR' ) ) {
return new \WP_Error( 'License Error', LEARNDASH_HUB_FETCH_ERROR );
}
$cached = get_site_option( 'learndash-hub-projects-api' );
if ( is_array( $cached )
&& isset( $cached['last_check'] )
&& strtotime( '+1 hour', $cached['last_check'] ) < time() ) {
$cached = array();
}
if ( ! is_array( $cached ) || empty( $cached['projects'] ) || is_wp_error( $cached['projects'] ) ) {
delete_site_option( 'learndash_hub_fetch_projects' );
delete_site_option( 'learndash_hub_update_plugins_cache' );
$projects = $this->do_api_request( '/repo/plugins' );
if ( is_wp_error( $projects ) ) {
// pageload cache.
define( 'LEARNDASH_HUB_FETCH_ERROR', $projects->get_error_message() );
}
$cached = array(
'projects' => $projects,
'last_check' => time(),
);
update_site_option( 'learndash-hub-projects-api', $cached );
}
return $cached['projects'];
}
/**
* Remove the domain from API side
*
* @return array|\WP_Error
*/
public function remove_domain() {
return $this->do_api_request(
'/site/domain',
'DELETE'
);
}
}