Caldera_Forms_API_CRUD

Abstract class the all REST API route collections that follow CRUD pattern should extend.

Defined (1)

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

/classes/api/crud.php  
  1. abstract class Caldera_Forms_API_CRUD implements Caldera_Forms_API_Route { 
  2.  
  3. /** 
  4. * Form object for this response 
  5. * @since 1.5.0 
  6. * @var Caldera_Forms_API_Form 
  7. */ 
  8. protected $form; 
  9.  
  10. /** 
  11. * Namespace for API 
  12. * @since 1.5.0 
  13. * @var string 
  14. */ 
  15. protected $namespace; 
  16.  
  17. /** 
  18. * @inheritdoc 
  19. * @since 1.4.4  
  20. */ 
  21. public function add_routes( $namespace ) { 
  22. $this->namespace = $namespace; 
  23. register_rest_route( $namespace, $this->non_id_endpoint_url(), array( 
  24. array( 
  25. 'methods' => \WP_REST_Server::READABLE,  
  26. 'callback' => array( $this, 'get_items' ),  
  27. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  28. 'args' => $this->get_items_args(),  
  29. ),  
  30. array( 
  31. 'methods' => \WP_REST_Server::CREATABLE,  
  32. 'callback' => array( $this, 'create_item' ),  
  33. 'permission_callback' => array( $this, 'create_item_permissions_check' ),  
  34. 'args' => $this->request_args() 
  35. ),  
  36. ); 
  37. register_rest_route( $namespace, $this->id_endpoint_url(), array( 
  38. array( 
  39. 'methods' => \WP_REST_Server::READABLE,  
  40. 'callback' => array( $this, 'get_item' ),  
  41. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  42. 'args' => $this->get_item_args() 
  43. ),  
  44. array( 
  45. 'methods' => \WP_REST_Server::EDITABLE,  
  46. 'callback' => array( $this, 'update_item' ),  
  47. 'permission_callback' => array( $this, 'update_item_permissions_check' ),  
  48. 'args' => $this->request_args( ) 
  49. ),  
  50. array( 
  51. 'methods' => \WP_REST_Server::DELETABLE,  
  52. 'callback' => array( $this, 'delete_item' ),  
  53. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),  
  54. 'args' => array( 
  55. 'force' => array( 
  56. 'default' => false,  
  57. 'required' => false,  
  58. ),  
  59. 'all' => array( 
  60. 'default' => false,  
  61. 'required' => false,  
  62. ),  
  63. 'id' => array( 
  64. 'default' => 0,  
  65. 'sanatization_callback' => 'absint' 
  66. ),  
  67. ),  
  68. ); 
  69.  
  70. register_rest_route( $namespace, '/' . $this->route_base(), array( 
  71. 'methods' => 'GET',  
  72. 'callback' => array( $this, 'index' ),  
  73. ) ); 
  74.  
  75. public function get_item_args() { 
  76. return array( 
  77. 'context' => array( 
  78. 'default' => 'view',  
  79. ),  
  80. 'entry_list_only_fields' => array( 
  81. 'required' => false,  
  82. 'default' => false,  
  83. ); 
  84.  
  85. /** 
  86. * Callback for the index of this collection 
  87. * * @since 1.4.4  
  88. * @param WP_REST_Request $request 
  89. * @return Caldera_Forms_API_Response 
  90. */ 
  91. public function index( WP_REST_Request $request ) { 
  92. /** @var WP_REST_Server $wp_rest_server */ 
  93. global $wp_rest_server; 
  94. $routes = $wp_rest_server->get_routes(); 
  95. $endpoints = array(); 
  96. foreach ( $routes as $route => $route_endpoints ) { 
  97. if( false !== strpos( $route, $this->namespace . '/' . $this->route_base() ) ) { 
  98. $endpoints[ $route ] = $route_endpoints; 
  99.  
  100. $data = array( 
  101. 'namespace' => $this->namespace,  
  102. 'routes' => $wp_rest_server->get_data_for_routes( $endpoints, $request['context'] ),  
  103. ); 
  104.  
  105. return new Caldera_Forms_API_Response( $data, 200, array() ); 
  106.  
  107.  
  108. /** 
  109. * Define query arguments 
  110. * @since 1.4.4  
  111. * @return array( 
  112. */ 
  113. public function request_args() { 
  114. //must ovveride, should be abstract but PHP5.2 
  115. _doing_it_wrong( __FUNCTION__, '', '1.5.0' ); 
  116.  
  117. /** 
  118. * Check if a given request has access to get items 
  119. * @since 1.4.4  
  120. * @param WP_REST_Request $request Full data about the request. 
  121. * @return \WP_Error|bool 
  122. */ 
  123. public function get_items_permissions_check( WP_REST_Request $request ) { 
  124. //must ovveride, should be abstract but PHP5.2 
  125. _doing_it_wrong( __FUNCTION__, '', '1.5.0' ); 
  126.  
  127. /** 
  128. * Check if a given request has access to get a specific item 
  129. * @since 1.4.4  
  130. * @param WP_REST_Request $request Full data about the request. 
  131. * @return \WP_Error|bool 
  132. */ 
  133. public function get_item_permissions_check( WP_REST_Request $request ) { 
  134. return $this->get_items_permissions_check( $request ); 
  135.  
  136. /** 
  137. * Check if a given request has access to create items 
  138. * @since 1.4.4  
  139. * @param WP_REST_Request $request Full data about the request. 
  140. * @return \WP_Error|bool 
  141. */ 
  142. public function create_item_permissions_check( WP_REST_Request $request ) { 
  143. //must ovveride, should be abstract but PHP5.2 
  144. _doing_it_wrong( __FUNCTION__, '', '1.5.0' ); 
  145.  
  146. /** 
  147. * Check if a given request has access to update a specific item 
  148. * @since 1.4.4  
  149. * @param WP_REST_Request $request Full data about the request. 
  150. * @return \WP_Error|bool 
  151. */ 
  152. public function update_item_permissions_check( WP_REST_Request $request ) { 
  153. return $this->create_item_permissions_check( $request ); 
  154.  
  155. /** 
  156. * Check if a given request has access to delete a specific item 
  157. * @since 1.4.4  
  158. * @param WP_REST_Request $request Full data about the request. 
  159. * @return \WP_Error|bool 
  160. */ 
  161. public function delete_item_permissions_check( WP_REST_Request $request ) { 
  162. return $this->create_item_permissions_check( $request ); 
  163.  
  164. /** 
  165. * Get a collection of items 
  166. * @since 1.4.4  
  167. * @param WP_REST_Request $request Full data about the request. 
  168. * @return \WP_Error|\WP_REST_Response 
  169. */ 
  170. public function get_items( WP_REST_Request $request ) { 
  171. return $this->not_yet_response(); 
  172.  
  173. /** 
  174. * Get one item from the collection 
  175. * @since 1.4.4  
  176. * @param WP_REST_Request $request Full data about the request. 
  177. * @return \WP_Error|\WP_REST_Response 
  178. */ 
  179. public function get_item( WP_REST_Request $request ) { 
  180. return $this->not_yet_response(); 
  181.  
  182. /** 
  183. * Create one item from the collection 
  184. * @since 1.4.4  
  185. * @param WP_REST_Request $request Full data about the request. 
  186. * @return Caldera_Forms_API_Response|Caldera_Forms_API_Error 
  187. */ 
  188. public function create_item( WP_REST_Request $request ) { 
  189. return $this->not_yet_response(); 
  190.  
  191. /** 
  192. * Update one item from the collection 
  193. * @since 1.4.4  
  194. * @param WP_REST_Request $request Full data about the request. 
  195. * @return Caldera_Forms_API_Response|Caldera_Forms_API_Error 
  196. */ 
  197. public function update_item( WP_REST_Request $request ) { 
  198. return $this->not_yet_response(); 
  199.  
  200. /** 
  201. * Delete one item from the collection 
  202. * @since 1.4.4  
  203. * @param WP_REST_Request $request Full data about the request. 
  204. * @return Caldera_Forms_API_Response|Caldera_Forms_API_Error 
  205. */ 
  206. public function delete_item( WP_REST_Request $request ) { 
  207. return $this->not_yet_response(); 
  208.  
  209. /** 
  210. * Return a 501 error for non-existant route 
  211. * @since 1.4.4  
  212. * @return Caldera_Forms_API_Response 
  213. */ 
  214. protected function not_yet_response() { 
  215. $error = new Caldera_Forms_API_Error( 'not-implemented-yet', __( 'Route Not Yet Implemented :(', 'caldera-forms' ) ); 
  216. return new Caldera_Forms_API_Response( $error, 501, array() ); 
  217.  
  218. /** 
  219. * Get class shortname and use as base 
  220. * @since 1.4.4  
  221. * MUST ovveride in subclass with a hardcoded string. 
  222. */ 
  223. protected function route_base() { 
  224. //must ovveride, should be abstract but PHP5.2 
  225. _doing_it_wrong( __FUNCTION__, '', '1.5.0' ); 
  226.  
  227. /** 
  228. * Form the endpoint URL that deos not include item ID 
  229. * Used by for get_items() and create_items() 
  230. * @since 1.4.4  
  231. * @return string 
  232. */ 
  233. protected function non_id_endpoint_url() { 
  234. return '/' . $this->route_base(); 
  235.  
  236.  
  237. /** 
  238. * Form the endpoint URL that includes item ID 
  239. * Used by for get_item() and update_time() and delete_item() 
  240. * @since 1.4.4  
  241. * @return string 
  242. */ 
  243. public function id_endpoint_url() { 
  244. return '/' . $this->route_base() . '/(?P<id>[\w-]+)'; 
  245.  
  246. /** 
  247. * @return array 
  248. */ 
  249. public function get_items_args() { 
  250. return array( 
  251. 'page' => array( 
  252. 'default' => 1,  
  253. 'sanitize_callback' => 'absint',  
  254. ),  
  255. 'per_page' => array( 
  256. 'default' => 20,  
  257. 'sanitize_callback' => 'absint',  
  258. ),  
  259. 'limit' => array( 
  260. 'default' => 10,  
  261. 'sanitize_callback' => 'absint',  
  262. ); 
  263.  
  264. /** 
  265. * Factory for Caldera_Forms_API_Form objects 
  266. * @since 1.5.0 
  267. * @param string $id Form ID 
  268. * @param WP_REST_Request $request Current REST API request 
  269. * @param bool $set_prop Optional. Set in $form property of object if true, the default. If false, return. 
  270. * @return Caldera_Forms_API_Form|void 
  271. * @throws Exception 
  272. */ 
  273. protected function form_object_factory( $id, WP_REST_Request $request, $set_prop = true ) { 
  274. $form = Caldera_Forms_Forms::get_form( $id ); 
  275. if( empty( $form ) ) { 
  276. throw new Exception(); 
  277.  
  278. $obj = new Caldera_Forms_API_Form( $form ); 
  279. $obj->set_request( $request ); 
  280. if ( $set_prop ) { 
  281. $this->form = $obj; 
  282.  
  283. } else { 
  284.  
  285. return $obj; 
  286.  
  287.