-
Notifications
You must be signed in to change notification settings - Fork 0
/
mudlet-release.php
271 lines (231 loc) · 8.56 KB
/
mudlet-release.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* Plugin Name: Mudlet release
* Description: Adds support for automatic Mudlet release posts creation
* Version: @version@
* Author: Piotr Wilczynski
*/
defined('ABSPATH') || exit;
require("vendor/autoload.php");
const GITHUB_API_URL = "https://api.github.com/repos/Mudlet/Mudlet/";
class MudletRelease
{
const SHORTCODE = "MudletRelease";
const RELEASE_CATEGORY = 173;
const USER_ID = 2; //Vadi
const ACTIONS = array('created', 'edited');
private $parsedown;
private $version;
function __construct()
{
$this->version = '@version@';
$this->parsedown = new Parsedown();
add_shortcode(self::SHORTCODE, array($this, 'mudlet_release'));
add_action('wp_ajax_post_newest_release', array($this, 'mudlet_post_release'));
add_action('wp_ajax_nopriv_post_newest_release', array($this, 'mudlet_post_release'));
new MudletUpdateCheck($this->version);
}
function mudlet_release($atts, $content)
{
$transient_name = $this->get_transient_name($content);
$body = get_transient($transient_name);
if ($body) {
$body = base64_decode($body);
} else {
$result = GetHttpWrapper::get(GITHUB_API_URL . "releases/tags/$content");
if ($result) {
$body = $this->parsedown->text($result->body);
set_transient($transient_name, base64_encode($body));
} else {
$body = "Can't get releases post for $content<br>You can view it on <a href=\"https://github.com/Mudlet/Mudlet/releases/$content\">Github</a>.";
}
}
return $body;
}
private function get_transient_name($id)
{
return sanitize_title("mudlet-release-$id");
}
private function get_release_posts($id)
{
$args = array(
'meta_query' => array(
array(
'key' => 'release-post',
'value' => $id
)
),
'post_status' => 'any'
);
return get_posts($args);
}
function mudlet_post_release()
{
if (isset($_POST['payload'])) {
$payload = json_decode(stripslashes($_POST['payload']));
if (isset($payload->release) && in_array($payload->action, self::ACTIONS)) {
$result = $payload->release;
} else {
wp_die('Releasse hook not applicable.');
}
} else {
wp_die('No release payload.');
}
if (substr($payload->release->tag_name, -2) != ".0") {
wp_die("Not major release. Skipping.");
}
if ($result->id) {
echo "Release webhook firing\n";
$release_posts = $this->get_release_posts($result->id);
$languages = pll_languages_list();
echo "Storing transient for release " . $result->id;
$parsed_body = $this->parsedown->text($result->body);
$post_body = base64_encode($parsed_body);
$post_excerpt = strip_tags(wp_trim_excerpt($parsed_body));
set_transient($this->get_transient_name($result->id), $post_body);
if (count($release_posts) == 0) {
echo "New release posts will be create for release " . $result->id . "\n";
$translations = array();
foreach ($languages as $code) {
$release_category = pll_get_term(self::RELEASE_CATEGORY, $code);
$post_id = wp_insert_post(array(
'post_author' => self::USER_ID,
'post_title' => $result->name,
'post_content' => '[' . self::SHORTCODE . ']' . $result->id . '[/' . self::SHORTCODE . ']',
'post_status' => $result->draft ? 'draft' : 'publish',
'post_category' => array($release_category),
'post_excerpt' => $post_excerpt
));
add_post_meta($post_id, 'release-post', $result->id, true);
pll_set_post_language($post_id, $code);
$translations[$code] = $post_id;
echo "Post $post_id created.";
}
pll_save_post_translations($translations);
} else {
echo "Release posts found. They will be updated.\n";
foreach ($languages as $code) {
$post_in_lang = pll_get_post($release_posts[0]->ID, $code);
wp_update_post(array(
'ID' => $post_in_lang,
'post_title' => $result->name,
'post_status' => $result->draft ? 'draft' : 'publish',
'post_excerpt' => $post_excerpt
));
echo "Post ID: $post_in_lang updated.\n";
}
}
wp_die();
}
}
}
class MudletUpdateCheck
{
private $version;
private $cache_key;
private $slug;
function __construct($version)
{
$this->version = $version;
$this->slug = plugin_basename(__DIR__);
$this->cache_key = 'mudlet_release';
add_filter('site_transient_update_plugins', array($this, 'push_update'));
add_filter('plugins_api', array($this, 'info'), 20, 3);
}
public function request_info()
{
return GetHttpWrapper::get('https://github.com/Mudlet/mudlet-release-plugin/releases/latest/download/info.json', $this->cache_key);
}
function info($res, $action, $args)
{
if ('plugin_information' !== $action) {
return $res;
}
if ($this->slug !== $args->slug) {
return $res;
}
$remote = $this->request_info();
if (!$remote) {
return $res;
}
$res = new stdClass();
$res->name = $remote->name;
$res->plugin = plugin_basename(__FILE__);
$res->slug = $this->slug;
$res->author = $remote->author;
$res->author_profile = $remote->author_profile;
$res->version = $remote->version;
//$res->tested = $remote->tested;
$res->requires = $remote->requires;
$res->requires_php = $remote->requires_php;
$res->download_link = $remote->download_url;
$res->trunk = $remote->download_url;
$res->last_updated = $remote->last_updated;
$res->sections = array(
'description' => $remote->sections->description
);
return $res;
}
function push_update($transient)
{
if (empty($transient->checked)) {
return $transient;
}
$remote = $this->request_info();
$res = new stdClass();
$res->slug = $remote->slug;
$res->plugin = plugin_basename(__FILE__);
$res->package = $remote->download_url;
if ($remote && version_compare($this->version, $remote->version, '<')) {
$res->new_version = $remote->version;
$transient->response[$res->plugin] = $res;
$transient->checked[$res->plugin] = $remote->version;
} else {
$res->new_version = $this->version;
$transient->no_update[$res->plugin] = $res;
}
return $transient;
}
}
class GetHttpWrapper
{
private $response;
public $error;
function __construct($url, $transient = false)
{
$this->response = $transient ? get_transient($transient) : false;
if (!$this->response) {
$remote = wp_remote_get(
$url,
array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json'
)
)
);
if ($this->is_ok($remote)) {
$this->response = json_decode(wp_remote_retrieve_body($remote));
if ($transient && $this->response) {
set_transient($transient, $this->response, HOUR_IN_SECONDS);
}
} else if (is_wp_error(($remote))) {
error_log($remote->get_error_message());
}
}
}
static function get($url, $transient = false)
{
$wrapper = new GetHttpWrapper($url, $transient);
return $wrapper->get_body();
}
private function is_ok($remote)
{
return !is_wp_error($remote) && 200 == wp_remote_retrieve_response_code($remote) && !empty(wp_remote_retrieve_body($remote));
}
function get_body()
{
return $this->response;
}
}
new MudletRelease();