|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace YahnisElsts\PluginUpdateChecker\v5p0\Vcs; |
|
4
|
|
|
|
|
5
|
|
|
use YahnisElsts\PluginUpdateChecker\v5p0\Plugin; |
|
6
|
|
|
|
|
7
|
|
|
if ( !class_exists(PluginUpdateChecker::class, false) ): |
|
8
|
|
|
|
|
9
|
|
|
class PluginUpdateChecker extends Plugin\UpdateChecker implements BaseChecker { |
|
10
|
|
|
use VcsCheckerMethods; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* PluginUpdateChecker constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Api $api |
|
16
|
|
|
* @param string $pluginFile |
|
17
|
|
|
* @param string $slug |
|
18
|
|
|
* @param int $checkPeriod |
|
19
|
|
|
* @param string $optionName |
|
20
|
|
|
* @param string $muPluginFile |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($api, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') { |
|
23
|
|
|
$this->api = $api; |
|
24
|
|
|
|
|
25
|
|
|
parent::__construct($api->getRepositoryUrl(), $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile); |
|
26
|
|
|
|
|
27
|
|
|
$this->api->setHttpFilterName($this->getUniqueName('request_info_options')); |
|
28
|
|
|
$this->api->setStrategyFilterName($this->getUniqueName('vcs_update_detection_strategies')); |
|
29
|
|
|
$this->api->setSlug($this->slug); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function requestInfo($unusedParameter = null) { |
|
33
|
|
|
//We have to make several remote API requests to gather all the necessary info |
|
34
|
|
|
//which can take a while on slow networks. |
|
35
|
|
|
if ( function_exists('set_time_limit') ) { |
|
36
|
|
|
@set_time_limit(60); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$api = $this->api; |
|
40
|
|
|
$api->setLocalDirectory($this->package->getAbsoluteDirectoryPath()); |
|
41
|
|
|
|
|
42
|
|
|
$info = new Plugin\PluginInfo(); |
|
43
|
|
|
$info->filename = $this->pluginFile; |
|
44
|
|
|
$info->slug = $this->slug; |
|
45
|
|
|
|
|
46
|
|
|
$this->setInfoFromHeader($this->package->getPluginHeader(), $info); |
|
47
|
|
|
$this->setIconsFromLocalAssets($info); |
|
48
|
|
|
$this->setBannersFromLocalAssets($info); |
|
49
|
|
|
|
|
50
|
|
|
//Pick a branch or tag. |
|
51
|
|
|
$updateSource = $api->chooseReference($this->branch); |
|
52
|
|
|
if ( $updateSource ) { |
|
53
|
|
|
$ref = $updateSource->name; |
|
54
|
|
|
$info->version = $updateSource->version; |
|
55
|
|
|
$info->last_updated = $updateSource->updated; |
|
56
|
|
|
$info->download_url = $updateSource->downloadUrl; |
|
57
|
|
|
|
|
58
|
|
|
if ( !empty($updateSource->changelog) ) { |
|
59
|
|
|
$info->sections['changelog'] = $updateSource->changelog; |
|
60
|
|
|
} |
|
61
|
|
|
if ( isset($updateSource->downloadCount) ) { |
|
62
|
|
|
$info->downloaded = $updateSource->downloadCount; |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
|
|
//There's probably a network problem or an authentication error. |
|
66
|
|
|
do_action( |
|
67
|
|
|
'puc_api_error', |
|
68
|
|
|
new \WP_Error( |
|
69
|
|
|
'puc-no-update-source', |
|
70
|
|
|
'Could not retrieve version information from the repository. ' |
|
71
|
|
|
. 'This usually means that the update checker either can\'t connect ' |
|
72
|
|
|
. 'to the repository or it\'s configured incorrectly.' |
|
73
|
|
|
), |
|
74
|
|
|
null, null, $this->slug |
|
75
|
|
|
); |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
//Get headers from the main plugin file in this branch/tag. Its "Version" header and other metadata |
|
80
|
|
|
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags. |
|
81
|
|
|
$mainPluginFile = basename($this->pluginFile); |
|
82
|
|
|
$remotePlugin = $api->getRemoteFile($mainPluginFile, $ref); |
|
83
|
|
|
if ( !empty($remotePlugin) ) { |
|
84
|
|
|
$remoteHeader = $this->package->getFileHeader($remotePlugin); |
|
85
|
|
|
$this->setInfoFromHeader($remoteHeader, $info); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
//Try parsing readme.txt. If it's formatted according to WordPress.org standards, it will contain |
|
89
|
|
|
//a lot of useful information like the required/tested WP version, changelog, and so on. |
|
90
|
|
|
if ( $this->readmeTxtExistsLocally() ) { |
|
91
|
|
|
$this->setInfoFromRemoteReadme($ref, $info); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
//The changelog might be in a separate file. |
|
95
|
|
|
if ( empty($info->sections['changelog']) ) { |
|
96
|
|
|
$info->sections['changelog'] = $api->getRemoteChangelog($ref, $this->package->getAbsoluteDirectoryPath()); |
|
97
|
|
|
if ( empty($info->sections['changelog']) ) { |
|
98
|
|
|
$info->sections['changelog'] = __('There is no changelog available.', 'plugin-update-checker'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ( empty($info->last_updated) ) { |
|
103
|
|
|
//Fetch the latest commit that changed the tag or branch and use it as the "last_updated" date. |
|
104
|
|
|
$latestCommitTime = $api->getLatestCommitTime($ref); |
|
105
|
|
|
if ( $latestCommitTime !== null ) { |
|
106
|
|
|
$info->last_updated = $latestCommitTime; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$info = apply_filters($this->getUniqueName('request_info_result'), $info, null); |
|
111
|
|
|
return $info; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Check if the currently installed version has a readme.txt file. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function readmeTxtExistsLocally() { |
|
120
|
|
|
return $this->package->fileExists($this->api->getLocalReadmeName()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Copy plugin metadata from a file header to a Plugin Info object. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $fileHeader |
|
127
|
|
|
* @param Plugin\PluginInfo $pluginInfo |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function setInfoFromHeader($fileHeader, $pluginInfo) { |
|
130
|
|
|
$headerToPropertyMap = array( |
|
131
|
|
|
'Version' => 'version', |
|
132
|
|
|
'Name' => 'name', |
|
133
|
|
|
'PluginURI' => 'homepage', |
|
134
|
|
|
'Author' => 'author', |
|
135
|
|
|
'AuthorName' => 'author', |
|
136
|
|
|
'AuthorURI' => 'author_homepage', |
|
137
|
|
|
|
|
138
|
|
|
'Requires WP' => 'requires', |
|
139
|
|
|
'Tested WP' => 'tested', |
|
140
|
|
|
'Requires at least' => 'requires', |
|
141
|
|
|
'Tested up to' => 'tested', |
|
142
|
|
|
|
|
143
|
|
|
'Requires PHP' => 'requires_php', |
|
144
|
|
|
); |
|
145
|
|
|
foreach ($headerToPropertyMap as $headerName => $property) { |
|
146
|
|
|
if ( isset($fileHeader[$headerName]) && !empty($fileHeader[$headerName]) ) { |
|
147
|
|
|
$pluginInfo->$property = $fileHeader[$headerName]; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ( !empty($fileHeader['Description']) ) { |
|
152
|
|
|
$pluginInfo->sections['description'] = $fileHeader['Description']; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Copy plugin metadata from the remote readme.txt file. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $ref GitHub tag or branch where to look for the readme. |
|
160
|
|
|
* @param Plugin\PluginInfo $pluginInfo |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function setInfoFromRemoteReadme($ref, $pluginInfo) { |
|
163
|
|
|
$readme = $this->api->getRemoteReadme($ref); |
|
164
|
|
|
if ( empty($readme) ) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ( isset($readme['sections']) ) { |
|
169
|
|
|
$pluginInfo->sections = array_merge($pluginInfo->sections, $readme['sections']); |
|
170
|
|
|
} |
|
171
|
|
|
if ( !empty($readme['tested_up_to']) ) { |
|
172
|
|
|
$pluginInfo->tested = $readme['tested_up_to']; |
|
173
|
|
|
} |
|
174
|
|
|
if ( !empty($readme['requires_at_least']) ) { |
|
175
|
|
|
$pluginInfo->requires = $readme['requires_at_least']; |
|
176
|
|
|
} |
|
177
|
|
|
if ( !empty($readme['requires_php']) ) { |
|
178
|
|
|
$pluginInfo->requires_php = $readme['requires_php']; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if ( isset($readme['upgrade_notice'], $readme['upgrade_notice'][$pluginInfo->version]) ) { |
|
182
|
|
|
$pluginInfo->upgrade_notice = $readme['upgrade_notice'][$pluginInfo->version]; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Add icons from the currently installed version to a Plugin Info object. |
|
188
|
|
|
* |
|
189
|
|
|
* The icons should be in a subdirectory named "assets". Supported image formats |
|
190
|
|
|
* and file names are described here: |
|
191
|
|
|
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-icons |
|
192
|
|
|
* |
|
193
|
|
|
* @param Plugin\PluginInfo $pluginInfo |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function setIconsFromLocalAssets($pluginInfo) { |
|
196
|
|
|
$icons = $this->getLocalAssetUrls(array( |
|
197
|
|
|
'icon.svg' => 'svg', |
|
198
|
|
|
'icon-256x256.png' => '2x', |
|
199
|
|
|
'icon-256x256.jpg' => '2x', |
|
200
|
|
|
'icon-128x128.png' => '1x', |
|
201
|
|
|
'icon-128x128.jpg' => '1x', |
|
202
|
|
|
)); |
|
203
|
|
|
|
|
204
|
|
|
if ( !empty($icons) ) { |
|
205
|
|
|
//The "default" key seems to be used only as last-resort fallback in WP core (5.8/5.9), |
|
206
|
|
|
//but we'll set it anyway in case some code somewhere needs it. |
|
207
|
|
|
reset($icons); |
|
208
|
|
|
$firstKey = key($icons); |
|
209
|
|
|
$icons['default'] = $icons[$firstKey]; |
|
210
|
|
|
|
|
211
|
|
|
$pluginInfo->icons = $icons; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Add banners from the currently installed version to a Plugin Info object. |
|
217
|
|
|
* |
|
218
|
|
|
* The banners should be in a subdirectory named "assets". Supported image formats |
|
219
|
|
|
* and file names are described here: |
|
220
|
|
|
* @link https://developer.wordpress.org/plugins/wordpress-org/plugin-assets/#plugin-headers |
|
221
|
|
|
* |
|
222
|
|
|
* @param Plugin\PluginInfo $pluginInfo |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function setBannersFromLocalAssets($pluginInfo) { |
|
225
|
|
|
$banners = $this->getLocalAssetUrls(array( |
|
226
|
|
|
'banner-772x250.png' => 'high', |
|
227
|
|
|
'banner-772x250.jpg' => 'high', |
|
228
|
|
|
'banner-1544x500.png' => 'low', |
|
229
|
|
|
'banner-1544x500.jpg' => 'low', |
|
230
|
|
|
)); |
|
231
|
|
|
|
|
232
|
|
|
if ( !empty($banners) ) { |
|
233
|
|
|
$pluginInfo->banners = $banners; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param array<string, string> $filesToKeys |
|
239
|
|
|
* @return array<string, string> |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function getLocalAssetUrls($filesToKeys) { |
|
242
|
|
|
$assetDirectory = $this->package->getAbsoluteDirectoryPath() . DIRECTORY_SEPARATOR . 'assets'; |
|
243
|
|
|
if ( !is_dir($assetDirectory) ) { |
|
244
|
|
|
return array(); |
|
245
|
|
|
} |
|
246
|
|
|
$assetBaseUrl = trailingslashit(plugins_url('', $assetDirectory . '/imaginary.file')); |
|
247
|
|
|
|
|
248
|
|
|
$foundAssets = array(); |
|
249
|
|
|
foreach ($filesToKeys as $fileName => $key) { |
|
250
|
|
|
$fullBannerPath = $assetDirectory . DIRECTORY_SEPARATOR . $fileName; |
|
251
|
|
|
if ( !isset($icons[$key]) && is_file($fullBannerPath) ) { |
|
|
|
|
|
|
252
|
|
|
$foundAssets[$key] = $assetBaseUrl . $fileName; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return $foundAssets; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
endif; |
|
261
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: