Newer
Older
<?php
/**
* Widget controller class.
*
* Widget endpoint, replacement for CiviCRM's 'extern/widget.php'
*
* @since 0.1
*/
namespace CiviCRM_WP_REST\Controller;
class Widget extends Base {
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
/**
* @var string
* The base route.
* @since 0.1
*/
protected $rest_base = 'widget';
/**
* Registers routes.
*
* @since 0.1
*/
public function register_routes() {
register_rest_route($this->get_namespace(), $this->get_rest_base(), [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'get_item'],
'permission_callback' => '__return_true',
'args' => $this->get_item_args(),
],
'schema' => [$this, 'get_item_schema'],
]);
}
/**
* Get item.
*
* @since 0.1
* @param WP_REST_Request $request
*/
public function get_item($request) {
/**
* Filter mandatory params.
*
* @since 0.1
* @param array $params
* @param WP_REST_Request $request
*/
$params = apply_filters(
'civi_wp_rest/controller/widget/params',
$this->get_mandatory_params($request),
$request
);
$jsonvar = 'jsondata';
if (!empty($request->get_param('format'))) {
$jsonvar .= $request->get_param('cpageId');
}
$data = \CRM_Contribute_BAO_Widget::getContributionPageData(...$params);
$response = 'var ' . $jsonvar . ' = ' . json_encode($data) . ';';
/**
* Adds our response data before dispatching.
*
* @since 0.1
* @param WP_HTTP_Response $result Result to send to client
* @param WP_REST_Server $server The REST server
* @param WP_REST_Request $request The request
* @return WP_HTTP_Response $result Result to send to client
*/
add_filter('rest_post_dispatch', function($result, $server, $request) use ($response) {
return rest_ensure_response($response);
}, 10, 3);
// serve javascript
add_filter('rest_pre_serve_request', [$this, 'serve_javascript'], 10, 4);
}
/**
* Get mandatory params from request.
*
* @since 0.1
* @param WP_REST_Resquest $request
* @return array $params The widget params
*/
protected function get_mandatory_params($request) {
$args = $request->get_params();
return [
$args['cpageId'],
$args['widgetId'],
$args['includePending'] ?? FALSE,
];
}
/**
* Serve jsondata response.
*
* @since 0.1
* @param bool $served Whether the request has already been served
* @param WP_REST_Response $result
* @param WP_REST_Request $request
* @param WP_REST_Server $server
* @return bool $served
*/
public function serve_javascript($served, $result, $request, $server) {
// set content type header
$server->send_header('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + 60));
$server->send_header('Content-Type', 'application/javascript');
$server->send_header('Cache-Control', 'max-age=60, public');
echo $result->get_data();
return TRUE;
}
/**
* Item schema.
*
* @since 0.1
* @return array $schema
*/
public function get_item_schema() {
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'civicrm_api3/v3/widget',
'description' => __('CiviCRM API3 wrapper', 'civicrm'),
'type' => 'object',
'required' => ['cpageId', 'widgetId'],
'properties' => [
'cpageId' => [
'type' => 'integer',
'minimum' => 1,
],
'widgetId' => [
'type' => 'integer',
'minimum' => 1,
],
'format' => [
'type' => 'integer',
],
'includePending' => [
'type' => 'boolean',
],
],
];
}
/**
* Item arguments.
*
* @since 0.1
* @return array $arguments
*/
public function get_item_args() {
return [
'cpageId' => [
'type' => 'integer',
'required' => TRUE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value);
},
],
'widgetId' => [
'type' => 'integer',
'required' => TRUE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value);
},
],
'format' => [
'type' => 'integer',
'required' => FALSE,
'validate_callback' => function($value, $request, $key) {
return is_numeric($value);
},
],
'includePending' => [
'type' => 'boolean',
'required' => FALSE,
'validate_callback' => function($value, $request, $key) {
return is_string($value);
},
],
];
}