-
Notifications
You must be signed in to change notification settings - Fork 0
/
learning-paths-api.php
68 lines (61 loc) · 1.96 KB
/
learning-paths-api.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
<?php
/**
* Plugin Name: Learning Paths API
*
* @author Pierre Duverneix
* @copyright 2022 Fondation UNIT
* @license GPL-2.0-or-later
* Plugin URI: https://example.com/plugin-name
* Description: Learning paths API of L'Université Numérique
* Version: 1.0.0
*/
require_once 'src/api.php';
register_activation_hook(__FILE__, 'learningpathsapi_activate');
register_deactivation_hook(__FILE__, 'learningpathsapi_deactivate');
register_uninstall_hook(__FILE__, 'learningpathsapi_uninstall');
function learningpathsapi_get_fields() {
$diplomaModel = new LearningPathApi();
return $diplomaModel->getFields();
}
function learningpathsapi_get_data($data) {
$diplomaId = $data['id'];
$diplomaModel = new LearningPathApi();
return $diplomaModel->getData($diplomaId);
}
function learningpathsapi_filter_data($data) {
$diplomaModel = new LearningPathApi();
return $diplomaModel->filterData($data['id'], $data['origin']);
}
add_action('rest_api_init', function () {
register_rest_route('learningpathsapi/v1', '/fields/all', array(
'methods' => 'GET',
'callback' => 'learningpathsapi_get_fields',
));
register_rest_route('learningpathsapi/v1', '/data/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'learningpathsapi_get_data',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric($param);
}
),
),
));
register_rest_route('learningpathsapi/v1', '/filter/id=(?P<id>\d+)&type=(?P<origin>[a-z]+)', array(
'methods' => 'GET',
'callback' => 'learningpathsapi_filter_data',
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric($param);
}
),
'origin' => array(
'validate_callback' => function($param, $request, $key) {
return is_string($param);
}
),
),
));
});