class-api.php
2.28 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
<?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.
*
* @return \WP_Error|bool
*/
public function verify_license( string $email, string $license_key ) {
$response = $this->do_api_request(
'/site/auth',
'POST',
array(
'site_url' => site_url(),
'license_key' => $license_key,
'email' => $email,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
update_site_option( $this->get_license_key_option_name(), $license_key );
update_site_option( $this->get_hub_email_option_name(), $email );
return true;
}
/**
* 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'
);
}
}