Caldera_Forms_Forms

Class Caldera_Forms_Forms.

Defined (1)

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

/classes/forms.php  
  1. class Caldera_Forms_Forms { 
  2.  
  3. /** 
  4. * Holds registry of forms 
  5. * @since 1.3.4 
  6. * @var array 
  7. */ 
  8. protected static $registry_cache; 
  9.  
  10. /** 
  11. * Cache key for storing form registry in 
  12. * @since 1.3.4 
  13. * @var string 
  14. */ 
  15. protected static $registry_cache_key = '_cadera_forms'; 
  16.  
  17. /** 
  18. * Holds simple index of form IDs 
  19. * @since 1.3.4 
  20. * @var array 
  21. */ 
  22. protected static $index; 
  23.  
  24. /** 
  25. * Holds stored forms 
  26. * @since 1.3.4 
  27. * @var array 
  28. */ 
  29. protected static $stored_forms; 
  30.  
  31. /** 
  32. * Option key for storing registry in 
  33. * @since 1.3.4 
  34. * @var string 
  35. */ 
  36. protected static $registry_option_key = '_caldera_forms_forms'; 
  37.  
  38. /** 
  39. * Fields used when converting flat registry to detailed registry 
  40. * @since 1.3.4 
  41. * @var array 
  42. */ 
  43. protected static $detail_fields = array( 
  44. 'ID',  
  45. 'name',  
  46. 'description',  
  47. 'success',  
  48. 'form_ajax',  
  49. 'hide_form',  
  50. 'db_support',  
  51. 'mailer',  
  52. 'pinned',  
  53. 'pin_roles',  
  54. 'hidden',  
  55. 'form_draft' 
  56. ); 
  57.  
  58. /** 
  59. * Load a form by ID or name 
  60. * @since 1.3.4 
  61. * @param string $id_name ID or name of form. 
  62. * @return array|null Form config array if found. If not null. 
  63. */ 
  64. public static function get_form( $id_name ) { 
  65. $id_name = sanitize_text_field( $id_name ); 
  66.  
  67. $forms = self::get_forms(); 
  68. $form = null; 
  69.  
  70. if ( self::is_internal_form( $id_name ) ) { 
  71. if ( isset( $forms[ $id_name ] ) ) { 
  72. $form = get_option( $forms[ $id_name ] ); 
  73. } else { 
  74. $forms = self::get_forms( true ); 
  75. foreach ( $forms as $form_id => $form_maybe ) { 
  76. if ( trim( strtolower( $id_name ) ) == strtolower( $form_maybe[ 'name' ] ) && empty( $form_maybe[ '_external_form' ] ) ) { 
  77. $form = get_option( $form_maybe[ 'ID' ] ); 
  78.  
  79. if( empty( $form ) ) { 
  80. $external = true; 
  81.  
  82. /** 
  83. * Filter settings of a form or all forms or use to define a form in file 
  84. * @param array $form Form config 
  85. * @param string $id_name ID or name of form 
  86. */ 
  87. $form = apply_filters( 'caldera_forms_get_form', $form, $id_name ); 
  88.  
  89. /** 
  90. * Filter settings of a specific form or all forms or use to define a form in file 
  91. * @param array $form Form config 
  92. * @param string $id_name ID or name of form 
  93. */ 
  94. $form = apply_filters( 'caldera_forms_get_form-' . $id_name, $form, $id_name ); 
  95.  
  96. if( is_array( $form ) && empty( $form['ID'] ) ) { 
  97. $form['ID'] = $id_name; 
  98.  
  99. if( ! empty( $form ) && ! empty( $external ) ) { 
  100. $form['_external_form'] = true; 
  101.  
  102. // remove submit on editing 
  103. if( !empty( $_GET['modal'] ) && $_GET['modal'] == 'view_entry' && !empty( $_GET['group'] ) && $_GET['group'] == 'editentry' ) { 
  104. if( !empty( $form['fields'] ) ) { 
  105. foreach( $form['fields'] as $field_id=>$field ) { 
  106. if( $field['type'] == 'button' && $field['config']['type'] == 'submit' ) { 
  107. unset( $form['fields'][ $field_id ] ); 
  108.  
  109. return $form; 
  110.  
  111. /** 
  112. * Get registry of forms 
  113. * @since 1.3.4 
  114. * @param bool|false $with_details Optional. If false, the default, just form IDs are returned. If true, basic details of each are returned. 
  115. * @param bool|false $internal_only Optional. If false, the default, all forms -- in DB and in files system -- are returned -- If true, only those in DB are returned. 
  116. * @return array|mixed|void 
  117. */ 
  118. public static function get_forms( $with_details = false, $internal_only = false ) { 
  119. if( isset( $_GET[ 'cf-cache-clear' ] ) ) { 
  120. self::clear_cache(); 
  121.  
  122. if( $with_details ) { 
  123. if( ! empty( self::$registry_cache ) ) { 
  124. return self::$registry_cache; 
  125. }elseif( false != ( self::$registry_cache = get_transient( self::$registry_cache_key ) ) ) { 
  126. if ( is_array( self::$registry_cache ) ) { 
  127. return self::$registry_cache; 
  128.  
  129.  
  130.  
  131. if ( empty( self::$index ) ) { 
  132. $base_forms = self::get_stored_forms(); 
  133. if ( true === $internal_only ) { 
  134. return $base_forms; 
  135.  
  136. /** 
  137. * Runs after getting internal forms, use to add forms defined in file system 
  138. * @since unknown 
  139. * @param array $base_forms Forms saved in DB 
  140. */ 
  141. $forms = apply_filters( 'caldera_forms_get_forms', $base_forms ); 
  142. if ( ! empty( $forms ) && is_array( $forms )) { 
  143. foreach ( $forms as $form_id => $form ) { 
  144. $forms[ $form_id ] = $form_id; 
  145.  
  146. self::$index = $forms; 
  147.  
  148. }else{ 
  149. $forms = self::$index; 
  150.  
  151. if( $with_details ) { 
  152. $forms = self::add_details( $forms ); 
  153.  
  154. if( ! is_array( $forms ) ) { 
  155. $forms = array(); 
  156.  
  157. return $forms; 
  158.  
  159.  
  160. /** 
  161. * Get forms stored in DB 
  162. * @since 1.3.4 
  163. * @return array|void 
  164. */ 
  165. protected static function get_stored_forms() { 
  166. if ( empty( self::$stored_forms ) ) { 
  167. self::$stored_forms = get_option( self::$registry_option_key, array() ); 
  168.  
  169. return self::$stored_forms; 
  170.  
  171. /** 
  172. * Import form 
  173. * @since 1.3.4 
  174. * @param array $data Form config 
  175. * @return string Form ID 
  176. */ 
  177. public static function import_form( $data ) { 
  178. $forms = self::get_forms(); 
  179. if ( isset( $data[ 'ID' ] ) && array_key_exists( $data[ 'ID' ], $forms ) ) { 
  180. // generate a new ID 
  181. $data[ 'ID' ] = uniqid( 'CF' ); 
  182.  
  183.  
  184. if( isset( $data[ 'ID' ] ) ) { 
  185. $id = $data[ 'ID' ]; 
  186. }else{ 
  187. $id = $data[ 'ID' ] = uniqid( 'CF' ); 
  188.  
  189.  
  190. $data[ 'ID' ] = trim( $id ); 
  191.  
  192. $new_form = self::save_form( $data ); 
  193. if( is_array( $new_form ) && isset( $new_form[ 'ID' ] ) ) { 
  194. $new_form = $new_form[ 'ID' ]; 
  195.  
  196. return $new_form; 
  197.  
  198.  
  199. /** 
  200. * Use to switch form arrays to ID strings 
  201. * @since 1.3.4 
  202. * @param array|int $val Index to convert 
  203. * @return string 
  204. */ 
  205. protected function force_string( $val ) { 
  206. if( is_array( $val ) ) { 
  207. $val = $val[ 'ID' ]; 
  208.  
  209. return $val; 
  210.  
  211. /** 
  212. * Add details to form registry 
  213. * @since 1.3.4 
  214. * @param array $forms 
  215. * @return array 
  216. */ 
  217. protected static function add_details( $forms ) { 
  218.  
  219. $valid_forms = array(); 
  220.  
  221. foreach( $forms as $id => $form ) { 
  222. $_form = self::get_form( $id ); 
  223. if( empty( $_form ) ) { 
  224. //if its empty, there is no form. we can't just make up stuff. 
  225. continue; 
  226.  
  227. $valid_forms[ $id ] = array(); 
  228. foreach( self::$detail_fields as $key ) { 
  229. if ( isset( $_form[ $key ] ) ) { 
  230. $valid_forms[ $id ][ $key ] = $_form[ $key ]; 
  231. } elseif ( 'name' == $key ) { 
  232. $valid_forms[ $id ][ $key ] = $id; 
  233. } elseif ( 'mailer' == $key ) { 
  234. $valid_forms[ $id ][ $key ] = array( 'on_insert' => 1 ); 
  235. } elseif ( in_array( $key, array( 'form_ajax', 'check_honey', 'hide_form', 'db_support' ) ) ) { 
  236. $valid_forms[ $id ][ $key ] = 1; 
  237. } elseif( 'form_draft' == $key ) { 
  238. $valid_forms[ $id ][ $key ] = 0; 
  239. }else { 
  240. $valid_forms[ $id ][ $key ] = ''; 
  241.  
  242.  
  243.  
  244.  
  245. $base_forms = self::get_stored_forms(); 
  246.  
  247. foreach ( $valid_forms as $form_id => $form ) { 
  248. if ( ! isset( $base_forms[ $form_id ] ) ) { 
  249. $forms[ $form_id ][ '_external_form' ] = true; 
  250. if ( empty( $forms[ $form_id ][ 'ID' ] ) ) { 
  251. $valid_forms[ $form_id ][ 'ID' ] = $form_id; 
  252.  
  253.  
  254. if ( ! empty( $valid_forms ) ) { 
  255. set_transient( self::$registry_cache_key, $valid_forms, HOUR_IN_SECONDS ); 
  256.  
  257. self::$registry_cache = $valid_forms; 
  258. return self::$registry_cache; 
  259.  
  260.  
  261. /** 
  262. * Save a form 
  263. * @since 1.3.4 
  264. * @param array $data 
  265. * @return string|bool Form ID if updated, false if not 
  266. */ 
  267. public static function save_form( $data ) { 
  268.  
  269. // option value labels 
  270. if(!empty($data['fields'])) { 
  271. foreach($data['fields'] as &$field) { 
  272. if(!empty($field['config']['option']) && is_array($field['config']['option'])) { 
  273. foreach($field['config']['option'] as &$option) { 
  274. if(!isset($option['value'])) { 
  275. $option['value'] = $option['label']; 
  276.  
  277. // combine structure pages 
  278. if ( isset( $data['layout_grid']['structure'] ) && is_array( $data['layout_grid']['structure'] ) ) { 
  279. $data[ 'layout_grid' ][ 'structure' ] = implode( '#', $data[ 'layout_grid' ][ 'structure' ] ); 
  280. // remove fields from conditions 
  281. if( !empty( $data['conditional_groups']['fields'] ) ) { 
  282. unset( $data['conditional_groups']['fields'] ); 
  283. // remove magics ( yes, not used yet.) 
  284. if( !empty( $data['conditional_groups']['magic'] ) ) { 
  285. unset( $data['conditional_groups']['magic'] ); 
  286. // sanitize condition values 
  287. if( !empty( $data['conditional_groups']['conditions'] ) ) { 
  288. foreach( $data['conditional_groups']['conditions'] as $condition_id => &$condition ) { 
  289. if( !empty( $condition['group'] ) ) { 
  290. $condition['name'] = htmlentities( $condition['name'] ); 
  291. foreach ($condition['group'] as $group_id => &$group) { 
  292. foreach( $group as $case_id=>&$case ) {  
  293. $case['value'] = htmlentities( $case['value'] ); 
  294.  
  295. $data[ '_last_updated' ] = date('r'); 
  296. $data[ 'version' ] = CFCORE_VER; 
  297.  
  298. /** 
  299. * Filter form config directly before saving 
  300. * @since 1.4.0 
  301. * @param array $data Form config 
  302. */ 
  303. $data = apply_filters( 'caldera_forms_presave_form', $data ); 
  304.  
  305. // add form to registry 
  306. self::update_registry( $data[ "ID" ] ); 
  307.  
  308. // add from to list 
  309. $updated = update_option( $data['ID'], $data); 
  310.  
  311. /** 
  312. * Fires after a form is saved 
  313. * @since unknown 
  314. * @param array $data The form data 
  315. * @param string $from_id The form ID 
  316. */ 
  317. do_action('caldera_forms_save_form', $data, $data['ID']); 
  318.  
  319. if( $updated && isset( $data[ 'ID' ] ) ) { 
  320. $updated = $data[ 'ID' ]; 
  321.  
  322. return $updated; 
  323.  
  324. /** 
  325. * Create a new form 
  326. * @since 1.3.4 
  327. * @param array $newform Data for new form 
  328. * @return array|mixed|void 
  329. */ 
  330. public static function create_form( $newform ) { 
  331. require_once( CFCORE_PATH . 'classes/admin.php' ); 
  332.  
  333. // get form templates (PROBABLY NEED TO MOVE METHOD INTO THIS CLASS) 
  334. $form_templates = Caldera_Forms_Admin::internal_form_templates(); 
  335.  
  336.  
  337. if(!empty($newform['clone'])) { 
  338. $clone = $newform['clone']; 
  339. // load template if any 
  340. if( !empty( $newform['template'] ) ) { 
  341. if( isset( $form_templates[ $newform['template'] ] ) && !empty( $form_templates[ $newform['template'] ]['template'] ) ) { 
  342. $form_template = $form_templates[ $newform['template'] ]['template']; 
  343.  
  344. $forms = self::get_forms(); 
  345. if( ! isset( $newform[ 'ID' ] ) || ( ! isset( $newform[ 'ID' ] ) && array_key_exists( $newform[ 'ID' ], $forms ) ) ) { 
  346. $id = uniqid('CF'); 
  347. }else{ 
  348. $id = $newform[ 'ID' ]; 
  349.  
  350. $id = trim( $id ); 
  351. $defaults = array( 
  352. "ID" => $id,  
  353. "name" => '',  
  354. "description" => '',  
  355. "success" => __('Form has been successfully submitted. Thank you.', 'caldera-forms'),  
  356. "form_ajax" => 1,  
  357. "hide_form" => 1,  
  358. "check_honey" => 1,  
  359. "db_support" => 1,  
  360. 'mailer' => array( 'on_insert' => 1 ) 
  361. ); 
  362.  
  363. $newform = wp_parse_args( $newform, $defaults ); 
  364.  
  365. // is template? 
  366. if( !empty( $form_template ) && is_array( $form_template ) ) { 
  367. $newform = array_merge( $form_template, $newform ); 
  368.  
  369. /** 
  370. * Filter newly created form before saving 
  371. * @since unknown 
  372. * @param array $newform New form config 
  373. */ 
  374. $newform = apply_filters( 'caldera_forms_create_form', $newform); 
  375.  
  376.  
  377. self::update_registry( $id ); 
  378.  
  379. if(!empty($clone)) { 
  380. $clone_form = self::get_form( $clone ); 
  381. if(!empty($clone_form['ID']) && $clone == $clone_form['ID']) { 
  382. $newform = array_merge($clone_form, $newform); 
  383.  
  384. // add form to db 
  385. $added = add_option( $id, $newform, false ); 
  386. if( ! $added ) { 
  387. return false; 
  388.  
  389.  
  390. /** 
  391. * Runs after form is created 
  392. * @since unkown 
  393. * @param array $newform New form config 
  394. */ 
  395. do_action('caldera_forms_create_form', $newform); 
  396. return $newform; 
  397.  
  398. /** 
  399. * Delete a form 
  400. * @since 1.3.4 
  401. * @param string $id Form ID 
  402. * @return bol 
  403. */ 
  404. public static function delete_form( $id ) { 
  405. $forms = self::get_forms(); 
  406. if( ! isset( $forms[ $id ] ) ) { 
  407. return false; 
  408.  
  409. unset( $forms[ $id ] ); 
  410. $deleted = delete_option( $id ); 
  411. if ( $deleted ) { 
  412. self::update_registry( $forms ); 
  413.  
  414. return $deleted; 
  415.  
  416. return false; 
  417.  
  418. /** 
  419. * Update form registry 
  420. * @since 1.3.4 
  421. * @param string|array $new If is string, new index will be added, if is array, whole registry will be updated. 
  422. * @return bool 
  423. */ 
  424. protected static function update_registry( $new ) { 
  425. if( is_string( $new ) ) { 
  426. $forms = self::get_stored_forms(); 
  427. $forms[ $new ] = $new; 
  428. }elseif( is_array( $new ) ) { 
  429. $forms = $new; 
  430. }else{ 
  431. return false; 
  432.  
  433. update_option( self::$registry_option_key, $forms, false ); 
  434. self::clear_cache(); 
  435. self::$index = $forms; 
  436.  
  437. /** 
  438. * Fires after form registry is updated by saving a from 
  439. * @since unknown 
  440. * @param array $deprecated 
  441. * @param array $forms Array of forms in registry 
  442. */ 
  443. do_action('caldera_forms_save_form_register', array(), $forms ); 
  444.  
  445.  
  446. /** 
  447. * Clear the caching performed by this class 
  448. * @since 1.3.4 
  449. */ 
  450. protected static function clear_cache() { 
  451. self::$index = array(); 
  452. self::$registry_cache = array(); 
  453. self::$stored_forms = array(); 
  454. wp_cache_delete( '_caldera_forms_forms', 'options' ); 
  455. delete_transient( self::$registry_cache_key ); 
  456.  
  457. /** 
  458. * Check if a form is stored in DB by name oir ID 
  459. * @since 1.3.4 
  460. * @param string $id_name Form name or ID 
  461. * @return bool 
  462. */ 
  463. public static function is_internal_form( $id_name ) { 
  464. return in_array( $id_name, self::get_stored_forms() ); 
  465.  
  466. /** 
  467. * Change a form's state form enabled to disabled or vise vera 
  468. * @since 1.3.5 
  469. * @param array $form Form config. 
  470. * @param bool|true $enable Optional. If true, enable form, if false, disable form. 
  471. */ 
  472. public static function form_state( $form, $enable = true ) { 
  473. if( $enable ) { 
  474. $form['form_draft'] = 0; 
  475.  
  476. }else{ 
  477. $form['form_draft'] = 1; 
  478.  
  479.  
  480. if( is_array( self::$registry_cache ) && isset( self::$registry_cache[ $form[ 'ID' ] ] ) ) { 
  481. delete_transient( self::$registry_cache_key ); 
  482. self::$registry_cache[ $form[ 'ID' ] ][ 'form_draft' ] = $form['form_draft']; 
  483.  
  484. self::save_form( $form ); 
  485.  
  486.  
  487. /** 
  488. * Get all fields of a form 
  489. * @since 1.4.4 
  490. * @param array $form The form config 
  491. * @param bool $in_order Optional. Return in layout order, the default, or in stored order (false). 
  492. * @return array|mixed 
  493. */ 
  494. public static function get_fields( array $form, $in_order = true ) { 
  495. if( empty( $form[ 'fields' ] ) ) { 
  496. return array(); 
  497.  
  498. $fields = $form[ 'fields' ]; 
  499.  
  500. if ( $in_order ) { 
  501.  
  502. if( isset( $form[ 'layout_grid' ][ 'fields' ] ) ) { 
  503. $order = array_keys( $form[ 'layout_grid' ][ 'fields' ] ); 
  504. }else{ 
  505. $order = array_keys( $fields ); 
  506.  
  507. /** 
  508. * Change order of fields 
  509. * Very useful for reordering fields outputted with {summary} magic tag 
  510. * @since 1.4.5 
  511. * @param array $order Order -- array of field IDs 
  512. * @param array $form Form config 
  513. */ 
  514. $order = apply_filters( 'caldera_forms_get_field_order', $order, $form ); 
  515.  
  516. $ordered = array(); 
  517. foreach ( $order as $key ) { 
  518. if ( isset( $fields[ $key ] ) ) { 
  519. $ordered[ $key ] = $fields[ $key ]; 
  520.  
  521.  
  522. return $ordered; 
  523.  
  524. }else{ 
  525. return $fields; 
  526.  
  527.  
  528.  
  529. /** 
  530. * Get entry list fields of a form 
  531. * @since 1.5.0 
  532. * @param array $form Form config 
  533. * @param bool $configs Optional. If true, field config arrays are returned. If false, the default, field IDs are returned 
  534. * @return array 
  535. */ 
  536. public static function entry_list_fields( array $form, $configs = false ) { 
  537. $fields = self::get_fields( $form ); 
  538. $entry_list_fields = array(); 
  539. foreach ( $fields as $field_id => $field ) { 
  540. if( ! empty( $field[ 'entry_list'])) { 
  541. if ( $configs ) { 
  542. $entry_list_fields[ $field_id ] = $field; 
  543. }else{ 
  544. $entry_list_fields[] = $field_id; 
  545.  
  546. return $entry_list_fields; 
  547.