Caldera_Forms_API_Form

Form abstraction for use by API responses.

Defined (1)

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

/classes/api/form.php  
  1. class Caldera_Forms_API_Form implements ArrayAccess { 
  2.  
  3. /** 
  4. * @since 1.5.0 
  5. * @var array 
  6. */ 
  7. protected $form; 
  8.  
  9. /** 
  10. * Fields of fields that can be used in this context 
  11. * @since 1.5.0 
  12. * @var array 
  13. */ 
  14. protected $fields; 
  15.  
  16. /** 
  17. * Current REST API request 
  18. * @since 1.5.0 
  19. * @var WP_REST_Request 
  20. */ 
  21. protected $request; 
  22.  
  23. /** 
  24. * Caldera_Forms_API_Form constructor. 
  25. * @since 1.5.0 
  26. * @param array $form Form config 
  27. */ 
  28. public function __construct( array $form ) { 
  29. $this->form = $form; 
  30. //JOSH - don't call $this->set_fields() here, or WP_REST_Request object will not be available to filter. 
  31.  
  32. /** 
  33. * Set current REST request in object 
  34. * @since 1.5.0 
  35. * @param WP_REST_Request $request 
  36. */ 
  37. public function set_request( WP_REST_Request $request ) { 
  38. $this->request = $request; 
  39.  
  40. /** 
  41. * Get the form config as an array 
  42. * @since 1.5.0 
  43. * @return array 
  44. */ 
  45. public function toArray() { 
  46. return $this->form; 
  47.  
  48. /** 
  49. * Get a field config IF it should be returned in REST API response. 
  50. * @since 1.5.0 
  51. * @param string $field_id Field ID 
  52. * @return array|null 
  53. */ 
  54. public function get_field( $field_id ) { 
  55. $this->maybe_set_fields(); 
  56. if( $this->is_api_field( $field_id )) { 
  57. return $this->is_api_field( $field_id ); 
  58.  
  59. return null; 
  60.  
  61.  
  62. /** 
  63. * Check if a field should be returned in REST API response. 
  64. * @since 1.5.0 
  65. * @param string $field_id Field ID 
  66. * @return bool 
  67. */ 
  68. public function is_api_field( $field_id ) { 
  69. $this->maybe_set_fields(); 
  70. return isset( $this->fields[ $field_id ] ); 
  71.  
  72. /** 
  73. * Get all fields that should be returned in REST API response. 
  74. * @since 1.5.0 
  75. * @return array 
  76. */ 
  77. public function get_fields() { 
  78. $this->maybe_set_fields(); 
  79. return $this->fields; 
  80.  
  81. /** 
  82. * Get all fields that should be returned in REST API response's entry list fields 
  83. * @since 1.5.0 
  84. * @return array 
  85. */ 
  86. public function get_entry_list_fields() { 
  87. $entry_list_fields = Caldera_Forms_Forms::entry_list_fields( $this->form, true ); 
  88. foreach ( $entry_list_fields as $field_id => $field ) { 
  89. if( ! isset( $this->fields[ $field_id ] ) ) { 
  90. unset( $entry_list_fields[ $field_id ] ); 
  91.  
  92.  
  93. return $entry_list_fields; 
  94.  
  95.  
  96. /** 
  97. * Set fields property 
  98. * @since 1.5.0 
  99. */ 
  100. protected function set_fields() { 
  101. $this->fields = Caldera_Forms_Forms::get_fields( $this->form, true, true ); 
  102. if( ! empty( $this->fields ) ) { 
  103. foreach ( $this->fields as $field_id => $field ) { 
  104.  
  105. /** 
  106. * Prevent a field from being shown in API responses 
  107. * @since 1.5.0 
  108. * @param bool $show If false, field is not returned. 
  109. * @param string $field_id ID of field 
  110. * @param array $field Field config 
  111. * @param array $form Form config 
  112. * @param WP_REST_Request $request Current REST API request 
  113. */ 
  114. if( false == apply_filters( 'caldera_forms_api_show_field', true, $field_id, $field, $this->form, $this->request ) ) { 
  115. unset( $this->fields[ $field_id ] ); 
  116. unset( $this->form[ 'fields' ][ $field_id ] ); 
  117.  
  118.  
  119.  
  120. /** 
  121. * Lazy-loader for fields property 
  122. * @since 1.5.0 
  123. */ 
  124. private function maybe_set_fields() { 
  125. if( empty( $this->fields ) ) { 
  126. $this->set_fields(); 
  127.  
  128.  
  129. /** 
  130. * @inheritdoc 
  131. */ 
  132. public function offsetSet($offset, $value) { 
  133. if (is_null($offset)) { 
  134. $this->form[] = $value; 
  135. } else { 
  136. $this->form[$offset] = $value; 
  137.  
  138. /** 
  139. * @inheritdoc 
  140. */ 
  141. public function offsetExists($offset) { 
  142. return isset($this->form[$offset]); 
  143.  
  144. /** 
  145. * @inheritdoc 
  146. */ 
  147. public function offsetUnset($offset) { 
  148. unset($this->form[$offset]); 
  149.  
  150. /** 
  151. * @inheritdoc 
  152. */ 
  153. public function offsetGet($offset) { 
  154. return isset($this->form[$offset]) ? $this->form[$offset] : null; 
  155.  
  156.  
  157.