PluginProbe
PowerPress Podcasting plugin by Blubrry / trunk
PowerPress Podcasting plugin by Blubrry vtrunk
11.17.9 11.17.8 11.17.7 11.17.6 11.17.4 11.17.3 11.17.2 11.17.1 11.17 11.16.11 11.16.10 11.16.9 11.16.8 11.16.7 11.16.6 11.16.5 11.16.4 11.16.3 11.16.2 11.16.1 11.9.13 11.9.14 11.9.15 11.9.16 11.9.17 All 383 releases
powerpress / api-data-transfer-bus.class.php

api-data-transfer-bus.class.php in PowerPress Podcasting plugin by Blubrry trunk, at api-data-transfer-bus.class.php

483 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class PowerpressNetworkDataBus{
4 function __construct()
5 {
6 //Nothing here
7 }
8
9 /**
10 * Returns requested data after deciding whether to pull the data from a cache or the API
11 * @param $creds array of oauth credentials for blubrry api
12 * @param $cacheName string
13 * @param $requestUrl string
14 * @param $needDirectAPI bool--true if we do not want a cached result
15 * @param $clientAuth bool
16 * @return array of data
17 */
18 static function getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI, $clientAuth = true)
19 {
20 // value of $requestUrl should not have
21 if( preg_match('/^https?:\/\/([^\/]*)(.*)$/', $requestUrl, $matches) ) {
22 $requestUrl = $matches[2];
23 }
24
25 if (!$creds) {
26 $creds = array();
27 }
28
29 if( empty($creds['post']) )
30 $creds['post'] = false;
31
32 $cache = get_option($cacheName, array());
33 //Need direct API: will not use cache but directly call an API
34 if ($needDirectAPI === false && !empty($cache)) {
35 if (!empty($cache['data']) && isset($cache['insert_timestamp']) && $cache['insert_timestamp'] > (time() - 4*60*60)) {
36 //mail ('use cache 1', 'use cache 1 ', print_r('use cache 1', true));
37 //case 1: there is still cache in database and it is created from less than 1 day
38 return $cache['data'];
39 } else if (isset($cache['last_error_timestamp']) && $cache['last_error_timestamp'] > time() - 60*60 && !empty($cache['data'])) {
40 //mail ('use cache 2', 'use cache 2 ', print_r('use cache 2', true));
41 //case 2: if the cache is created from more than 1 days, but the last error is less than 1 hour
42 return $cache['data'];
43 } else {
44 $props = $GLOBALS['ppn_object']->requestAPI($requestUrl, $clientAuth, $creds['post']);
45 if (empty($props['error'])) {
46 $cache['data'] = $props;
47 $cache['insert_timestamp'] = time();
48 update_option($cacheName, $cache);
49 //case 3: there is no useful cache in case 1 and 2, call an API. If successful, insert new cache to database
50 //mail ('use API', 'use API ', print_r('use API', true));
51 return $props;
52 } else {
53 $cache['last_error_timestamp'] = time();
54 $cache['insert_timestamp'] = 0;
55 $cache['returned_data'] = $props;
56 update_option($cacheName, $cache);
57 if (isset($cache['data'])) {
58 //mail ('use cache 3', 'use cache 3 ', print_r('use cache 3', true));
59 //case 4: if the new API call also produces error, update the timestamp error
60 return $cache['data'];
61 } else {
62 //mail ('empty', 'empty', print_r('empty', true));
63 //case 5: nothing is useful, return error
64 return array('error' => $cache['error']);
65 }
66 }
67 }
68 } else{
69 $props = $GLOBALS['ppn_object']->requestAPI($requestUrl, $clientAuth, $creds['post']);
70 if (isset($cacheName)) {
71 if (empty($props['error'])) {
72 $cache['data'] = $props;
73 $cache['insert_timestamp'] = time();
74 update_option($cacheName, $cache);
75 //mail ('use API', 'use API ', print_r('use API', true));
76 return $props;
77 } else {
78 $cache['last_error_timestamp'] = time();
79 $cache['insert_timestamp'] = 0;
80 $cache['error'] = $props['error'];
81 $cache['returned_data'] = $props;
82 update_option($cacheName, $cache);
83 if (isset($cache['data'])) {
84 //mail ('use cache 3', 'use cache 3 ', print_r('use cache 3', true));
85 return $cache['data'];
86 } else {
87 //mail ('empty', 'empty', print_r('empty', true));
88 return array('error' => $cache['error']);
89 }
90 }
91 }
92 return $props;
93 }
94 }
95
96 /**
97 * Clears the specified cache
98 * @param $cacheName string
99 * @return true on success bool
100 */
101 static function clearCache($cacheName) {
102 return delete_option($cacheName);
103 }
104
105 /**
106 * Clears cache using LIKE
107 */
108 static function clearCacheByLike(string $pattern): void {
109 global $wpdb;
110 $rows = $wpdb->get_col(
111 $wpdb->prepare(
112 "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
113 $pattern
114 )
115 );
116 foreach ($rows as $name) {
117 delete_option($name);
118 }
119 }
120
121 /**
122 * Returns networks associated to the account
123 * @param $creds array of oauth credentials for blubrry api
124 * @return array of networks
125 */
126 static function getNetworksInAccount($creds)
127 {
128 $cacheName = 'ppn-cache n';
129 $requestUrl = '/2/powerpress/network?cache=' . md5( rand(0, 999) . time()) ;
130 $result = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, true);
131
132 if (!is_array($result)) {
133 return [];
134 }
135
136 if (isset($result['error'])) {
137 powerpress_page_message_add_error($result['error']);
138 }
139
140 $networks = [];
141 foreach ($result as $key => $row) {
142 if (!is_int($key) || !is_array($row) || empty($row['network_id'])) {
143 continue;
144 }
145
146 $networks[] = [
147 'network_id' => $row['network_id'],
148 'network_title' => $row['network_title'] ?? '',
149 ];
150 }
151
152 $cache = get_option($cacheName, []);
153 $cache['data'] = $networks;
154 update_option($cacheName, $cache);
155
156 return $networks;
157 }
158
159 /**
160 * Returns data about a specific network
161 * @param $apiUrl string
162 * @param $creds array of oauth credentials for blubrry api
163 * @param $networkInfo array
164 * @param $needDirectAPI bool--true if we do not want a cached result
165 * @return array|bool--either the desired data or false
166 */
167 static function getSpecificNetworkInAccount($apiUrl, $creds, $networkInfo, $needDirectAPI)
168 {
169 if (isset($networkInfo['network_id'])) {
170 $cacheName = 'ppn-cache n-' . $networkInfo['network_id'];
171 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id']. '?cache=' . md5( rand(0, 999) . time()) ;
172 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI);
173 return $props;
174 }
175 return false;
176 }
177
178 /**
179 * Returns all programs in a network
180 * @param $apiUrl string
181 * @param $creds array of oauth credentials for blubrry api
182 * @param $networkInfo array
183 * @param $needDirectAPI bool--true if we do not want a cached result
184 * @return array of programs
185 */
186 static function getProgramsInNetwork($apiUrl, $creds, $networkInfo, $needDirectAPI)
187 {
188 $cacheName = 'ppn-cache n-'.$networkInfo['network_id'].'-p';
189 $requestUrl = '/2/powerpress/network/' . $networkInfo['network_id'] . '/programs?cache=' . md5( rand(0, 999) . time()) ;
190 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI);
191 }
192
193 /**
194 * Changes the status of an applicant to the network
195 * @param $apiUrl string
196 * @param $creds array of oauth credentials for blubrry api
197 * @param $networkInfo array
198 * @param $appAction array with action and applicant id
199 * @return array with return data or error
200 */
201 static function changeApplicationStatus($apiUrl, $creds, $networkInfo, $appAction)
202 {
203 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/applicant/changestatus?action=' . $appAction['appAction'] . '&applicantId=' . $appAction['applicantId'];
204 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
205 }
206
207 /**
208 * Returns all lists in a network
209 * @param $apiUrl string
210 * @param $creds array of oauth credentials for blubrry api
211 * @param $networkInfo array
212 * @param $needDirectAPI bool--true if we do not want a cached result
213 * @return array of lists
214 */
215 static function getListsInNetwork($apiUrl, $creds, $networkInfo, $needDirectAPI)
216 {
217 $cacheName = 'ppn-cache n-'.$networkInfo['network_id'].'-l';
218 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/?cache=' . md5( rand(0, 999) . time()) ;
219 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI);
220 return $props;
221 }
222
223 /**
224 * Creates a new list in a network
225 * @param $apiUrl string
226 * @param $creds array of oauth credentials for blubrry api
227 * @param $networkInfo array
228 * @param $create array of data about the new list
229 * @return array with return data or error
230 */
231 static function createNewList($apiUrl, $creds, $networkInfo, $create)
232 {
233 $create['newListTitle'] = urlencode($create['newListTitle']);
234 $create['newListDescription'] = urlencode($create['newListDescription']);
235 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/new?title=' . $create['newListTitle'] . '&description=' . $create['newListDescription'];
236 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
237 }
238
239 /**
240 * Replaces all programs inside a list with the input array of programs
241 * @param $apiUrl string
242 * @param $creds array of oauth credentials for blubrry api
243 * @param $networkInfo array
244 * @param $addedPrograms array of programs
245 * @return array with return data or error
246 */
247 static function updateProgramsInSpecificList($apiUrl, $creds, $networkInfo, $addedPrograms)
248 {
249 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/' . $networkInfo['list_id'] . '/update-programs?';
250 if (isset($addedPrograms)) {
251 foreach ($addedPrograms as $programId) {
252 $requestUrl .= 'programId[]=' . $programId . '&';
253 }
254 }
255 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
256 }
257
258 /**
259 * Adds one program to a list
260 * @param $apiUrl string
261 * @param $creds array of oauth credentials for blubrry api
262 * @param $networkInfo array
263 * @param $addedProgram int program id
264 * @return array with return data or error
265 */
266 static function addProgramToList($apiUrl, $creds, $networkInfo, $addedProgram)
267 {
268 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/' . $networkInfo['list_id'] . '/add-program-to-list?';
269 $requestUrl .= 'programId=' . $addedProgram . '&';
270
271 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
272 }
273
274 /**
275 * Deletes a list
276 * @param $apiUrl string
277 * @param $creds array of oauth credentials for blubrry api
278 * @param $networkInfo array which includes the id of the list to be deleted
279 * @return array with return data or error
280 */
281 static function deleteSpecificList($apiUrl, $creds, $networkInfo)
282 {
283 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/' . $networkInfo['list_id'] . '/delete';
284 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
285 }
286
287 /**
288 * Returns all programs that have applied to a network
289 * @param $apiUrl string
290 * @param $creds array of oauth credentials for blubrry api
291 * @param $networkInfo array
292 * @param $needDirectAPI bool--true if we do not want a cached result
293 * @return array of applicants
294 */
295 static function getApplicantsInNetwork($apiUrl, $creds, $networkInfo, $needDirectAPI)
296 {
297 $cacheName = "ppn-cache n-".$networkInfo['network_id']."-a";
298 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/applicant?cache=' . md5( rand(0, 999) . time() );
299 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI);
300 }
301
302 /**
303 * Returns data relating to one specific list
304 * @param $apiUrl string
305 * @param $creds array of oauth credentials for blubrry api
306 * @param $networkInfo array which includes the id of the desired list
307 * @return array|bool--array of list data or false
308 */
309 static function getSpecificListInNetwork ($apiUrl, $creds, $networkInfo, $needDirectAPI)
310 {
311 if (!empty($networkInfo['network_id']) && !empty ($networkInfo['list_id'])) {
312 $cacheName = 'ppn-cache n-' . $networkInfo['network_id'] . '-l-' . $networkInfo['list_id'];
313 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/' . $networkInfo['list_id'] . '/programs?cache=' . md5( rand(0, 999) . time()) ;
314 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI = true);
315 if (!empty($props['list_info']['list_title']) && !empty($props['list_info']['list_description'])) {
316 PowerPressNetwork::insertOption('list_title', $props['list_info']['list_title']);
317 PowerPressNetwork::insertOption('list_description', $props['list_info']['list_description']);
318 }
319 return $props;
320 }
321 return false;
322 }
323
324 /**
325 * Returns data relating to one specific list
326 * @param $apiUrl string
327 * @param $creds array of oauth credentials for blubrry api
328 * @param $networkInfo array which includes the id of the desired program
329 * @param $needDirectAPI bool--true if we do not want a cached result
330 * @return array|bool--array of program data or false
331 */
332 static function getSpecificProgramInNetwork($apiUrl, $creds, $networkInfo, $needDirectAPI)
333 {
334 $cacheName = 'ppn-cache n-'.$networkInfo['network_id'].'-p-'.$networkInfo['program_id'];
335 if (!empty($networkInfo['network_id']) && !empty($networkInfo['program_id']) ) {
336 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/programs/' . $networkInfo['program_id'] . '?cache=' . md5( rand(0, 999) . time()) ;
337 $props = PowerpressNetworkDataBus::getCacheOrCallAPI(false, $cacheName, $requestUrl, $needDirectAPI);
338 if (!empty($props['program_info']['program_id']) && !empty($props['program_info']['program_title'])) {
339 PowerPressNetwork::insertOption('program_id', $props['program_info']['program_id']);
340 PowerPressNetwork::insertOption('program_title', $props['program_info']['program_title']);
341 }
342 return $props;
343 }
344 return false;
345 }
346
347 /**
348 * Removes a program from a network
349 * @param $apiUrl string
350 * @param $creds array of oauth credentials for blubrry api
351 * @param $networkInfo array which includes the id of the program to be removed
352 * @param $needDirectAPI bool--true if we do not want a cached result
353 * @return array|bool--array of return data or false
354 */
355 static function removeSpecificProgramInNetwork($apiUrl, $creds, $networkInfo, $needDirectAPI)
356 {
357 $cacheName = 'ppn-cache n-'.$networkInfo['network_id'].'-p-'.$networkInfo['program_id'];
358 if (!empty($networkInfo['network_id']) && !empty($networkInfo['program_id']) ) {
359 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/programs/' . $networkInfo['program_id'] . '/delete';
360 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, $cacheName, $requestUrl, $needDirectAPI);
361 if (!empty($props['program_info']['program_id']) && !empty($props['program_info']['program_title'])) {
362 PowerPressNetwork::removeOption('program_id', $props['program_info']['program_id']);
363 PowerPressNetwork::removeOption('program_title', $props['program_info']['program_title']);
364 }
365 return $props;
366 }
367 return false;
368 }
369
370 /**
371 * Returns data about the user account associated to the network
372 * @param $apiUrl string
373 * @param $creds array of oauth credentials for blubrry api
374 * @param $networkInfo array which includes the id of the desired program
375 * @param $needDirectAPI bool--true if we do not want a cached result
376 * @return array|bool--array of account data or false
377 */
378 static function getNetworkOwnerInformation($apiUrl, $creds, $networkInfo, $needDirectAPI)
379 {
380 if(!empty($networkInfo['network_id']))
381 {
382 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/account/information/?cache=' . md5( rand(0, 999) . time()) ;
383 $props = self::getCacheOrCallAPI($creds, null, $requestUrl, $needDirectAPI);
384 return $props;
385 } else {
386 return false;
387 }
388 }
389
390 /**
391 * Given a feed URL, returns information about the applicant
392 * @param $apiUrl string url to Blubrry api
393 * @param $createUrl string url to wordpress site
394 * @param $tokenInfo array which includes the id of the desired program
395 * @return array of credentials
396 */
397 static function refreshTokenToGetCredentials($apiUrl, $createUrl, $tokenInfo)
398 {
399 $clientAuth = $tokenInfo['client_auth'];
400 $refreshToken = $tokenInfo['refresh_token'];
401 $oldAccessToken = $tokenInfo['access_token'];
402 $requestUrl = $apiUrl . 'oauth2/token?grant_type=refresh_token&refresh_token=' . $refreshToken;
403 $post = array('grant_type' => 'refresh_token', 'refresh_token' => $refreshToken, 'redirect_uri' => $createUrl . 'wp-admin/admin.php?page=network-plugin');
404 $creds['post'] = $post;
405 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true, $clientAuth);
406 $props['client_auth'] = $clientAuth;
407 $props['refresh_token'] = $refreshToken;
408 return $props;
409 }
410
411 /**
412 * Given a feed URL, returns information about the applicant
413 * @param $apiUrl string
414 * @param $creds array of oauth credentials for blubrry api
415 * @param $networkInfo array which includes the id of the desired program
416 * @param $feedUrl string
417 * @return array of applicant data
418 */
419 static function getFeedFromLink($apiUrl, $creds, $networkInfo, $feedUrl)
420 {
421 $feedUrl = trim($feedUrl);
422 $feedUrl = preg_replace('#^(https?://|ftps?://)?(www.)?#', '', $feedUrl);
423 $feedUrl = urlencode($feedUrl);
424 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/applicant/findshow?feedUrl=' . $feedUrl . '&cache=' . md5( rand(0, 999) . time()) ;
425 $props = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
426 $requestUrl = $apiUrl.'2/powerpress/network/'.$networkInfo['network_id'].'/lists/';
427 $props['list'] = PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
428 return $props;
429 }
430
431 /**
432 * Returns data relating to one specific list
433 * @param $apiUrl string
434 * @param $creds array of oauth credentials for blubrry api
435 * @param $networkInfo array which includes the id of the desired program
436 * @param $add array of data about the applicant
437 * @return array with return data or error
438 */
439 static function submitApplication($apiUrl, $creds, $networkInfo, $add)
440 {
441 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/applicant/submit?listId=' . $add['listIdForApp'] . '&programId=' . $add['programIdForApp'] . '&feedUrl=' . $add['feedUrl'] . '&webName=' . $add['appLabel'];
442 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
443 }
444
445 /**
446 * Returns data relating to one specific list
447 * @param $apiUrl string
448 * @param $creds array of oauth credentials for blubrry api
449 * @param $networkInfo array which includes the id of the desired program
450 * @param $update array of new data to overwrite the existing list data
451 * @return array with return data or error
452 */
453 static function updateList($apiUrl, $creds, $networkInfo, $update)
454 {
455 $update['editListTitle'] = urlencode($update['editListTitle']);
456 $update['editListDescription'] = urlencode($update['editListDescription']);
457 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/lists/'.$networkInfo['list_id'].'/edit?title=' . $update['editListTitle'] . '&description=' . $update['editListDescription'];
458 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);;
459
460 }
461
462 /**
463 * Given a feed URL, returns information about the applicant
464 * @param $apiUrl string
465 * @param $post array
466 * @return array of token data
467 */
468 static function checkTokenValidity($apiUrl, $post)
469 {
470 $creds['post'] = $post;
471 $requestUrl = $apiUrl . '2/powerpress/network/token';
472 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
473 }
474
475 /** updates the network record's own title and description */
476 static function updateNetwork(string $apiUrl, $creds, array $networkInfo, array $update) {
477 $title = urlencode($update['editNetworkTitle']);
478 $description = urlencode($update['editNetworkDescription']);
479 $requestUrl = $apiUrl . '2/powerpress/network/' . $networkInfo['network_id'] . '/edit?title=' . $title . '&description=' . $description;
480 return PowerpressNetworkDataBus::getCacheOrCallAPI($creds, null, $requestUrl, true);
481 }
482 }
483