-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathherp-api.php
More file actions
54 lines (49 loc) · 1.84 KB
/
herp-api.php
File metadata and controls
54 lines (49 loc) · 1.84 KB
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
<?php
/**
* Plugin Name: HERP API
* Description: Custom REST API endpoints for your app.
*/
if (!defined('ABSPATH')) {
exit;
}
require_once plugin_dir_path(__FILE__) . 'includes/get-user-records.php';
require_once plugin_dir_path(__FILE__) . 'includes/create-record.php';
require_once plugin_dir_path(__FILE__) . 'includes/get-current-user.php';
add_action('rest_api_init', function () {
register_rest_route('herp/v1', '/records', [
[
// GET /wp-json/herp/v1/records?user_id=123
'methods' => 'GET',
'callback' => 'herp_get_user_records',
'permission_callback' => '__return_true', // for now, open — secure later!
'args' => [
'user_id' => [
'required' => true,
'validate_callback' => static function ($param) {
return is_numeric($param) && intval($param) > 0;
},
],
],
],
[
// POST /wp-json/herp/v1/records
'methods' => 'POST',
'callback' => 'herp_create_record',
'permission_callback' => 'herp_can_create_record',
],
]);
// Back-compat: GET /wp-json/herp/v1/records/123
register_rest_route('herp/v1', '/records/(?P<user_id>\d+)', [
'methods' => 'GET',
'callback' => 'herp_get_user_records',
'permission_callback' => '__return_true', // for now, open — secure later!
]);
// GET /wp-json/herp/v1/me
register_rest_route('herp/v1', '/me', [
'methods' => 'GET',
'callback' => 'herp_get_current_user',
'permission_callback' => static function () {
return is_user_logged_in();
},
]);
});