1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Extensions |
7
|
|
|
* @package HM\BackUpWordPress |
8
|
|
|
*/ |
9
|
|
|
class Extensions { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Contains the instantiated Extensions instance. |
13
|
|
|
* |
14
|
|
|
* @var Extensions $this->instance |
15
|
|
|
*/ |
16
|
|
|
private static $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Holds the root URL of the API. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $root_url = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Extensions constructor. |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
function __construct() { |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->root_url = 'https://bwp.hmn.md/wp-json/wp/v2/'; |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the *Singleton* instance of this class. |
37
|
|
|
* |
38
|
|
|
* @staticvar Extensions $instance The *Singleton* instances of this class. |
39
|
|
|
* |
40
|
|
|
* @return Extensions The *Singleton* instance. |
41
|
|
|
*/ |
42
|
|
|
public static function get_instance() { |
43
|
|
|
|
44
|
|
|
if ( ! ( self::$instance instanceof Extensions ) ) { |
45
|
|
|
|
46
|
|
|
self::$instance = new Extensions(); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return self::$instance; |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Parses the body of the API response and returns it. |
56
|
|
|
* |
57
|
|
|
* @return array|bool|mixed|object |
58
|
|
|
*/ |
59
|
|
|
public function get_edd_data() { |
60
|
|
|
|
61
|
|
|
$response = $this->fetch( 'edd-downloads', 600 ); |
62
|
|
|
|
63
|
|
|
if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return json_decode( $response['body'] ); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Makes a request to the JSON API or retrieves the cached response. Caches the response for one day. |
73
|
|
|
* |
74
|
|
|
* @param $endpoint |
75
|
|
|
* @param int $ttl |
76
|
|
|
* |
77
|
|
|
* @return array|mixed|\WP_Error |
78
|
|
|
*/ |
79
|
|
|
function fetch( $endpoint, $ttl = DAY_IN_SECONDS ) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
$request_url = $this->root_url . $endpoint; |
82
|
|
|
|
83
|
|
|
$cache_key = md5( $request_url ); |
84
|
|
|
|
85
|
|
|
$cached = get_transient( 'bwp_' . $cache_key ); |
86
|
|
|
|
87
|
|
|
if ( $cached ) { |
88
|
|
|
return $cached; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$response = wp_remote_get( $request_url ); |
92
|
|
|
|
93
|
|
|
set_transient( 'bwp_' . $cache_key, $response, $ttl ); |
94
|
|
|
|
95
|
|
|
return $response; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.