-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtootpress_api.php
More file actions
270 lines (199 loc) · 5.84 KB
/
tootpress_api.php
File metadata and controls
270 lines (199 loc) · 5.84 KB
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
<?php
/**
* Mastodon API Requests
*
* @package TootPress
* @since 0.1
*/
// Security: Stops code execution if WordPress is not loaded
if (!defined('ABSPATH')) { exit; }
/**
* Create HTTP Authorization Header
*
* @since 0.1
*
* @return array Authorization Header with Auth Tokken
*/
function tootpress_create_authorization_header() {
$mastodon_auth_token=tootpress_get_mastodon_auth_token();
$authorization_header=array(
'Authorization' => 'Bearer ' . $mastodon_auth_token
);
return $authorization_header;
};
/**
* Create API Request Query Parameter
*
* API Method: Get User Statuses (Account)
*
* @since 0.1
*
* @param string Timeline Direction (forwards/backwords)
* @return array Query Parameter
*/
function tootpress_create_query_param_get_account_statuses($direction) {
// 1st Mastodon API Request
if(!tootpress_get_latest_toot() OR !tootpress_get_oldest_toot()) {
// Get 40 Toots without Boosts and Replys
$query_param=array(
'limit' => 40,
'exclude_replies' => true,
'exclude_reblogs' => true,
);
// Request Mastodon API forwards
} elseif($direction=='forwards') {
$since_id=tootpress_get_latest_toot();
if($since_id) {
// Get 40 Toots since the last Toot without Boosts and Replys
$query_param=array(
'limit' => 40,
'since_id' => $since_id,
'exclude_replies' => true,
'exclude_reblogs' => true,
);
}
// Reqest the Mastodon API backwards
} elseif($direction=='backwards') {
$max_id=tootpress_get_oldest_toot();
if($max_id) {
// Get 40 Toots before $max_id without Boosts and Replys
$query_param=array(
'limit' => 40,
'max_id' => $max_id,
'exclude_replies' => true,
'exclude_reblogs' => true,
);
}
}
return $query_param;
};
/**
* Get Statuses
*
* API Method: Get Statuses (Account)
*
* @since 0.1
*
* @param string Timeline Reading Direction (forwards/backwords)
* @return array API Response JSON
*/
function tootpress_mastodon_apirequest_account_get_statuses($direction) {
$mastodon_instance=tootpress_get_mastodon_instance();
$mastodon_account_id=tootpress_get_mastodon_account_id();
$endpoint='https://'.$mastodon_instance.'/api/v1/accounts/'.$mastodon_account_id."/statuses";
$args = array(
'body' => tootpress_create_query_param_get_account_statuses($direction),
'headers' => tootpress_create_authorization_header()
);
$response = wp_remote_get( $endpoint, $args );
$body=wp_remote_retrieve_body($response);
$json=json_decode($body, true);
tootpress_mastodon_api_inkrement_amount_of_requests();
return $json;
}
/**
* Verify Credentials
*
* API Method: Verify Credentials (Account)
*
* @since 0.1
*
* @return array API Response JSON
*/
function tootpress_mastodon_apirequest_account_verify_credentials () {
$mastodon_instance=tootpress_get_mastodon_instance();
$endpoint='https://'.$mastodon_instance.'/api/v1/accounts/verify_credentials';
$args = array(
'headers' => tootpress_create_authorization_header()
);
$response = wp_remote_get( $endpoint, $args );
$body=wp_remote_retrieve_body($response);
$json=json_decode($body, true);
tootpress_mastodon_api_inkrement_amount_of_requests();
return $json;
}
/**
* Verifies instance
*
* API Method: View Server Information (Instance)
*
* @since 0.1
*
* @param string Mastodon Instance
* @return string Status Code
*/
function tootpress_mastodon_apirequest_instance_verify ($instance) {
$endpoint='https://'.$instance.'/api/v2/instance';
$response = wp_remote_get( $endpoint );
$status_code=wp_remote_retrieve_response_code ($response);
tootpress_mastodon_api_inkrement_amount_of_requests();
return $status_code;
}
/**
* Verifies the authcode
*
* API Method: Verify Credentials (Account)
*
* @since 0.1
*
* @param string Auth Code
* @return string Status Code
*/
function tootpress_mastodon_apirequest_authcode_verify ($authcode) {
$mastodon_instance=tootpress_get_mastodon_instance();
$endpoint='https://'.$mastodon_instance.'/api/v1/accounts/verify_credentials';
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $authcode
)
);
$response = wp_remote_get( $endpoint, $args );
$status_code=wp_remote_retrieve_response_code ($response);
tootpress_mastodon_api_inkrement_amount_of_requests();
return $status_code;
}
/**
* Verifies Readyness for API Requests with Authentication
*
* @since 0.1
*
* @return bool
*/
function tootpress_ready_to_authenticate_with_mastodon_api() {
$instance=tootpress_get_mastodon_instance();
$accesstoken=tootpress_get_mastodon_auth_token();
if($instance=='' OR $accesstoken == '') {
return false;
} else {
return true;
}
}
/**
* Verifies Readyness for API Requests to retrieve Toots
*
* @since 0.1
*
* @return bool
*/
function tootpress_ready_to_retrieve_toots_from_mastodon_api() {
$instance=tootpress_get_mastodon_instance();
$accesstoken=tootpress_get_mastodon_auth_token();
$accountid=tootpress_get_mastodon_account_id();
if($instance=='' OR $accesstoken=='' OR $accountid=='') {
return false;
} else {
return true;
}
}
/**
* Inkrements the Number of previous API Requests
*
* @since 0.1
*
*/
function tootpress_mastodon_api_inkrement_amount_of_requests() {
$amount_of_requests=get_option('tootpress_mastodon_amount_of_requests');
++$amount_of_requests;
update_option('tootpress_mastodon_amount_of_requests',$amount_of_requests);
};
?>