Caldera_Forms_API_Entries

CRUD via REST API for entries.

Defined (1)

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

/classes/api/entries.php  
  1. class Caldera_Forms_API_Entries extends Caldera_Forms_API_CRUD { 
  2.  
  3. /** 
  4. * Get an entry 
  5. * GET /cf-api/v2/entries/form-id/entry-id 
  6. * @since 1.5.0 
  7. * @param WP_REST_Request $request 
  8. * @return Caldera_Forms_API_Error|Caldera_Forms_API_Response 
  9. */ 
  10. public function get_item( WP_REST_Request $request ) { 
  11. try{ 
  12. $this->form_object_factory( $request[ 'form_id' ], $request ); 
  13. }catch ( Exception $e ) { 
  14. return Caldera_Forms_API_Response_Factory::error_form_not_found(); 
  15.  
  16. $entry = new Caldera_Forms_Entry( $this->form->toArray(), $request[ 'entry_id' ] ); 
  17.  
  18. if( null == $entry->get_entry() ) { 
  19. return Caldera_Forms_API_Response_Factory::error_entry_not_found(); 
  20.  
  21. $data = $this->add_entry_to_response( $entry, array() ); 
  22. $data = $data[ $request[ 'entry_id' ] ]; 
  23. return Caldera_Forms_API_Response_Factory::entry_data( $data, 1, 1 ); 
  24.  
  25.  
  26. /** 
  27. * Get entries 
  28. * GET /cf-api/v2/entries/form-id 
  29. * @since 1.5.0 
  30. * @param WP_REST_Request $request 
  31. * @return Caldera_Forms_API_Error|Caldera_Forms_API_Response 
  32. */ 
  33. public function get_items( WP_REST_Request $request ) { 
  34. try{ 
  35. $this->form_object_factory( $request[ 'form_id' ], $request ); 
  36. }catch ( Exception $e ) { 
  37. return Caldera_Forms_API_Response_Factory::error_form_not_found(); 
  38.  
  39. $per_page = $request[ 'per_page' ]; 
  40. if( 0 == $request[ 'per_page' ] ) { 
  41. $per_page = 1; 
  42.  
  43. $entries = new Caldera_Forms_Entry_Entries( $this->form->toArray(), $per_page ); 
  44. $data = $this->prepare_entries_for_response( $entries->get_page( $request[ 'page' ], $request[ 'status' ] ) ); 
  45. $entries->get_page( $request[ 'page' ], $request[ 'status' ] ); 
  46. $pages = ceil( $entries->get_total( $request[ 'status' ] ) / $per_page ); 
  47.  
  48. return Caldera_Forms_API_Response_Factory::entry_data( 
  49. $data,  
  50. count( $data ),  
  51. $pages 
  52. ); 
  53.  
  54. /** 
  55. * Delete an entry 
  56. * DELETE /cf-api/v2/entries/form-id/entry-id 
  57. * @since 1.5.0 
  58. * @param WP_REST_Request $request 
  59. * @return Caldera_Forms_API_Error|Caldera_Forms_API_Response 
  60. */ 
  61. public function delete_item( WP_REST_Request $request ) { 
  62. $form_id = $request[ 'form_id' ]; 
  63. $form = Caldera_Forms_Forms::get_form( $form_id ); 
  64. if( ! is_array( $form ) ) { 
  65. return Caldera_Forms_API_Response_Factory::error_form_not_found(); 
  66.  
  67. $result = Caldera_Forms_Entry_Bulk::delete_entries( array( $request[ 'entry_id' ] ) ); 
  68. if( $result ) { 
  69. return new Caldera_Forms_API_Response( __( 'Entry Deleted', 'caldera-forms' ), 200, array() ); 
  70. }else{ 
  71. return new Caldera_Forms_API_Error( 'not-deleted', __( 'Entry Not Deleted', 'caldera-forms' ) ); 
  72.  
  73. /** 
  74. * Prepare entry data for a response 
  75. * @since 1.0.5 
  76. * @param array $entries Array of found Caldera_Forms_Entry objects 
  77. * @return array 
  78. */ 
  79. protected function prepare_entries_for_response( $entries ) { 
  80. $response_data = array(); 
  81.  
  82. /** @var Caldera_Forms_Entry $entry Entry Object */ 
  83. foreach( $entries as $id => $entry ) { 
  84. $response_data = $this->add_entry_to_response( $entry, $response_data ); 
  85.  
  86.  
  87. return $response_data; 
  88.  
  89.  
  90. /** 
  91. * Add an entry to a response collection 
  92. * @since 1.5.0 
  93. * @param Caldera_Forms_Entry $entry Entry object 
  94. * @param array $response_data Current response collection 
  95. * @return array 
  96. */ 
  97. protected function add_entry_to_response( Caldera_Forms_Entry $entry, array $response_data ) { 
  98. $id = $entry->get_entry_id(); 
  99. $response_data[ $id ] = array(); 
  100.  
  101. $response_data[ $id ] = $entry->get_entry()->to_array( false ); 
  102. $response_data[ $id ][ 'user' ] = array( 
  103. 'id' => '',  
  104. 'name' => '',  
  105. 'email' => '' 
  106. ); 
  107. $user = get_user_by( 'ID', $entry->get_entry()->user_id ); 
  108.  
  109. if( is_object( $user ) ) { 
  110. $response_data[ $id ][ 'user' ][ 'name' ] = $user->display_name; 
  111. if( current_user_can( 'edit_users' ) ) { 
  112. $response_data[ $id ][ 'user' ][ 'email' ] = $user->user_email; 
  113. $response_data[ $id ][ 'user' ][ 'id' ] = $entry->get_entry()->user_id; 
  114.  
  115. unset( $response_data[ $id ][ 'user_id' ] ); 
  116.  
  117. $fields = $entry->get_fields(); 
  118. $response_data[ $id ][ 'fields' ] = array(); 
  119. if( ! empty( $fields ) ) { 
  120. /** @var Caldera_Forms_Entry_Field $field */ 
  121. foreach( $fields as $field ) { 
  122. if ( $this->form->is_api_field( $field->field_id ) && is_object( $field ) ) { 
  123. $response_data[ $id ][ 'fields' ][ $field->field_id ] = $field->to_array( false ); 
  124.  
  125.  
  126. $metas = $entry->get_meta(); 
  127. $response_data[ $id ][ 'meta' ] = array(); 
  128. if( ! empty( $metas ) ) { 
  129. /** @var Caldera_Forms_Entry_Meta $meta */ 
  130. foreach ( $metas as $meta ) { 
  131. if( is_object( $meta ) ) { 
  132. $response_data[ $id ][ 'meta' ][ $meta->id ] = $meta->to_array( false ); 
  133.  
  134.  
  135.  
  136. return $response_data; 
  137.  
  138. /** 
  139. * Permissions for entry read 
  140. * @since 1.5.0 
  141. * @param WP_REST_Request $request 
  142. * @return bool 
  143. */ 
  144. public function get_items_permissions_check( WP_REST_Request $request ) { 
  145. $form_id = $request[ 'form_id' ]; 
  146. $allowed = current_user_can( Caldera_Forms::get_manage_cap( 'entry-view' ), $form_id ); 
  147.  
  148. if( ! $allowed ) { 
  149. $allowed = Caldera_Forms_API_Util::check_api_token( $request ); 
  150.  
  151. /** 
  152. * Filter permissions for viewing entries via Caldera Forms REST API 
  153. * @since 1.5.0 
  154. * @param bool $allowed Is request authorized? 
  155. * @param string $form_id The form ID 
  156. * @param WP_REST_Request $request The current request 
  157. */ 
  158. return apply_filters( 'caldera_forms_api_allow_entry_view', $allowed, $form_id, $request ); 
  159.  
  160.  
  161. /** 
  162. * Permissions for entry create/update/delete 
  163. * @since 1.5.0 
  164. * @param WP_REST_Request $request 
  165. * @return bool 
  166. */ 
  167. public function create_item_permissions_check( WP_REST_Request $request ) { 
  168. $allowed = current_user_can( Caldera_Forms::get_manage_cap( 'entry-edit' ), $request[ 'form_id' ] ); 
  169.  
  170. /** 
  171. * Filter permissions for creating, updating or deleting entries via Caldera Forms REST API 
  172. * @since 1.5.0 
  173. * @param bool $allowed Is request authorized? 
  174. * @param string $form_id The form ID 
  175. * @param WP_REST_Request $request The current request 
  176. */ 
  177. return apply_filters( 'caldera_forms_api_allow_entry_edit', $allowed, $request[ 'form_id' ], $request ); 
  178.  
  179.  
  180. /** 
  181. * @inheritdoc 
  182. * @since 1.5.0 
  183. */ 
  184. public function get_items_args() { 
  185. return wp_parse_args( array( 
  186. 'status' => array( 
  187. 'default' => 'active',  
  188. 'validate_callback' => array( $this, 'validate_status' ) 
  189. ), parent::get_items_args() ); 
  190.  
  191. /** 
  192. * Form the endpoint URL that deos not include item ID 
  193. * Used by for get_items() and create_items() 
  194. * @since 1.5.0 
  195. * @return string 
  196. */ 
  197. protected function non_id_endpoint_url() { 
  198. return '/' . $this->route_base() . '/(?P<form_id>[\w-]+)'; 
  199.  
  200.  
  201. /** 
  202. * @inheritdoc 
  203. * @since 1.5.0 
  204. */ 
  205. public function request_args() { 
  206. return array( 
  207. ); 
  208.  
  209. /** 
  210. * Form the endpoint URL that includes item ID 
  211. * Used by for get_item() and update_time() and delete_item() 
  212. * @since 1.5.0 
  213. * @return string 
  214. */ 
  215. public function id_endpoint_url() { 
  216. return $this->non_id_endpoint_url() . '/' . '(?P<entry_id>[\d]+)'; 
  217.  
  218. /** 
  219. * @inheritdoc 
  220. * @since 1.5.0 
  221. */ 
  222. protected function route_base() { 
  223. return 'entries'; 
  224.  
  225. /** 
  226. * Validate status argument 
  227. * @since 1.5.0 
  228. * @param $value 
  229. * @return bool 
  230. */ 
  231. public function validate_status( $value ) { 
  232. return in_array( $value, array( 
  233. 'active',  
  234. 'pending',  
  235. 'trash' 
  236. )); 
  237.  
  238.