Caldera_Forms_Field_Util

Handles file uploading from file fields.

Defined (1)

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

/classes/field/util.php  
  1. class Caldera_Forms_Field_Util { 
  2.  
  3. /** 
  4. * Get a field's type 
  5. * @since 1.4.4 
  6. * @param string|array $field Field ID or config array 
  7. * @param array|null $form Optional. Form config. MUST USE if $field is ID not config array 
  8. * @return bool 
  9. */ 
  10. public static function get_type( $field, array $form = null) { 
  11. if( is_string( $field ) && is_array( $form ) ) { 
  12. $field = self::get_field( $field, $form ); 
  13. }elseif ( is_string( $field ) && null == $form ) { 
  14. return false; 
  15.  
  16. return $field[ 'type' ]; 
  17.  
  18.  
  19. /** 
  20. * Get field config by ID 
  21. * @since 1.4.4 
  22. * @param string|array $field Field ID. If you pass an array, that will be returned. 
  23. * @param array|null $form 
  24. * @param bool $filter Optional. Apply field filters? Default is false. Added in 1.5.0 
  25. * @return bool|array 
  26. */ 
  27. public static function get_field( $field, array $form = null, $filter = false ) { 
  28. if ( ! is_array( $field ) ) { 
  29. if( ! is_array( $form ) ) { 
  30. global $form; 
  31.  
  32. $fields = Caldera_Forms_Forms::get_fields( $form, false ); 
  33. if ( isset( $fields[ $field ] ) ) { 
  34. $field = $fields[ $field ]; 
  35. }else{ 
  36. return false; 
  37.  
  38. if( $filter ) { 
  39. $field = self::apply_field_filters( $field, $form ); 
  40.  
  41. return $field; 
  42.  
  43. /** 
  44. * Get field ID attr 
  45. * @since 1.5.0 
  46. * @param string|array $field Field config or field ID 
  47. * @param null $current_form_count Optional. Current form count. If is nbull, the default, global will be used. 
  48. * @return string 
  49. */ 
  50. public static function get_base_id( $field, $current_form_count = null, $form = null ) { 
  51. $field = self::get_field( $field, $form ); 
  52. if( null == $current_form_count ) { 
  53. $current_form_count = Caldera_Forms_Render_Util::get_current_form_count(); 
  54.  
  55. return $field['ID'] . '_' . $current_form_count; 
  56.  
  57.  
  58. /** 
  59. * Check if a field is a file field of either type 
  60. * @since 1.4.4 
  61. * @param array|string $field Field ID or config array 
  62. * @param array|null $form Optional. Form config array. MUST USE if $field is ID not config array. 
  63. * @return bool 
  64. */ 
  65. public static function is_file_field( $field, array $form = null ) { 
  66. $field = self::get_field( $field, $form ); 
  67. return in_array( self::get_type( $field, $form ), Caldera_Forms_Files::types() ); 
  68.  
  69. /** 
  70. * Get ID attribute for the element that displays star field stars 
  71. * @since 1.5.0 
  72. * @param string $id_attr ID attribute of actual input 
  73. * @return string 
  74. */ 
  75. public static function star_target( $id_attr ) { 
  76. return $id_attr . '_stars'; 
  77.  
  78. protected static $field_classes = array(); 
  79.  
  80. /** 
  81. * Prepare field classes 
  82. * @since 1.5.0 
  83. * @param array $field Field config array 
  84. * @param array $form Form config array 
  85. * @return array 
  86. */ 
  87. public static function prepare_field_classes( $field, $form ) { 
  88. $current_form_count = Caldera_Forms_Render_Util::get_current_form_count(); 
  89. if( ! isset( self::$field_classes[ $form[ 'ID' ] ] ) ) { 
  90. self::$field_classes[ $form[ 'ID' ] ] = array( 
  91.  
  92. ); 
  93.  
  94. if( ! isset( self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ]) ) { 
  95. self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ] = array( 
  96.  
  97. ); 
  98.  
  99. if ( empty( self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ][ $field[ 'ID' ] ] ) ) { 
  100. self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ][ $field[ 'ID' ] ] = array(); 
  101. $field_classes = array( 
  102. "control_wrapper" => array( "form-group" ),  
  103. "field_label" => array( "control-label" ),  
  104. "field_required_tag" => array( "field_required" ),  
  105. "field_wrapper" => array(),  
  106. "field" => array( "form-control" ),  
  107. "field_caption" => array( "help-block" ),  
  108. "field_error" => array( "has-error" ),  
  109. ); 
  110.  
  111.  
  112.  
  113. $field_classes = apply_filters( 'caldera_forms_render_field_classes', $field_classes, $field, $form ); 
  114. $field_classes = apply_filters( 'caldera_forms_render_field_classes_type-' . $field[ 'type' ], $field_classes, $field, $form ); 
  115. $field_classes = apply_filters( 'caldera_forms_render_field_classes_slug-' . $field[ 'slug' ], $field_classes, $field, $form ); 
  116.  
  117. self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ][ $field[ 'ID' ] ] = $field_classes; 
  118.  
  119. return self::$field_classes[ $form[ 'ID' ] ][ $current_form_count ][ $field[ 'ID' ] ]; 
  120.  
  121. /** 
  122. * Prepare aria attributes for a field 
  123. * @since 1.5.0 
  124. * @param array $field_structure Field structure 
  125. * @return array 
  126. */ 
  127. public static function prepare_aria_attrs( $field_structure, $field ) { 
  128. // if has label 
  129. if( empty( $field['hide_label'] ) ) { 
  130. // visible label, set labelled by 
  131. $field_structure['aria']['labelledby'] = $field['ID'] . 'Label'; 
  132. }else{ 
  133. // hidden label, aria label instead 
  134. $field_structure['aria']['label'] = $field['label']; 
  135. // if has caption 
  136. if( !empty( $field['caption'] ) ) { 
  137. $field_structure['aria']['describedby'] = $field['ID'] . 'Caption'; 
  138.  
  139.  
  140. return $field_structure; 
  141.  
  142. /** 
  143. * Get allowed math functions with ability to filter allowed function 
  144. * @since 1.5.0 
  145. * @param array $form 
  146. * @return array 
  147. */ 
  148. public static function get_math_functions( array $form ) { 
  149. $math_functions = array( 
  150. 'pow',  
  151. 'abs',  
  152. 'acos',  
  153. 'asin',  
  154. 'atan',  
  155. 'atan2',  
  156. 'ciel',  
  157. 'cos',  
  158. 'exp',  
  159. 'floor',  
  160. 'log',  
  161. 'max',  
  162. 'min',  
  163. 'random',  
  164. 'round',  
  165. 'sin',  
  166. 'sqrt',  
  167. 'tan' 
  168. ); 
  169.  
  170. /** 
  171. * Filter the allowed math functions 
  172. * Useful for removing functions 
  173. * Add functions with extreme caution, must be name of a function in PHP global scope and a method of JavaScript Math object. 
  174. * @since 1.5.0 
  175. * @param array $math_functions Functions allowed 
  176. * @param array $form Form Config 
  177. */ 
  178. return apply_filters( 'caldera_forms_field_util_math_functions', $math_functions, $form ); 
  179.  
  180.  
  181. /** 
  182. * Get the configuration for a field. 
  183. * @since 1.5.0 
  184. * @param string $slug Slug of field to get config for. 
  185. * @param array $form Form config array. 
  186. * @return bool|mixed|void 
  187. */ 
  188. public static function get_field_by_slug($slug, $form) { 
  189.  
  190. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  191.  
  192. if ( $field[ 'slug' ] == $slug ) { 
  193.  
  194. return self::apply_field_filters( $field, $form ); 
  195.  
  196.  
  197. return false; 
  198.  
  199.  
  200. /** 
  201. * Wrapper for multi-use field filters 
  202. * @since 1.5.0 
  203. * @param array $field Field config 
  204. * @param array $form Form config 
  205. * @return array 
  206. */ 
  207. public static function apply_field_filters( $field, $form ) { 
  208.  
  209. /** 
  210. * Filter field config. 
  211. * @since unknown 
  212. * @param array $field The field config. 
  213. * @param array $form The form config. 
  214. */ 
  215. $field = apply_filters( 'caldera_forms_render_get_field', $field, $form ); 
  216.  
  217. /** 
  218. * Filter field config for fields of a given type. 
  219. * Filter name is dynamic, based on field type. For example "caldera_forms_render_get_field_type-hidden" or "caldera_forms_render_get_field_type-radio" 
  220. * @since unknown 
  221. * @param array $field The field config. 
  222. * @param array $form The form config. 
  223. */ 
  224. $field = apply_filters( 'caldera_forms_render_get_field_type-' . $field[ 'type' ], $field, $form ); 
  225.  
  226. /** 
  227. * Filter field config for fields with a given slug 
  228. * Filter name is dynamic, based on field type. For example "caldera_forms_render_get_field_slug-salsa" or "caldera_forms_render_get_field_slug-chips" 
  229. * @since unknown 
  230. * @param array $field The field config. 
  231. * @param array $form The form config. 
  232. */ 
  233. $field = apply_filters( 'caldera_forms_render_get_field_slug-' . $field[ 'slug' ], $field, $form ); 
  234.  
  235. return $field; 
  236.  
  237. /** 
  238. * Get types of credit cards we can do UI stuff to with CC field 
  239. * @since 1.5.0 
  240. * @param array $field Field config 
  241. * @param array $form Form config 
  242. * @return array 
  243. */ 
  244. public static function credit_card_types( $field, $form ) { 
  245. $types = array( 
  246. 'amex',  
  247. 'china_union_pay',  
  248. 'dankort',  
  249. 'diners_club_carte_blanche',  
  250. 'diners_club_international',  
  251. 'diners_club_us_and_canada',  
  252. 'discover',  
  253. 'jcb',  
  254. 'laser',  
  255. 'maestro',  
  256. 'mastercard',  
  257. 'visa',  
  258. 'visa_electron' 
  259. ); 
  260.  
  261. /** 
  262. * Change types of credit cards we can do UI stuff to with CC field 
  263. * @since 1.5.0 
  264. * @param array $field Field config 
  265. * @param array $form Form config 
  266. */ 
  267. return apply_filters( 'caldera_forms_credit_card_types', $types, $field, $form ); 
  268.  
  269. /** 
  270. * Check if a form has a type of field 
  271. * @since 1.5.0 
  272. * @param string $type Type to check for 
  273. * @param array $form Form config to check in 
  274. * @return bool 
  275. */ 
  276. public static function has_field_type( $type, array $form ) { 
  277. $types = wp_list_pluck( $form[ 'fields' ], 'type' ); 
  278. return in_array( $type, array_values( $types ) ); 
  279.  
  280. /** 
  281. * Get field default value 
  282. * @since 1.5.0 
  283. * @param array|string $field Field config or field ID 
  284. * @param array $form Form config 
  285. * @return bool 
  286. */ 
  287. public static function get_default( $field, array $form ) { 
  288. if( is_string( $field ) ) { 
  289. $field = self::get_field( $field, $form ); 
  290.  
  291. if( ! is_array( $field ) || empty( $field[ 'config' ][ 'default' ] ) ) { 
  292. return false; 
  293.  
  294. return $field[ 'config' ][ 'default' ]; 
  295.  
  296.  
  297. /** 
  298. * Check a field's conditional logic 
  299. * @since 1.5.0.4 
  300. * @param array|string $field Field config or field ID 
  301. * @param array $form Form config 
  302. * @return bool 
  303. */ 
  304. public static function check_conditional( $field, array $form ) { 
  305. if( is_string( $field ) ) { 
  306. $field = self::get_field( $field, $form ); 
  307.  
  308. if ( ! empty( $field[ 'conditions' ][ 'type' ] ) ) { 
  309. $conditional = $field[ 'conditions' ]; 
  310. if ( ! empty( $form[ 'conditional_groups'][ 'conditions'][ $field[ 'conditions' ]['type' ] ] ) ) { 
  311. $conditional = $form[ 'conditional_groups' ][ 'conditions' ][ $field[ 'conditions' ][ 'type' ] ]; 
  312. return Caldera_Forms::check_condition( $conditional, $form ); 
  313.  
  314. return true; 
  315.  
  316. /** 
  317. * Apply formatting, such as money formatting to a calculation field value 
  318. * @since 1.5.0.7 
  319. * @param array $field Field config 
  320. * @param string|float|int $value Value 
  321. * @return string 
  322. */ 
  323. public static function format_calc_field( $field, $value ) { 
  324. if ( isset( $field[ 'config' ][ 'fixed' ] ) ) { 
  325. $money = true; 
  326. }else{ 
  327. $money = false; 
  328.  
  329.  
  330. if ( $money ) { 
  331. if ( function_exists( 'money_format' ) ) { 
  332. $value = money_format( '%i', $value ); 
  333. } else { 
  334. $value = sprintf( '%01.2f', $value ); 
  335.  
  336.  
  337. return $value; 
  338.