-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtootpress_tools.php
410 lines (327 loc) · 9.52 KB
/
tootpress_tools.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/**
* Tools
*
* @package TootPress
* @since 0.1
*/
// Security: Stops code execution if WordPress is not loaded
if (!defined('ABSPATH')) { exit; }
/**
* Adds Page to Tool Menu.
*
* @since 0.1
*/
function tootpress_tools_menu() {
add_management_page(
'Toots',
'Toots',
'manage_options',
'tootpress-tools-menu',
'tootpress_tools_controller'
);
}
add_action('admin_menu', 'tootpress_tools_menu');
/**
* Controls the tool page.
*
* @since 0.1
*/
function tootpress_tools_controller() {
echo '<div class="wrap">';
// Apps
$tootpress_copy_toots=false;
$tootpress_cron_newtoots=false;
$tootpress_cron_alltoots=false;
$tootpress_retrieveid=false;
$tootpress_healthy_check=false;
$tootpress_factory_settings=false;
/*
Which App to load?
*/
// Copy Toots
if(isset($_GET['copytoots'])) {
if($_GET['copytoots']=='true')
{
$tootpress_copy_toots=true;
}
}
// Switch Cron New Toots
if(isset($_GET['cronnewtoots'])) {
if($_GET['cronnewtoots']=='true')
{
$tootpress_cron_newtoots=true;
}
}
// Trigger Cron All Toots
if(isset($_GET['cronalltoots'])) {
if($_GET['cronalltoots']=='true')
{
$tootpress_cron_alltoots=true;
}
}
// Receive ID from Mastodon
if(isset($_GET['retrieveid'])) {
if($_GET['retrieveid']=='true')
{
$tootpress_retrieveid=true;
}
}
// Plugin Healthy Check
if(isset($_GET['healthy'])) {
if($_GET['healthy']=='true')
{
$tootpress_healthy_check=true;
}
}
// Factory Settings
if(isset($_GET['factorysettings'])) {
if($_GET['factorysettings']=='true')
{
$tootpress_factory_settings=true;
}
}
// App Loader
if ($tootpress_copy_toots) {
tootpress_copy_toots_load();
} elseif ($tootpress_cron_newtoots) {
tootpress_switch_cron_newtoots_load();
} elseif ($tootpress_cron_alltoots) {
tootpress_trigger_cron_alltoots_load();
} elseif ($tootpress_retrieveid) {
tootpress_retrieve_mastodonid();
} elseif ($tootpress_healthy_check) {
tootpress_healthy_check_load();
} elseif ($tootpress_factory_settings) {
tootpress_factory_settings_load();
} else {
// Display Tools, if no App selected
tootpress_tools();
}
echo '</div>';
}
/**
* Displays the tools
*
* @since 0.1
*/
function tootpress_tools() {
// Create Dynamic Label: Switch Cron New Toots
if(get_option('tootpress_cron_newtoots_status')) {
$button_newtoots_label='Deactivate';
} else {
$button_newtoots_label='Activate';
}
// Create Dynamic Label: Trigger Cron All Tools
if(get_option('tootpress_cron_alltoots_status')) {
$button_alltoots_label='In progress';
} elseif (get_option('tootpress_timeline_complete')) {
$button_alltoots_label='Loaded';
} else {
$button_alltoots_label='Load';
}
echo '
<!-- headline -->
<h1 class="tootpress_tools_headline">Toots</h1>
<p class="tootpress_tools_description">TootPress Tools</p>
<table class="form-table">
<!-- List of Applications -->
<!-- Copy Toots -->
<tr valign="top">
<th scope="row">
<label for="copy-toots">Mastodon API Request</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu©toots=true">Run</a>
</td>
<!-- Steady Fetch -->
<tr valign="top">
<th scope="row">
<label for="cron-newtoots">Steady Fetch</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu&cronnewtoots=true">'.esc_html($button_newtoots_label).'</a>
</td>
<!-- Complete Archiv -->
<tr valign="top">
<th scope="row">
<label for="cron-alltoots">Complete Timeline</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu&cronalltoots=true">'.esc_html($button_alltoots_label).'</a>
</td>
<!-- Receive ID -->
<tr valign="top">
<th scope="row">
<label for="retrieve-id">Account ID</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu&retrieveid=true">Retrieve</a>
</td>
<!-- Plugin Healthy Check -->
<tr valign="top">
<th scope="row">
<label for="plugin-healthy-check">Healthy Check</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu&healthy=true">Show Results</a>
</td>
<!-- Factory Settings -->
<tr valign="top">
<th scope="row">
<label for="factory-settings">Factory Settings</label>
</th>
<td>
<a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu&factorysettings=true">Reset</a>
</td>
</table>';
}
/**
* Outputs the bottom of the tool page
*
* @since 0.1
*/
function tootpress_tools_close() {
echo '<p> <br/><a class="button" href="'.esc_url(admin_url()).'tools.php?page=tootpress-tools-menu">Back to TootPress Tools</a></p>';
}
/**
* Request Mastodon API
*
* @since 0.1
*/
function tootpress_copy_toots_load() {
echo '<h1 class="tootpress_tools_headline">Toots</h1>';
echo '<p class="tootpress_tools_description">Mastodon API Request<br/> </p>';
if(tootpress_ready_to_retrieve_toots_from_mastodon_api()) {
// Run API Request
$api_response=tootpress_copy_toots_from_mastodon("forwards");
// Result API Request
if($api_response) {
echo '<p>Request done.<br/>';
echo 'New toots have been loaded.</p>';
} else {
echo '<p>Request done.<br/>';
echo 'No new toots on Mastodon.</p>';
}
} else {
// TootPress is not ready to run
echo '<p>Request not possible.</p>';
echo wp_kses( tootpress_error_message_required_api_options_missing(), tootpress_escaping_allowed_html() );
}
tootpress_tools_close();
}
/**
* Steady Load
*
* @since 0.1
*/
function tootpress_switch_cron_newtoots_load() {
echo '<h1 class="tootpress_tools_headline">Toots</h1>';
echo '<p class="tootpress_tools_description">Steady Fetch<br/> </p>';
// Current cron status
$status_cron_newtoots=get_option('tootpress_cron_newtoots_status');
if(!$status_cron_newtoots) {
if(tootpress_ready_to_retrieve_toots_from_mastodon_api()) {
// Activate Steady Load
update_option('tootpress_cron_newtoots_status','1');
$period=tootpress_get_custom_cron_period_in_minutes();
echo '<p>Cron was activated.<br/>';
echo 'Steady Fetch runs every '.esc_html($period).' Minutes.<br/>';
echo 'New Toots will be added automatically.</p>';
} else {
// TootPress is not ready to run
echo '<p>Steady Fetch could not be activated.</p>';
echo wp_kses( tootpress_error_message_required_api_options_missing(), tootpress_escaping_allowed_html() );
}
} else {
// Deactivate Steady Load
update_option('tootpress_cron_newtoots_status','0');
echo '<p>Cron was deactivated.<br/>';
echo 'New toots are no longer loaded automatically.</p>';
}
tootpress_tools_close();
}
/**
* Complete Timeline
*
* @since 0.1
*/
function tootpress_trigger_cron_alltoots_load() {
echo '<h1 class="tootpress_tools_headline">Toots</h1>';
echo '<p class="tootpress_tools_description">Complete Timeline<br/> </p>';
// Has the timeline already loaded?
if(get_option('tootpress_timeline_complete')) {
echo '<p>Your Mastodon Timeline is already loaded completely.</p>';
} else {
// Current Cron Status
$status_cron_alltoots=get_option('tootpress_cron_alltoots_status');
if($status_cron_alltoots) {
// Complete Timeline Load is already running
echo '<p>Procedure in progress.<br/>';
echo '480 toots are loaded per hour.<br/>';
echo 'Until the timeline is complete.</p>';
} else {
if(tootpress_ready_to_retrieve_toots_from_mastodon_api()) {
// Initiate Complete Timeline Load
update_option('tootpress_cron_alltoots_status','1');
echo '<p>Procedure is initated.<br/>';
echo 'Your Mastodon Timeline will be loaded shortly.</p>';
} else {
// TootPress is not ready to run
echo '<p>Procedure could not be activated.</p>';
echo wp_kses( tootpress_error_message_required_api_options_missing(), tootpress_escaping_allowed_html() );
}
}
}
tootpress_tools_close();
}
/**
* Receive the Mastodon ID
*
* @since 0.1
*/
function tootpress_retrieve_mastodonid() {
echo '<h1 class="tootpress_tools_headline">TootPress › Mastodon ID</h1>';
echo '<p class="tootpress_tools_description">Retrieve<br/> </p>';
if(tootpress_ready_to_authenticate_with_mastodon_api()) {
// API Request: Verify Credentials
$verifycrendentials=tootpress_mastodon_apirequest_account_verify_credentials();
$mastodonid=$verifycrendentials['id'];
echo '<p>Your Mastodon Account ID is the following.</p>';
echo '<p>'.esc_html($mastodonid).'</p>';
} else {
echo wp_kses( tootpress_error_message_instance_andor_token_missing(), tootpress_escaping_allowed_html() );
}
tootpress_tools_close();
}
/**
* Prepares the page for Healthy Check
*
* @since 0.1
*/
function tootpress_healthy_check_load() {
echo '<h1 class="tootpress_tools_headline">TootPress › Healthy Check</h1>';
echo '<p class="tootpress_tools_description">Analysis<br/> </p>';
$health_status=tootpress_healthy_check();
echo wp_kses( $health_status, tootpress_escaping_allowed_html() );
tootpress_tools_close();
}
/**
* Factory Settings
*
* @since 0.1
*/
function tootpress_factory_settings_load() {
echo '<h1 class="tootpress_tools_headline">TootPress › Factory Settings</h1>';
echo '<p class="tootpress_tools_description">Restore<br/> </p>';
tootpress_restore_factory_settings();
echo '<p>';
echo '* Toots were removed<br/>';
echo '* Media were removed<br/>';
echo '* Cron was deactivated<br/>';
echo '* Settings set to default<br/>';
echo '* Files in Uploads have to be removed manually<br/>';
echo '</p>';
tootpress_tools_close();
}
?>