-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.php
244 lines (220 loc) · 9.86 KB
/
map.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
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
<?php
if (!defined('ABSPATH') || !defined('CASTORS_THEME_VERSION')) exit;
class Castors_Map {
public static function activate() {
get_role('administrator')->add_cap('castors_map_show_members');
get_role('administrator')->add_cap('castors_map_show_worksites');
get_role('administrator')->add_cap('castors_map_show_experts');
get_role('administrator')->add_cap('castors_map_show_meetings');
}
public static function deactivate() {
get_role('administrator')->remove_cap('castors_map_show_members');
get_role('administrator')->remove_cap('castors_map_show_worksites');
get_role('administrator')->remove_cap('castors_map_show_experts');
get_role('administrator')->remove_cap('castors_map_show_meetings');
}
public static function enqueue_scripts() {
global $post;
if(is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'castors_map')) {
wp_enqueue_script('leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js');
wp_enqueue_script('leaflet-markercluster', 'https://unpkg.com/[email protected]/dist/leaflet.markercluster.js');
wp_enqueue_script('castors-map', CASTORS_THEME_URI . 'js/map.min.js', ['leaflet'], false, ['strategy' =>'defer', 'in_footer' => true]);
wp_enqueue_style('leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css');
wp_enqueue_style('leaflet-markercluster', 'https://unpkg.com/[email protected]/dist/MarkerCluster.css');
wp_enqueue_style('leaflet-markercluster-default', 'https://unpkg.com/[email protected]/dist/MarkerCluster.Default.css');
$user = wp_get_current_user();
$config = [
'root' => esc_url_raw(rest_url()),
'nonce' => wp_create_nonce('wp_rest'),
'center' => [45.7461252, 4.8331952],
'zoom' => 13,
];
try {
$location_details = $user->castors_location_details;
if ($location_details) {
$location = json_decode(htmlspecialchars_decode($location_details));
$config['center'] = array_reverse($location->coordinates);
}
} catch(Exception $e) {}
wp_localize_script('castors-map', 'castorsMapApiSettings', $config);
}
}
public static function init() {
add_action('rest_api_init', [__CLASS__, 'add_api_routes']);
static::addShortcodes();
//static::activate(); // Uncomment this line if activate method has been modified, then recomment after init
}
public static function admin_init() {
}
public static function addShortcodes() {
add_shortcode('castors_map', [__CLASS__, 'shortcode_map']);
add_shortcode('castors_map_layers', [__CLASS__, 'shortcode_map_layers']);
}
public static function shortcode_map($args) {
return '<div id="castors-map"></div>';
}
public static function shortcode_map_layers($args) {
$layers = '<div id="castors-map-layers">';
if (current_user_can('castors_map_show_members')) {
$layers .= <<<EOF
<div class="castors-map-layer" data-icon="member" data-layer="adherents">
<div class="castors-map-layer-check">
<i class="fa-solid fa-square-check"></i>
<i class="fa-regular fa-square"></i>
</div>
<div class="castors-map-layer-icon"></div>
<div class="castors-map-layer-name"></div>
</div>
EOF;
}
if (current_user_can('castors_map_show_experts')) {
$layers .= <<<EOF
<div class="castors-map-layer" data-icon="expert" data-layer="pros">
<div class="castors-map-layer-check">
<i class="fa-solid fa-square-check"></i>
<i class="fa-regular fa-square"></i>
</div>
<div class="castors-map-layer-icon"></div>
<div class="castors-map-layer-name"></div>
</div>
EOF;
}
if (current_user_can('castors_map_show_worksites')) {
$layers .= <<<EOF
<div class="castors-map-layer" data-icon="worksite" data-layer="chantiers">
<div class="castors-map-layer-check">
<i class="fa-solid fa-square-check"></i>
<i class="fa-regular fa-square"></i>
</div>
<div class="castors-map-layer-icon"></div>
<p class="castors-map-layer-name"></p>
</div>
EOF;
}
if (current_user_can('castors_map_show_meetings')) {
$layers .= <<<EOF
<div class="castors-map-layer" data-icon="meeting" data-layer="rencontres">
<div class="castors-map-layer-check">
<i class="fa-solid fa-square-check"></i>
<i class="fa-regular fa-square"></i>
</div>
<div class="castors-map-layer-icon"></div>
<p class="castors-map-layer-name"></p>
</div>
EOF;
}
$layers .= '</div>';
return $layers;
}
public static function add_api_routes() {
register_rest_route(
'castors/v1',
'/map/layer/(?P<layer>.+)',
[
'methods' => WP_REST_Server::READABLE,
'callback' => [__CLASS__, 'map_layer'],
'permission_callback' => [__CLASS__, 'map_layer_check_permissions'],
]
);
}
public static function map_layer($request) {
switch ($request['layer']) {
case 'adherents':
$users = get_users(['meta_key' => 'castors_location_details']);
$features = array_filter(array_map([__CLASS__, 'map_layer_member_geojson'], $users));
return rest_ensure_response([
'type' => 'FeatureCollection',
'features' => array_values($features)
]);
case 'chantiers':
$worksites = get_posts(['post_type' => 'worksite', 'meta_key' => 'castors_location_details']);
$features = array_filter(array_map([__CLASS__, 'map_layer_worksite_geojson'], $worksites));
return rest_ensure_response([
'type' => 'FeatureCollection',
'features' => array_values($features)
]);
default:
return [];
}
}
public static function map_layer_member_geojson($user) {
try {
$location_details = $user->castors_location_details;
$location = json_decode(htmlspecialchars_decode($location_details));
if ($location) {
return [
'type' => 'Feature',
'properties' => [
'type' => 'member',
'id' => $user->ID,
'username' => $user->user_login,
'name' => $user->display_name,
'location' => $location->value,
],
'geometry' => [
'type' => 'Point',
'coordinates' => $location->coordinates,
]
];
}
} catch(Exception $e) {}
return null;
}
public static function map_layer_worksite_geojson($worksite) {
try {
$location_details = $worksite->castors_location_details;
$location = json_decode(htmlspecialchars_decode($location_details));
if ($location) {
return [
'type' => 'Feature',
'properties' => [
'type' => 'worksite',
'id' => $worksite->ID,
'name' => $worksite->post_title,
'author' => [
'id' => $worksite->post_author,
'username' => get_the_author_meta('login', $worksite->post_author),
'name' => get_the_author_meta('display_name', $worksite->post_author),
],
'location' => $location->value,
],
'geometry' => [
'type' => 'Point',
'coordinates' => $location->coordinates,
]
];
}
} catch(Exception $e) {}
return null;
}
public static function map_layer_check_permissions($request) {
switch ($request['layer']) {
case 'adherents':
if (current_user_can('castors_map_show_members')) {
return true;
}
break;
case 'pros':
if (current_user_can('castors_map_show_experts')) {
return true;
}
break;
case 'chantiers':
if (current_user_can('castors_map_show_worksites')) {
return true;
}
break;
case 'rencontres':
if (current_user_can('castors_map_show_meetings')) {
return true;
}
break;
default:
break;
}
return new WP_Error('rest_forbidden', esc_html__("Sorry, you cannot do that !", 'castors'), array('status' => 401));
}
public static function locationAutocompleteScript() {
wp_enqueue_script('castors-location', CASTORS_THEME_URI . 'js/location.min.js', ['jquery-ui-autocomplete'], false, ['strategy' =>'defer', 'in_footer' => true]);
}
}