-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtootpress_process.php
280 lines (207 loc) · 6.21 KB
/
tootpress_process.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
<?php
/**
* Process
*
* @package TootPress
* @since 0.1
*/
// Security: Stops code execution if WordPress is not loaded
if (!defined('ABSPATH')) { exit; }
/**
* Core Process
*
* @since 0.1
*
* @param string API Reading Direction ('forwards'/'backwords')
* @return bool Have new toots been loaded?
*/
function tootpress_copy_toots_from_mastodon($direction) {
// Make Mastodon API Call and retrieve data
$array_toots=tootpress_mastodon_apirequest_account_get_statuses($direction);
// Stop process, if no new toots are existing
if(!array_key_exists('0', $array_toots)) {
// Shutdown AllToots Cron, if last toot is loaded
if($direction=='backwards') {
update_option('tootpress_cron_alltoots_status','0');
update_option('tootpress_timeline_complete', "1");
}
return false;
}
// Save API Response as File
tootpress_create_mastodon_apiresponse_file($array_toots);
// Create filtered Toot and Media Caches
$toot_cache=tootpress_create_toot_cache($array_toots);
$media_cache=tootpress_create_media_cache($array_toots);
// Retrieve Images
$media_cache=tootpress_copy_images_from_mastodon($media_cache);
// Save Caches to Database
tootpress_save_tootcache_into_database($toot_cache);
tootpress_save_media_into_database($media_cache);
// Update Parameter
tootpress_set_latest_toot();
tootpress_set_oldest_toot();
tootpress_set_last_insert();
// Fire WordPress Hook
tootpress_fire_toots_update();
return true;
}
/**
* Creates the toot cache
*
* @since 0.1
*
* @param array JSON Response from Mastodon API
* @return array Array with Toots and attributes in scope
*/
function tootpress_create_toot_cache($json_toots) {
$toot_cache=array();
foreach($json_toots as $attribute) {
// Should this toot be processed?
if(tootpress_is_toot_public($attribute['visibility'])) {
$toot_media=0;
// Does the toot contain attachments?
if(array_key_exists('0', $attribute['media_attachments'])) {
$toot_media=1;
}
// Convert the ISO 8601 to MySQL Datetime Format
$toot_date=iso8601_to_datetime($attribute['created_at']);
// Fill array
$toot_cache[]=array(
'mastodon_id' => $attribute['id'],
'date' => $toot_date,
'content' => $attribute['content'],
'media' => $toot_media
);
}
}
// Ensure a chrononical order in database storage
sort($toot_cache);
return $toot_cache;
}
/**
* Creates the media cache
*
* @since 0.1
*
* @param array JSON Response from Mastodon API
* @return array Array with media and attributes in scope
*/
function tootpress_create_media_cache($json_toots) {
$media_cache=array();
foreach($json_toots as $attribute) {
// Should this toot be processed?
if(tootpress_is_toot_public($attribute['visibility'])) {
// Does the toot contain attachments?
if(array_key_exists('0', $attribute['media_attachments'])) {
// Keep Reference Toot
$toot_id=$attribute['id'];
foreach($attribute['media_attachments'] as $media_attribute) {
// Should this attachment be processed?
if(tootpress_is_attachment_image($media_attribute['type'])) {
// Fill array
$media_cache[]=array(
'mastodon_id' => $media_attribute['id'],
'reference_toot' => $toot_id,
'file' => $media_attribute['url'],
'description' => $media_attribute['description'],
'width' => $media_attribute['meta']['original']['width'],
'height' => $media_attribute['meta']['original']['height']
);
}
}
}
}
}
// Ensure a chrononical order in the database
sort($media_cache);
return $media_cache;
}
/**
* Saves the toot cache into database
*
* @since 0.1
*
* @param array Toot Cache
*/
function tootpress_save_tootcache_into_database($toot_cache) {
foreach($toot_cache as $toot) {
tootpress_add_toot(
$toot['mastodon_id'],
$toot['date'],
$toot['content'],
$toot['media']
);
}
}
/**
* Saves the media cache into database
*
* @since 0.1
*
* @param array Toot Cache
*/
function tootpress_save_media_into_database($media_cache) {
// Does exist an entry in Media Cache?
if(array_key_exists('0', $media_cache)) {
foreach($media_cache as $attachment) {
tootpress_add_media_attachment(
$attachment['mastodon_id'],
$attachment['reference_toot'],
$attachment['file'],
$attachment['description'],
$attachment['width'],
$attachment['height']
);
}
}
}
/**
* Copy Images from Mastodon
*
* @since 0.1
*
* @param array Media Cache
*/
function tootpress_copy_images_from_mastodon($media_cache) {
// Does exist an entry in Media Cache?
if(array_key_exists('0', $media_cache)) {
foreach($media_cache as &$attribute) {
// Retrieve image file
tootpress_retrieve_image_from_mastodon($attribute['file']);
// Transfrom image url to file name
$attribute['file']=tootpress_get_image_name($attribute['file']);
}
}
return $media_cache;
}
/**
* Verifies if Toot is public
*
* @since 0.1
*
* @param string Visibility Attribute
* @return bool
*/
function tootpress_is_toot_public($visibility) {
if ($visibility=='public') {
return true;
} else {
return false;
}
}
/**
* Verifies if attachment is an image
*
* @since 0.1
*
* @param string Type Attribute
* @return bool
*/
function tootpress_is_attachment_image($type) {
if ($type=='image') {
return true;
} else {
return false;
}
}
?>