Caldera_Forms_API_Forms

CRUD via REST API for forms.

Defined (1)

The class is defined in the following location(s).

/classes/api/forms.php  
  1. class Caldera_Forms_API_Forms extends Caldera_Forms_API_CRUD { 
  2.  
  3.  
  4. /** 
  5. * Get a form via REST API 
  6. * /cf-api/v2/forms/<form_id> 
  7. * @since 1.5.0 
  8. * @param WP_REST_Request $request 
  9. * @return Caldera_Forms_API_Error|Caldera_Forms_API_Response 
  10. */ 
  11. public function get_item(WP_REST_Request $request) { 
  12. try{ 
  13. $this->form_object_factory( $request[ 'form_id' ], $request ); 
  14. }catch ( Exception $e ) { 
  15. return Caldera_Forms_API_Response_Factory::error_form_not_found(); 
  16.  
  17. $response_form = $this->prepare_form_for_response( $this->form, $request ); 
  18. return new Caldera_Forms_API_Response( $response_form, 200, array( ) ); 
  19.  
  20.  
  21. /** 
  22. * Get all form via REST API 
  23. * /cf-api/v2/forms/ 
  24. * @since 1.5.0 
  25. * @param WP_REST_Request $request 
  26. * @return Caldera_Forms_API_Error|Caldera_Forms_API_Response 
  27. */ 
  28. public function get_items(WP_REST_Request $request) { 
  29.  
  30. $forms = Caldera_Forms_Forms::get_forms( $request[ 'details' ] ); 
  31. $prepared = array(); 
  32. if( ! empty( $forms ) && $request[ 'full' ] ) { 
  33. foreach( $forms as $id => $form ) { 
  34. try{ 
  35. $form = $this->form_object_factory( $id, $request ); 
  36. }catch ( Exception $e ) { 
  37. continue; 
  38. $prepared[ $id ] = $this->prepare_form_for_response( $form, $request ); 
  39.  
  40.  
  41. $response = new Caldera_Forms_API_Response( $prepared, 200, array( ) ); 
  42. $response->set_total_header( count( $forms ) ); 
  43. return $response; 
  44.  
  45. /** 
  46. * @since 1.5.0 
  47. * @inheritdoc 
  48. */ 
  49. public function route_base() { 
  50. return 'forms'; 
  51.  
  52. /** 
  53. * @since 1.5.0 
  54. * @inheritdoc 
  55. */ 
  56. public function request_args() { 
  57. return array( 
  58. 'details' => array( 
  59. 'required' => false,  
  60. 'default' => true,  
  61. ),  
  62. 'full' => array( 
  63. 'required' => false,  
  64. 'default' => false,  
  65. ); 
  66.  
  67.  
  68. /** 
  69. * Permissions for form read 
  70. * @since 1.5.0 
  71. * @param WP_REST_Request $request 
  72. * @return bool 
  73. */ 
  74. public function get_items_permissions_check( WP_REST_Request $request ) { 
  75. $allowed = current_user_can( Caldera_Forms::get_manage_cap( 'entry-view' ), $request[ 'form_id' ] ); 
  76. if( ! $allowed ) { 
  77. $allowed = Caldera_Forms_API_Util::check_api_token( $request ); 
  78. /** 
  79. * Filter permissions for viewing form config via Caldera Forms REST API 
  80. * @since 1.5.0 
  81. * @param bool $allowed Is request authorized? 
  82. * @param string $form_id The form ID 
  83. * @param WP_REST_Request $request The current request 
  84. */ 
  85. return apply_filters( 'caldera_forms_api_allow_form_view', $allowed, $request[ 'form_id' ], $request ); 
  86.  
  87.  
  88. /** 
  89. * Permissions for form create/update/delete 
  90. * @since 1.5.0 
  91. * @param WP_REST_Request $request 
  92. * @return bool 
  93. */ 
  94. public function create_item_permissions_check( WP_REST_Request $request ) { 
  95. $allowed = current_user_can( Caldera_Forms::get_manage_cap( 'entry-edit' ), $request[ 'form_id' ] ); 
  96.  
  97. /** 
  98. * Filter permissions for creating, updating or deleting forms via Caldera Forms REST API 
  99. * @since 1.5.0 
  100. * @param bool $allowed Is request authorized? 
  101. * @param string $form_id The form ID 
  102. * @param WP_REST_Request $request The current request 
  103. */ 
  104. return apply_filters( 'caldera_forms_api_allow_form_edit', $allowed, $request[ 'form_id' ], $request ); 
  105.  
  106.  
  107. /** 
  108. * Form the endpoint URL that includes item ID 
  109. * Used by for get_item() and update_time() and delete_item() 
  110. * @since 1.5.0 
  111. * @return string 
  112. */ 
  113. public function id_endpoint_url() { 
  114. return '/' . $this->route_base() . '/(?P<form_id>[\w-]+)'; 
  115.  
  116. /** 
  117. * Format repsonse for form 
  118. * @param Caldera_Forms_API_Form $form 
  119. * @param WP_REST_Request $request 
  120. * @return array|mixed 
  121. */ 
  122. protected function prepare_form_for_response( Caldera_Forms_API_Form $form, WP_REST_Request $request ) { 
  123.  
  124. $form = $this->prepare_field_details( $form, $request ); 
  125.  
  126. $form = $this->prepare_processors_for_response( $form ); 
  127.  
  128. $form = $this->prepare_mailer_for_response( $form ); 
  129.  
  130. return $form; 
  131.  
  132.  
  133. /** 
  134. * Prepare field details section of form response 
  135. * @since 1.5.0 
  136. * @param Caldera_Forms_API_Form $form Form config 
  137. * @return array 
  138. */ 
  139. protected function prepare_field_details( Caldera_Forms_API_Form $form, WP_REST_Request $request ) { 
  140. $order = $form->get_fields(); 
  141. $entry_list = $form->get_entry_list_fields(); 
  142.  
  143. $form = $form->toArray(); 
  144.  
  145. $form[ 'field_details' ] = array( 
  146. 'order' => array(),  
  147. 'entry_list' => array() 
  148. ); 
  149.  
  150. array_walk( $order, array( $this, 'prepare_field' ) ); 
  151. array_walk( $entry_list, array( $this, 'prepare_field' ) ); 
  152.  
  153. if( false == $request[ 'entry_list_only_fields' ] ) { 
  154. foreach ( $order as $field_id => $field ) { 
  155. $type = Caldera_Forms_Field_Util::get_type( Caldera_Forms_Field_Util::get_field( $field_id, $form ) ); 
  156. if ( Caldera_Forms_Fields::not_support( $type, 'entry_list' ) ) { 
  157. unset( $order[ $field_id ] ); 
  158.  
  159. foreach ( $entry_list as $field_id => $field ) { 
  160. $type = Caldera_Forms_Field_Util::get_type( Caldera_Forms_Field_Util::get_field( $field_id, $form ) ); 
  161. if ( Caldera_Forms_Fields::not_support( $type, 'entry_list' ) ) { 
  162. unset( $entry_list[ $field_id ] ); 
  163.  
  164.  
  165. $form[ 'field_details' ][ 'order' ] = $order; 
  166. $entry_list_defaults = array( 
  167. 'id' => array( 
  168. 'id' => 'id',  
  169. 'label' => __( 'ID', 'caldera-forms' ) 
  170. ),  
  171. 'datestamp' => array( 
  172. 'id' => 'datestamp',  
  173. 'label' => __( 'Submitted', 'caldera-forms' ) 
  174. ),  
  175. ); 
  176.  
  177. if( is_array( $entry_list ) && ! empty( $entry_list ) ) { 
  178. $form[ 'field_details' ][ 'entry_list' ] = array_merge( $entry_list_defaults, $entry_list ); 
  179. }else{ 
  180. $form[ 'field_details' ][ 'entry_list' ] = $entry_list_defaults; 
  181.  
  182. return $form; 
  183.  
  184.  
  185. /** 
  186. * Reduce field to id/label 
  187. * Designed to be callback for array_walk used in $this->prepare_field_details 
  188. * @since 1.5.0 
  189. * @param $field 
  190. */ 
  191. protected function prepare_field( &$field ) { 
  192. $field = array( 
  193. 'id' => $field[ 'ID' ],  
  194. 'label' => $field[ 'label' ] 
  195. ); 
  196.  
  197. /** 
  198. * @param $form 
  199. * @return mixed 
  200. */ 
  201. protected function prepare_processors_for_response($form) { 
  202. if (!empty($form['processors'])) { 
  203. $processors = array(); 
  204. foreach ($form['processors'] as $id => $processor) { 
  205. $processors[$id] = array( 
  206. 'type' => $processor['type'],  
  207. 'id' => $id 
  208. ); 
  209. $form['processors'] = $processors; 
  210. return $form; 
  211.  
  212. return $form; 
  213.  
  214. /** 
  215. * @param $form 
  216. * @return mixed 
  217. */ 
  218. protected function prepare_mailer_for_response($form) { 
  219. if (!empty($form['mailer'])) { 
  220. if (!empty($form['mailer']['on_insert'])) { 
  221.  
  222. $form['mailer'] = array( 
  223. 'active' => true 
  224. ); 
  225. return $form; 
  226. } else { 
  227. $form['mailer'] = array( 
  228. 'active' => false 
  229. ); 
  230. return $form; 
  231. } else { 
  232.  
  233. $form['mailer'] = array( 
  234. 'active' => false 
  235. ); 
  236. return $form; 
  237.