/includes/rest-api.php

  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. register_rest_route( $namespace,  
  22. '/contact-forms/(?P<id>\d+)',  
  23. array( 
  24. array( 
  25. 'methods' => WP_REST_Server::READABLE,  
  26. 'callback' => 'wpcf7_rest_get_contact_form',  
  27. ),  
  28. array( 
  29. 'methods' => WP_REST_Server::EDITABLE,  
  30. 'callback' => 'wpcf7_rest_update_contact_form',  
  31. ),  
  32. array( 
  33. 'methods' => WP_REST_Server::DELETABLE,  
  34. 'callback' => 'wpcf7_rest_delete_contact_form',  
  35. ),  
  36. ); 
  37.  
  38. function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) { 
  39. if ( ! current_user_can( 'wpcf7_read_contact_forms' ) ) { 
  40. return new WP_Error( 'wpcf7_forbidden',  
  41. __( "You are not allowed to access contact forms.", 'contact-form-7' ),  
  42. array( 'status' => 403 ) ); 
  43.  
  44. $args = array(); 
  45.  
  46. $per_page = $request->get_param( 'per_page' ); 
  47.  
  48. if ( null !== $per_page ) { 
  49. $args['posts_per_page'] = (int) $per_page; 
  50.  
  51. $offset = $request->get_param( 'offset' ); 
  52.  
  53. if ( null !== $offset ) { 
  54. $args['offset'] = (int) $offset; 
  55.  
  56. $order = $request->get_param( 'order' ); 
  57.  
  58. if ( null !== $order ) { 
  59. $args['order'] = (string) $order; 
  60.  
  61. $orderby = $request->get_param( 'orderby' ); 
  62.  
  63. if ( null !== $orderby ) { 
  64. $args['orderby'] = (string) $orderby; 
  65.  
  66. $search = $request->get_param( 'search' ); 
  67.  
  68. if ( null !== $search ) { 
  69. $args['s'] = (string) $search; 
  70.  
  71. $items = WPCF7_ContactForm::find( $args ); 
  72.  
  73. $response = array(); 
  74.  
  75. foreach ( $items as $item ) { 
  76. $response[] = array( 
  77. 'id' => $item->id(),  
  78. 'slug' => $item->name(),  
  79. 'title' => $item->title(),  
  80. 'locale' => $item->locale(),  
  81. ); 
  82.  
  83. return rest_ensure_response( $response ); 
  84.  
  85. function wpcf7_rest_create_contact_form( WP_REST_Request $request ) { 
  86. $id = (int) $request->get_param( 'id' ); 
  87.  
  88. if ( $id ) { 
  89. return new WP_Error( 'wpcf7_post_exists',  
  90. __( "Cannot create existing contact form.", 'contact-form-7' ),  
  91. array( 'status' => 400 ) ); 
  92.  
  93. if ( ! current_user_can( 'wpcf7_edit_contact_forms' ) ) { 
  94. return new WP_Error( 'wpcf7_forbidden',  
  95. __( "You are not allowed to create a contact form.", 'contact-form-7' ),  
  96. array( 'status' => 403 ) ); 
  97.  
  98. $args = $request->get_params(); 
  99. $args['id'] = -1; // Create 
  100. $context = $request->get_param( 'context' ); 
  101. $item = wpcf7_save_contact_form( $args, $context ); 
  102.  
  103. if ( ! $item ) { 
  104. return new WP_Error( 'wpcf7_cannot_save',  
  105. __( "There was an error saving the contact form.", 'contact-form-7' ),  
  106. array( 'status' => 500 ) ); 
  107.  
  108. $response = array( 
  109. 'id' => $item->id(),  
  110. 'slug' => $item->name(),  
  111. 'title' => $item->title(),  
  112. 'locale' => $item->locale(),  
  113. 'properties' => $item->get_properties(),  
  114. 'config_errors' => array(),  
  115. ); 
  116.  
  117. if ( wpcf7_validate_configuration() ) { 
  118. $config_validator = new WPCF7_ConfigValidator( $item ); 
  119. $config_validator->validate(); 
  120.  
  121. $response['config_errors'] = $config_validator->collect_error_messages(); 
  122.  
  123. if ( 'save' == $context ) { 
  124. $config_validator->save(); 
  125.  
  126. return rest_ensure_response( $response ); 
  127.  
  128. function wpcf7_rest_get_contact_form( WP_REST_Request $request ) { 
  129. $id = (int) $request->get_param( 'id' ); 
  130. $item = wpcf7_contact_form( $id ); 
  131.  
  132. if ( ! $item ) { 
  133. return new WP_Error( 'wpcf7_not_found',  
  134. __( "The requested contact form was not found.", 'contact-form-7' ),  
  135. array( 'status' => 404 ) ); 
  136.  
  137. if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) { 
  138. return new WP_Error( 'wpcf7_forbidden',  
  139. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),  
  140. array( 'status' => 403 ) ); 
  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. ); 
  149.  
  150. return rest_ensure_response( $response ); 
  151.  
  152. function wpcf7_rest_update_contact_form( WP_REST_Request $request ) { 
  153. $id = (int) $request->get_param( 'id' ); 
  154. $item = wpcf7_contact_form( $id ); 
  155.  
  156. if ( ! $item ) { 
  157. return new WP_Error( 'wpcf7_not_found',  
  158. __( "The requested contact form was not found.", 'contact-form-7' ),  
  159. array( 'status' => 404 ) ); 
  160.  
  161. if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) { 
  162. return new WP_Error( 'wpcf7_forbidden',  
  163. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),  
  164. array( 'status' => 403 ) ); 
  165.  
  166. $args = $request->get_params(); 
  167. $context = $request->get_param( 'context' ); 
  168. $item = wpcf7_save_contact_form( $args, $context ); 
  169.  
  170. if ( ! $item ) { 
  171. return new WP_Error( 'wpcf7_cannot_save',  
  172. __( "There was an error saving the contact form.", 'contact-form-7' ),  
  173. array( 'status' => 500 ) ); 
  174.  
  175. $response = array( 
  176. 'id' => $item->id(),  
  177. 'slug' => $item->name(),  
  178. 'title' => $item->title(),  
  179. 'locale' => $item->locale(),  
  180. 'properties' => $item->get_properties(),  
  181. 'config_errors' => array(),  
  182. ); 
  183.  
  184. if ( wpcf7_validate_configuration() ) { 
  185. $config_validator = new WPCF7_ConfigValidator( $item ); 
  186. $config_validator->validate(); 
  187.  
  188. $response['config_errors'] = $config_validator->collect_error_messages(); 
  189.  
  190. if ( 'save' == $context ) { 
  191. $config_validator->save(); 
  192.  
  193. return rest_ensure_response( $response ); 
  194.  
  195. function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) { 
  196. $id = (int) $request->get_param( 'id' ); 
  197. $item = wpcf7_contact_form( $id ); 
  198.  
  199. if ( ! $item ) { 
  200. return new WP_Error( 'wpcf7_not_found',  
  201. __( "The requested contact form was not found.", 'contact-form-7' ),  
  202. array( 'status' => 404 ) ); 
  203.  
  204. if ( ! current_user_can( 'wpcf7_delete_contact_form', $id ) ) { 
  205. return new WP_Error( 'wpcf7_forbidden',  
  206. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),  
  207. array( 'status' => 403 ) ); 
  208.  
  209. $result = $item->delete(); 
  210.  
  211. if ( ! $result ) { 
  212. return new WP_Error( 'wpcf7_cannot_delete',  
  213. __( "There was an error deleting the contact form.", 'contact-form-7' ),  
  214. array( 'status' => 500 ) ); 
  215.  
  216. $response = array( 'deleted' => true ); 
  217.  
  218. return rest_ensure_response( $response ); 
.