-
Notifications
You must be signed in to change notification settings - Fork 4
/
updater.php
146 lines (129 loc) · 5.5 KB
/
updater.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
<?php
namespace Postino;
class Updater
{
protected $config;
protected $github;
public function __construct()
{
$this->config = [
'slug' => 'postino/postino.php',
'proper_folder_name' => 'postino'
];
$this->github = [
'user' => 'caffeinalab',
'repository' => 'postino'
];
}
public function bootUpdateService()
{
// define the alternative API for updating checking
add_filter("pre_set_site_transient_update_plugins", array($this, "checkVersion"));
// Define the alternative response for information checking
add_filter("plugins_api", array($this, "setPluginInfo"), 10, 3);
// reactivate plugin
add_filter("upgrader_post_install", array($this, "postInstall"), 10, 3);
}
private function getPluginData()
{
if (isset($this->pluginData)) {
return $this->pluginData;
}
include_once ABSPATH.'/wp-admin/includes/plugin.php';
$this->pluginData = get_plugin_data(WP_PLUGIN_DIR.'/'.$this->config['slug']);
$githubUrl = parse_url($this->pluginData["PluginURI"]);
$githubInfo = explode('/', $githubUrl['path']);
$this->github = [
"user" => $githubInfo[1],
"repository" => $githubInfo[2]
];
}
private function getRepoReleaseInfo()
{
// Only do this once
if (!empty($this->githubAPIResult)) {
return;
}
$transient = get_transient("{$this->github["user"]}_{$this->github["repository"]}_transient_update");
if ($transient !== false) {
return $transient;
}
// Query the GitHub API
$url = "https://api.github.com/repos/{$this->github["user"]}/{$this->github["repository"]}/releases";
// Get the results
$this->githubAPIResult = wp_remote_retrieve_body(wp_remote_get($url));
if (!empty($this->githubAPIResult)) {
$this->githubAPIResult = @json_decode($this->githubAPIResult);
}
// Use only the latest release
if (is_array($this->githubAPIResult)) {
$this->githubAPIResult = $this->githubAPIResult[0];
}
set_transient("{$this->github["user"]}_{$this->github["repository"]}_transient_update", $this->githubAPIResult, 3.600);
}
public function checkVersion( $transient )
{
if (!empty($transient)&& empty($transient->checked)) {
return $transient;
}
// Get plugin & GitHub release information
$this->getPluginData();
$this->getRepoReleaseInfo();
if (!isset($this->githubAPIResult->tag_name)) {
return $transient;
}
$doUpdate = version_compare($this->githubAPIResult->tag_name, $transient->checked[$this->config['slug']]);
if ($doUpdate == 1) {
$package = $this->githubAPIResult->zipball_url;
// Include the access token for private GitHub repos
if (!empty($this->config["access_token"])) {
$package = add_query_arg(array("access_token" => $this->config["access_token"]), $package);
}
$obj = new \StdClass();
$obj->slug = $this->config["slug"];
$obj->new_version = $this->githubAPIResult->tag_name;
$obj->url = $this->pluginData["PluginURI"];
$obj->package = $package;
$obj->plugin = $this->config["slug"];
$obj->icons = ['1x' => '/wp-content/plugins/postino/res/128.png', '2x' => '/wp-content/plugins/postino/res/128.png'];
$transient->response[$this->config["slug"]] = $obj;
}
return $transient;
}
public function setPluginInfo( $false, $action, $response ) {
// Get plugin & GitHub release information
$this->getPluginData();
$this->getRepoReleaseInfo();
// If nothing is found, do nothing
if (empty($response->slug) || $response->slug != $this->config["slug"]) {
return false;
}
// Add our plugin information
$response->last_updated = $this->githubAPIResult->published_at;
$response->slug = $this->config["slug"];
$response->name = $this->pluginData["Name"];
$response->plugin_name = $this->pluginData["Name"];
$response->version = $this->githubAPIResult->tag_name;
$response->author = $this->pluginData["AuthorName"];
$response->homepage = $this->pluginData["PluginURI"];
$response->sections = array( 'description' =>$this->githubAPIResult->body);
// This is our release download zip file
$downloadLink = $this->githubAPIResult->zipball_url;
$response->download_link = $downloadLink;
return $response;
}
public function postInstall( $true, $hook_extra, $result )
{
global $wp_filesystem;
// Move & Activate
$proper_destination = WP_PLUGIN_DIR.'/'.$this->config['proper_folder_name'];
$wp_filesystem->move($result['destination'], $proper_destination);
$result['destination'] = $proper_destination;
$activate = activate_plugin(WP_PLUGIN_DIR.'/'.$this->config['slug']);
// Output the update message
$fail = __('The plugin has been updated, but could not be reactivated. Please reactivate it manually.', 'github_plugin_updater');
$success = __('Plugin reactivated successfully.', 'github_plugin_updater');
echo is_wp_error($activate) ? $fail : $success;
return $result;
}
}