rest-api.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. add_action( 'rest_api_init', 'wpcf7_rest_api_init', 10, 0 );
  3. function wpcf7_rest_api_init() {
  4. $namespace = 'contact-form-7/v1';
  5. register_rest_route( $namespace,
  6. '/contact-forms',
  7. array(
  8. array(
  9. 'methods' => WP_REST_Server::READABLE,
  10. 'callback' => 'wpcf7_rest_get_contact_forms',
  11. ),
  12. array(
  13. 'methods' => WP_REST_Server::CREATABLE,
  14. 'callback' => 'wpcf7_rest_create_contact_form',
  15. ),
  16. )
  17. );
  18. register_rest_route( $namespace,
  19. '/contact-forms/(?P<id>\d+)',
  20. array(
  21. array(
  22. 'methods' => WP_REST_Server::READABLE,
  23. 'callback' => 'wpcf7_rest_get_contact_form',
  24. ),
  25. array(
  26. 'methods' => WP_REST_Server::EDITABLE,
  27. 'callback' => 'wpcf7_rest_update_contact_form',
  28. ),
  29. array(
  30. 'methods' => WP_REST_Server::DELETABLE,
  31. 'callback' => 'wpcf7_rest_delete_contact_form',
  32. ),
  33. )
  34. );
  35. register_rest_route( $namespace,
  36. '/contact-forms/(?P<id>\d+)/feedback',
  37. array(
  38. array(
  39. 'methods' => WP_REST_Server::CREATABLE,
  40. 'callback' => 'wpcf7_rest_create_feedback',
  41. ),
  42. )
  43. );
  44. register_rest_route( $namespace,
  45. '/contact-forms/(?P<id>\d+)/refill',
  46. array(
  47. array(
  48. 'methods' => WP_REST_Server::READABLE,
  49. 'callback' => 'wpcf7_rest_get_refill',
  50. ),
  51. )
  52. );
  53. }
  54. function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) {
  55. if ( ! current_user_can( 'wpcf7_read_contact_forms' ) ) {
  56. return new WP_Error( 'wpcf7_forbidden',
  57. __( "You are not allowed to access contact forms.", 'contact-form-7' ),
  58. array( 'status' => 403 ) );
  59. }
  60. $args = array();
  61. $per_page = $request->get_param( 'per_page' );
  62. if ( null !== $per_page ) {
  63. $args['posts_per_page'] = (int) $per_page;
  64. }
  65. $offset = $request->get_param( 'offset' );
  66. if ( null !== $offset ) {
  67. $args['offset'] = (int) $offset;
  68. }
  69. $order = $request->get_param( 'order' );
  70. if ( null !== $order ) {
  71. $args['order'] = (string) $order;
  72. }
  73. $orderby = $request->get_param( 'orderby' );
  74. if ( null !== $orderby ) {
  75. $args['orderby'] = (string) $orderby;
  76. }
  77. $search = $request->get_param( 'search' );
  78. if ( null !== $search ) {
  79. $args['s'] = (string) $search;
  80. }
  81. $items = WPCF7_ContactForm::find( $args );
  82. $response = array();
  83. foreach ( $items as $item ) {
  84. $response[] = array(
  85. 'id' => $item->id(),
  86. 'slug' => $item->name(),
  87. 'title' => $item->title(),
  88. 'locale' => $item->locale(),
  89. );
  90. }
  91. return rest_ensure_response( $response );
  92. }
  93. function wpcf7_rest_create_contact_form( WP_REST_Request $request ) {
  94. $id = (int) $request->get_param( 'id' );
  95. if ( $id ) {
  96. return new WP_Error( 'wpcf7_post_exists',
  97. __( "Cannot create existing contact form.", 'contact-form-7' ),
  98. array( 'status' => 400 ) );
  99. }
  100. if ( ! current_user_can( 'wpcf7_edit_contact_forms' ) ) {
  101. return new WP_Error( 'wpcf7_forbidden',
  102. __( "You are not allowed to create a contact form.", 'contact-form-7' ),
  103. array( 'status' => 403 ) );
  104. }
  105. $args = $request->get_params();
  106. $args['id'] = -1; // Create
  107. $context = $request->get_param( 'context' );
  108. $item = wpcf7_save_contact_form( $args, $context );
  109. if ( ! $item ) {
  110. return new WP_Error( 'wpcf7_cannot_save',
  111. __( "There was an error saving the contact form.", 'contact-form-7' ),
  112. array( 'status' => 500 ) );
  113. }
  114. $response = array(
  115. 'id' => $item->id(),
  116. 'slug' => $item->name(),
  117. 'title' => $item->title(),
  118. 'locale' => $item->locale(),
  119. 'properties' => $item->get_properties(),
  120. 'config_errors' => array(),
  121. );
  122. if ( wpcf7_validate_configuration() ) {
  123. $config_validator = new WPCF7_ConfigValidator( $item );
  124. $config_validator->validate();
  125. $response['config_errors'] = $config_validator->collect_error_messages();
  126. if ( 'save' == $context ) {
  127. $config_validator->save();
  128. }
  129. }
  130. return rest_ensure_response( $response );
  131. }
  132. function wpcf7_rest_get_contact_form( WP_REST_Request $request ) {
  133. $id = (int) $request->get_param( 'id' );
  134. $item = wpcf7_contact_form( $id );
  135. if ( ! $item ) {
  136. return new WP_Error( 'wpcf7_not_found',
  137. __( "The requested contact form was not found.", 'contact-form-7' ),
  138. array( 'status' => 404 ) );
  139. }
  140. if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
  141. return new WP_Error( 'wpcf7_forbidden',
  142. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
  143. array( 'status' => 403 ) );
  144. }
  145. $response = array(
  146. 'id' => $item->id(),
  147. 'slug' => $item->name(),
  148. 'title' => $item->title(),
  149. 'locale' => $item->locale(),
  150. 'properties' => $item->get_properties(),
  151. );
  152. return rest_ensure_response( $response );
  153. }
  154. function wpcf7_rest_update_contact_form( WP_REST_Request $request ) {
  155. $id = (int) $request->get_param( 'id' );
  156. $item = wpcf7_contact_form( $id );
  157. if ( ! $item ) {
  158. return new WP_Error( 'wpcf7_not_found',
  159. __( "The requested contact form was not found.", 'contact-form-7' ),
  160. array( 'status' => 404 ) );
  161. }
  162. if ( ! current_user_can( 'wpcf7_edit_contact_form', $id ) ) {
  163. return new WP_Error( 'wpcf7_forbidden',
  164. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
  165. array( 'status' => 403 ) );
  166. }
  167. $args = $request->get_params();
  168. $context = $request->get_param( 'context' );
  169. $item = wpcf7_save_contact_form( $args, $context );
  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. if ( wpcf7_validate_configuration() ) {
  184. $config_validator = new WPCF7_ConfigValidator( $item );
  185. $config_validator->validate();
  186. $response['config_errors'] = $config_validator->collect_error_messages();
  187. if ( 'save' == $context ) {
  188. $config_validator->save();
  189. }
  190. }
  191. return rest_ensure_response( $response );
  192. }
  193. function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) {
  194. $id = (int) $request->get_param( 'id' );
  195. $item = wpcf7_contact_form( $id );
  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. if ( ! current_user_can( 'wpcf7_delete_contact_form', $id ) ) {
  202. return new WP_Error( 'wpcf7_forbidden',
  203. __( "You are not allowed to access the requested contact form.", 'contact-form-7' ),
  204. array( 'status' => 403 ) );
  205. }
  206. $result = $item->delete();
  207. if ( ! $result ) {
  208. return new WP_Error( 'wpcf7_cannot_delete',
  209. __( "There was an error deleting the contact form.", 'contact-form-7' ),
  210. array( 'status' => 500 ) );
  211. }
  212. $response = array( 'deleted' => true );
  213. return rest_ensure_response( $response );
  214. }
  215. function wpcf7_rest_create_feedback( WP_REST_Request $request ) {
  216. $url_params = $request->get_url_params();
  217. $item = null;
  218. if ( ! empty( $url_params['id'] ) ) {
  219. $item = wpcf7_contact_form( $url_params['id'] );
  220. }
  221. if ( ! $item ) {
  222. return new WP_Error( 'wpcf7_not_found',
  223. __( "The requested contact form was not found.", 'contact-form-7' ),
  224. array( 'status' => 404 ) );
  225. }
  226. $result = $item->submit();
  227. $unit_tag = $request->get_param( '_wpcf7_unit_tag' );
  228. $response = array(
  229. 'into' => '#' . wpcf7_sanitize_unit_tag( $unit_tag ),
  230. 'status' => $result['status'],
  231. 'message' => $result['message'],
  232. );
  233. if ( 'validation_failed' == $result['status'] ) {
  234. $invalid_fields = array();
  235. foreach ( (array) $result['invalid_fields'] as $name => $field ) {
  236. $invalid_fields[] = array(
  237. 'into' => 'span.wpcf7-form-control-wrap.'
  238. . sanitize_html_class( $name ),
  239. 'message' => $field['reason'],
  240. 'idref' => $field['idref'],
  241. );
  242. }
  243. $response['invalidFields'] = $invalid_fields;
  244. }
  245. $response = apply_filters( 'wpcf7_ajax_json_echo', $response, $result );
  246. return rest_ensure_response( $response );
  247. }
  248. function wpcf7_rest_get_refill( WP_REST_Request $request ) {
  249. $id = (int) $request->get_param( 'id' );
  250. $item = wpcf7_contact_form( $id );
  251. if ( ! $item ) {
  252. return new WP_Error( 'wpcf7_not_found',
  253. __( "The requested contact form was not found.", 'contact-form-7' ),
  254. array( 'status' => 404 ) );
  255. }
  256. $response = apply_filters( 'wpcf7_ajax_onload', array() );
  257. return rest_ensure_response( $response );
  258. }