-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravityforms.php
59 lines (52 loc) · 1.39 KB
/
gravityforms.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
<?php
class Headless_GravityForms
{
public $rest_base = 'gf/forms';
public function __construct($namespace)
{
/**
* @api {get} /glamrock/v1/gf/forms/1
* @apiName GetForm
* @apiGroup GravityForms
* @apiDescription Retreive a single form
* @apiParam {Number} form_id ID of the form
*
* @apiSuccess {Object[]} GF_Form Object (excluding notifications)
*/
register_rest_route($namespace, $this->rest_base . '/(?P<form_id>[\d]+)', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_form'],
'args' => [
'context' => [
'default' => 'view',
],
],
],
]);
}
/**
* Retreive a single form and all fields and options (exluding notifications)
* @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response
*/
public function get_form(WP_REST_Request $request)
{
$form_id = $request['form_id'];
$form = GFAPI::get_form($form_id);
if ($form) {
// Strip data we do not want to share
unset($form['notifications']);
return new WP_REST_Response($form, 200);
} else {
return new WP_Error('not_found', 'Form not found', ['status' => 404]);
}
}
}
/**
* Register custom API routes
*/
add_action('rest_api_init', function () {
$api_namespace = 'glamrock/v1';
new Headless_GravityForms($api_namespace);
});