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 3.9 3.9-beta 3.9.1 3.9.2 3.9.3 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1-beta 4.1.1 4.1.2 4.2 4.2-beta 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6 4.6.1 4.7 4.8 4.8.1 4.9 4.9.1 4.9.2 5.0 5.0.1
contact-form-7 / includes / rest-api.php
contact-form-7 / includes Last commit date
css 9 years ago js 8 years ago capabilities.php 8 years ago config-validator.php 8 years ago contact-form-functions.php 9 years ago contact-form-template.php 8 years ago contact-form.php 8 years ago controller.php 8 years ago form-tag.php 9 years ago form-tags-manager.php 8 years ago formatting.php 8 years ago functions.php 8 years ago integration.php 9 years ago l10n.php 9 years ago mail.php 8 years ago pipe.php 9 years ago rest-api.php 8 years ago shortcodes.php 9 years ago submission.php 8 years ago upgrade.php 9 years ago validation.php 9 years ago
rest-api.php
332 lines
1 <?php
2
3 add_action( 'rest_api_init', 'wpcf7_rest_api_init' );
4
5 function wpcf7_rest_api_init() {
6 $namespace = 'contact-form-7/v1';
7
8 register_rest_route( $namespace,
9 '/contact-forms',
10 array(
11 array(
12 'methods' => WP_REST_Server::READABLE,
13 'callback' => 'wpcf7_rest_get_contact_forms',
14 ),
15 array(
16 'methods' => WP_REST_Server::CREATABLE,
17 'callback' => 'wpcf7_rest_create_contact_form',
18 ),
19 )
20 );
21
22 register_rest_route( $namespace,
23 '/contact-forms/(?P<id>\d+)',
24 array(
25 array(
26 'methods' => WP_REST_Server::READABLE,
27 'callback' => 'wpcf7_rest_get_contact_form',
28 ),
29 array(
30 'methods' => WP_REST_Server::EDITABLE,
31 'callback' => 'wpcf7_rest_update_contact_form',
32 ),
33 array(
34 'methods' => WP_REST_Server::DELETABLE,
35 'callback' => 'wpcf7_rest_delete_contact_form',
36 ),
37 )
38 );
39
40 register_rest_route( $namespace,
41 '/contact-forms/(?P<id>\d+)/feedback',
42 array(
43 array(
44 'methods' => WP_REST_Server::CREATABLE,
45 'callback' => 'wpcf7_rest_create_feedback',
46 ),
47 )
48 );
49
50 register_rest_route( $namespace,
51 '/contact-forms/(?P<id>\d+)/refill',
52 array(
53 array(
54 'methods' => WP_REST_Server::READABLE,
55 'callback' => 'wpcf7_rest_get_refill',
56 ),
57 )
58 );
59 }
60
61 function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) {
62 if ( ! current_user_can( 'wpcf7_read_contact_forms' ) ) {
63 return new WP_Error( 'wpcf7_forbidden',
64 __( "You are not allowed to access contact forms.", 'contact-form-7' ),
65 array( 'status' => 403 ) );
66 }
67
68 $args = array();
69
70 $per_page = $request->get_param( 'per_page' );
71
72 if ( null !== $per_page ) {
73 $args['posts_per_page'] = (int) $per_page;
74 }
75
76 $offset = $request->get_param( 'offset' );
77
78 if ( null !== $offset ) {
79 $args['offset'] = (int) $offset;
80 }
81
82 $order = $request->get_param( 'order' );
83
84 if ( null !== $order ) {
85 $args['order'] = (string) $order;
86 }
87
88 $orderby = $request->get_param( 'orderby' );
89
90 if ( null !== $orderby ) {
91 $args['orderby'] = (string) $orderby;
92 }
93
94 $search = $request->get_param( 'search' );
95
96 if ( null !== $search ) {
97 $args['s'] = (string) $search;
98 }
99
100 $items = WPCF7_ContactForm::find( $args );
101
102 $response = array();
103
104 foreach ( $items as $item ) {
105 $response[] = array(
106 'id' => $item->id(),
107 'slug' => $item->name(),
108 'title' => $item->title(),
109 'locale' => $item->locale(),
110 );
111 }
112
113 return rest_ensure_response( $response );
114 }
115
116 function wpcf7_rest_create_contact_form( WP_REST_Request $request ) {
117 $id = (int) $request->get_param( 'id' );
118
119 if ( $id ) {
120 return new WP_Error( 'wpcf7_post_exists',
121 __( "Cannot create existing contact form.", 'contact-form-7' ),
122 array( 'status' => 409 ) );
123 }
124
125 if ( ! current_user_can( 'wpcf7_edit_contact_forms' ) ) {
126 return new WP_Error( 'wpcf7_forbidden',
127 __( "You are not allowed to create a contact form.", 'contact-form-7' ),
128 array( 'status' => 403 ) );
129 }
130
131 $args = $request->get_params();
132 $args['id'] = -1; // Create
133 $context = $request->get_param( 'context' );
134 $item = wpcf7_save_contact_form( $args, $context );
135
136 if ( ! $item ) {
137 return new WP_Error( 'wpcf7_cannot_save',
138 __( "There was an error saving the contact form.", 'contact-form-7' ),
139 array( 'status' => 500 ) );
140 }
141
142 $response = array(
143 'id' => $item->id(),
144 'slug' => $item->name(),
145 'title' => $item->title(),
146 'locale' => $item->locale(),
147 'properties' => $item->get_properties(),
148 'config_errors' => array(),
149 );
150
151 if ( wpcf7_validate_configuration() ) {
152 $config_validator = new WPCF7_ConfigValidator( $item );
153 $config_validator->validate();
154
155 $response['config_errors'] = $config_validator->collect_error_messages();
156
157 if ( 'save' == $context ) {
158 $config_validator->save();
159 }
160 }
161
162 return rest_ensure_response( $response );
163 }
164
165 function wpcf7_rest_get_contact_form( WP_REST_Request $request ) {
166 $id = (int) $request->get_param( 'id' );
167 $item = wpcf7_contact_form( $id );
168
169 if ( ! $item ) {
170 return new WP_Error( 'wpcf7_not_found',
171 __( "The requested contact form was not found.", 'contact-form-7' ),
172 array( 'status' => 404 ) );
173 }
174
175 if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
176 return new WP_Error( 'wpcf7_forbidden',
177 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
178 array( 'status' => 403 ) );
179 }
180
181 $response = array(
182 'id' => $item->id(),
183 'slug' => $item->name(),
184 'title' => $item->title(),
185 'locale' => $item->locale(),
186 'properties' => $item->get_properties(),
187 );
188
189 return rest_ensure_response( $response );
190 }
191
192 function wpcf7_rest_update_contact_form( WP_REST_Request $request ) {
193 $id = (int) $request->get_param( 'id' );
194 $item = wpcf7_contact_form( $id );
195
196 if ( ! $item ) {
197 return new WP_Error( 'wpcf7_not_found',
198 __( "The requested contact form was not found.", 'contact-form-7' ),
199 array( 'status' => 404 ) );
200 }
201
202 if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
203 return new WP_Error( 'wpcf7_forbidden',
204 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
205 array( 'status' => 403 ) );
206 }
207
208 $args = $request->get_params();
209 $context = $request->get_param( 'context' );
210 $item = wpcf7_save_contact_form( $args, $context );
211
212 if ( ! $item ) {
213 return new WP_Error( 'wpcf7_cannot_save',
214 __( "There was an error saving the contact form.", 'contact-form-7' ),
215 array( 'status' => 500 ) );
216 }
217
218 $response = array(
219 'id' => $item->id(),
220 'slug' => $item->name(),
221 'title' => $item->title(),
222 'locale' => $item->locale(),
223 'properties' => $item->get_properties(),
224 'config_errors' => array(),
225 );
226
227 if ( wpcf7_validate_configuration() ) {
228 $config_validator = new WPCF7_ConfigValidator( $item );
229 $config_validator->validate();
230
231 $response['config_errors'] = $config_validator->collect_error_messages();
232
233 if ( 'save' == $context ) {
234 $config_validator->save();
235 }
236 }
237
238 return rest_ensure_response( $response );
239 }
240
241 function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) {
242 $id = (int) $request->get_param( 'id' );
243 $item = wpcf7_contact_form( $id );
244
245 if ( ! $item ) {
246 return new WP_Error( 'wpcf7_not_found',
247 __( "The requested contact form was not found.", 'contact-form-7' ),
248 array( 'status' => 404 ) );
249 }
250
251 if ( ! current_user_can( 'wpcf7_delete_contact_form', $id ) ) {
252 return new WP_Error( 'wpcf7_forbidden',
253 __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
254 array( 'status' => 403 ) );
255 }
256
257 $result = $item->delete();
258
259 if ( ! $result ) {
260 return new WP_Error( 'wpcf7_cannot_delete',
261 __( "There was an error deleting the contact form.", 'contact-form-7' ),
262 array( 'status' => 500 ) );
263 }
264
265 $response = array( 'deleted' => true );
266
267 return rest_ensure_response( $response );
268 }
269
270 function wpcf7_rest_create_feedback( WP_REST_Request $request ) {
271 $id = (int) $request->get_param( 'id' );
272 $item = wpcf7_contact_form( $id );
273
274 if ( ! $item ) {
275 return new WP_Error( 'wpcf7_not_found',
276 __( "The requested contact form was not found.", 'contact-form-7' ),
277 array( 'status' => 404 ) );
278 }
279
280 $result = $item->submit();
281
282 $unit_tag = $request->get_param( '_wpcf7_unit_tag' );
283
284 $response = array(
285 'into' => '#' . wpcf7_sanitize_unit_tag( $unit_tag ),
286 'status' => $result['status'],
287 'message' => $result['message'],
288 );
289
290 if ( 'validation_failed' == $result['status'] ) {
291 $invalid_fields = array();
292
293 foreach ( (array) $result['invalid_fields'] as $name => $field ) {
294 $invalid_fields[] = array(
295 'into' => 'span.wpcf7-form-control-wrap.'
296 . sanitize_html_class( $name ),
297 'message' => $field['reason'],
298 'idref' => $field['idref'],
299 );
300 }
301
302 $response['invalidFields'] = $invalid_fields;
303 }
304
305 if ( ! empty( $result['scripts_on_sent_ok'] ) ) {
306 $response['onSentOk'] = $result['scripts_on_sent_ok'];
307 }
308
309 if ( ! empty( $result['scripts_on_submit'] ) ) {
310 $response['onSubmit'] = $result['scripts_on_submit'];
311 }
312
313 $response = apply_filters( 'wpcf7_ajax_json_echo', $response, $result );
314
315 return rest_ensure_response( $response );
316 }
317
318 function wpcf7_rest_get_refill( WP_REST_Request $request ) {
319 $id = (int) $request->get_param( 'id' );
320 $item = wpcf7_contact_form( $id );
321
322 if ( ! $item ) {
323 return new WP_Error( 'wpcf7_not_found',
324 __( "The requested contact form was not found.", 'contact-form-7' ),
325 array( 'status' => 404 ) );
326 }
327
328 $response = apply_filters( 'wpcf7_ajax_onload', array() );
329
330 return rest_ensure_response( $response );
331 }
332