-
Notifications
You must be signed in to change notification settings - Fork 0
/
fab3-bailiffs-endpoint.php
148 lines (134 loc) · 3.87 KB
/
fab3-bailiffs-endpoint.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
<?php
/*
Plugin Name: FAB3 Bailiffs Endpoint
Plugin URI: https://it.auth.gr
Description: Used to return the list of all local Bailiffs in a custom endpoint
Version: 1.0.0
Author: Georgios Nikolaos Tsakonas
License: GPLv2 or later
Text Domain: fab3-bailiffs
*/
/*
* Function that registers the custom endpoint getBailiffs/v2/getAll
*/
// GET resource
add_action( 'rest_api_init', 'fab3_bailiffs_endpoint_register_route' );
function fab3_bailiffs_endpoint_register_route() {
register_rest_route( 'getBailiffs/v2', '/getAll', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'fab3_get_bailiffs_list',
)
);
}
// POST resource
add_action( 'rest_api_init', 'fab3_bailiffs_postalcode_endpoint_register_route' );
function fab3_bailiffs_postalcode_endpoint_register_route() {
register_rest_route( 'getBailiffs/v2', '/getAllFiltered', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'fab3_get_bailiffs_postalcode_list',
)
);
}
/*
* Function that creates the json schema for the custom endpoint
*/
function fab3_get_bailiffs_list() {
/*
* All the bailiffs are returned
*/
$args = array(
'posts_per_page' => -1,
'post_type' => 'kohtutaiturids',
'orderby' => 'ID',
'order' => 'ASC',
'suppress_filters' => false
);
// The bailiffs data are returned based on the previews array
$items = get_posts( $args );
/*
* The json schema is cretaed for the custom endpoint
* It includes the following info for every bailiff:
* - ID
* - Country
* - Details: name, lang, address, postalCode, municipality, tel
*/
$array_new = '';
$array_body = array();
foreach($items as $item){
$array_body[] = array(
"id" => $item->ID,
"country" => 'EE',
"details" => array(
array(
"name" => $item->post_title,
"lang" => get_field('languages_spoken', $item->ID),
"address" => get_field('koh_adress', $item->ID),
"postalCode" => get_field('postal_code', $item->ID),
"municipality" => get_field('koh_linn', $item->ID),
"tel" => get_field('koh_phone', $item->ID),
)
)
);
}
$array_new = array(
"state" => "answered",
"competentBodies" => $array_body
);
echo json_encode($array_new);
}
/*
* Function that create the json schema for the custom endpoint that
* will display bailiffs based on their postal code
*/
function fab3_get_bailiffs_postalcode_list() {
/*
* If the filter postalCode is not present in the URL,
* all the bailiffs will be returned. Otherwise we get
* only the bailiffs who have the specific postalCode
* in their details
*/
if ( isset($_REQUEST['postalCode']) ) {
$args = array(
'posts_per_page' => -1,
'post_type' => 'kohtutaiturids',
'orderby' => 'ID',
'order' => 'ASC',
'meta_key' => 'postal_code',
'meta_value' => $_REQUEST['postalCode'],
'suppress_filters' => false
);
}
// The bailiffs data are returned based on the previews array
$items = get_posts( $args );
/*
* The json schema is cretaed for the custom endpoint
* It includes the following info for every bailiff:
* - ID
* - Country
* - Details: name, lang, address, postalCode, municipality, tel
*/
$array_new = '';
$array_body = array();
foreach($items as $item){
$array_body[] = array(
"id" => $item->ID,
"country" => 'EE',
"details" => array(
array(
"name" => $item->post_title,
"lang" => get_field('languages_spoken', $item->ID),
"address" => get_field('koh_adress', $item->ID),
"postalCode" => get_field('postal_code', $item->ID),
"municipality" => get_field('koh_linn', $item->ID),
"tel" => get_field('koh_phone', $item->ID),
)
)
);
}
$array_new = array(
"state" => "answered",
"competentBodies" => $array_body
);
echo json_encode($array_new);
}
?>