-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-dm-pluginupdate.php
204 lines (180 loc) · 4.58 KB
/
class-dm-pluginupdate.php
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
<?php
/**
* Plugin Update
*
* @package DarkMatter
*
* @since 2.2.0
*/
/**
* Class DM_PluginUpdate
*
* @since 2.2.0
*/
class DM_PluginUpdate {
/**
* Cache key for the API response from WP Update Server.
*
* @since 2.2.0
*
* @var string
*/
private $cache_key = 'dark_matter_update_api_response';
/**
* Stores the plugin slug.
*
* @since 2.2.0
*
* @var string
*/
private $plugin_slug = 'dark-matter/dark-matter.php';
/**
* Constructor
*
* @since 2.2.0
*/
public function __construct() {
add_filter( 'plugins_api', [ $this, 'plugin_info' ], 20, 3 );
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'push_update' ], 10, 1 );
}
/**
* Check to ensure that the currently installed version of the plugin needs to be updated.
*
* @since 2.2.0
*
* @param object $data Plugin information as an object.
* @return bool Returns true if update is required. False otherwise.
*/
private function needs_update( $data = null ) {
/**
* No data, no update. Not strictly true but suffices for this logic.
*/
if ( empty( $data ) ) {
return false;
}
return version_compare( DM_VERSION, $data->version, '<' );
}
/**
* Method for calling Dark Matter Cloud to get the plugin information.
*
* @since 2.2.0
*
* @return false|mixed Dark Matter plugin information.
*/
private function request() {
$response = get_transient( $this->cache_key );
if ( ! empty( $response ) ) {
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Construct the URL for Dark Matter Cloud.
*
* @link https://github.com/YahnisElsts/wp-update-server
*/
$url = add_query_arg(
[
'action' => 'get_metadata',
'slug' => 'dark-matter',
],
'https://plugins.darkmattercloud.com/'
);
$response = wp_remote_get(
$url,
[
'timeout' => 3,
'headers' => [
'Accept' => 'application/json',
],
]
);
/**
* Validate the response and ensure it is useful.
*/
if (
is_wp_error( $response )
|| 200 === wp_remote_retrieve_response_code( $response )
) {
$body = wp_remote_retrieve_body( $response );
/**
* Update the transient and return the encoded JSON.
*/
if ( ! empty( $body ) ) {
set_transient( $this->cache_key, $response, DAY_IN_SECONDS );
return json_decode( wp_remote_retrieve_body( $response ) );
}
}
/**
* Got here, then the validation failed.
*/
return false;
}
/**
* Gets the plugin information.
*
* @since 2.2.0
*
* @param false|object|array $result The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Installation API.
* @param object $args Plugin API arguments.
* @return false|object|array
*/
public function plugin_info( $result = false, $action = '', $args = null ) {
/**
* Ensure we are only requesting the API at the most appropriate points.
*/
if ( 'plugin_information' !== $action || 'dark-matter' !== $args->slug ) {
return false;
}
/**
* Retrieve the plugin data.
*/
$data = $this->request();
if ( empty( $data ) ) {
return false;
}
$result = new stdClass();
$result->name = $data->name;
$result->slug = $data->slug;
$result->version = $data->version;
$result->new_version = $data->version;
$result->tested = $data->tested;
$result->requires = $data->requires;
$result->author = $data->author;
$result->author_profile = $data->author_homepage;
$result->download_link = $data->download_url;
$result->trunk = $data->download_url;
$result->requires_php = $data->requires_php;
$result->last_updated = $data->last_updated;
$result->sections = [
'description' => $data->sections->description,
'installation' => $data->sections->installation,
'changelog' => $data->sections->changelog,
];
if ( ! empty( $data->banners ) ) {
$result->banners = [
'low' => $data->banners->low,
'high' => $data->banners->high,
];
}
return $result;
}
/**
* Adds any updates to Dark Matter to the relevant transient in WordPress.
*
* @since 2.2.0
*
* @param mixed $value Value of site transient.
* @return mixed New value of site transient.
*/
public function push_update( $value = null ) {
$data = $this->request();
$data->new_version = $data->version;
$data->package = $data->download_url;
if ( $this->needs_update( $data ) ) {
$value->response[ $this->plugin_slug ] = $data;
} else {
$value->no_update[ $this->plugin_slug ] = $data;
}
return $value;
}
}