-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitdown.php
369 lines (303 loc) · 13.8 KB
/
gitdown.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/*
Plugin Name: Gitdown
Plugin URI: https://github.com/Maximinodotpy/Gitdown
Description: Use this Plugin to create, update, delete and manage markdown articles hosted on a remote repository.
Version: __MGD_VERSION__
Author: Maxim Maeder
Author URI: https://maximmaeder.com
Text Domain: gitdown
*/
/*
bn: maximmaeder
pw: $P$BNA.8vIf6NPsAd4qY67U0f7HvXGJce.
https://codebeautify.org/wordpress-password-hash-generator
*/
use Inc\Helpers;
defined( 'ABSPATH' ) or die( 'Hey, what are you doing here? You silly human!' );
class Gitdown
{
private $article_collection;
private $option_slugs = [];
public function __construct()
{
require_once 'vendor/autoload.php';
// Plugin Prefix: mgd(_)
// The Root path of this Plugin Directory
define('MGD_ROOT_PATH', __DIR__ . '/');
define('MGD_ROOT_URL', plugins_url('/', __FILE__));
// Option names, labels, and default values
$this->option_slugs = (object) [
'mgd_glob_setting' => (object) [
'default' => 'simple/*.md',
'label' => 'Glob Pattern',
],
'mgd_repo_setting' => (object) [
'default' => 'https://github.com/Maximinodotpy/gitdown-test-repository.git',
'label' => 'Repository Location',
],
'mgd_resolver_setting' => (object) [
'default' => 'simple',
'label' => 'Resolver',
],
'mgd_cron_setting' => (object) [
'default' => false,
'label' => 'Automatic Updates',
],
];
// Where the current Repository is located depends on the repo url
$repo_nice_name = Inc\Helpers::string_to_slug(
'mgd_'
. basename(dirname(get_option('mgd_repo_setting')))
. '-'
. rtrim(basename(get_option('mgd_repo_setting')), '.git')
);
define('MGD_MIRROR_PATH', WP_CONTENT_DIR . '/mgd_mirror/' . $repo_nice_name . '/');
define('MGD_MIRROR_URL', WP_CONTENT_URL . '/mgd_mirror/' . $repo_nice_name . '/');
define('MGD_REMOTE_IS_CLONED', is_dir(MGD_MIRROR_PATH . '.git'));
// Create the Directory where the files are stored in case it does not exist.
if (!is_dir(MGD_MIRROR_PATH)) {
mkdir(MGD_MIRROR_PATH, 0777, true);
}
$this->article_collection = new Inc\ArticleCollection();
// Setting up the Action Hooks
$this->setupActions();
// Deleting unneded Mirror folders.
chdir(dirname(MGD_MIRROR_PATH));
$d = dir(".");
while (false !== ($entry = $d->read()))
{
if (is_dir($entry) && ($entry != '.') && ($entry != '..') && ($entry != $repo_nice_name)) {
Helpers::delete_directory($entry);
}
}
$d->close();
}
/**
* Setup admin actions and hooks.
*/
private function setupActions()
{
// Activation and Deactivation Hook
register_activation_hook(__FILE__, function () { $this->activate(); });
register_deactivation_hook(__FILE__, function () { $this->deactivate(); });
add_action('admin_init', function () {
// Redirect if the plugin has been activated.
if (get_option('mgd_do_activation_redirect', false)) {
delete_option('mgd_do_activation_redirect');
wp_redirect(home_url('/wp-admin/admin.php?page=mgd-article-manager&how_to'));
}
add_settings_section(
'mgd-settings-section',
'Gitdown Settings',
function () { include(MGD_ROOT_PATH . 'templates/settings/head.php'); },
'reading'
);
// Register the Settings for the reading page.
foreach ($this->option_slugs as $slug => $slug_meta) {
register_setting('reading', $slug);
// Add the settings section
add_settings_field(
$slug,
$slug_meta->label,
function () use ($slug) { include(MGD_ROOT_PATH . 'templates/settings/'. $slug .'.php'); },
'reading',
'mgd-settings-section',
);
}
});
add_action("plugin_action_links_" . plugin_basename(__FILE__), function($links) {
array_push($links, '<a href="options-reading.php">Settings</a>');
array_push($links, '<a href="admin.php?page=mgd-article-manager">Overview</a>');
return $links;
});
// Adding the Admin Menu
add_action(
'admin_menu',
function () {
add_menu_page(
'Gitdown',
'Gitdown',
'manage_options',
'mgd-article-manager',
function () {
if (isset($_GET['how_to'])) include(MGD_ROOT_PATH . 'templates/how_to/how_to.php');
else if (isset($_GET['raw_data'])) {
$data = isset($_GET['slug']) ? $this->article_collection->get_by_slug($_GET['slug']) : $this->article_collection->get_all();
echo '<div class="wrap"><pre style="white-space: pre-wrap;" id="raw_data_container">';
echo '</pre>';
echo '
<script>
const raw_data = ' . json_encode($data) . ';
renderJson({ data: raw_data, top_level_root: raw_data_container });
var element = document.createElement("link");
element.setAttribute("rel", "stylesheet");
element.setAttribute("type", "text/css");
element.setAttribute("href", "' . MGD_ROOT_URL . 'css/json.css");
document.getElementsByTagName("head")[0].appendChild(element);
</script>';
}
else include(MGD_ROOT_PATH . 'templates/articles.php');
},
'data:image/svg+xml;base64,' . base64_encode(file_get_contents(MGD_ROOT_PATH . 'images/icon.svg')),
110,
);
add_action('admin_enqueue_scripts', function () {
$plugin_version = get_plugin_data(MGD_ROOT_PATH.'gitdown.php')['Version'];
wp_enqueue_script('mgd_vuejs', MGD_ROOT_URL . 'js/vue.js', [], $plugin_version);
wp_enqueue_script('mgd_jsonjs', MGD_ROOT_URL . 'js/json.js', [], $plugin_version);
wp_enqueue_script('mgd_adminjs', MGD_ROOT_URL . 'js/admin.js', [], $plugin_version);
wp_enqueue_style('mgd_styles', MGD_ROOT_URL . 'css/gitdown.css', [], $plugin_version);
});
}
);
add_action('admin_enqueue_scripts', function ($hook) {
if ('post.php' != $hook) return;
if (!$this->article_collection->get_by_id($_GET['post'])->_is_published) return;
add_action('wp_print_scripts', function() {
?><script>alert('<?php _e('This Article was added via Gitdown and your specified repository, If you edit the article here it will be overwritten by Gitdown next time you try to update it there.') ?>')</script><?php
});
});
$custom_column_head_callback = function ($columns) {
return array_merge($columns, ['MGD_status' => 'Gitdown Status']);
};
$custom_column_callback = function ($column_key, $post_id) {
if ($column_key == 'MGD_status') {
$post_data = $this->article_collection->get_by_id($post_id);
if ($post_data->_is_published) {
echo '<div class="tw-font-semibold" >✅ from Repository <br/>';
echo '<code>';
echo $post_data->remote->last_commit == $post_data->local->last_commit ?
'Up To Date' : 'Outdated';
echo '</code>';
echo '</div>';
} else {
echo '<div class="tw-font-semibold" >❌ Not from Repository</div>';
}
}
};
$row_actions = function ( $actions, $post ) {
$postData = $this->article_collection->get_by_id($post->ID);
if ($postData->_is_published) {
unset( $actions['inline hide-if-no-js'] );
}
return $actions;
};
// Add these filters for Posts and Pages
add_filter('manage_post_posts_columns', $custom_column_head_callback);
add_filter('manage_pages_columns', $custom_column_head_callback);
add_action('manage_post_posts_custom_column', $custom_column_callback, 10, 2);
add_action('manage_pages_custom_column', $custom_column_callback, 10, 2);
add_filter('post_row_actions', $row_actions, 10, 2 );
add_filter('page_row_actions', $row_actions, 10, 2 );
function verify_ajax() {
if ( !current_user_can('edit_posts') ) {
echo json_encode(false);
die();
};
}
// Ajax Calls
add_action("wp_ajax_get_all_articles", function () {
verify_ajax();
echo json_encode(array(
'posts' => $this->article_collection->get_all(),
'reports' => $this->article_collection->reports
));
die();
});
add_action("wp_ajax_update_article", function () {
echo json_encode($this->article_collection->update_post($_REQUEST['slug']));
die();
});
add_action("wp_ajax_delete_article", function () {
verify_ajax();
echo json_encode($this->article_collection->delete_post($_REQUEST['slug']));
die();
});
add_action("wp_ajax_mgd_get_outdated", function () {
if (! (bool) get_option('mgd_cron_setting') ) return;
echo json_encode($this->article_collection->get_outdated());
die();
});
add_action('init', function() {
if (! (bool) get_option('mgd_cron_setting') ) return;
add_action('wp_print_scripts', function() {
?>
<script>
console.log('MGD Autoupdate starting ...');
(async () => {
console.log('MGD Autoupdate Request ...');
const form_data = new FormData()
form_data.append('action', 'mgd_get_outdated')
const re = await fetch(ajaxurl, {
method: 'POST',
body: form_data,
})
try {
const articles = await re.json()
console.log(articles);
if (articles.constructor != Array) return;
for (const article of articles ) {
console.log('MGD Autoupdate Updating:', article.remote.slug);
const form_data = new FormData()
form_data.append('action', 'update_article')
form_data.append('slug', article.remote.slug)
fetch(ajaxurl, {
method: 'POST',
body: form_data,
}).then( re => {
re.json().then(result => {
console.log('MGD Autoupdate Result ...', result)
});
})
}
} catch (error) {
console.log(error);
}
})()
</script>
<?php
});
});
// Add a custom API Endpoint which updates outdated articles
add_action('rest_api_init', function () {
register_rest_route('mgd/v1', '/update_outdated', [
'methods' => 'GET',
'callback' => function () {
$outdated = $this->article_collection->get_outdated();
foreach ($outdated as $key => $value) {
$this->article_collection->update_post($value->remote->slug);
}
echo sizeof($outdated) . ' Articles Updated';
foreach ($outdated as $key => $value) {
echo '<br/>- ' . $value->remote->slug;
}
},
]);
});
// Where do i call this link
// https://maximmaeder.com/wp-json/mgd/v1/update_outdated
}
public function activate() {
// Loop over all option slugs and add them and their default values
foreach ($this->option_slugs as $slug => $slug_options) {
add_option($slug, $slug_options->default);
}
// Do this to later show the documentation
add_option('mgd_do_activation_redirect', true);
}
public function deactivate()
{
// Deleting all Options
foreach ($this->option_slugs as $key => $value) {
delete_option($key);
}
// Delete Mirror Directory
Helpers::delete_directory(dirname(MGD_MIRROR_PATH));
// Delete mgd_last_updated for every post
foreach ($this->article_collection->get_all() as $key => $value) {
delete_post_meta($value->local->id, 'mgd_last_updated');
}
}
};
$b8cc4bfd_b866_4956_89db_2f0eeb671e61 = new Gitdown();