/
plugin.php
146 lines (104 loc) · 3.39 KB
/
plugin.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
<?php
/*
Plugin Name: WP JSON v2 Custom Endpoints
Plugin URI:
Description: Shows an example of a plugin that creates a custom post type and custom endpoints for the v2 wp-json api
Version: 1.0
Author: Will Haley, Boddie Noell Enterprise
Author URI: https://surc.us
License: None
*/
class BNE_Registration {
function __construct() {
add_action( 'init', array( $this, 'init' ) );
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
}
function init() {
/**
* Registering a custom post type called Units. The "show_in_rest" option means that it will not be
* available in via the rest api...
*/
register_post_type( 'units',
array(
'labels' => array(
'name' => __( 'Units' ),
'singular_name' => __( 'Unit' )
),
'public' => true,
'has_archive' => false,
'show_in_rest' => false, //Excludes the custom post type to the rest api
)
);
}
function rest_api_init() {
register_rest_route(
'bne/v1',
'preregister/(?P<id>\d+)',
array(
'method' => WP_REST_Server::READABLE,
'callback' => array( $this, 'preregister' )
)
);
register_rest_route(
'bne/v1',
'register/(?P<id>\d+)/(?P<lat>\d+\.\d+)/(?P<long>[+-]\d+\.\d+)/(?P<nonce>\w+)',
array(
'method' => WP_REST_Server::READABLE,
'callback' => array( $this, 'register' )
)
);
register_rest_route(
'bne/v1',
'unit/(?P<post_id>\d+)/(?P<number>\d+)/(?P<nonce>\w+)',
array(
'method' => WP_REST_Server::READABLE,
'callback' => array( $this, 'unit_number' )
)
);
register_rest_route(
'bne/v1',
'push/(?P<post_id>\d+)/(?P<key>\d+)/(?P<nonce>\w+)',
array(
'method' => WP_REST_Server::READABLE,
'callback' => array( $this, 'push_key' )
)
);
}
function preregister( WP_REST_Request $request ) {
$id = $request->get_param( 'id' );
return wp_create_nonce( 'register_' . $id );
}
function register( WP_REST_Request $request ) {
$id = $request->get_param( 'id' );
$nonce = $request->get_param( 'nonce' );
$lat = $request->get_param( 'lat' );
$long = $request->get_param( 'long' );
if ( ! wp_verify_nonce( $nonce, 'register_' . $id ) ) {
return new WP_Error( 'registration', 'Validation Fail', array( 'status' => 404 ) );
}
$geo = array( 'address' => '', 'lat' => $lat, 'lng' => $long );
$post = get_page_by_title( $id, OBJECT, 'units' );
$post_id = ( $post ) ? $post->ID : wp_insert_post( array( 'post_title' => $id, 'post_type' => 'units' ) );
update_post_meta( $post_id, 'location', $geo );
return $post_id;
}
function unit_number( WP_REST_Request $request ) {
$post_id = $request->get_param( 'post_id' );
$nonce = $request->get_param( 'nonce' );
$number = $request->get_param( 'number' );
if ( ! wp_verify_nonce( $nonce, 'register_' . $number ) ) {
return new WP_Error( 'registration', 'Validation Fail', array( 'status' => 404 ) );
}
return ( update_post_meta( $post_id, 'unit_number', $number ) );
}
function push_key( WP_REST_Request $request ) {
$post_id = $request->get_param( 'post_id' );
$nonce = $request->get_param( 'nonce' );
$key = $request->get_param( 'key' );
if ( ! wp_verify_nonce( $nonce, 'register_' . $key ) ) {
return new WP_Error( 'registration', 'Validation Fail', array( 'status' => 404 ) );
}
return ( update_post_meta( $post_id, 'push_key', $key ) );
}
}
$bne_registration = new BNE_Registration();