1
0
mirror of https://github.com/dwaxweiler/connector-mobilizon synced 2025-06-05 21:59:25 +02:00
Files
connector-wordpress/source/includes/Api.php
Daniel Waxweiler 761b1d1c46 wip
2023-11-11 20:12:30 +01:00

53 lines
1.4 KiB
PHP

<?php
namespace MobilizonConnector;
class Api {
public static function init() {
add_action('rest_api_init', 'MobilizonConnector\Api::init_api');
}
public static function init_api() {
register_rest_route(
NAME . '/v1',
'/events',
[
'methods' => 'GET',
'callback' => 'MobilizonConnector\Api::get_events',
'args' => [
'eventsCount' => [
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric($param) && $param > 0;
}
],
'groupName' => [
'validate_callback' => function($param, $request, $key) {
return !is_numeric($param);
}
]
]
]
);
}
public static function get_events($request) {
$eventsCount = $request['eventsCount'];
$groupName = isset($request['groupName']) ? $request['groupName'] : '';
$url = Settings::getUrl();
try {
if ($groupName) {
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
} else {
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
}
return $events;
} catch (GeneralException $e) {
return 'The events could not be loaded!';
} catch (GroupNotFoundException $e) {
return sprintf('The group "%s" could not be found!', $groupName);
}
}
}