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 |