Caldera_Forms

Caldera_Forms Plugin class.

Defined (1)

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

/classes/core.php  
  1. class Caldera_Forms { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. const VERSION = CFCORE_VER; 
  7.  
  8. /** 
  9. * @since 1.4.2 
  10. */ 
  11. const PLUGIN_SLUG = 'caldera-forms'; 
  12.  
  13. /** 
  14. * @var string 
  15. */ 
  16. protected $plugin_slug = 'caldera-forms'; 
  17. /** 
  18. * @var string 
  19. */ 
  20. protected $screen_prefix = null; 
  21. /** 
  22. * @var object 
  23. */ 
  24. protected static $instance = null; 
  25.  
  26. /** 
  27. * Used to track if system has initialized and prevent recursion in init_cf_internal() method. 
  28. * @since 1.3.5 
  29. * @var bool 
  30. */ 
  31. private static $internal_init = false; 
  32.  
  33. /** 
  34. * Holds modal HTML to be loaded in footer 
  35. * @deprecated 1.5.0.7 
  36. * @since 1.4.2 
  37. * @var string 
  38. */ 
  39. protected static $footer_modals; 
  40.  
  41. /** 
  42. * CF-API v2 
  43. * @since 1.4.4 
  44. * @var Caldera_Forms_API_Load 
  45. */ 
  46. protected static $api; 
  47.  
  48. /** 
  49. * Initialize the plugin by setting localization, filters, and administration functions. 
  50. */ 
  51. function __construct() { 
  52.  
  53. // Load plugin text domain 
  54. add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); 
  55.  
  56. add_action( 'template_redirect', array( $this, 'api_handler' ) ); 
  57.  
  58. // add element & fields filters 
  59. add_filter( 'caldera_forms_get_form_processors', array( $this, 'get_form_processors' ) ); 
  60. add_filter( 'caldera_forms_submit_redirect_complete', array( $this, 'do_redirect' ), 10, 4 ); 
  61. add_action( 'caldera_forms_edit_end', array( $this, 'calculations_templates' ) ); 
  62. add_filter( 'caldera_forms_render_get_field', array( $this, 'auto_populate_options_field' ), 10, 2 ); 
  63. add_filter( 'caldera_forms_render_get_field', array( $this, 'apply_conditional_groups' ), 10, 2 ); 
  64. add_filter( 'caldera_forms_view_field_paragraph', 'wpautop' ); 
  65.  
  66. //mailer magic 
  67. add_filter( 'caldera_forms_get_magic_tags', array( $this, 'set_magic_tags' ), 1 ); 
  68. add_filter( 'caldera_forms_mailer', array( $this, 'mail_attachment_check' ), 10, 3 ); 
  69.  
  70. // action 
  71. add_action( 'caldera_forms_submit_complete', array( $this, 'save_final_form' ), 50 ); 
  72.  
  73. // find if profile is loaded 
  74. add_action( 'wp_loaded', array( $this, 'cf_init_system' ), 25 ); 
  75. add_action( 'wp', array( $this, 'cf_init_preview' ) ); 
  76.  
  77. // render shortcode 
  78. add_shortcode( 'caldera_form', array( $this, 'shortcode_handler' ) ); 
  79. // modal shortcode 
  80. add_shortcode( 'caldera_form_modal', array( $this, 'shortcode_handler' ) ); 
  81. add_action( 'wp_footer', array( 'Caldera_Forms_Render_Modals', 'render_footer_modals' ) ); 
  82.  
  83. //filter shortcode atts for defaults 
  84. add_filter( 'shortcode_atts_caldera_form', array( 'Caldera_Forms_Shortcode_Atts', 'allow_default_set' ), 5, 4 ); 
  85. add_filter( 'shortcode_atts_caldera_form_modal', array( 'Caldera_Forms_Shortcode_Atts', 'allow_default_set' ), 5, 4 ); 
  86.  
  87. //emails 
  88. add_action( 'caldera_forms_core_init', array( 'Caldera_Forms_Email_Settings', 'maybe_add_hooks' ) ); 
  89. add_action( 'caldera_forms_admin_footer', array( 'Caldera_Forms_Email_Settings', 'ui' ) ); 
  90. add_filter( 'pre_update_option__caldera_forms_email_api_settings', array( 
  91. 'Caldera_Forms_Email_Settings',  
  92. 'sanitize_save' 
  93. ) ); 
  94. if ( current_user_can( Caldera_Forms::get_manage_cap( 'admin' ) ) ) { 
  95. add_action( 'wp_ajax_cf_email_save', array( 'Caldera_Forms_Email_Settings', 'save' ) ); 
  96.  
  97. //auto-population via Easy Pods/ Easy Queries 
  98. add_action( 'caldera_forms_render_start', array( __CLASS__, 'easy_pods_queries_setup' ) ); 
  99.  
  100. //delete file uploads that are not going in media library 
  101. add_action( 'caldera_forms_submit_complete', array( 'Caldera_Forms_Files', 'cleanup' ) ); 
  102. add_action( Caldera_Forms_Files::CRON_ACTION, array( 'Caldera_Forms_Files', 'cleanup_via_cron' ) ); 
  103.  
  104. //entry permission 
  105. add_filter( 'caldera_forms_manage_cap', array( 'Caldera_Forms_Entry_UI', 'permissions_filter' ), 9, 3 ); 
  106.  
  107. if( current_user_can( Caldera_Forms::get_manage_cap( 'admin' ) ) ) { 
  108. $id = null; 
  109.  
  110. $view = false; 
  111. if ( isset( $_GET[ 'cf-email-preview' ], $_GET[ 'cf-email-preview-form' ] ) ) { 
  112. if ( wp_verify_nonce( $_GET[ 'cf-email-preview' ], $_GET[ 'cf-email-preview-form' ] ) ) { 
  113. $id = $_GET[ 'cf-email-preview-form' ]; 
  114. $view = true; 
  115.  
  116. new Caldera_Forms_Email_Previews( $id, $view ); 
  117.  
  118.  
  119. //check for forms on page 
  120. add_action( 'template_redirect', array( $this, 'maybe_load_styles' ) ); 
  121.  
  122. //format email 
  123. add_filter( 'caldera_forms_mailer', array( $this, 'format_message' ) ); 
  124.  
  125. /** Entry Viewer v1 */ 
  126. add_action( 'wp_ajax_browse_entries', array( Caldera_Forms_Entry_UI::get_instance(), 'view_entries' ) ); 
  127. add_action( 'wp_ajax_get_entry', array( Caldera_Forms_Entry_UI::get_instance(), 'view_entry' ) ); 
  128. add_action( 'caldera_forms_entry_actions', array( Caldera_Forms_Entry_UI::get_instance(), 'get_entry_actions'), 1); 
  129.  
  130. add_action( 'rest_api_init', array( __CLASS__, 'init_rest_api' ) ); 
  131.  
  132. //entry viewer shortcode 
  133. add_shortcode( Caldera_Forms_Entry_Shortcode::get_shortcode_name(), array( 'Caldera_Forms_Entry_Shortcode', 'shortcode_callback' ) ); 
  134.  
  135. //init Credit card # hash class here, not on hook, so it can't be unhooked 
  136. new Caldera_Forms_Field_Credit; 
  137.  
  138. /** Load magic tag system */ 
  139. new Caldera_Forms_Magic(); 
  140.  
  141. //clear syncer cache on form update 
  142. add_action( 'caldera_forms_save_form', array( 'Caldera_Forms_Sync_Factory', 'clear_cache' ) ); 
  143.  
  144.  
  145.  
  146. /** 
  147. * Runs after Caldera Forms core is initialized 
  148. * @since 1.3.5.3 
  149. */ 
  150. do_action( 'caldera_forms_core_init' ); 
  151.  
  152. /** Adding anything to this constructor after caldera_forms_core_init action is a violation of intergalactic law */ 
  153.  
  154.  
  155. /** 
  156. * Load a form by ID or name 
  157. * @param string $id_name ID or name of form. 
  158. * @return array|null Form config array if found. If not null. 
  159. */ 
  160. public static function get_form( $id_name ) { 
  161. return Caldera_Forms_Forms::get_form( $id_name ); 
  162.  
  163. /** 
  164. * Load all forms 
  165. * @param bool $internal Optional. If false, the default, all forms are returned. If true, only those saved in DB are returned. 
  166. * @return mixed|void 
  167. */ 
  168. public static function get_forms( $internal = false ) { 
  169. return Caldera_Forms_Forms::get_forms( true, $internal ); 
  170.  
  171.  
  172. /** 
  173. * Load styles early if shortcodes or widgets are used on page 
  174. * @uses "template_redirect" action 
  175. * @since 1.5.0 
  176. */ 
  177. public static function maybe_load_styles() { 
  178. /** 
  179. * Use this filter to force Caldera Forms styles to enqueue early -- IE force them into header 
  180. * @since 1.5.0 
  181. * @param bool $use To use or not 
  182. */ 
  183. $use = apply_filters( 'caldera_forms_force_enqueue_styles_early', false ); 
  184. $on_page = self::check_for_forms_on_page(); 
  185. if( $use || $on_page ) { 
  186. add_action( 'wp_enqueue_scripts', array( 'Caldera_Forms_Render_Assets' , 'optional_style_includes' ) ); 
  187.  
  188.  
  189.  
  190. /** 
  191. * Check if there are forms on page 
  192. * @since 1.5.0 
  193. * @param WP_Post $post Optional, current post object 
  194. * @return bool 
  195. */ 
  196. public static function check_for_forms_on_page( $post = null ) { 
  197. if( is_null( $post ) ) { 
  198. global $post; 
  199.  
  200. if ( ! isset( $post->post_content ) ) { 
  201. return false; 
  202.  
  203. $has_shortcode = has_shortcode( $post->post_content, 'caldera_form' ); 
  204. if( $has_shortcode ) { 
  205. return true; 
  206.  
  207. $has_shortcode = has_shortcode( $post->post_content, 'caldera_form_modal' ); 
  208. if( $has_shortcode ) { 
  209. return true; 
  210.  
  211.  
  212. // check active widgets 
  213. $sidebars = get_option( 'sidebars_widgets' ); 
  214. if ( is_array( $sidebars ) && ! empty( $sidebars ) ) { 
  215. $form_widgets = get_option( 'widget_caldera_forms_widget' ); 
  216. unset( $sidebars[ 'wp_inactive_widgets' ] ); 
  217. foreach ( $sidebars as $sidebar => $set ) { 
  218. if ( is_active_sidebar( $sidebar ) ) { 
  219. foreach ( $set as $setup ) { 
  220. if ( false !== strpos( $setup, 'caldera_forms_widget-' ) ) { 
  221. $widget_instance = str_replace( 'caldera_forms_widget-', '', $setup ); 
  222. if ( ! empty( $form_widgets[ $widget_instance ][ 'form' ] ) ) { 
  223. return true; 
  224.  
  225.  
  226. return false; 
  227.  
  228.  
  229. /** 
  230. * Create a modal button's HTML 
  231. * @since 1.5.0.4 
  232. * @deprecated 1.5.0.7 
  233. * @param array $atts Form atts 
  234. * @param string $content Content for opener 
  235. * @param array $form Form config 
  236. * @return string 
  237. */ 
  238. protected static function modal_button( $atts, $content, $form, $modal_id ) { 
  239. _deprecated_function( __METHOD__, '1.5.0.7', 'Caldera_Forms_Render_Modals::modal_button' ); 
  240. return Caldera_Forms_Render_Modals::modal_button( $atts, $content, $form, $modal_id ); 
  241.  
  242. /** 
  243. * Load a field from form 
  244. * @since 1.4.2 
  245. * @param array $form Form config 
  246. * @param string $field_base_id Field ID 
  247. * @return array 
  248. */ 
  249. public static function load_field( $form, $field_base_id ) { 
  250. if ( ! empty( $form[ 'fields' ][ $field_base_id ] ) ) { 
  251. $field = $form[ 'fields' ][ $field_base_id ]; 
  252. } else { 
  253. //probably bad, but opportunity to defined using "caldera_forms_render_setup_field" 
  254. $field = array(); 
  255. /** 
  256. * Filter the field setup before render 
  257. * Note, $field might be empty, must be array after this or field will not render, which is useful for preventing render or creating field types at this filter. 
  258. * @since unknown 
  259. * @param string $notice Notices HTML 
  260. * @param array $config Form config 
  261. */ 
  262. $field = apply_filters( 'caldera_forms_render_setup_field', $field, $form ); 
  263.  
  264. return $field; 
  265.  
  266. /** 
  267. * Load the plugin text domain for translation. 
  268. */ 
  269. public function load_plugin_textdomain() { 
  270.  
  271. load_plugin_textdomain( $this->plugin_slug, false, basename( CFCORE_PATH ) . '/languages' ); 
  272.  
  273. /** 
  274. * Setup internals / AKA activate stuffs 
  275. */ 
  276. public static function init_cf_internal() { 
  277.  
  278. if ( false == self::$internal_init ) { 
  279. add_rewrite_tag( '%cf_api%', '([^&]+)' ); 
  280. add_rewrite_tag( '%cf_entry%', '([^&]+)' ); 
  281. // INIT API 
  282. add_rewrite_rule( '^cf-api/([^/]*)/([^/]*)/?', 'index.php?cf_api=$matches[1]&cf_entry=$matches[2]', 'top' ); 
  283. add_rewrite_rule( '^cf-api/([^/]*)/?', 'index.php?cf_api=$matches[1]', 'top' ); 
  284.  
  285. self::$internal_init = true; 
  286.  
  287.  
  288.  
  289.  
  290. // check update version 
  291. $db_version = get_option( 'CF_DB', 0 ); 
  292. $force_update = false; 
  293. if ( is_admin() && isset( $_GET[ 'cal_db_update' ] ) ) { // ensure that admin can only force update 
  294. $force_update = (bool) wp_verify_nonce( $_GET[ 'cal_db_update' ] ); 
  295.  
  296. include_once CFCORE_PATH . 'includes/updater.php'; 
  297.  
  298. if ( CF_DB > $db_version || $force_update ) { 
  299. if ( $db_version < 2 || $force_update ) { 
  300. caldera_forms_db_v2_update(); 
  301.  
  302. if ( $db_version < 4 || $force_update ) { 
  303. self::activate_caldera_forms( true ); 
  304. caldera_forms_write_db_flag( 4 ); 
  305.  
  306. }else{ 
  307. $version = caldera_forms_get_last_update_version(); 
  308. if ( empty( $version ) || version_compare( $version, CFCORE_VER ) !== 0 ) { 
  309. flush_rewrite_rules(); 
  310. update_option( '_calderaforms_lastupdate', CFCORE_VER ); 
  311.  
  312.  
  313.  
  314.  
  315.  
  316. /** 
  317. * Activate and setup plugin 
  318. * @param bool $force Optional. If true, tables are checked no matter what. Default is false 
  319. */ 
  320. public static function activate_caldera_forms( $force = false ) { 
  321. include_once CFCORE_PATH . 'includes/updater.php'; 
  322. $version = caldera_forms_get_last_update_version(); 
  323.  
  324. wp_schedule_event( time(), 'daily', 'caldera_forms_tracking_send_rows' ); 
  325. global $wpdb; 
  326.  
  327. // ensure urls are there 
  328. self::init_cf_internal(); 
  329.  
  330. // ensure rewrites 
  331. flush_rewrite_rules(); 
  332.  
  333. $charset_collate = ''; 
  334.  
  335. if ( ! empty( $wpdb->charset ) ) { 
  336. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 
  337.  
  338. if ( ! empty( $wpdb->collate ) ) { 
  339. $charset_collate .= " COLLATE $wpdb->collate"; 
  340.  
  341. /** 
  342. * Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that. 
  343. * As of WordPress 4.2, however, we moved to utf8mb4, which uses 4 bytes per character. This means that an index which 
  344. * used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters. 
  345. */ 
  346. $max_index_length = 191; 
  347.  
  348. $tables = $wpdb->get_results( "SHOW TABLES", ARRAY_A ); 
  349. foreach ( $tables as $table ) { 
  350. $alltables[] = implode( $table ); 
  351.  
  352. // meta table 
  353. if ( ! in_array( $wpdb->prefix . 'cf_form_entry_meta', $alltables ) ) { 
  354. // create meta tables 
  355. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  356.  
  357. $meta_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entry_meta` ( 
  358. `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  
  359. `entry_id` bigint(20) unsigned NOT NULL DEFAULT '0',  
  360. `process_id` varchar(255) DEFAULT NULL,  
  361. `meta_key` varchar(255) DEFAULT NULL,  
  362. `meta_value` longtext,  
  363. PRIMARY KEY (`meta_id`),  
  364. KEY `meta_key` (meta_key(" . $max_index_length . ")),  
  365. KEY `entry_id` (`entry_id`) 
  366. ) " . $charset_collate . ";"; 
  367.  
  368. dbDelta( $meta_table ); 
  369.  
  370.  
  371. // tracking table 
  372. if ( ! in_array( $wpdb->prefix . 'cf_tracking', $alltables ) ) { 
  373. // create meta tables 
  374. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  375. $tacking_table = "CREATE TABLE `" . $wpdb->prefix . "cf_tracking` ( 
  376. `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  
  377. `form_id` varchar(255) DEFAULT NULL,  
  378. `process_id` varchar(255) DEFAULT NULL,  
  379. PRIMARY KEY (`ID`) 
  380. ) " . $charset_collate . ";"; 
  381.  
  382. dbDelta( $tacking_table ); 
  383.  
  384.  
  385. //tracking meta 
  386. if ( ! in_array( $wpdb->prefix . 'cf_tracking_meta', $alltables ) ) { 
  387. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  388.  
  389. $meta_table = "CREATE TABLE `" . $wpdb->prefix . "cf_tracking_meta` ( 
  390. `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  
  391. `event_id` bigint(20) unsigned NOT NULL DEFAULT '0',  
  392. `meta_key` varchar(255) DEFAULT NULL,  
  393. `meta_value` longtext,  
  394. PRIMARY KEY (`meta_id`),  
  395. KEY `meta_key` (`meta_key`(" . $max_index_length . ")),  
  396. KEY `event_id` (`event_id`) 
  397. ) " . $charset_collate . ";"; 
  398.  
  399. dbDelta( $meta_table ); 
  400.  
  401.  
  402.  
  403. if ( ! in_array( $wpdb->prefix . 'cf_form_entries', $alltables ) || ! in_array( $wpdb->prefix . 'cf_form_entry_values', $alltables ) ) { 
  404. // create tables 
  405. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  406.  
  407. if ( ! in_array( $wpdb->prefix . 'cf_form_entries', $alltables ) ) { 
  408.  
  409. $entry_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entries` ( 
  410. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  411. `form_id` varchar(18) NOT NULL DEFAULT '',  
  412. `user_id` int(11) NOT NULL,  
  413. `datestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  414. `status` varchar(20) NOT NULL DEFAULT 'active',  
  415. PRIMARY KEY (`id`),  
  416. KEY `form_id` (`form_id`),  
  417. KEY `user_id` (`user_id`),  
  418. KEY `date_time` (`datestamp`),  
  419. KEY `status` (`status`) 
  420. ) " . $charset_collate . ";"; 
  421.  
  422.  
  423. dbDelta( $entry_table ); 
  424.  
  425. if ( ! in_array( $wpdb->prefix . 'cf_form_entry_values', $alltables ) ) { 
  426.  
  427. $values_table = "CREATE TABLE `" . $wpdb->prefix . "cf_form_entry_values` ( 
  428. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  429. `entry_id` int(11) NOT NULL,  
  430. `field_id` varchar(20) NOT NULL,  
  431. `slug` varchar(255) NOT NULL DEFAULT '',  
  432. `value` longtext NOT NULL,  
  433. PRIMARY KEY (`id`),  
  434. KEY `form_id` (`entry_id`),  
  435. KEY `field_id` (`field_id`),  
  436. KEY `slug` (`slug`(" . $max_index_length . ")) 
  437. ) " . $charset_collate . ";"; 
  438.  
  439. dbDelta( $values_table ); 
  440.  
  441. } else { 
  442. if ( $version >= '1.1.5' ) { 
  443. return; // only if 1.1.4 or lower 
  444. // check for field_id from 1.0.4 
  445. $columns = $wpdb->get_results( "SHOW COLUMNS FROM `" . $wpdb->prefix . "cf_form_entry_values`", ARRAY_A ); 
  446. $fields = array(); 
  447. foreach ( $columns as $column ) { 
  448. $fields[] = $column[ 'Field' ]; 
  449. if ( ! in_array( 'field_id', $fields ) ) { 
  450. $wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "cf_form_entry_values` ADD `field_id` varchar(20) NOT NULL AFTER `entry_id`;" ); 
  451. $wpdb->query( "CREATE INDEX `field_id` ON `" . $wpdb->prefix . "cf_form_entry_values` (`field_id`); " ); 
  452. // update all entries 
  453. $forms = $wpdb->get_results( "SELECT `id`, `form_id` FROM `" . $wpdb->prefix . "cf_form_entries`", ARRAY_A ); 
  454. $known = array(); 
  455. if ( ! empty( $forms ) ) { 
  456. foreach ( $forms as $form ) { 
  457. if ( ! isset( $known[ $form[ 'form_id' ] ] ) ) { 
  458. $config = Caldera_Forms_Forms::get_form( $form[ 'form_id' ] ); 
  459. if ( empty( $config ) ) { 
  460. continue; 
  461. $known[ $form[ 'form_id' ] ] = $config; 
  462. } else { 
  463. $config = $known[ $form[ 'form_id' ] ]; 
  464.  
  465. foreach ( $config[ 'fields' ] as $field_id => $field ) { 
  466. $wpdb->update( $wpdb->prefix . "cf_form_entry_values", array( 'field_id' => $field_id ), array( 
  467. 'entry_id' => $form[ 'id' ],  
  468. 'slug' => $field[ 'slug' ] 
  469. ) ); 
  470.  
  471. // add status 
  472. $columns = $wpdb->get_results( "SHOW COLUMNS FROM `" . $wpdb->prefix . "cf_form_entries`", ARRAY_A ); 
  473. $fields = array(); 
  474.  
  475. if ( ! in_array( 'status', $fields ) && $version < '1.2.0' ) { 
  476. $wpdb->query( "ALTER TABLE `" . $wpdb->prefix . "cf_form_entries` ADD `status` varchar(20) NOT NULL DEFAULT 'active' AFTER `datestamp`;" ); 
  477. $wpdb->query( "CREATE INDEX `status` ON `" . $wpdb->prefix . "cf_form_entries` (`status`); " ); 
  478.  
  479.  
  480.  
  481. /** 
  482. * View a star rating form value 
  483. * @param int $value Value for star ratring 
  484. * @param array $field Field config 
  485. * @param array $form Form config 
  486. * @return string HTML markup 
  487. */ 
  488. public static function star_rating_viewer( $value, $field, $form ) { 
  489.  
  490. $out = "<div style=\"color: " . $field[ 'config' ][ 'color' ] . "; font-size: 10px;display: inline;\" >"; 
  491. if ( ! empty( $field[ 'config' ][ 'number' ] ) ) { 
  492. for ( $i = 1; $i <= $field[ 'config' ][ 'number' ]; $i ++ ) { 
  493. $star = 'raty-' . $field[ 'config' ][ 'type' ] . '-off'; 
  494. if ( $i <= $value ) { 
  495. $star = 'raty-' . $field[ 'config' ][ 'type' ] . '-on'; 
  496. $out .= '<span data-alt="' . $i . '" class="' . $star . '" title="' . $i . '" style="margin-right: -2px;"></span> '; 
  497. $out .= '</div>'; 
  498.  
  499. return $out; 
  500.  
  501. /** 
  502. * Output markup for file fields 
  503. * @param array $value Saved file paths 
  504. * @param array $field Field config 
  505. * @param array $form Form config 
  506. * @return string 
  507. */ 
  508. public static function handle_file_view( $value, $field, $form ) { 
  509. $out = array(); 
  510. foreach ( (array) $value as $file_url ) { 
  511. $out[] = '<a href="' . $file_url . '" target="_blank">' . basename( $file_url ) . '</a>'; 
  512.  
  513. return implode( ', ', $out ); 
  514.  
  515.  
  516. /** 
  517. * Prepare email attachments 
  518. * @param array $mail Email data 
  519. * @param array $data Form data 
  520. * @param array $form For config 
  521. * @return array 
  522. */ 
  523. public static function mail_attachment_check( $mail, $data, $form) { 
  524. foreach ( Caldera_Forms_Forms::get_fields( $form, false ) as $field_id => $field ) { 
  525. if ( Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  526. if( ! Caldera_Forms_Files::should_attach( $field, $form ) ) { 
  527. continue; 
  528. $dir = wp_upload_dir(); 
  529. if ( isset( $data[ $field_id ] ) && is_array( $data[ $field_id ] ) ) { 
  530. foreach ( $data[ $field_id ] as $file ) { 
  531. $file = str_replace( $dir[ 'baseurl' ], $dir[ 'basedir' ], $file ); 
  532. if ( file_exists( $file ) ) { 
  533. $mail[ 'attachments' ][] = $file; 
  534. continue; 
  535. if ( isset( $data[ $field_id ] ) ) { 
  536. $file = $data[ $field_id ]; 
  537. } else { 
  538. $file = self::get_field_data( $field_id, $form ); 
  539. if ( is_array( $file ) ) { 
  540. foreach ( $file as $a_file ) { 
  541. $file = str_replace( $dir[ 'baseurl' ], $dir[ 'basedir' ], $file ); 
  542. if ( is_string( $a_file ) && file_exists( $a_file ) ) { 
  543. $mail[ 'attachments' ][] = $a_file; 
  544. continue; 
  545. } else { 
  546. $file = str_replace( $dir[ 'baseurl' ], $dir[ 'basedir' ], $file ); 
  547. if ( is_string( $file ) && file_exists( $file ) ) { 
  548. $mail[ 'attachments' ][] = $file; 
  549. } else { 
  550. if ( isset( $data[ $field_id ] ) && filter_var( $data[ $field_id ], FILTER_VALIDATE_URL ) ) { 
  551. $mail[ 'attachments' ][] = $data[ $field_id ]; 
  552. } elseif ( isset( $_POST[ $field_id ] ) && filter_var( $_POST[ $field_id ], FILTER_VALIDATE_URL ) && 0 === strpos( $_POST[ $field_id ], $dir[ 'url' ] ) ) { 
  553. $mail[ 'attachments' ][] = $_POST[ $field_id ]; 
  554. } else { 
  555. continue; 
  556. return $mail; 
  557.  
  558. /** 
  559. * Check a captcha 
  560. * @param string $value Attempted captcha value 
  561. * @param array $field Field config 
  562. * @param array $form Form config 
  563. * @return bool|\WP_Error True if valid, WP_Error if not 
  564. */ 
  565. public static function captcha_check( $value, $field, $form ) { 
  566. return true; 
  567.  
  568. if ( ! isset( $_POST[ 'g-recaptcha-response' ] ) || empty( $_POST[ 'g-recaptcha-response' ] ) ) { 
  569. return new WP_Error( 'error' ); 
  570.  
  571. $args = array( 
  572. 'secret' => $field[ 'config' ][ 'private_key' ],  
  573. 'response' => sanitize_text_field( $_POST[ 'g-recaptcha-response' ] ) 
  574. ); 
  575.  
  576. $request = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?' . build_query( $args ) ); 
  577. $result = json_decode( wp_remote_retrieve_body( $request ) ); 
  578. if ( empty( $result->success ) ) { 
  579. return new WP_Error( 'error', __( "The wasn't entered correct.", 'caldera-forms' ) . ' <a href="#" class="reset_' . sanitize_text_field( $_POST[ $field[ 'ID' ] ] ) . '">' . __( 'Reset', 'caldera-forms' ) . '<a>.' ); 
  580.  
  581.  
  582.  
  583. /** 
  584. * Update saved entry data for a field. 
  585. * @param array $field Field config 
  586. * @param int $entry_id The entry ID 
  587. * @param array $form Form config 
  588. */ 
  589. public static function update_field_data( $field, $entry_id, $form ) { 
  590. global $wpdb, $form; 
  591.  
  592. // is capture? 
  593. $field_type = Caldera_Forms_Field_Util::get_type( $field, $form ); 
  594. $not_support = Caldera_Forms_Fields::not_support( $field_type, 'entry_list' ); 
  595. if ( $not_support ) { 
  596. return; 
  597.  
  598. $new_data = self::get_field_data( $field[ 'ID' ], $form ); 
  599. $original_data = self::get_field_data( $field[ 'ID' ], $form, $entry_id ); 
  600.  
  601. if ( $original_data === $new_data ) { 
  602. // no change 
  603. return; 
  604.  
  605. if ( has_filter( 'caldera_forms_save_field' ) ) { 
  606. $new_data = apply_filters( 'caldera_forms_update_field', $new_data, $field, $form ); 
  607.  
  608. if ( has_filter( 'caldera_forms_save_field_' . $field[ 'type' ] ) ) { 
  609. $new_data = apply_filters( 'caldera_forms_update_field_' . $field[ 'type' ], $new_data, $field, $form ); 
  610.  
  611. if ( $original_data !== null ) { 
  612. $wpdb->delete( $wpdb->prefix . 'cf_form_entry_values', array( 
  613. 'entry_id' => $entry_id,  
  614. 'field_id' => $field[ 'ID' ] 
  615. ) ); 
  616.  
  617. foreach ( (array) $new_data as $entry_data ) { 
  618. // no entry - add first 
  619. $new_entry = array( 
  620. 'entry_id' => $entry_id,  
  621. 'field_id' => $field[ 'ID' ],  
  622. 'slug' => $field[ 'slug' ],  
  623. 'value' => $entry_data,  
  624. ); 
  625. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_values', $new_entry ); 
  626.  
  627.  
  628. /** 
  629. * Save entry data for a field. 
  630. * @param array $field Field config 
  631. * @param int $entry_id The entry ID 
  632. * @param array $form Form config 
  633. */ 
  634. public static function save_field_data( $field, $entry_id, $form ) { 
  635. global $wpdb, $form; 
  636.  
  637. if ( ! empty( $field[ 'conditions' ][ 'type' ] ) ) { 
  638. if ( ! self::check_condition( $field[ 'conditions' ], $form ) ) { 
  639. return; 
  640.  
  641. if ( ! isset( $field[ 'ID' ], $field[ 'slug' ] ) ) { 
  642. return; 
  643.  
  644. $data = self::get_field_data( $field[ 'ID' ], $form ); 
  645.  
  646. if ( empty( $data ) && 0 != $data ) { 
  647. return; 
  648.  
  649. foreach ( (array) $data as $key => $raw_entry ) { 
  650.  
  651. $entry = Caldera_Forms_Sanitize::sanitize( $raw_entry ); 
  652.  
  653. if ( has_filter( 'caldera_forms_save_field' ) ) { 
  654. $entry = apply_filters( 'caldera_forms_save_field', $entry, $field ); 
  655.  
  656. if ( has_filter( 'caldera_forms_save_field_' . $field[ 'type' ] ) ) { 
  657. $entry = apply_filters( 'caldera_forms_save_field_' . $field[ 'type' ], $entry, $field ); 
  658.  
  659. $field_item = array( 
  660. 'entry_id' => $entry_id,  
  661. 'field_id' => $field[ 'ID' ],  
  662. 'slug' => $field[ 'slug' ],  
  663. 'value' => self::do_magic_tags( $entry ) 
  664. ); 
  665.  
  666. // named key kets .key to slug 
  667. if ( ! is_int( $key ) ) { 
  668. // Keyed 
  669. $keyed = true; 
  670. $field_item[ 'slug' ] .= '.' . $key; 
  671. // Save 
  672. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_values', $field_item ); 
  673.  
  674. if ( ! empty( $keyed ) ) { 
  675.  
  676. if ( has_filter( 'caldera_forms_save_field_combined' . $field[ 'type' ] ) ) { 
  677. $data = apply_filters( 'caldera_forms_save_field_combined' . $field[ 'type' ], $entry, $field ); 
  678.  
  679. $field_item = array( 
  680. 'entry_id' => $entry_id,  
  681. 'field_id' => $field[ 'ID' ],  
  682. 'slug' => $field[ 'slug' ],  
  683. 'value' => json_encode( $data ) 
  684. ); 
  685. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_values', $field_item ); 
  686.  
  687.  
  688. /** 
  689. * Save final form data 
  690. * @param array $form Form config 
  691. * @return void|\WP_Error 
  692. */ 
  693. public static function save_final_form( $form ) { 
  694. global $transdata; 
  695.  
  696. $entryid = null; 
  697. // check submit type (new or update) 
  698. if ( isset( $_POST[ '_cf_frm_edt' ] ) ) { 
  699. // is edit 
  700. //check user can edit this item. 
  701. $user_id = get_current_user_id(); 
  702. $details = Caldera_Forms::get_entry_detail( $_POST[ '_cf_frm_edt' ], $form ); 
  703.  
  704. // check token 
  705. if ( isset( $_POST[ '_cf_frm_edt_tkn' ] ) ) { 
  706.  
  707. $validated = Caldera_Forms_Entry_Token::verify_token( $_POST[ '_cf_frm_edt_tkn' ], $details[ 'id' ], $form[ 'ID' ] ); 
  708. if ( is_wp_error( $validated ) ) { 
  709. return $validated; 
  710. } else { 
  711. $entry_id = (int) $details[ 'id' ]; 
  712. $edit_token = Caldera_Forms_Entry_Token::create_entry_token( $entry_id, $form[ 'ID' ] ); 
  713.  
  714.  
  715. } else { 
  716.  
  717. if ( ! empty( $user_id ) ) { 
  718. if ( ! empty( $details ) ) { 
  719. // check user can edit 
  720. if ( current_user_can( 'edit_posts' ) || $details[ 'user_id' ] === $user_id ) { 
  721. $entryid = $_POST[ '_cf_frm_edt' ]; 
  722. } else { 
  723. return new WP_Error( 'error', __( "Permission denied.", 'caldera-forms' ) ); 
  724.  
  725.  
  726.  
  727. if ( ! empty( $form[ 'db_support' ] ) ) { 
  728. Caldera_Forms_Save_Final::save_in_db( $form, $entryid ); 
  729.  
  730.  
  731. if ( self::should_send_mail( $form, $transdata ) ) { 
  732. Caldera_Forms_Save_Final::do_mailer( $form, $entryid ); 
  733.  
  734.  
  735.  
  736. /** 
  737. * Creates a send log to debug mailer problems 
  738. * @param object $phpmailer The phpmailer object 
  739. */ 
  740. public static function debug_mail_send( $phpmailer ) { 
  741. global $transdata, $wpdb; 
  742.  
  743. // this is a hack since there is not filter / action for a failed mail... yet 
  744. //$phpmailer->SMTPDebug = 3; 
  745. ob_start(); 
  746. $phpmailer->SMTPDebug = 3; 
  747. try { 
  748. $phpmailer->Send(); 
  749. } catch ( phpmailerException $e ) { 
  750. print_r( $phpmailer->ErrorInfo ); 
  751. print_r( $phpmailer ); 
  752. $phpmailer->SMTPDebug = 0; 
  753. $result = ob_get_clean(); 
  754.  
  755. $meta_entry = array( 
  756. 'entry_id' => $transdata[ 'entry_id' ],  
  757. 'process_id' => '_debug_log',  
  758. 'meta_key' => 'debug_log',  
  759. 'meta_value' => $result 
  760. ); 
  761.  
  762. $wpdb->insert( $wpdb->prefix . 'cf_form_entry_meta', $meta_entry ); 
  763.  
  764.  
  765.  
  766. /** 
  767. * Return an instance of this class. 
  768. * @return object A single instance of this class. 
  769. */ 
  770. public static function get_instance() { 
  771.  
  772. // If the single instance hasn't been set, set it now. 
  773. if ( null == self::$instance ) { 
  774. self::$instance = new self; 
  775.  
  776. return self::$instance; 
  777.  
  778. /** 
  779. * Change redirect notices using magic tags. 
  780. * @param array $notices Current notices 
  781. * @param array $form Form config 
  782. * @return array 
  783. */ 
  784. public static function override_redirect_notice( $notices, $form ) { 
  785.  
  786. if ( isset( $form[ 'processors' ] ) ) { 
  787. foreach ( $form[ 'processors' ] as $processor ) { 
  788. if ( $processor[ 'type' ] == 'form_redirect' ) { 
  789.  
  790. if ( isset( $processor[ 'conditions' ] ) && ! empty( $processor[ 'conditions' ][ 'type' ] ) ) { 
  791. if ( ! self::check_condition( $processor[ 'conditions' ], $form ) ) { 
  792. continue; 
  793.  
  794. $notices[ 'success' ][ 'note' ] = self::do_magic_tags( $processor[ 'config' ][ 'message' ] ); 
  795.  
  796. return $notices; 
  797.  
  798. /** 
  799. * Do the redirect 
  800. * @param string $referrer Reffering URL 
  801. * @param array $form Form config 
  802. * @param $processid 
  803. * @return string URL to redirect to. 
  804. */ 
  805. public static function do_redirect( $referrer, $form, $processid ) { 
  806. if ( isset( $form[ 'processors' ] ) ) { 
  807. foreach ( $form[ 'processors' ] as $processor ) { 
  808. if ( $processor[ 'type' ] == 'form_redirect' ) { 
  809.  
  810. if ( isset( $processor[ 'conditions' ] ) && ! empty( $processor[ 'conditions' ][ 'type' ] ) ) { 
  811. if ( ! self::check_condition( $processor[ 'conditions' ], $form ) ) { 
  812. continue; 
  813. if ( ! empty( $processor[ 'config' ][ 'url' ] ) ) { 
  814.  
  815. // set message 
  816. add_filter( 'caldera_forms_render_notices', array( 
  817. 'Caldera_Forms',  
  818. 'override_redirect_notice' 
  819. ), 10, 2 ); 
  820.  
  821. //passback urls 
  822. $referrer = parse_url( $referrer ); 
  823. $query_vars = array(); 
  824. if ( ! empty( $referrer[ 'query' ] ) ) { 
  825. parse_str( $referrer[ 'query' ], $referrer[ 'query' ] ); 
  826. if ( isset( $referrer[ 'query' ][ 'cf_su' ] ) ) { 
  827. unset( $referrer[ 'query' ][ 'cf_su' ] ); 
  828. $query_vars = array_merge( $query_vars, $referrer[ 'query' ] ); 
  829. // get vars in url 
  830. $base_redirect = self::do_magic_tags( $processor[ 'config' ][ 'url' ] ); 
  831. $redirect = parse_url( $base_redirect ); 
  832.  
  833. if ( ! empty( $redirect[ 'query' ] ) ) { 
  834. parse_str( $redirect[ 'query' ], $redirect[ 'query' ] ); 
  835. $base_redirect = explode( '?', $base_redirect, 2 ); 
  836. $query_vars = array_merge( $redirect[ 'query' ], $query_vars ); 
  837. $redirect = $base_redirect[ 0 ] . '?' . http_build_query( $query_vars ); 
  838. } else { 
  839. $redirect = $base_redirect . '?' . http_build_query( $query_vars ); 
  840.  
  841. return $redirect; 
  842.  
  843. return $referrer; 
  844.  
  845. /** 
  846. * Process the auto-responder 
  847. * @since unknown 
  848. * @param array $config Processor config 
  849. * @param array $form Form config 
  850. */ 
  851. public static function send_auto_response( $config, $form ) { 
  852. global $form; 
  853.  
  854. // new filter to alter the config. 
  855. $config = apply_filters( 'caldera_forms_autoresponse_config', $config, $form ); 
  856. // remove required bounds. 
  857. unset( $config[ '_required_bounds' ] ); 
  858.  
  859. $message = $config[ 'message' ]; 
  860. foreach ( $config as $tag => &$value ) { 
  861. if ( $tag !== 'message' ) { 
  862. $message = str_replace( '%' . $tag . '%', $value, $message ); 
  863. $value = self::do_magic_tags( $value ); 
  864. // set header 
  865. $headers[] = 'From: ' . $config[ 'sender_name' ] . ' <' . $config[ 'sender_email' ] . '>'; 
  866.  
  867. if( empty( $message ) ) { 
  868. $message = ' '; 
  869.  
  870. $html = false; 
  871. if ( ! isset( $config[ 'html' ] ) || true == $config[ 'html' ] ) { 
  872. $headers[] = "Content-type: text/html"; 
  873. $message = wpautop( self::do_magic_tags( $message ) ); 
  874. $html = true; 
  875.  
  876. } else { 
  877. $message = self::do_magic_tags( $message ); 
  878.  
  879. if( ! $html ) { 
  880. $message = strip_tags( $message ); 
  881.  
  882. // setup mailer 
  883. $subject = $config[ 'subject' ]; 
  884.  
  885. $email_message = array( 
  886. 'recipients' => array( 
  887. $config[ 'recipient_name' ] . ' <' . $config[ 'recipient_email' ] . '>' 
  888. ),  
  889. 'subject' => $subject,  
  890. 'message' => $message,  
  891. 'headers' => $headers,  
  892. 'attachments' => array() 
  893. ); 
  894.  
  895. if ( ! is_email( $config[ 'sender_email' ] ) ) { 
  896. $config[ 'sender_email' ] = get_option( 'admin_email' ); 
  897.  
  898. $email_message = apply_filters( 'caldera_forms_autoresponse_mail', $email_message, $config, $form ); 
  899.  
  900. if ( 'wp' !== Caldera_Forms_Email_Settings::get_method() ) { 
  901. $email_message[ 'from' ] = $email_message[ 'replyto' ] = $config[ 'sender_email' ]; 
  902. $email_message[ 'from_name' ] = $config[ 'sender_name' ]; 
  903. $email_message[ 'bcc' ] = $email_message[ 'csv' ] = false; 
  904.  
  905. Caldera_Forms_Save_Final::do_mailer( $form, null, null, $email_message ); 
  906.  
  907. return; 
  908.  
  909. do_action( 'caldera_forms_do_autoresponse', $config, $form ); 
  910.  
  911. // send mail 
  912. $sent = wp_mail( $email_message[ 'recipients' ], $email_message[ 'subject' ], implode( "\r\n", (array) $email_message[ 'message' ] ), implode( "\r\n", (array) $email_message[ 'headers' ] ), $email_message[ 'attachments' ] ); 
  913.  
  914. if ( ! $sent ) { 
  915. /** 
  916. * Fires if wp_mail returns false in autoresponder 
  917. * @since 1.2.3 
  918. * @param array $email_message Email data 
  919. * @param array $config Auto responder settings 
  920. * @param array $form The form config 
  921. */ 
  922. do_action( 'caldera_forms_autoresponder_failed', $email_message, $config, $form ); 
  923.  
  924.  
  925.  
  926. /** 
  927. * Load built-in form processors 
  928. * @param array $processors 
  929. * @return array 
  930. */ 
  931. public function get_form_processors( $processors ) { 
  932. $internal_processors = array( 
  933. 'auto_responder' => array( 
  934. "name" => __( 'Auto Responder', 'caldera-forms' ),  
  935. "description" => __( 'Sends out an auto response e-mail', 'caldera-forms' ),  
  936. "post_processor" => array( $this, 'send_auto_response' ),  
  937. "template" => CFCORE_PATH . "processors/auto_responder/config.php",  
  938. "default" => array( 
  939. 'subject' => __( 'Thank you for contacting us', 'caldera-forms' ) 
  940. ),  
  941. ),  
  942. 'form_redirect' => array( 
  943. "name" => __( 'Redirect', 'caldera-forms' ),  
  944. "description" => __( 'Redirects user to URL on successful submit', 'caldera-forms' ),  
  945. "template" => CFCORE_PATH . "processors/redirect/config.php",  
  946. "single" => false 
  947. ),  
  948. 'conditional_recipient' => array( 
  949. "name" => __( 'Conditional Recipient', 'caldera-forms' ),  
  950. "description" => __( 'Send email to different recipients depending on conditions', 'caldera-forms' ),  
  951. "template" => CFCORE_PATH . "processors/conditional_recipient/config.php",  
  952. 'post_processor' => array( 'Caldera_Forms_Processor_Conditional_Recipient', 'post_processor' ),  
  953. "single" => false 
  954. ),  
  955. 'increment_capture' => array( 
  956. "name" => __( 'Increment Value', 'caldera-forms' ),  
  957. "description" => __( 'Increment a value per entry.', 'caldera-forms' ),  
  958. "processor" => array( $this, 'increment_value' ),  
  959. "template" => CFCORE_PATH . "processors/increment/config.php",  
  960. "single" => true,  
  961. "conditionals" => false,  
  962. "magic_tags" => array( 
  963. 'increment_value' 
  964. ); 
  965. // akismet 
  966. $wp_api_key = get_option( 'wordpress_api_key' ); 
  967. if ( ! empty( $wp_api_key ) ) { 
  968. $internal_processors[ 'akismet' ] = array( 
  969. "name" => __( 'Akismet', 'caldera-forms' ),  
  970. "description" => __( 'Anti-spam filtering', 'caldera-forms' ),  
  971. "pre_processor" => array( $this, 'akismet_scanner' ),  
  972. "template" => CFCORE_PATH . "processors/akismet/config.php",  
  973. "single" => false,  
  974. ); 
  975.  
  976. if ( ! is_array( $processors ) || empty( $processors ) ) { 
  977. return $internal_processors; 
  978.  
  979. return array_merge( $processors, $internal_processors ); 
  980.  
  981.  
  982. /** 
  983. * Increment value using the incrimental value processor 
  984. * @since unknown 
  985. * @param array $config Processor config 
  986. * @param array $form Form config 
  987. * @return array Key is new value. 
  988. */ 
  989. public function increment_value( $config, $form ) { 
  990. $option = '_increment_' . $config[ 'processor_id' ]; 
  991. $field_id = $field_value = false; 
  992.  
  993. if ( ! empty( $config[ 'field' ] ) ) { 
  994. $field_id = $config[ 'field' ]; 
  995. $field_value = Caldera_Forms::get_field_data( $field_id, $form ); 
  996.  
  997. $saved_value = get_option( $option, $config[ 'start' ] ); 
  998.  
  999. if ( is_numeric( $field_value ) ) { 
  1000. $increment_value = $saved_value + $field_value; 
  1001. } else { 
  1002. $increment_value = $saved_value + 1; 
  1003.  
  1004. /** 
  1005. * Filter value for incremental processor 
  1006. * Runs after logic of incremental value is calculated, before is written to field value or tracking option 
  1007. * @since 1.4.5 
  1008. * @param int $increment_value New value 
  1009. * @param int $saved_value Previous value 
  1010. * @param array $config Processor config 
  1011. * @param array $form Form config 
  1012. */ 
  1013. $increment_value = apply_filters( 'caldera_forms_incremental_value', $increment_value, $saved_value, $config, $form ); 
  1014. update_option( $option, $increment_value ); 
  1015.  
  1016. if ( $field_id ) { 
  1017. self::set_field_data( $field_id, $increment_value, $form ); 
  1018.  
  1019. return array( 
  1020. 'increment_value' => $increment_value 
  1021. ); 
  1022.  
  1023.  
  1024.  
  1025. /** 
  1026. * Apply Akismets 
  1027. * @param array $config Processor config 
  1028. * @param array $form Form config 
  1029. * @return array 
  1030. */ 
  1031. static public function akismet_scanner( $config, $form ) { 
  1032. global $post; 
  1033.  
  1034. $wp_api_key = get_option( 'wordpress_api_key' ); 
  1035. if ( empty( $wp_api_key ) ) { 
  1036. return array( 'type' => 'error', 'note' => __( 'Akismet not setup.' ) ); 
  1037. // set permalink 
  1038. if ( $post->ID ) { 
  1039. $permalink = get_permalink( $post->ID ); 
  1040. } else { 
  1041. $permalink = get_home_url(); 
  1042. // is contact form or reg form 
  1043. $regform = self::get_processor_by_type( 'user_register', $form ); 
  1044. if ( ! empty( $regform ) ) { 
  1045. $type = 'signup'; 
  1046. } else { 
  1047. $type = 'contact-form'; 
  1048. // Call to comment check 
  1049. $data = array( 
  1050. 'blog' => get_home_url(),  
  1051. 'user_ip' => $_SERVER[ 'REMOTE_ADDR' ],  
  1052. 'user_agent' => $_SERVER[ 'HTTP_USER_AGENT' ],  
  1053. 'referrer' => $_SERVER[ 'HTTP_REFERER' ],  
  1054. 'permalink' => $permalink,  
  1055. 'comment_type' => $type,  
  1056. 'comment_author' => self::do_magic_tags( $config[ 'sender_name' ] ),  
  1057. 'comment_author_email' => self::do_magic_tags( $config[ 'sender_email' ] ) 
  1058. ); 
  1059.  
  1060. if ( ! empty( $config[ 'url' ] ) ) { 
  1061. $data[ 'comment_author_url' ] = self::do_magic_tags( $config[ 'url' ] ); 
  1062. }; 
  1063. if ( ! empty( $config[ 'content' ] ) ) { 
  1064. $data[ 'comment_content' ] = self::do_magic_tags( $config[ 'content' ] ); 
  1065. }; 
  1066.  
  1067. $request = http_build_query( $data ); 
  1068.  
  1069. $host = $http_host = $wp_api_key . '.rest.akismet.com'; 
  1070. $path = '/1.1/comment-check'; 
  1071. $port = 80; 
  1072. $akismet_ua = "WordPress/3.8.1 | Akismet/2.5.9"; 
  1073. $content_length = strlen( $request ); 
  1074. $http_request = "POST $path HTTP/1.0\r\n"; 
  1075. $http_request .= "Host: $host\r\n"; 
  1076. $http_request .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
  1077. $http_request .= "Content-Length: {$content_length}\r\n"; 
  1078. $http_request .= "User-Agent: {$akismet_ua}\r\n"; 
  1079. $http_request .= "\r\n"; 
  1080. $http_request .= $request; 
  1081. $response = ''; 
  1082. if ( false != ( $fs = @fsockopen( $http_host, $port, $errno, $errstr, 10 ) ) ) { 
  1083.  
  1084. fwrite( $fs, $http_request ); 
  1085.  
  1086. while ( ! feof( $fs ) ) { 
  1087. $response .= fgets( $fs, 1160 ); 
  1088. } // One TCP-IP packet 
  1089. fclose( $fs ); 
  1090.  
  1091. $response = explode( "\r\n\r\n", $response, 2 ); 
  1092.  
  1093.  
  1094. if ( 'true' == $response[ 1 ] ) { 
  1095. return array( 'type' => 'error', 'note' => self::do_magic_tags( $config[ 'error' ] ) ); 
  1096.  
  1097.  
  1098. /** 
  1099. * Process a calculation field. 
  1100. * @param string $value The calculation to run 
  1101. * @param array $field Field config 
  1102. * @param array $form Form config 
  1103. * @return int|string 
  1104. */ 
  1105. static public function run_calculation( $value, $field, $form ) { 
  1106. if( false === Caldera_Forms_Field_Util::check_conditional( $field, $form ) ) { 
  1107. return; 
  1108.  
  1109. $formula = $field[ 'config' ][ 'formular' ]; 
  1110.  
  1111. // get manual 
  1112. if ( ! empty( $field[ 'config' ][ 'manual' ] ) ) { 
  1113. $formula = $field[ 'config' ][ 'manual_formula' ]; 
  1114. preg_match_all( "/%(.+?)%/", $formula, $hastags ); 
  1115. if ( ! empty( $hastags[ 1 ] ) ) { 
  1116. $binds = array(); 
  1117.  
  1118. foreach ( $hastags[ 1 ] as $tag_key => $tag ) { 
  1119.  
  1120. foreach ( $form[ 'fields' ] as $key_id => $fcfg ) { 
  1121. if ( $fcfg[ 'slug' ] === $tag ) { 
  1122. $binds[] = '#' . $key_id; 
  1123. $bindfields[] = '"' . $key_id . '"'; 
  1124. $formula = str_replace( $hastags[ 0 ][ $tag_key ], $key_id, $formula ); 
  1125.  
  1126. if ( empty( $formula ) ) { 
  1127. return 0; 
  1128.  
  1129. $formula = self::do_magic_tags( $formula, null, $form ); 
  1130. if ( false !== strpos( $formula, 'Math.' ) ) { 
  1131. $formula = str_replace( 'Math.', '', $formula ); 
  1132. foreach ( $form[ 'fields' ] as $fid => $cfg ) { 
  1133. if ( false !== strpos( $formula, $fid ) ) { 
  1134. $entry_value = self::get_field_data( $fid, $form ); 
  1135.  
  1136. if ( is_array( $entry_value ) ) { 
  1137. $number = floatval( array_sum( $entry_value ) ); 
  1138. } else { 
  1139. $number = floatval( $entry_value ); 
  1140.  
  1141. $formula = str_replace( $fid, $number, $formula ); 
  1142.  
  1143. if( false !== strpos( $formula, '/0' ) ) { 
  1144. return new WP_Error( $field[ 'ID' ] . '-calculation', __( 'Calculation is invalid (division by zero)', 'caldera-forms' ) ); 
  1145.  
  1146. $total_function = create_function( null, 'return ' . $formula . ';' ); 
  1147. $total = $total_function(); 
  1148.  
  1149. if ( is_infinite( $total ) || ! is_numeric( $total ) ) { 
  1150. return new WP_Error( $field[ 'ID' ] . '-calculation', __( 'Calculation is invalid', 'caldera-forms' ) ); 
  1151.  
  1152. $total = Caldera_Forms_Field_Util::format_calc_field( $field, $total ); 
  1153.  
  1154. return $total; 
  1155.  
  1156. /** 
  1157. * Include the template for a calculation field 
  1158. * @return string HTML for field. 
  1159. */ 
  1160. static public function calculations_templates() { 
  1161. include CFCORE_PATH . "fields/calculation/line-templates.php"; 
  1162.  
  1163. /** 
  1164. * Check to see if the field is used in a calculation (use to display the label) 
  1165. * @param string $value Value to filter. 
  1166. * @param array $field Field config. 
  1167. * @param array $form Form congig. 
  1168. * @return array|string 
  1169. */ 
  1170. function filter_options_calculator( $value, $field, $form ) { 
  1171. // 
  1172. if ( ! empty( $form ) ) { 
  1173. foreach ( $form[ 'fields' ] as $field_id => $field_conf ) { 
  1174. if ( $field_conf[ 'type' ] !== 'calculation' ) { 
  1175. continue; 
  1176. // auto 
  1177. if ( ! empty( $field_conf[ 'config' ] ) ) { 
  1178. $binddown = json_encode( $field_conf[ 'config' ][ 'config' ] ); 
  1179. if ( false !== strpos( $binddown, $field[ 'ID' ] ) || false !== strpos( $field_conf[ 'config' ][ 'manual_formula' ], $field[ 'ID' ] ) ) { 
  1180. foreach ( $field[ 'config' ][ 'option' ] as $option_id => $option ) { 
  1181. if ( is_array( $value ) ) { 
  1182. if ( in_array( $option[ 'value' ], $value ) ) { 
  1183. $key = array_search( $option[ 'value' ], $value ); 
  1184. $value[ $key ] = $option[ 'label' ] . ' <small class="view_option_value">(' . $value[ $key ] . ')</small>'; 
  1185. } else { 
  1186. if ( $option[ 'value' ] == $value ) { 
  1187. return $option[ 'label' ] . ' <small class="view_option_value">(' . $value . ')</small>'; 
  1188.  
  1189. if ( is_array( $value ) ) { 
  1190. $value = implode( '<br>', $value ); 
  1191.  
  1192. return $value; 
  1193.  
  1194. /** 
  1195. * Applies the inline rules for fields conditionals 
  1196. * @param array $field Field config 
  1197. * @param array $form Form config 
  1198. * @return array Options for field 
  1199. */ 
  1200. public function apply_conditional_groups( $field, $form ) { 
  1201.  
  1202. if ( ! empty( $form[ 'conditional_groups' ][ 'conditions' ][ $field[ 'conditions' ][ 'type' ] ] ) ) { 
  1203. $group = $form[ 'conditional_groups' ][ 'conditions' ][ $field[ 'conditions' ][ 'type' ] ]; 
  1204. if ( ! isset( $field[ 'conditions' ][ 'group' ] ) ) { 
  1205. $field[ 'conditions' ][ 'group' ] = array(); 
  1206.  
  1207. if ( ! isset( $group[ 'group' ] ) ) { 
  1208. $group[ 'group' ] = array(); 
  1209.  
  1210. $field[ 'conditions' ][ 'type' ] = $group[ 'type' ]; 
  1211. $field[ 'conditions' ][ 'group' ] = $group[ 'group' ]; 
  1212.  
  1213. return $field; 
  1214.  
  1215. /** 
  1216. * Default callback for auto populating select fields 
  1217. * @param array $field Field config 
  1218. * @param array $form Form config 
  1219. * @return array Options for field 
  1220. */ 
  1221. public function auto_populate_options_field( $field, $form ) { 
  1222.  
  1223. if ( ! empty( $field[ 'config' ][ 'auto' ] ) ) { 
  1224. $field[ 'config' ][ 'option' ] = array(); 
  1225. switch ( $field[ 'config' ][ 'auto_type' ] ) { 
  1226. case 'post_type': 
  1227. case 'easy-query' : 
  1228.  
  1229. if ( ! isset( $field[ 'config' ][ 'orderby_post' ] ) ) { 
  1230. $field[ 'config' ][ 'orderby_post' ] = 'date'; 
  1231.  
  1232. if ( ! isset( $field[ 'config' ][ 'order' ] ) ) { 
  1233. $field[ 'config' ][ 'order' ] = 'ASC'; 
  1234.  
  1235. $args = array( 
  1236. 'post_type' => $field[ 'config' ][ 'post_type' ],  
  1237. 'post_status' => 'publish',  
  1238. 'posts_per_page' => - 1,  
  1239. 'order' => $field[ 'config' ][ 'order' ],  
  1240. 'orderby' => $field[ 'config' ][ 'orderby_post' ] 
  1241. ); 
  1242.  
  1243. /** 
  1244. * Modify arguments for WP_Query used to auto-populate post type fields 
  1245. * @since unknown 
  1246. * @param array $args Args for WP_Query 
  1247. * @param array $form Form config 
  1248. */ 
  1249. $args = apply_filters( 'caldera_forms_autopopulate_post_type_args', $args, $field ); 
  1250.  
  1251. $posts = get_posts( $args ); 
  1252.  
  1253. if ( $field[ 'config' ][ 'value_field' ] === 'id' ) { 
  1254. $field[ 'config' ][ 'value_field' ] = 'ID'; 
  1255. } elseif ( $field[ 'config' ][ 'value_field' ] === 'name' ) { 
  1256. $field[ 'config' ][ 'value_field' ] = 'post_name'; 
  1257.  
  1258. /** 
  1259. * Filter which field is used for the VALUE when getting autopopulate option values when autopopulating options from post types 
  1260. * Value can be any WP_Post field, or a meta key (be careful will return an empty string if that meta key isn't set for the post. 
  1261. * @since 1.2.2 
  1262. * @param string $field What field to use for the value. Default is "ID". 
  1263. * @param array $field Config for the field. 
  1264. * @param array $form Config for the form. 
  1265. * @param array $posts Current post collection. 
  1266. */ 
  1267. $field_for_value = apply_filters( 'caldera_forms_autopopulate_options_post_value_field', $field[ 'config' ][ 'value_field' ], $field, $form, $posts ); 
  1268. $field[ 'config' ][ 'value_field' ] = $field_for_value; 
  1269.  
  1270. /** 
  1271. * Filter which field is used for the LABEL when getting autopopulate option values when autopopulating options from post types 
  1272. * Value can be any WP_Post field, or a meta key (be careful will return an empty string if that meta key isn't set for the post. 
  1273. * @since 1.2.2 
  1274. * @param string $field What field to use for the label. Default is "post_title". 
  1275. * @param array $field Config for the field. 
  1276. * @param array $form Config for the form. 
  1277. * @param array $posts Current post collection. 
  1278. */ 
  1279. $field_for_label = apply_filters( 'caldera_forms_autopopulate_options_post_label_field', 'post_title', $field, $form, $posts ); 
  1280. foreach ( $posts as $post_item ) { 
  1281. $field[ 'config' ][ 'option' ][ $post_item->ID ] = array( 
  1282. 'value' => $post_item->{$field_for_value},  
  1283. 'label' => $post_item->{$field_for_label} 
  1284. ); 
  1285.  
  1286. break; 
  1287. case 'taxonomy': 
  1288. if ( $field[ 'config' ][ 'value_field' ] === 'id' ) { 
  1289. $field[ 'config' ][ 'value_field' ] = 'term_id'; 
  1290.  
  1291. if ( ! isset( $field[ 'config' ][ 'orderby_tax' ] ) ) { 
  1292. $field[ 'config' ][ 'orderby_tax' ] = 'count'; 
  1293.  
  1294. if ( ! isset( $field[ 'config' ][ 'order' ] ) ) { 
  1295. $field[ 'config' ][ 'order' ] = 'ASC'; 
  1296.  
  1297. $args = array( 
  1298. 'orderby' => $field[ 'config' ][ 'orderby_tax' ],  
  1299. 'order' => $field[ 'config' ][ 'order' ],  
  1300. 'hide_empty' => 0 
  1301. ); 
  1302.  
  1303.  
  1304. /** 
  1305. * Modify arguments for get_terms() used to auto-populate taxononmy type fields 
  1306. * @since unknown 
  1307. * @param array $args Args for get_terms() 
  1308. * @param array $form Form config 
  1309. */ 
  1310. $args = apply_filters( 'caldera_forms_autopopulate_taxonomy_args', $args ); 
  1311.  
  1312. $terms = get_terms( $field[ 'config' ][ 'taxonomy' ], $args ); 
  1313.  
  1314. /** 
  1315. * Filter which field is used for the VALUE when getting autopopulate option values when autopopulating options from post types 
  1316. * Value must be a standard taxonomy term field. 
  1317. * @since 1.2.2 
  1318. * @param string $field What field to use for the value. Default is "term_id". 
  1319. * @param array $field Config for the field. 
  1320. * @param array $form Config for the form. 
  1321. * @param array $posts Current term collection. 
  1322. */ 
  1323. $field_for_value = apply_filters( 'caldera_forms_autopopulate_options_taxonomy_value_field', $field[ 'config' ][ 'value_field' ], $field, $form, $terms ); 
  1324. $field[ 'config' ][ 'value_field' ] = $field_for_value; 
  1325.  
  1326. /** 
  1327. * Filter which field is used for the LABEL when getting autopopulate option values when autopopulating options from post types 
  1328. * Value must be a standard taxonomy term field. 
  1329. * @since 1.2.2 
  1330. * @param string $field What field to use for the label. Default is "name". 
  1331. * @param array $field Config for the field. 
  1332. * @param array $form Config for the form. 
  1333. * @param array $posts Current term collection. 
  1334. */ 
  1335. $field_for_label = apply_filters( 'caldera_forms_autopopulate_options_taxonomy_label_field', 'name', $field, $form, $terms ); 
  1336.  
  1337. foreach ( $terms as $term ) { 
  1338. $field[ 'config' ][ 'option' ][ $term->term_id ] = array( 
  1339. 'value' => $term->{$field_for_value},  
  1340. 'label' => $term->{$field_for_label} 
  1341. ); 
  1342. break; 
  1343.  
  1344.  
  1345. } else { 
  1346. $field = self::format_select_options( $field ); 
  1347.  
  1348. return $field; 
  1349.  
  1350.  
  1351.  
  1352. /** 
  1353. * Verify and format select options 
  1354. * @since 1.3.2 
  1355. * @param array $field Field config 
  1356. * @return array 
  1357. */ 
  1358. static public function format_select_options( $field ) { 
  1359.  
  1360.  
  1361. if ( ! empty( $field[ 'config' ][ 'option' ] ) ) { 
  1362. foreach ( $field[ 'config' ][ 'option' ] as &$option ) { 
  1363. if ( strlen( $option[ 'value' ] ) === 0 ) { 
  1364. $option[ 'value' ] = $option[ 'label' ] = self::do_magic_tags( $option[ 'label' ] ); 
  1365. } else { 
  1366. $option[ 'value' ] = self::do_magic_tags( $option[ 'value' ] ); 
  1367. $option[ 'label' ] = self::do_magic_tags( $option[ 'label' ] ); 
  1368.  
  1369.  
  1370. return $field; 
  1371.  
  1372.  
  1373. /** 
  1374. * Evaluate a conditional. 
  1375. * @param array $conditions Conditions. 
  1376. * @param array $form Form config. 
  1377. * @param null|int $entry_id Optional. Entry ID to test by. 
  1378. * @return bool 
  1379. */ 
  1380. static public function check_condition( $conditions, $form, $entry_id = null ) { 
  1381.  
  1382. $trues = array(); 
  1383. if ( empty( $conditions[ 'group' ] ) ) { 
  1384. return true; 
  1385.  
  1386. foreach ( $conditions[ 'group' ] as $groupid => $lines ) { 
  1387. $truelines = array(); 
  1388.  
  1389. foreach ( $lines as $lineid => $line ) { 
  1390.  
  1391. if ( isset( $form[ 'fields' ][ $line[ 'field' ] ][ 'config' ][ 'option' ][ $line[ 'value' ] ] ) ) { 
  1392. $line[ 'value' ] = $form[ 'fields' ][ $line[ 'field' ] ][ 'config' ][ 'option' ][ $line[ 'value' ] ][ 'value' ]; 
  1393.  
  1394. $line[ 'value' ] = self::do_magic_tags( $line[ 'value' ] ); 
  1395.  
  1396. $value = (array) self::get_field_data( $line[ 'field' ], $form, $entry_id ); 
  1397. if ( empty( $value ) ) { 
  1398. $value = array( '' ); 
  1399. // do field value replaces 
  1400. if ( false !== strpos( $line[ 'value' ], '%' ) ) { 
  1401. $isslug = self::get_slug_data( trim( $line[ 'value' ], '%' ), $form, $entry_id ); 
  1402. if ( $isslug !== null ) { 
  1403. $line[ 'value' ] = $isslug; 
  1404.  
  1405.  
  1406. $truelines[ $lineid ] = false; 
  1407.  
  1408. switch ( $line[ 'compare' ] ) { 
  1409. case 'is': 
  1410. if ( is_array( $value ) ) { 
  1411. if ( in_array( $line[ 'value' ], $value ) ) { 
  1412. $truelines[ $lineid ] = true; 
  1413. } else { 
  1414. if ( $value == $line[ 'value' ] ) { 
  1415. $truelines[ $lineid ] = true; 
  1416. break; 
  1417. case 'isnot': 
  1418. if ( is_array( $value ) ) { 
  1419. if ( ! in_array( $line[ 'value' ], $value ) ) { 
  1420. $truelines[ $lineid ] = true; 
  1421. } else { 
  1422. if ( $value != $line[ 'value' ] ) { 
  1423. $truelines[ $lineid ] = true; 
  1424. break; 
  1425. case '>': 
  1426. case 'greater': 
  1427. if ( is_array( $value ) ) { 
  1428. if ( array_sum( $value ) > $line[ 'value' ] ) { 
  1429. $truelines[ $lineid ] = true; 
  1430. } else { 
  1431. if ( $value > $line[ 'value' ] ) { 
  1432. $truelines[ $lineid ] = true; 
  1433. break; 
  1434. case '<': 
  1435. case 'smaller': 
  1436. if ( is_array( $value ) ) { 
  1437. if ( array_sum( $value ) < $line[ 'value' ] ) { 
  1438. $truelines[ $lineid ] = true; 
  1439. } else { 
  1440. if ( $value < $line[ 'value' ] ) { 
  1441. $truelines[ $lineid ] = true; 
  1442. break; 
  1443. case 'startswith': 
  1444. if ( is_array( $value ) ) { 
  1445. foreach ( $value as $part ) { 
  1446. if ( 0 === strpos( $part, $line[ 'value' ] ) ) { 
  1447. $truelines[ $lineid ] = true; 
  1448. } else { 
  1449. if ( substr( $value, 0, strlen( $line[ 'value' ] ) ) == $line[ 'value' ] ) { 
  1450. $truelines[ $lineid ] = true; 
  1451. break; 
  1452. case 'endswith': 
  1453. if ( is_array( $value ) ) { 
  1454. foreach ( $value as $part ) { 
  1455. if ( substr( $part, strlen( $part ) - strlen( $line[ 'value' ] ) ) == $line[ 'value' ] ) { 
  1456. $truelines[ $lineid ] = true; 
  1457. } else { 
  1458. if ( substr( $value, strlen( $value ) - strlen( $line[ 'value' ] ) ) == $line[ 'value' ] ) { 
  1459. $truelines[ $lineid ] = true; 
  1460. break; 
  1461. case 'contains': 
  1462. if ( is_array( $value ) ) { 
  1463. if ( false !== strpos( implode( '', $value ), $line[ 'value' ] ) ) { 
  1464. $truelines[ $lineid ] = true; 
  1465. } else { 
  1466. if ( false !== strpos( $value, $line[ 'value' ] ) ) { 
  1467. $truelines[ $lineid ] = true; 
  1468. break; 
  1469.  
  1470.  
  1471. $trues[ $groupid ] = in_array( false, $truelines ) ? false : true; 
  1472.  
  1473. if ( $conditions[ 'type' ] == 'use' || $conditions[ 'type' ] == 'show' ) { 
  1474. if ( in_array( true, $trues ) ) { 
  1475. return true; 
  1476. } elseif ( $conditions[ 'type' ] == 'not' || $conditions[ 'type' ] == 'hide' || $conditions[ 'type' ] == 'disable' ) { 
  1477. if ( ! in_array( true, $trues ) ) { 
  1478. return true; 
  1479.  
  1480. // false if nothing happens 
  1481. return false; 
  1482.  
  1483. // FRONT END STUFFF 
  1484. /** 
  1485. * Perform redirect 
  1486. * @param string $type Type of redirect being performed. 
  1487. * @param string $url URL to redirect to. 
  1488. * @param array $form Form config. 
  1489. * @param string $processid Process ID for process calling the redirect. 
  1490. */ 
  1491. static public function form_redirect( $type, $url, $form, $processid ) { 
  1492.  
  1493. $url = apply_filters( 'caldera_forms_redirect_url', $url, $form, $processid ); 
  1494. $url = apply_filters( 'caldera_forms_redirect_url_' . $type, $url, $form, $processid ); 
  1495.  
  1496. if ( headers_sent() ) { 
  1497. remove_action( 'caldera_forms_redirect', 'cf_ajax_redirect', 10 ); 
  1498.  
  1499. do_action( 'caldera_forms_redirect', $type, $url, $form, $processid ); 
  1500. do_action( 'caldera_forms_redirect_' . $type, $url, $form, $processid ); 
  1501.  
  1502. if ( ! empty( $url ) ) { 
  1503. cf_redirect( $url, 302 ); 
  1504. exit; 
  1505.  
  1506.  
  1507. /** 
  1508. * Add default magic tags 
  1509. * @param array $tags 
  1510. * @return array 
  1511. */ 
  1512. public function set_magic_tags( $tags ) { 
  1513.  
  1514. // get internal tags 
  1515. $system_tags = array( 
  1516. 'entry_id',  
  1517. 'entry_token',  
  1518. 'ip',  
  1519. 'user:id',  
  1520. 'user:user_login',  
  1521. 'user:first_name',  
  1522. 'user:last_name',  
  1523. 'user:user_email' => array( 
  1524. 'text',  
  1525. 'email' 
  1526. ),  
  1527. 'get:*',  
  1528. 'post:*',  
  1529. 'request:*',  
  1530. 'post_meta:*',  
  1531. 'embed_post:ID',  
  1532. 'embed_post:post_title',  
  1533. 'embed_post:permalink',  
  1534. 'embed_post:post_date' => array( 
  1535. 'text',  
  1536. 'date_picker' 
  1537. ),  
  1538. 'date:Y-m-d H:i:s' => array( 
  1539. 'text',  
  1540. 'date_picker' 
  1541. ),  
  1542. 'date:Y/m/d',  
  1543. 'date:Y/d/m',  
  1544. 'login_url',  
  1545. 'logout_url',  
  1546. 'register_url',  
  1547. 'lostpassword_url' 
  1548.  
  1549. ); 
  1550.  
  1551. $tags[ 'system' ] = array( 
  1552. 'type' => __( 'System Tags', 'caldera-forms' ),  
  1553. 'tags' => $system_tags,  
  1554. 'wrap' => array( '{', '}' ) 
  1555. ); 
  1556.  
  1557. // get processor tags 
  1558. $processors = Caldera_Forms_Processor_Load::get_instance()->get_processors(); 
  1559. if ( ! empty( $processors ) ) { 
  1560. foreach ( $processors as $processor_key => $processor ) { 
  1561. if ( isset( $processor[ 'magic_tags' ] ) ) { 
  1562. foreach ( $processor[ 'magic_tags' ] as $key_tag => $value_tag ) { 
  1563.  
  1564. if ( ! isset( $tags[ $processor_key ] ) ) { 
  1565. $tags[ $processor_key ] = array( 
  1566. 'type' => $processor[ 'name' ],  
  1567. 'tags' => array(),  
  1568. 'wrap' => array( '{', '}' ) 
  1569. ); 
  1570. if ( is_array( $value_tag ) ) { 
  1571.  
  1572. // compatibility specific 
  1573. $tag = $processor_key . ':' . $key_tag; 
  1574. if ( ! isset( $tags[ $processor_key ][ 'tags' ][ $tag ] ) ) { 
  1575. if ( ! in_array( 'text', $value_tag ) ) { 
  1576. $value_tag[] = 'text'; 
  1577. $tags[ $processor_key ][ 'tags' ][ $tag ] = $value_tag; 
  1578. } else { 
  1579. // compatibility text 
  1580. $tag = $processor_key . ':' . $value_tag; 
  1581. if ( ! in_array( $tag, $tags ) ) { 
  1582. $tags[ $processor_key ][ 'tags' ][] = $tag; 
  1583.  
  1584.  
  1585. return $tags; 
  1586.  
  1587. /** 
  1588. * Parse magic tags 
  1589. * @param string $value 
  1590. * @param null|int $entry_id Optional. Entry ID to test by. 
  1591. * @param array $magic_caller The form/processorr/entry to evaluate against. May also be a powerful wizard. 
  1592. * @return mixed 
  1593. */ 
  1594. static public function do_magic_tags( $value, $entry_id = null, $magic_caller = array() ) { 
  1595.  
  1596. global $form, $referrer; 
  1597.  
  1598. $entry_details = array(); 
  1599. $input_value = $value; 
  1600. $this_form = $form; 
  1601. // pull in the metadata for entry ID 
  1602. if ( null !== $entry_id ) { 
  1603. $entry_details = Caldera_Forms_Magic_Doer::magic_tag_meta_prepare( $entry_id ); 
  1604.  
  1605.  
  1606. if ( is_string( $value ) ) { 
  1607. if( ! empty( $entry_details ) ) { 
  1608. $value = Caldera_Forms_Magic_Doer::do_processor_magic( $value, $entry_details ); 
  1609.  
  1610. // check for magics 
  1611. $value = Caldera_Forms_Magic_Doer::do_bracket_magic( $value, $this_form, $entry_id, $magic_caller, $referrer ); 
  1612.  
  1613. // fields 
  1614. $value = Caldera_Forms_Magic_Doer::do_field_magic( $value, $entry_id, $this_form); 
  1615.  
  1616. return $value; 
  1617.  
  1618. /** 
  1619. * Get all types of fields currently available. 
  1620. * @deprecated Soft deprecated in 1.5.0, will hard deprecated in 1.5.1 
  1621. * @return array Array of field types. 
  1622. */ 
  1623. static public function get_field_types() { 
  1624.  
  1625.  
  1626. return Caldera_Forms_Fields::get_all(); 
  1627.  
  1628.  
  1629. /** 
  1630. * Get all processors, in a form, of a specific type 
  1631. * @param string $type Processor type. 
  1632. * @param array $form Form config 
  1633. * @return array|bool Processor config if found. False if not. 
  1634. */ 
  1635. static public function get_processor_by_type( $type, $form ) { 
  1636. if ( is_string( $form ) ) { 
  1637. $form_cfg = Caldera_Forms_Forms::get_form( $form ); 
  1638. if ( ! empty( $form_cfg[ 'ID' ] ) ) { 
  1639. if ( $form_cfg[ 'ID' ] !== $form || empty( $form_cfg[ 'processors' ] ) ) { 
  1640. return false; 
  1641. $form = $form_cfg; 
  1642.  
  1643. if ( ! empty( $form[ 'processors' ] ) ) { 
  1644. $processors = array(); 
  1645. foreach ( $form[ 'processors' ] as $processor ) { 
  1646. if ( $processor[ 'type' ] == $type ) { 
  1647. $processors[] = $processor; 
  1648. $processors[ $processor[ 'ID' ] ] = $processor; 
  1649. if ( empty( $processors ) ) { 
  1650. return false; 
  1651.  
  1652. return $processors; 
  1653.  
  1654. return false; 
  1655.  
  1656. /** 
  1657. * Set a specific meta key from form meta. 
  1658. * @param string $key Name of key. 
  1659. * @param mixed $value Value to save. 
  1660. * @param string|array form Form config array or ID of form. 
  1661. * @param string $processor_id Optional. ID of processor. Default is "meta" 
  1662. * @return bool 
  1663. */ 
  1664. static public function set_submission_meta( $key, $value, $form, $processor_id = 'meta' ) { 
  1665. global $processed_meta; 
  1666.  
  1667. if ( is_string( $form ) ) { 
  1668. $form[ 'ID' ] = $form; 
  1669.  
  1670. // set value 
  1671. if ( isset( $form[ 'ID' ] ) ) { 
  1672. if ( isset( $processed_meta[ $form[ 'ID' ] ][ $processor_id ][ $key ] ) ) { 
  1673. if ( in_array( $value, $processed_meta[ $form[ 'ID' ] ][ $processor_id ][ $key ] ) ) { 
  1674. return true; 
  1675. $processed_meta[ $form[ 'ID' ] ][ $processor_id ][ $key ][] = $value; 
  1676.  
  1677. return true; 
  1678.  
  1679. /** 
  1680. * Set a field's data to be saved form a form entry. 
  1681. * @param string $field_id ID of field. 
  1682. * @param mixed $data Data to save. 
  1683. * @param string|array form Form config array or ID of form. 
  1684. * @param bool|false $entry_id Optional. Entry ID to save in. 
  1685. * @return mixed 
  1686. */ 
  1687. static public function set_field_data( $field_id, $data, $form, $entry_id = false ) { 
  1688. global $processed_data; 
  1689.  
  1690. $current_data = self::get_field_data( $field_id, $form, $entry_id ); 
  1691.  
  1692. if ( is_string( $form ) ) { 
  1693. $form = Caldera_Forms_Forms::get_form( $form ); 
  1694.  
  1695. if( ! is_array( $form ) ) { 
  1696. global $form; 
  1697.  
  1698. if( ! is_array( $form ) ) { 
  1699. return null; 
  1700.  
  1701. $field = Caldera_Forms_Field_Util::get_field( $field_id, $form ); 
  1702. if( is_array( $field ) && false === Caldera_Forms_Field_Util::check_conditional( $field, $form ) ) { 
  1703. return; 
  1704.  
  1705.  
  1706. // form object 
  1707. if ( isset( $form[ 'ID' ] ) ) { 
  1708. if ( isset( $form[ 'fields' ][ $field_id ] ) ) { 
  1709. $processed_data[ $form[ 'ID' ] ][ $field_id ] = $data; 
  1710.  
  1711. return true; 
  1712. } else { 
  1713. // is field_id a slug perhaps? 
  1714. foreach ( $form[ 'fields' ] as $field ) { 
  1715. if ( $field[ 'slug' ] == $field_id ) { 
  1716. $processed_data[ $form[ 'ID' ] ][ $field[ 'ID' ] ] = $data; 
  1717.  
  1718. return true; 
  1719.  
  1720. // generic field data 
  1721. $processed_data[ $form[ 'ID' ] ][ $field_id ] = $data; 
  1722.  
  1723. return true; 
  1724.  
  1725. /** 
  1726. * Get a field's data. 
  1727. * @since 1.5.0.8 
  1728. * @param string $field_id ID of field. 
  1729. * @param string|array $form Form config array or ID of form. 
  1730. * @param bool|false $entry_id Optional. Entry ID to save in. 
  1731. * @param bool $check_conditionals. Optional. If conditionals should be checked. Default is true. @since 1.5.0.8 
  1732. * @return bool 
  1733. */ 
  1734. static public function get_field_data( $field_id, $form, $entry_id = false, $check_conditionals = true ) { 
  1735. global $processed_data; 
  1736.  
  1737. if ( is_string( $form ) ) { 
  1738. $form = Caldera_Forms_Forms::get_form( $form ); 
  1739. if ( ! isset( $form[ 'ID' ] ) || $form[ 'ID' ] !== $form ) { 
  1740. return null; 
  1741.  
  1742. if( ! is_array( $form ) ) { 
  1743. global $form; 
  1744.  
  1745. if( ! is_array( $form ) ) { 
  1746. return null; 
  1747.  
  1748. $field = Caldera_Forms_Field_Util::get_field( $field_id, $form ); 
  1749. if( $check_conditionals && is_array( $field ) && false === Caldera_Forms_Field_Util::check_conditional( $field, $form ) ) { 
  1750. return; 
  1751.  
  1752. $indexkey = $form[ 'ID' ]; 
  1753. if ( ! empty( $entry_id ) ) { 
  1754. $indexkey = $form[ 'ID' ] . '_' . $entry_id; 
  1755. // is ID or slug? 
  1756. if ( ! isset( $form[ 'fields' ][ $field_id ] ) ) { 
  1757. foreach ( $form[ 'fields' ] as $field ) { 
  1758. if ( $field[ 'slug' ] == $field_id ) { 
  1759. $field_id = $field[ 'ID' ]; 
  1760. break; 
  1761.  
  1762. // get processed cached item 
  1763. if ( isset( $processed_data[ $indexkey ][ $field_id ] ) ) { 
  1764. return $processed_data[ $indexkey ][ $field_id ]; 
  1765.  
  1766. // entry fetch 
  1767. if ( ! empty( $entry_id ) && isset( $form[ 'fields' ][ $field_id ] ) ) { 
  1768.  
  1769. global $wpdb; 
  1770.  
  1771. $entry = $wpdb->get_results( $wpdb->prepare( " 
  1772. SELECT `value` FROM `" . $wpdb->prefix . "cf_form_entry_values` WHERE `entry_id` = %d AND `field_id` = %s AND `slug` = %s", $entry_id, $field_id, $form[ 'fields' ][ $field_id ][ 'slug' ] ), ARRAY_A ); 
  1773.  
  1774. //allow plugins to alter the value 
  1775. $entry = apply_filters( 'caldera_forms_get_field_entry', $entry, $field_id, $form, $entry_id ); 
  1776.  
  1777. if ( ! empty( $entry ) ) { 
  1778. if ( count( $entry ) > 1 ) { 
  1779. $out = array(); 
  1780. foreach ( $entry as $item ) { 
  1781. $out[] = $item[ 'value' ]; 
  1782. $processed_data[ $indexkey ][ $field_id ] = $out; 
  1783. } else { 
  1784. $processed_data[ $indexkey ][ $field_id ] = $entry[ 0 ][ 'value' ]; 
  1785. } else { 
  1786. $processed_data[ $indexkey ][ $field_id ] = null; 
  1787.  
  1788. return $processed_data[ $indexkey ][ $field_id ]; 
  1789.  
  1790.  
  1791. if ( isset( $form[ 'fields' ][ $field_id ] ) ) { 
  1792.  
  1793. // get field 
  1794. $field = apply_filters( 'caldera_forms_render_setup_field', $form[ 'fields' ][ $field_id ], $form ); 
  1795.  
  1796. if ( empty( $field ) || ! isset( $field[ 'ID' ] ) ) { 
  1797. return null; 
  1798. // get field types 
  1799. $field_types = Caldera_Forms_Fields::get_all(); 
  1800.  
  1801. if ( ! isset( $field_types[ $field[ 'type' ] ] ) ) { 
  1802. return null; 
  1803. $entry = null; 
  1804. // dont bother if conditions say it shouldnt be here. 
  1805.  
  1806. if ( ! empty( $field[ 'conditions' ][ 'type' ] ) ) { 
  1807. if ( ! self::check_condition( $field[ 'conditions' ], $form, $entry_id ) ) { 
  1808. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1809.  
  1810. return $entry; 
  1811.  
  1812.  
  1813. // check condition to see if field should be there first. 
  1814. // check if conditions match first. ignore vailators if not part of condition 
  1815. if ( isset( $_POST[ $field_id ] ) ) { 
  1816. $entry = stripslashes_deep( $_POST[ $field_id ] ); 
  1817.  
  1818. } elseif ( isset( $_POST[ $field[ 'slug' ] ] ) ) { 
  1819. // is slug maybe? 
  1820. $entry = stripslashes_deep( $_POST[ $field[ 'slug' ] ] ); 
  1821. // apply field filter 
  1822. if ( has_filter( 'caldera_forms_process_field_' . $field[ 'type' ] ) ) { 
  1823. $entry = apply_filters( 'caldera_forms_process_field_' . $field[ 'type' ], $entry, $field, $form ); 
  1824. if ( is_wp_error( $entry ) ) { 
  1825. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1826. return $entry; 
  1827.  
  1828. if ( is_string( $entry ) && strlen( $entry ) <= 0 ) { 
  1829. $entry = null; 
  1830. // is static 
  1831. if ( ! empty( $field_types[ $field[ 'type' ] ][ 'static' ] ) ) { 
  1832. // is options or not 
  1833. if ( ! empty( $field_types[ $field[ 'type' ] ][ 'options' ] ) ) { 
  1834. if ( is_array( $entry ) ) { 
  1835. $out = array(); 
  1836. foreach ( $entry as $option_id => $option ) { 
  1837. if ( isset( $field[ 'config' ][ 'option' ][ $option_id ] ) ) { 
  1838. if ( ! isset( $field[ 'config' ][ 'option' ][ $option_id ][ 'value' ] ) ) { 
  1839. $field[ 'config' ][ 'option' ][ $option_id ][ 'value' ] = $field[ 'config' ][ 'option' ][ $option_id ][ 'label' ]; 
  1840. if( empty( $field[ 'config' ][ 'option' ][ $option_id ][ 'value' ] ) ) { 
  1841. $field[ 'config' ][ 'option' ][ $option_id ][ 'value' ] = $field[ 'config' ][ 'option' ][ $option_id ][ 'label' ]; 
  1842. $out[ $option_id ] = self::do_magic_tags( $field[ 'config' ][ 'option' ][ $option_id ][ 'value' ] ); 
  1843. } elseif ( isset( $field[ 'config' ][ 'option' ][ $option ] ) ) { 
  1844. if ( ! isset( $field[ 'config' ][ 'option' ][ $option ][ 'value' ] ) ) { 
  1845. $field[ 'config' ][ 'option' ][ $option ][ 'value' ] = $field[ 'config' ][ 'option' ][ $option ][ 'label' ]; 
  1846. $out[ $option_id ] = self::do_magic_tags( $field[ 'config' ][ 'option' ][ $option ][ 'value' ] ); 
  1847. } else { 
  1848. // array based / check value agains submitted array. 
  1849. foreach ( $field[ 'config' ][ 'option' ] as $option_id => $set_option ) { 
  1850. if ( $set_option[ 'value' ] === $option ) { 
  1851. $out[] = self::do_magic_tags( $set_option[ 'value' ] ); 
  1852.  
  1853.  
  1854. $processed_data[ $indexkey ][ $field_id ] = $out; 
  1855. } else { 
  1856. if ( ! empty( $field[ 'config' ][ 'option' ] ) ) { 
  1857. foreach ( $field[ 'config' ][ 'option' ] as $option ) { 
  1858. if ( $option[ 'value' ] == $entry ) { 
  1859. $processed_data[ $indexkey ][ $field_id ] = self::do_magic_tags( $entry ); 
  1860. break; 
  1861. }else{ 
  1862. if( '0' === $entry || 0 === $entry ) { 
  1863. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1864. }elseif ( ! empty( $entry ) ) { 
  1865. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1866. } elseif( isset( $field[ 'config' ][ 'default' ] )) { 
  1867. $processed_data[ $indexkey ][ $field_id ] = self::do_magic_tags( $field[ 'config' ][ 'default' ] ); 
  1868. }else{ 
  1869. $processed_data[ $indexkey ][ $field_id ] = ''; 
  1870. }else{ 
  1871. // dynamic 
  1872. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1873. } else { 
  1874.  
  1875. $is_tag = self::do_magic_tags( $field_id ); 
  1876. if ( $is_tag !== $field_id ) { 
  1877. $processed_data[ $indexkey ][ $field_id ] = $is_tag; 
  1878.  
  1879. if ( isset( $processed_data[ $indexkey ][ $field_id ] ) ) { 
  1880. return $processed_data[ $indexkey ][ $field_id ]; 
  1881. }elseif( ! empty( $entry ) ) { 
  1882. $processed_data[ $indexkey ][ $field_id ] = $entry; 
  1883. return $entry; 
  1884.  
  1885.  
  1886. return null; 
  1887.  
  1888. /** 
  1889. * Get the configuration for a field. 
  1890. * @deprecated (Soft deprecated in 1.5.0, will be for reals deprecated in 1.6.0) 
  1891. * @param string $slug Slug of field to get config for. 
  1892. * @param array $form Form config array. 
  1893. * @return bool|mixed|void 
  1894. */ 
  1895. static public function get_field_by_slug( $slug, $form ) { 
  1896.  
  1897. return Caldera_Forms_Field_Util::get_field_by_slug( $slug, $form ); 
  1898.  
  1899.  
  1900. /** 
  1901. * Get field data, by slug, and by entry. 
  1902. * @param string $slug Slug of field to get config for. 
  1903. * @param array $form Form config array. 
  1904. * @param bool|false $entry_id Optional. The entry ID. 
  1905. * @return bool|array 
  1906. */ 
  1907. static public function get_slug_data( $slug, $form, $entry_id = false ) { 
  1908.  
  1909.  
  1910. $out = array(); 
  1911. if ( false !== strpos( $slug, '.' ) ) { 
  1912. $slug_parts = explode( '.', $slug ); 
  1913. $slug = array_shift( $slug_parts ); 
  1914.  
  1915. $field_types = Caldera_Forms_Fields::get_all(); 
  1916.  
  1917. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  1918.  
  1919. if ( $field[ 'slug' ] == $slug ) { 
  1920.  
  1921. return self::get_field_data( $field_id, $form, $entry_id ); 
  1922.  
  1923.  
  1924.  
  1925. /** 
  1926. * Get saved data for a form entry 
  1927. * @param int $entry_id Entry ID 
  1928. * @param null|array $form Optional. Form config. 
  1929. * @return array|null|void 
  1930. */ 
  1931. static public function get_entry_detail( $entry_id, $form = null ) { 
  1932. global $wpdb, $form; 
  1933.  
  1934. $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `" . $wpdb->prefix . "cf_form_entries` WHERE `id` = %d", $entry_id ), ARRAY_A ); 
  1935. if ( ! empty( $entry ) ) { 
  1936. if ( null === $form ) { 
  1937. $form = Caldera_Forms_Forms::get_form( $entry[ 'form_id' ] ); 
  1938. if ( empty( $form ) ) { 
  1939. return null; 
  1940. // get meta if any 
  1941. $meta = self::get_entry_meta( $entry_id, $form ); 
  1942. if ( ! empty( $meta ) ) { 
  1943. $entry[ 'meta' ] = $meta; 
  1944. $entry = apply_filters( 'caldera_forms_get_entry_detail', $entry, $entry_id, $form ); 
  1945.  
  1946. return $entry; 
  1947.  
  1948. /** 
  1949. * Get all meta for an entry. 
  1950. * @param int $entry_id Entry ID 
  1951. * @param array $form Form config. 
  1952. * @param null|string $type Optional. Type of meta to get. If null, the default, all meta is returned. 
  1953. * @return array 
  1954. */ 
  1955. static public function get_entry_meta( $entry_id, $form, $type = null ) { 
  1956. global $wpdb; 
  1957.  
  1958. $entry_meta = array(); 
  1959.  
  1960. $entry_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `" . $wpdb->prefix . "cf_form_entry_meta` WHERE `entry_id` = %d", $entry_id ), ARRAY_A ); 
  1961.  
  1962. if ( ! empty( $entry_meta_data ) ) { 
  1963. $processors = Caldera_Forms_Processor_Load::get_instance()->get_processors(); 
  1964. foreach ( $entry_meta_data as $meta_index => $meta ) { 
  1965.  
  1966. // is json? 
  1967. $is_json = @json_decode( $meta[ 'meta_value' ], ARRAY_A ); 
  1968. if ( ! empty( $is_json ) ) { 
  1969. $meta[ 'meta_value' ] = $is_json; 
  1970.  
  1971. $group = 'meta'; 
  1972. $meta = apply_filters( 'caldera_forms_get_entry_meta', $meta, $form ); 
  1973.  
  1974. if ( isset( $form[ 'processors' ][ $meta[ 'process_id' ] ] ) || $meta[ 'process_id' ] == '_debug_log' ) { 
  1975.  
  1976. if ( $meta[ 'process_id' ] == '_debug_log' ) { 
  1977. $meta[ 'meta_value' ] = '<pre>' . $meta[ 'meta_value' ] . '</pre>'; 
  1978. $entry_meta[ 'debug' ] = array( 
  1979. 'name' => __( 'Mailer Debug', 'caldera-forms' ),  
  1980. 'data' => array( 
  1981. '_debug_log' => array( 
  1982. 'entry' => array( 
  1983. 'log' => $meta 
  1984. ); 
  1985. continue; 
  1986.  
  1987. $process_config = array(); 
  1988. if ( isset( $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'config' ] ) ) { 
  1989. $process_config = $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'config' ]; 
  1990.  
  1991. $group = $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ]; 
  1992. if ( ! empty( $type ) ) { 
  1993. if ( $group != $type ) { 
  1994. continue; 
  1995. $meta = apply_filters( 'caldera_forms_get_entry_meta_' . $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ], $meta, $process_config, $form ); 
  1996.  
  1997.  
  1998. // allows plugins to remove it. 
  1999. if ( ! empty( $meta ) ) { 
  2000. if ( ! isset( $entry_meta[ $group ] ) ) { 
  2001. // is processor 
  2002. if ( isset( $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ] ) && isset( $processors[ $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ] ] ) ) { 
  2003. $meta_name = $processors[ $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ] ][ 'name' ]; 
  2004. } else { 
  2005. if ( $meta[ 'process_id' ] == '_debug_log' ) { 
  2006. $meta_name = __( 'Mailer Debug', 'caldera-forms' ); 
  2007. } else { 
  2008. $meta_name = $meta[ 'process_id' ]; 
  2009.  
  2010. $entry_meta[ $group ] = array( 
  2011. 'name' => $meta_name,  
  2012. 'data' => array() 
  2013. ); 
  2014. // custom template 
  2015. if ( isset( $processors[ $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ] ][ 'meta_template' ] ) && file_exists( $processors[ $form[ 'processors' ][ $meta[ 'process_id' ] ][ 'type' ] ][ 'meta_template' ] ) ) { 
  2016. $entry_meta[ $group ][ $group . '_template' ] = $entry_meta[ $group ][ 'template' ] = true; 
  2017.  
  2018. //if(!empty($meta['meta_title'])) { 
  2019. // $entry_meta[$group]['data'][$meta['process_id']]['title'] = $meta['meta_title']; 
  2020. //} 
  2021.  
  2022. $entry_meta[ $group ][ 'data' ][ $meta[ 'process_id' ] ][ 'entry' ][ $meta[ 'meta_key' ] ] = $meta; 
  2023.  
  2024.  
  2025. /**if(is_array($meta['meta_value'])) { 
  2026. foreach($meta['meta_value'] as $mkey=>$mval) { 
  2027. $entry['meta'][$group]['data'][$meta['process_id']]['title'] = $meta['meta_key']; 
  2028. $entry['meta'][$group]['data'][$meta['process_id']]['entry'][] = array( 
  2029. 'meta_key' => $mkey,  
  2030. 'meta_value' => $mval 
  2031. ); 
  2032. }else{ 
  2033. $entry['meta'][$group]['data'][$meta['process_id']]['entry'][] = array( 
  2034. 'meta_key' => $meta['meta_key'],  
  2035. 'meta_value' => $meta['meta_value'] 
  2036. ); 
  2037. }*/ 
  2038.  
  2039. // if type 
  2040. if ( ! empty( $type ) ) { 
  2041. //return only type 
  2042. if ( ! empty( $entry_meta[ $type ][ 'data' ] ) ) { 
  2043. return $entry_meta[ $type ][ 'data' ]; 
  2044.  
  2045. return $entry_meta; 
  2046.  
  2047. /** 
  2048. * Get submission data from a form being submitted or a saved entry 
  2049. * @since unknown 
  2050. * @param array $form Form Config. 
  2051. * @param bool|false $entry_id Optional. Entry ID to get data for, or if false, the default, get form current submission. 
  2052. * @param bool $check_conditionals. Optional. If conditionals should be checked. Default is true. @since 1.5.0.8 
  2053. * @return array|\WP_Error 
  2054. */ 
  2055. static public function get_submission_data( $form, $entry_id = false, $check_conditionals = true ) { 
  2056. global $processed_data; 
  2057.  
  2058. if ( is_string( $form ) ) { 
  2059. $form_id = $form; 
  2060. $form = Caldera_Forms_Forms::get_form( $form ); 
  2061. if ( ! isset( $form[ 'ID' ] ) || $form[ 'ID' ] !== $form_id ) { 
  2062. return new WP_Error( 'fail', __( 'Invalid form ID', 'caldera-forms' ) ); 
  2063.  
  2064. $indexkey = $form[ 'ID' ]; 
  2065. if ( ! empty( $entry_id ) ) { 
  2066. $indexkey = $form[ 'ID' ] . '_' . $entry_id; 
  2067.  
  2068. // get processed cached item using the form id 
  2069. if ( isset( $processed_data[ $indexkey ] ) ) { 
  2070. return $processed_data[ $indexkey ]; 
  2071. // prep data array 
  2072. $processed_data[ $indexkey ] = array(); 
  2073.  
  2074. // initialize process data 
  2075. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  2076. // get data 
  2077. if ( ! empty( $field[ 'conditions' ][ 'type' ] ) ) { 
  2078. if ( $check_conditionals && ! self::check_condition( $field[ 'conditions' ], $form, $entry_id ) ) { 
  2079. continue; 
  2080.  
  2081. self::get_field_data( $field_id, $form, $entry_id, $check_conditionals ); 
  2082.  
  2083. return $processed_data[ $indexkey ]; 
  2084.  
  2085.  
  2086. /** 
  2087. * Process current POST data as form submission. 
  2088. */ 
  2089. static public function process_submission() { 
  2090. //ob_flush(); 
  2091. global $post; 
  2092. global $process_id; 
  2093. global $form; 
  2094. global $field_types; 
  2095. global $rawdata; 
  2096. global $processed_data; 
  2097. global $transdata; 
  2098. global $wpdb; 
  2099. global $referrer; 
  2100.  
  2101. // clean out referrer 
  2102. if ( empty( $_POST[ '_wp_http_referer_true' ] ) ) { 
  2103. $_POST[ '_wp_http_referer_true' ] = $_SERVER[ 'HTTP_REFERER' ]; 
  2104.  
  2105. $referrer = parse_url( $_POST[ '_wp_http_referer_true' ] ); 
  2106. if ( ! empty( $referrer[ 'query' ] ) ) { 
  2107. parse_str( $referrer[ 'query' ], $referrer[ 'query' ] ); 
  2108. if ( isset( $referrer[ 'query' ][ 'cf_er' ] ) ) { 
  2109. unset( $referrer[ 'query' ][ 'cf_er' ] ); 
  2110. if ( isset( $referrer[ 'query' ][ 'cf_su' ] ) ) { 
  2111. unset( $referrer[ 'query' ][ 'cf_su' ] ); 
  2112. if ( ( isset( $_POST[ '_cf_cr_pst' ] ) && ! is_object( $post ) ) || ( isset( $_POST[ '_cf_cr_pst' ] ) && $post->ID !== (int) $_POST[ '_cf_cr_pst' ] ) ) { 
  2113. $post = get_post( (int) $_POST[ '_cf_cr_pst' ] ); 
  2114. // get form and check 
  2115. $form = Caldera_Forms_Forms::get_form( $_POST[ '_cf_frm_id' ] ); 
  2116. if ( empty( $form[ 'ID' ] ) || $form[ 'ID' ] != $_POST[ '_cf_frm_id' ] ) { 
  2117. return; 
  2118.  
  2119. // instance number 
  2120. $form_instance_number = 1; 
  2121. if ( isset( $_POST[ '_cf_frm_ct' ] ) ) { 
  2122. $form_instance_number = $_POST[ '_cf_frm_ct' ]; 
  2123.  
  2124. // check honey 
  2125. if ( isset( $form[ 'check_honey' ] ) ) { 
  2126. // use multiple honey words 
  2127. $honey_words = apply_filters( 'caldera_forms_get_honey_words', array( 
  2128. 'web_site',  
  2129. 'url',  
  2130. 'email',  
  2131. 'company',  
  2132. 'name' 
  2133. ) ); 
  2134. foreach ( $_POST as $honey_word => $honey_value ) { 
  2135.  
  2136. if ( ! is_array( $honey_value ) && strlen( $honey_value ) && in_array( $honey_word, $honey_words ) ) { 
  2137. // yupo - bye bye 
  2138. $referrer[ 'query' ][ 'cf_su' ] = $form_instance_number; 
  2139. $query_str = array( 
  2140. 'cf_er' => $process_id 
  2141. ); 
  2142. if ( ! empty( $referrer[ 'query' ] ) ) { 
  2143. $query_str = array_merge( $referrer[ 'query' ], $query_str ); 
  2144. $referrer = $referrer[ 'path' ] . '?' . http_build_query( $query_str ); 
  2145.  
  2146. return self::form_redirect( 'complete', $referrer, $form, uniqid( '_cf_bliss_' ) ); 
  2147. // init filter 
  2148. $form = apply_filters( 'caldera_forms_submit_get_form', $form ); 
  2149.  
  2150.  
  2151. /** 
  2152. * Runs at beginning of process of submitting form 
  2153. * @since unknown 
  2154. * @param array $form Form config 
  2155. * @param string $process_id Process ID, may not be set yet. 
  2156. */ 
  2157. do_action( 'caldera_forms_submit_start', $form, $process_id ); 
  2158.  
  2159.  
  2160. if ( ! empty( $form[ 'fields' ] ) ) { 
  2161. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  2162. $field = Caldera_Forms_Field_Util::get_field( $field, $form, true ); 
  2163. if ( ! is_array( $field ) || empty( $field ) ) { 
  2164. unset( $form[ 'fields' ][ $field_id ] ); 
  2165. } else { 
  2166. $form[ 'fields' ][ $field_id ] = $field; 
  2167.  
  2168.  
  2169. // check source is ajax to overide 
  2170. if ( ! empty( $_POST[ 'cfajax' ] ) && $_POST[ 'cfajax' ] == $form[ 'ID' ] ) { 
  2171. $form[ 'form_ajax' ] = 1; 
  2172.  
  2173. // get all fieldtype 
  2174. $field_types = Caldera_Forms_Fields::get_all(); 
  2175.  
  2176. // setup fieldtypes field submissions 
  2177. if ( ! empty( $field_types ) ) { 
  2178. foreach ( $field_types as $fieldType => $fieldConfig ) { 
  2179. // check for a handler 
  2180. if ( isset( $fieldConfig[ 'handler' ] ) ) { 
  2181. add_filter( 'caldera_forms_process_field_' . $fieldType, $fieldConfig[ 'handler' ], 10, 3 ); 
  2182. // check for a hash 
  2183. if ( isset( $fieldConfig[ 'save' ] ) ) { 
  2184. add_filter( 'caldera_forms_save_field_' . $fieldType, $fieldConfig[ 'save' ], 10, 3 ); 
  2185. // check for a hash 
  2186. if ( isset( $fieldConfig[ 'validate' ] ) ) { 
  2187. add_filter( 'caldera_forms_validate_field_' . $fieldType, $fieldConfig[ 'validate' ], 10, 3 ); 
  2188.  
  2189. // SET process ID 
  2190. if ( isset( $_GET[ 'cf_er' ] ) ) { 
  2191. $_POST[ '_cf_frm_tr' ] = $_GET[ 'cf_er' ]; 
  2192.  
  2193. if(isset($_POST['_cf_frm_tr'])) { 
  2194. $pretransient = Caldera_Forms_Transient::get_transient( $_POST['_cf_frm_tr'] ); 
  2195. if( !empty( $pretransient['transient'] ) && $pretransient['transient'] === $_POST['_cf_frm_tr']) { 
  2196. $transdata = $pretransient; 
  2197. $process_id = $transdata['transient']; 
  2198.  
  2199. // unset error details 
  2200. if ( isset( $transdata[ 'type' ] ) ) { 
  2201. unset( $transdata[ 'type' ] ); 
  2202. if ( isset( $transdata[ 'note' ] ) ) { 
  2203. unset( $transdata[ 'note' ] ); 
  2204. if ( isset( $transdata[ 'error' ] ) ) { 
  2205. unset( $transdata[ 'error' ] ); 
  2206. if ( isset( $transdata[ 'fields' ] ) ) { 
  2207. unset( $transdata[ 'fields' ] ); 
  2208.  
  2209. if ( empty( $process_id ) ) { 
  2210. $process_id = uniqid( '_cf_process_' ); 
  2211.  
  2212. // initialize data 
  2213. $entry_id = false; 
  2214. if ( isset( $_POST[ '_cf_frm_edt' ] ) ) { 
  2215. $entry_id = (int) $_POST[ '_cf_frm_edt' ]; 
  2216.  
  2217. $data = self::get_submission_data( $form ); 
  2218.  
  2219. // set transient for returns submissions 
  2220. if ( empty( $transdata ) ) { 
  2221. $transdata = array( 
  2222. 'transient' => $process_id,  
  2223. 'form_instance' => $form_instance_number,  
  2224. 'expire' => 600,  
  2225. 'data' => array_merge( $_POST, $data ),  
  2226. ); 
  2227.  
  2228. // remove AJAX value for tp_ 
  2229. if ( isset( $transdata[ 'data' ][ 'cfajax' ] ) ) { 
  2230. unset( $transdata[ 'data' ][ 'cfajax' ] ); 
  2231. // setup transient data 
  2232. $transdata = apply_filters( 'caldera_forms_submit_transient_setup', $transdata ); 
  2233.  
  2234. // setup processor bound requieds 
  2235. if ( ! empty( $form[ 'processors' ] ) ) { 
  2236. $bound_fields = array(); 
  2237. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  2238.  
  2239. if ( ! empty( $processor[ 'config' ][ '_required_bounds' ] ) ) { 
  2240. foreach ( $processor[ 'config' ] as $slug => &$value ) { 
  2241. if ( $slug == '_required_bounds' ) { 
  2242. continue; 
  2243.  
  2244. if ( in_array( $slug, $processor[ 'config' ][ '_required_bounds' ] ) ) { 
  2245. if ( isset( $form[ 'fields' ][ $value ] ) ) { 
  2246. if ( ! isset( $process_data[ $value ] ) ) { 
  2247. $form[ 'fields' ][ $value ][ 'required' ] = 1; 
  2248.  
  2249. // check submit type (new or update) 
  2250. if ( isset( $_POST[ '_cf_frm_edt' ] ) ) { 
  2251. // is edit 
  2252. //check user can edit this item. 
  2253. $transdata[ 'edit' ] = (int) $_POST[ '_cf_frm_edt' ]; 
  2254. // set entry_id 
  2255. self::set_field_data( '_entry_id', $transdata[ 'edit' ], $form ); 
  2256. $details = self::get_entry_detail( $_POST[ '_cf_frm_edt' ], $form ); 
  2257. $user_id = get_current_user_id(); 
  2258.  
  2259. // check token 
  2260. if ( isset( $_POST[ '_cf_frm_edt_tkn' ] ) ) { 
  2261.  
  2262. $validated = Caldera_Forms_Entry_Token::verify_token( $_POST[ '_cf_frm_edt_tkn' ], $entry_id, $form[ 'ID' ] ); 
  2263. if ( is_wp_error( $validated ) ) { 
  2264. return $validated; 
  2265. } else { 
  2266. $entry_id = (int) $details[ 'id' ]; 
  2267. $edit_token = Caldera_Forms_Entry_Token::create_entry_token( $entry_id, $form[ 'ID' ] ); 
  2268.  
  2269. } else { 
  2270.  
  2271. if ( empty( $user_id ) ) { 
  2272. $transdata[ 'error' ] = true; 
  2273. $transdata[ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  2274. } else { 
  2275.  
  2276. if ( empty( $details ) ) { 
  2277. $transdata[ 'error' ] = true; 
  2278. $transdata[ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  2279. } else { 
  2280. // check user can edit 
  2281. if ( current_user_can( 'edit_posts' ) || $details[ 'user_id' ] === $user_id ) { 
  2282. // can edit. 
  2283. } else { 
  2284. $transdata[ 'error' ] = true; 
  2285. $transdata[ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  2286.  
  2287.  
  2288.  
  2289.  
  2290. // start brining in entries 
  2291. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  2292.  
  2293. $entry = self::get_field_data( $field_id, $form ); 
  2294.  
  2295. if ( is_wp_error( $entry ) ) { 
  2296. $transdata[ 'fields' ][ $field_id ] = $entry->get_error_message(); 
  2297. } else { 
  2298. // required check 
  2299. $failed = false; 
  2300. // run validators 
  2301. if ( has_filter( 'caldera_forms_validate_field_' . $field[ 'type' ] ) ) { 
  2302. /** 
  2303. * Add custom validation by field type or change field value. 
  2304. * Return WP_Error to trigger validation error 
  2305. * @since unknown 
  2306. * @param mixed $entry Field value 
  2307. * @param array $field Field config 
  2308. * @param array $form Form config 
  2309. */ 
  2310. $entry = apply_filters( 'caldera_forms_validate_field_' . $field[ 'type' ], $entry, $field, $form ); 
  2311.  
  2312. /** 
  2313. * Add custom validation by field ID or change field value. 
  2314. * Return WP_Error to trigger validation error 
  2315. * @since 1.5.0 
  2316. * @param mixed $entry Field value 
  2317. * @param array $field Field config 
  2318. * @param array $form Form config 
  2319. */ 
  2320. $entry = apply_filters( 'caldera_forms_validate_field_' . $field[ 'ID' ], $entry, $field, $form ); 
  2321.  
  2322. // if required, check the validators returned errors or not. 
  2323. if ( ! empty( $field[ 'required' ] ) ) { 
  2324. // check is supported 
  2325. if ( isset( $field_types[ $field[ 'type' ] ][ 'setup' ][ 'not_supported' ] ) && in_array( 'required', (array) $field_types[ $field[ 'type' ] ][ 'setup' ][ 'not_supported' ] ) ) { 
  2326. continue; 
  2327. // check if conditions match first. ignore vailators if not part of condition 
  2328. if ( false === Caldera_Forms_Field_Util::check_conditional( $field, $form ) ) { 
  2329. continue; 
  2330.  
  2331. // if error - return so 
  2332. if ( is_wp_error( $entry ) ) { 
  2333. $transdata[ 'fields' ][ $field_id ] = $entry->get_error_message(); 
  2334. } elseif ( $entry === null ) { 
  2335. $transdata[ 'fields' ][ $field_id ] = $field[ 'label' ] . ' ' . __( 'is required', 'caldera-forms' ); 
  2336.  
  2337.  
  2338. // check requireds 
  2339. if ( ! empty( $transdata[ 'fields' ] ) || ! empty( $transdata[ 'error' ] ) ) { 
  2340. $transdata[ 'type' ] = 'error'; 
  2341. // set error transient 
  2342. $transdata = apply_filters( 'caldera_forms_submit_return_transient', $transdata, $form, $referrer, $process_id ); 
  2343. $transdata = apply_filters( 'caldera_forms_submit_return_transient_required', $transdata, $form, $referrer, $process_id ); 
  2344.  
  2345. // back to form 
  2346. $query_str = array( 
  2347. 'cf_er' => $process_id 
  2348. ); 
  2349. if ( ! empty( $referrer[ 'query' ] ) ) { 
  2350. $query_str = array_merge( $referrer[ 'query' ], $query_str ); 
  2351. $referrer = $referrer[ 'path' ] . '?' . http_build_query( $query_str ); 
  2352. $referrer = apply_filters( 'caldera_forms_submit_return_redirect', $referrer, $form, $process_id ); 
  2353. $referrer = apply_filters( 'caldera_forms_submit_return_redirect_required', $referrer, $form, $process_id ); 
  2354.  
  2355. Caldera_Forms_Transient::set_transient( $process_id, $transdata, $transdata['expire']); 
  2356.  
  2357. return self::form_redirect( 'error', $referrer, $form, $process_id ); 
  2358.  
  2359.  
  2360.  
  2361. // has processors 
  2362. do_action( 'caldera_forms_submit_start_processors', $form, $referrer, $process_id ); 
  2363. if ( ! isset( $form[ 'processors' ] ) ) { 
  2364. $form[ 'processors' ] = array(); 
  2365.  
  2366. // get all form processors 
  2367. $form_processors = Caldera_Forms_Processor_Load::get_instance()->get_processors(); 
  2368.  
  2369. /** 
  2370. * Runs before the 1st stage of processors "pre-process" 
  2371. * @since unknown 
  2372. * @param array $form Form config 
  2373. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2374. * @param string $process_id Unique ID for this processing 
  2375. */ 
  2376. do_action( 'caldera_forms_submit_pre_process_start', $form, $referrer, $process_id ); 
  2377.  
  2378. /** 
  2379. * Remove processors that are not allowed to run on this pass 
  2380. * @since 1.3.2 
  2381. */ 
  2382. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  2383. // the cf_version value in the form was introduced in 1.3.2 
  2384. // so if its set, its safe to asume the runtimes are set. 
  2385. if ( ! isset( $form[ 'cf_version' ] ) ) { 
  2386. // nope 
  2387. if ( ! empty( $transdata[ 'edit' ] ) ) { 
  2388. unset( $form[ 'processors' ][ $processor_id ] ); 
  2389.  
  2390. continue; 
  2391. // normal check within version 
  2392. // chec if editing and is allowed 
  2393. if ( ! empty( $transdata[ 'edit' ] ) && empty( $processor[ 'runtimes' ][ 'update' ] ) ) { 
  2394. // is editing and is set to not allow it so remove processor 
  2395. unset( $form[ 'processors' ][ $processor_id ] ); 
  2396. continue; 
  2397. if ( empty( $transdata[ 'edit' ] ) && empty( $processor[ 'runtimes' ][ 'insert' ] ) ) { 
  2398. // is editing and is set to not allow it 
  2399. unset( $form[ 'processors' ][ $processor_id ] ); 
  2400. continue; 
  2401.  
  2402.  
  2403. // PRE PROCESS 
  2404. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  2405.  
  2406. if ( isset( $form_processors[ $processor[ 'type' ] ] ) ) { 
  2407.  
  2408. // Do Conditional 
  2409. if ( isset( $processor[ 'conditions' ] ) && ! empty( $processor[ 'conditions' ][ 'type' ] ) ) { 
  2410. if ( ! self::check_condition( $processor[ 'conditions' ], $form ) ) { 
  2411. continue; 
  2412.  
  2413. // has processor 
  2414. $process = $form_processors[ $processor[ 'type' ] ]; 
  2415. if ( ! isset( $process[ 'pre_processor' ] ) ) { 
  2416. continue; 
  2417.  
  2418.  
  2419. // set default config 
  2420. $config = array(); 
  2421. $config[ 'processor_id' ] = $processor_id; 
  2422.  
  2423. if ( isset( $process[ 'default' ] ) ) { 
  2424. $config = $process[ 'default' ]; 
  2425. if ( ! empty( $processor[ 'config' ] ) ) { 
  2426.  
  2427. $config = array_merge( $config, $processor[ 'config' ] ); 
  2428. if ( is_array( $process[ 'pre_processor' ] ) ) { 
  2429. $process_line_data = call_user_func_array( $process[ 'pre_processor' ], array( 
  2430. $config,  
  2431. $form,  
  2432. $process_id 
  2433. ) ); 
  2434. } else { 
  2435. if ( function_exists( $process[ 'pre_processor' ] ) ) { 
  2436. $func = $process[ 'pre_processor' ]; 
  2437. $process_line_data = $func( $config, $form, $process_id ); 
  2438. // pre processors should not return unless a break in action for further 
  2439. // Returned something - check it 
  2440. if ( ! empty( $process_line_data ) ) { 
  2441. if ( is_array( $process_line_data ) ) { 
  2442. //type 
  2443. if ( ! empty( $process_line_data[ 'type' ] ) ) { 
  2444. $transdata[ 'type' ] = $process_line_data[ 'type' ]; 
  2445. // has note? 
  2446. if ( ! empty( $process_line_data[ 'note' ] ) ) { 
  2447. $transdata[ 'note' ] = $process_line_data[ 'note' ]; 
  2448.  
  2449. // fields involved? 
  2450. if ( ! empty( $process_line_data[ 'fields' ] ) ) { 
  2451. $transdata[ 'fields' ] = $process_line_data[ 'fields' ]; 
  2452.  
  2453. // set error transient 
  2454. $transdata = apply_filters( 'caldera_forms_submit_return_transient', $transdata, $form, $referrer, $process_id ); 
  2455. $transdata = apply_filters( 'caldera_forms_submit_return_transient_pre_process', $transdata, $form, $referrer, $process_id ); 
  2456.  
  2457. // back to form 
  2458. $query_str = array( 
  2459. 'cf_er' => $process_id 
  2460. ); 
  2461. if ( ! empty( $referrer[ 'query' ] ) ) { 
  2462. $query_str = array_merge( $referrer[ 'query' ], $query_str ); 
  2463. $referrer = $referrer[ 'path' ] . '?' . http_build_query( $query_str ); 
  2464. $referrer = apply_filters( 'caldera_forms_submit_return_redirect', $referrer, $form, $process_id ); 
  2465. $referrer = apply_filters( 'caldera_forms_submit_return_redirect-' . $processor[ 'type' ], $referrer, $config, $form, $process_id ); 
  2466.  
  2467. // set transient data 
  2468. Caldera_Forms_Transient::set_transient( $process_id, $transdata, $transdata['expire']); 
  2469.  
  2470. return self::form_redirect( 'preprocess', $referrer, $form, $process_id ); 
  2471.  
  2472. /** 
  2473. * Runs after the 1st stage of processors "pre-process" 
  2474. * @since unknown 
  2475. * @param array $form Form config 
  2476. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2477. * @param string $process_id Unique ID for this processing 
  2478. */ 
  2479. do_action( 'caldera_forms_submit_pre_process_end', $form, $referrer, $process_id ); 
  2480. /// AFTER PRE-PROCESS - check for errors etc to return else continue to process. 
  2481. if ( empty( $transdata[ 'edit' ] ) && ! empty( $form[ 'db_support' ] ) ) { 
  2482. // CREATE ENTRY 
  2483. $new_entry = array( 
  2484. 'form_id' => $form[ 'ID' ],  
  2485. 'user_id' => 0,  
  2486. 'datestamp' => date_i18n( 'Y-m-d H:i:s', time(), 0 ),  
  2487. 'status' => 'pending' 
  2488. ); 
  2489. // if user logged in 
  2490. if ( is_user_logged_in() ) { 
  2491. $new_entry[ 'user_id' ] = get_current_user_id(); 
  2492. } else { 
  2493. if ( isset( $data[ '_user_id' ] ) ) { 
  2494. $new_entry[ 'user_id' ] = $data[ '_user_id' ]; 
  2495.  
  2496. $wpdb->insert( $wpdb->prefix . 'cf_form_entries', $new_entry ); 
  2497. $entryid = $wpdb->insert_id; 
  2498.  
  2499. /** 
  2500. * Runs after an entry is saved 
  2501. * @since 1.2.1 
  2502. * @param int $entryid The ID of the entry that was just saved. 
  2503. * @param array $new_entry Data that was saved 
  2504. * @param array $form Form being processed 
  2505. */ 
  2506. do_action( 'caldera_forms_entry_saved', $entryid, $new_entry, $form ); 
  2507.  
  2508. // save entry_id 
  2509. self::set_field_data( '_entry_id', $entryid, $form ); 
  2510. $token = Caldera_Forms_Entry_Token::create_entry_token( $entryid, $form ); 
  2511.  
  2512. // set edit token 
  2513. self::set_field_data( '_entry_token', $token, $form ); 
  2514.  
  2515. } elseif ( ! empty( $transdata[ 'edit' ] ) ) { 
  2516. $entryid = $transdata[ 'edit' ]; 
  2517. } else { 
  2518. $entryid = false; 
  2519.  
  2520. /** 
  2521. * Runs before the 2nd stage of processors "process" 
  2522. * @since unknown 
  2523. * @param array $form Form config 
  2524. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2525. * @param string $process_id Unique ID for this processing 
  2526. * @param int|false $entryid Current entry ID or false if not set or being saved. 
  2527. */ 
  2528. do_action( 'caldera_forms_submit_process_start', $form, $referrer, $process_id, $entryid ); 
  2529. /// PROCESS 
  2530. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  2531. if ( isset( $form_processors[ $processor[ 'type' ] ] ) ) { 
  2532. // has processor 
  2533. // Do Conditional 
  2534. if ( isset( $processor[ 'conditions' ] ) && ! empty( $processor[ 'conditions' ][ 'type' ] ) ) { 
  2535. if ( ! self::check_condition( $processor[ 'conditions' ], $form ) ) { 
  2536. continue; 
  2537.  
  2538. $process = $form_processors[ $processor[ 'type' ] ]; 
  2539. if ( ! isset( $process[ 'processor' ] ) ) { 
  2540. continue; 
  2541. $hasmeta = null; 
  2542. // set default config 
  2543. $config = array(); 
  2544. $config[ 'processor_id' ] = $processor_id; 
  2545. if ( isset( $process[ 'default' ] ) ) { 
  2546. $config = $process[ 'default' ]; 
  2547. if ( ! empty( $processor[ 'config' ] ) ) { 
  2548.  
  2549. $config = array_merge( $config, $processor[ 'config' ] ); 
  2550. if ( is_array( $process[ 'processor' ] ) ) { 
  2551. $hasmeta = call_user_func_array( $process[ 'processor' ], array( $config, $form, $process_id ) ); 
  2552. } else { 
  2553. if ( function_exists( $process[ 'processor' ] ) ) { 
  2554. $func = $process[ 'processor' ]; 
  2555. $hasmeta = $func( $config, $form, $process_id ); 
  2556. if ( $hasmeta !== null ) { 
  2557. foreach ( (array) $hasmeta as $metakey => $metavalue ) { 
  2558. $meta_process_id = $processor_id; 
  2559. // single processors are generallay used so not processor id is needed 
  2560. if ( ! empty( $form_processors[ $processor[ 'type' ] ][ 'single' ] ) ) { 
  2561. $meta_process_id = $processor[ 'type' ]; 
  2562. self::set_submission_meta( $metakey, $metavalue, $form, $processor_id ); 
  2563. } // check for transdata errors 
  2564.  
  2565. if ( ! empty( $transdata[ 'error' ] ) ) { 
  2566. // remove pending entry 
  2567. if ( ! empty( $entryid ) && ! empty( $new_entry ) && $new_entry[ 'status' ] == 'pending' ) { 
  2568. // kill it with fire 
  2569. $wpdb->delete( $wpdb->prefix . 'cf_form_entries', array( 'id' => $entryid ) ); 
  2570. // set error transient 
  2571. $transdata = apply_filters( 'caldera_forms_submit_error_transient', $transdata, $form, $referrer, $process_id ); 
  2572. $transdata = apply_filters( 'caldera_forms_submit_error_transient_pre_process', $transdata, $form, $referrer, $process_id ); 
  2573.  
  2574. // back to form 
  2575. $query_str = array( 
  2576. 'cf_er' => $process_id 
  2577. ); 
  2578. if ( ! empty( $referrer[ 'query' ] ) ) { 
  2579. $query_str = array_merge( $referrer[ 'query' ], $query_str ); 
  2580. $referrer = $referrer[ 'path' ] . '?' . http_build_query( $query_str ); 
  2581. $referrer = apply_filters( 'caldera_forms_submit_error_redirect', $referrer, $form, $process_id ); 
  2582. $referrer = apply_filters( 'caldera_forms_submit_error_redirect_pre_process', $referrer, $form, $process_id ); 
  2583.  
  2584. // set transient data 
  2585. Caldera_Forms_Transient::set_transient( $process_id, $transdata, $transdata['expire']); 
  2586.  
  2587. return self::form_redirect( 'error', $referrer, $form, $process_id ); 
  2588.  
  2589.  
  2590.  
  2591. /** 
  2592. * Runs after the 2nd stage of processors "process" 
  2593. * @since unknown 
  2594. * @param array $form Form config 
  2595. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2596. * @param string $process_id Unique ID for this processing 
  2597. * @param int|false $entryid Current entry ID or false if not set or being saved. 
  2598. */ 
  2599. do_action( 'caldera_forms_submit_process_end', $form, $referrer, $process_id, $entryid ); 
  2600. // AFTER PROCESS - do post process for any additional stuff 
  2601.  
  2602. /** 
  2603. * Runs before the 3rd and final stage of processors "post-process" 
  2604. * @since unknown 
  2605. * @param array $form Form config 
  2606. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2607. * @param string $process_id Unique ID for this processing 
  2608. * @param int|false $entryid Current entry ID or false if not set or being saved. 
  2609. */ 
  2610. do_action( 'caldera_forms_submit_post_process', $form, $referrer, $process_id, $entryid ); 
  2611. // POST PROCESS 
  2612. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  2613. if ( isset( $form_processors[ $processor[ 'type' ] ] ) ) { 
  2614. // has processor 
  2615. // Do Conditional 
  2616. if ( isset( $processor[ 'conditions' ] ) && ! empty( $processor[ 'conditions' ][ 'type' ] ) ) { 
  2617. if ( ! self::check_condition( $processor[ 'conditions' ], $form ) ) { 
  2618. continue; 
  2619.  
  2620. $process = $form_processors[ $processor[ 'type' ] ]; 
  2621. if ( ! isset( $process[ 'post_processor' ] ) ) { 
  2622. continue; 
  2623. // set default config 
  2624. $config = array(); 
  2625. $config[ 'processor_id' ] = $processor_id; 
  2626. if ( isset( $process[ 'default' ] ) ) { 
  2627. $config = $process[ 'default' ]; 
  2628. if ( ! empty( $processor[ 'config' ] ) ) { 
  2629.  
  2630. $config = array_merge( $config, $processor[ 'config' ] ); 
  2631. if ( is_array( $process[ 'post_processor' ] ) ) { 
  2632. $hasmeta = call_user_func_array( $process[ 'post_processor' ], array( 
  2633. $config,  
  2634. $form,  
  2635. $process_id 
  2636. ) ); 
  2637. } else { 
  2638. if ( function_exists( $process[ 'post_processor' ] ) ) { 
  2639. $func = $process[ 'post_processor' ]; 
  2640. $hasmeta = $func( $config, $form, $process_id ); 
  2641. if ( $hasmeta !== null ) { 
  2642. foreach ( (array) $hasmeta as $metakey => $metavalue ) { 
  2643. self::set_submission_meta( $metakey, $metavalue, $form, $processor_id ); 
  2644.  
  2645.  
  2646. /** 
  2647. * Runs after the 3rd and final stage of processors "post-process" 
  2648. * @since unknown 
  2649. * @param array $form Form config 
  2650. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2651. * @param string $process_id Unique ID for this processing 
  2652. * @param int|false $entryid Current entry ID or false if not set or being saved. 
  2653. */ 
  2654. do_action( 'caldera_forms_submit_post_process_end', $form, $referrer, $process_id, $entryid ); 
  2655.  
  2656. /** 
  2657. * Runs after all processing for form completes 
  2658. * @since unknown 
  2659. * @param array $form Form config 
  2660. * @param array $referrer URL form was submitted via -- is passed through parse_url() before this point. 
  2661. * @param string $process_id Unique ID for this processing 
  2662. * @param int|false $entryid Current entry ID or false if not set or being saved. 
  2663. */ 
  2664. do_action( 'caldera_forms_submit_complete', $form, $referrer, $process_id, $entryid ); 
  2665.  
  2666. // redirect back or to result page 
  2667. $referrer[ 'query' ][ 'cf_su' ] = $form_instance_number; 
  2668.  
  2669. // last entry id - new only 
  2670. if ( empty( $transdata[ 'edit' ] ) ) { 
  2671. $cf_id = self::do_magic_tags( '{entry_id}' ); 
  2672. if ( ! empty( $cf_id ) ) { 
  2673. $referrer[ 'query' ][ 'cf_id' ] = self::do_magic_tags( '{entry_id}' ); 
  2674.  
  2675. // passback values 
  2676. if ( ! empty( $form[ 'variables' ][ 'types' ] ) ) { 
  2677. foreach ( $form[ 'variables' ][ 'types' ] as $variable_index => $behavior_type ) { 
  2678. if ( $behavior_type == 'passback' ) { 
  2679. $referrer[ 'query' ][ $form[ 'variables' ][ 'keys' ][ $variable_index ] ] = self::do_magic_tags( $form[ 'variables' ][ 'values' ][ $variable_index ] ); 
  2680. $referrer = $referrer[ 'path' ] . '?' . http_build_query( $referrer[ 'query' ] ); 
  2681.  
  2682. // filter refer 
  2683. $referrer = apply_filters( 'caldera_forms_submit_redirect', $referrer, $form, $process_id ); 
  2684. $referrer = apply_filters( 'caldera_forms_submit_redirect_complete', $referrer, $form, $process_id ); 
  2685.  
  2686. // kill transient data 
  2687. delete_transient( $process_id ); 
  2688.  
  2689. return self::form_redirect( 'complete', $referrer, $form, $process_id ); 
  2690.  
  2691.  
  2692. /** 
  2693. * Makes Caldera Forms load the preview 
  2694. */ 
  2695. static public function cf_init_preview() { 
  2696. if( ! isset( $_GET, $_GET['cf_preview'] ) ) { 
  2697. return; 
  2698.  
  2699. global $post, $form; 
  2700.  
  2701. $preview_id = trim( $_GET['cf_preview'] ); 
  2702. if(!empty( $preview_id )) { 
  2703. $form = Caldera_Forms_Forms::get_form($preview_id ); 
  2704.  
  2705. $userid = get_current_user_id(); 
  2706. if( !empty( $userid ) ) { 
  2707.  
  2708. if(empty( $form['ID']) || $form['ID'] !== trim( $preview_id ) ) { 
  2709. return; 
  2710. if( empty($post) || $post->post_title !== 'Caldera Forms Preview' ) { 
  2711. $temp_page = get_page_by_title('Caldera Forms Preview'); 
  2712. if(empty($temp_page)) { 
  2713. // create page 
  2714. $post = array( 
  2715. 'post_content' => '',  
  2716. 'post_name' => 'caldera_forms_preview',  
  2717. 'post_title' => 'Caldera Forms Preview',  
  2718. 'post_status' => 'draft',  
  2719. 'post_type' => 'page',  
  2720. 'ping_status' => 'closed',  
  2721. 'comment_status' => 'closed' 
  2722. ); 
  2723. $page_id = wp_insert_post( $post ); 
  2724. wp_redirect( trailingslashit( get_home_url() ) . '?page_id='.$page_id.'&preview=true&cf_preview='.$preview_id ); 
  2725. exit; 
  2726. if( $temp_page->post_status !== 'draft') { 
  2727. wp_update_post( array( 'ID' => $temp_page->ID, 'post_status' => 'draft' ) ); 
  2728. wp_redirect( trailingslashit( get_home_url() ) . '?page_id='.$temp_page->ID.'&preview=true&cf_preview='.$preview_id ); 
  2729. exit; 
  2730. $post->post_title = $form['name']; 
  2731. $post->post_content = '[caldera_form id="'.$_GET['cf_preview'].'"]'; 
  2732.  
  2733.  
  2734. $form = self::check_for_forms_on_page( ); 
  2735.  
  2736.  
  2737. public function api_handler() { 
  2738. global $wp_query; 
  2739.  
  2740. // check for API 
  2741. // if this is not a request for json or a singular object then bail 
  2742. if ( ! isset( $wp_query->query_vars[ 'cf_api' ] ) ) { 
  2743. return; 
  2744.  
  2745. // check if form exists 
  2746. $form = Caldera_Forms_Forms::get_form( $wp_query->query_vars[ 'cf_api' ] ); 
  2747. $atts = array( 
  2748. 'id' => $wp_query->query_vars[ 'cf_api' ],  
  2749. 'ajax' => true 
  2750. ); 
  2751. if ( ! empty( $_REQUEST[ 'cf_instance' ] ) ) { 
  2752. $atts[ 'instance' ] = $_REQUEST[ 'cf_instance' ]; 
  2753. // push 200 status. in some cases plugins or permalink config may cause a 404 before going out 
  2754. header( "HTTP/1.1 200 OK", true ); 
  2755. if ( ! empty( $form[ 'ID' ] ) ) { 
  2756. if ( $form[ 'ID' ] === $wp_query->query_vars[ 'cf_api' ] ) { 
  2757. // got it! 
  2758. // need entry? 
  2759. if ( ! empty( $wp_query->query_vars[ 'cf_entry' ] ) ) { 
  2760. $atts[ 'entry' ] = (int) $wp_query->query_vars[ 'cf_entry' ]; 
  2761. //$entry = Caldera_Forms::get_entry($wp_query->query_vars['cf_entry'], $form); 
  2762. //wp_send_json( $entry ); 
  2763. // is a post? 
  2764. if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { 
  2765.  
  2766. if( !empty( $_POST['control'] ) ) { 
  2767. $transient_name = sanitize_key( $_POST['control'] ); 
  2768. $transdata = Caldera_Forms_Transient::get_transient( $transient_name ); 
  2769. if( false === $transdata ) { 
  2770. $transdata = array(); 
  2771. if ( ! empty( $_FILES ) && ! empty( $_POST[ 'field' ] ) ) { 
  2772. $form = Caldera_Forms_Forms::get_form( $wp_query->query_vars[ 'cf_api' ] ); 
  2773. $field = $form[ 'fields' ][ $_POST[ 'field' ] ]; 
  2774.  
  2775. $data = cf_handle_file_upload( true, $field, $form ); 
  2776. if ( is_wp_error( $data ) ) { 
  2777. wp_send_json_error( $data->get_error_message() ); 
  2778.  
  2779. $transdata[] = $data; 
  2780. //set 
  2781. Caldera_Forms_Transient::set_transient( $transient_name, $transdata, DAY_IN_SECONDS ); 
  2782. // maybe put in some checks on file then can say yea or nei 
  2783. wp_send_json_success( array() ); 
  2784.  
  2785. $_POST[ '_wp_http_referer_true' ] = 'api'; 
  2786. $_POST[ '_cf_frm_id' ] = $_POST[ 'cfajax' ] = $wp_query->query_vars[ 'cf_api' ]; 
  2787.  
  2788. Caldera_Forms::process_form_via_post(); 
  2789.  
  2790.  
  2791. echo self::render_form( $atts ); 
  2792. exit; 
  2793.  
  2794. /** 
  2795. * Retrieves the URL to the endpoint. 
  2796. * Note: The returned URL is NOT escaped. 
  2797. * @since 1.3.2 
  2798. * @param string $form_id form ID 
  2799. * @return string Full URL 
  2800. */ 
  2801. static function get_submit_url( $form_id = '' ) { 
  2802.  
  2803. if ( is_multisite() && get_blog_option( null, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) { 
  2804. $url = get_home_url(); 
  2805. $url .= '/cf-api/' . ltrim( $form_id, '/' ); 
  2806. } else { 
  2807. $url = trailingslashit( get_home_url() ); 
  2808. $url = add_query_arg( 'cf_api', $form_id, $url ); 
  2809. if ( is_ssl() ) { 
  2810. if ( $_SERVER[ 'SERVER_NAME' ] === parse_url( get_home_url(), PHP_URL_HOST ) ) { 
  2811. $url = set_url_scheme( $url, 'https' ); 
  2812.  
  2813. /** 
  2814. * Filter the Caldera Forms APU url 
  2815. * @since 1.3.2 
  2816. * @param string $url URL. 
  2817. * @param string $form_id ID of form. 
  2818. */ 
  2819. return apply_filters( 'caldera_forms_submission_url', $url, $form_id ); 
  2820.  
  2821.  
  2822. /** 
  2823. * Makes Caldera Forms go in front-end! 
  2824. */ 
  2825. static public function cf_init_system() { 
  2826.  
  2827. if(!empty($_GET['cf_tp'])) { 
  2828.  
  2829. // process a transient stored entry 
  2830. $data = Caldera_Forms_Transient::get_transient( $_GET[ 'cf_tp' ] ); 
  2831. if(!empty($data) && $data['transient'] === $_GET['cf_tp'] && isset($data['data'])) { 
  2832. // create post values 
  2833. $_POST = array_merge( $_POST, $data['data']); 
  2834. // set transient id 
  2835. $_POST['_cf_frm_tr'] = $data['transient']; 
  2836.  
  2837. // hook into submission 
  2838. self::process_form_via_post(); 
  2839.  
  2840.  
  2841.  
  2842. /** 
  2843. * Recursive array search. 
  2844. * @param string|array $needle Value to search for. 
  2845. * @param array $haystack Array to search in 
  2846. * @param array $found Optional. Array to add found items to. Default is an empty array. 
  2847. * @return array Array of found needles. 
  2848. */ 
  2849. static function search_array_fields( $needle, $haystack, $found = array() ) { 
  2850.  
  2851. if ( is_array( $needle ) ) { 
  2852. foreach ( $needle as $pin ) { 
  2853. $found = array_merge( $found, self::search_array_fields( $pin, $haystack ) ); 
  2854. } else { 
  2855. if ( in_array( $needle, $haystack ) ) { 
  2856. $found[] = $needle; 
  2857.  
  2858. return $found; 
  2859.  
  2860. /** 
  2861. * Load a saved entry. 
  2862. * @since unknown 
  2863. * @param int $entry_id Entry ID 
  2864. * @param string|array $form Optional. Config array, or ID of form. 
  2865. * @return array|WP_Error|void 
  2866. */ 
  2867. static public function get_entry( $entry_id, $form ) { 
  2868. if ( is_string( $form ) ) { 
  2869. $form_id = $form; 
  2870. $form = Caldera_Forms_Forms::get_form( $form ); 
  2871. if ( ! is_array( $form ) || ! isset( $form[ 'ID' ] ) || $form[ 'ID' ] !== $form_id ) { 
  2872. return new WP_Error( 'fail', esc_html__( 'Invalid form ID', 'caldera-forms' ) ); 
  2873.  
  2874. if ( empty( $form ) ) { 
  2875. return; 
  2876.  
  2877.  
  2878. // get fields 
  2879. $field_types = Caldera_Forms_Fields::get_all(); 
  2880.  
  2881. //False for third arg added in 1.5.0.8 to prevent conditions from being shown 
  2882. //See: https://github.com/CalderaWP/Caldera-Forms/issues/1494 
  2883. $entry = self::get_submission_data( $form, $entry_id, false ); 
  2884. $data = array( 
  2885. 'data' => array() 
  2886. ); 
  2887.  
  2888. foreach ( $entry as $field_id => $field_value ) { 
  2889.  
  2890. if ( ! isset( $form[ 'fields' ][ $field_id ] ) || ! isset( $field_types[ $form[ 'fields' ][ $field_id ][ 'type' ] ] ) ) { 
  2891. continue; 
  2892.  
  2893. //remove if not_supported 
  2894. $type = Caldera_Forms_Field_Util::get_type( Caldera_Forms_Field_Util::get_field( $field_id, $form ) ); 
  2895. if ( Caldera_Forms_Fields::not_support( $type, 'entry_list' ) ) { 
  2896. continue; 
  2897.  
  2898.  
  2899. $field = $form[ 'fields' ][ $field_id ]; 
  2900.  
  2901. $field = Caldera_Forms_Field_Util::get_field( $field, $form, true ); 
  2902.  
  2903. if ( is_string( $field_value ) ) { 
  2904. // maybe json? 
  2905. $is_json = json_decode( $field_value, ARRAY_A ); 
  2906. if ( ! empty( $is_json ) && is_array( $is_json ) ) { 
  2907. $field_value = $is_json; 
  2908. } else { 
  2909. $field_value = esc_html( stripslashes_deep( $field_value ) ); 
  2910. // set view 
  2911. $field_view = apply_filters( 'caldera_forms_view_field_' . $field[ 'type' ], $field_value, $field, $form ); 
  2912.  
  2913. // has options? 
  2914. if ( ! empty( $field[ 'config' ][ 'option' ] ) ) { 
  2915. $i = 0; 
  2916. foreach ( $field[ 'config' ][ 'option' ] as $opt => $option ) { 
  2917. if ( $option[ 'value' ] == $field_view ) { 
  2918. $field_view = $option[ 'label' ]; 
  2919.  
  2920. if ( is_array( $field_value ) ) { 
  2921. if ( isset( $field_value[ $opt ] ) ) { 
  2922. $field_value = $field_value[ $opt ]; 
  2923. } else { 
  2924. $field_value = ''; 
  2925.  
  2926.  
  2927. if ( empty( $field_types[ $field[ 'type' ] ][ 'options' ] ) ) { 
  2928. $data[ 'data' ][ $field_id . '_' . $i ] = array( 
  2929. 'label' => $field[ 'label' ],  
  2930. 'view' => $field_view,  
  2931. 'value' => $field_value 
  2932. ); 
  2933. $i ++; 
  2934.  
  2935.  
  2936. $data[ 'data' ][ $field_id ] = array( 
  2937. 'label' => $field[ 'label' ],  
  2938. 'view' => $field_view,  
  2939. 'value' => $field_value 
  2940. ); 
  2941.  
  2942.  
  2943.  
  2944. // get meta 
  2945. $entry_detail = self::get_entry_detail( $entry_id, $form ); 
  2946. $data[ 'date' ] = self::localize_time( $entry_detail[ 'datestamp' ] ); 
  2947.  
  2948. if ( ! empty( $entry_detail[ 'meta' ] ) ) { 
  2949. $data[ 'meta' ] = $entry_detail[ 'meta' ]; 
  2950.  
  2951.  
  2952. if ( ! empty( $entry_detail[ 'user_id' ] ) ) { 
  2953. $user = get_userdata( $entry_detail[ 'user_id' ] ); 
  2954. if ( ! empty( $user ) ) { 
  2955. $data[ 'user' ] = array( 
  2956. 'ID' => $user->ID,  
  2957. 'name' => $user->data->display_name,  
  2958. 'email' => $user->data->user_email,  
  2959. 'avatar' => get_avatar( $user->ID, 150, 'identicon' ),  
  2960. ); 
  2961. } else { 
  2962. $avatar_field = null; 
  2963. if ( ! empty( $form[ 'avatar_field' ] ) ) { 
  2964. $avatar_field = self::get_field_data( $form[ 'avatar_field' ], $form, $entry_id ); 
  2965. $data[ 'user' ] = array( 
  2966. 'avatar' => get_avatar( $avatar_field, 150 ),  
  2967. ); 
  2968.  
  2969. if ( ! empty( $form[ 'variables' ][ 'types' ] ) ) { 
  2970. foreach ( $form[ 'variables' ][ 'types' ] as $var_key => $var_type ) { 
  2971. if ( $var_type == 'entryitem' ) { 
  2972. $var_val = Caldera_Forms::do_magic_tags( $form[ 'variables' ][ 'values' ][ $var_key ], $entry_id ); 
  2973. $data[ 'data' ][ '_var_' . $form[ 'variables' ][ 'keys' ][ $var_key ] ] = array( 
  2974. 'label' => ucwords( str_replace( '_', ' ', $form[ 'variables' ][ 'keys' ][ $var_key ] ) ),  
  2975. 'view' => $var_val,  
  2976. 'value' => sanitize_text_field( $var_val ) 
  2977. ); 
  2978.  
  2979. if ( ! isset( $data[ 'user' ] ) ) { 
  2980. $data[ 'user' ] = array(); 
  2981.  
  2982. /** 
  2983. * Allows changes to user profile of entry 
  2984. * @since unknown 
  2985. * @param array $data_user The user data 
  2986. * @param int $entry_id ID of entry being returned 
  2987. * @param array $form Form config 
  2988. */ 
  2989. $data[ 'user' ] = apply_filters( 'caldera_forms_get_entry_user', $data[ 'user' ], $entry_id, $form ); 
  2990.  
  2991. // set the entry status 
  2992. $data[ 'status' ] = $entry_detail[ 'status' ]; 
  2993.  
  2994. /** 
  2995. * Changes entry being returned. 
  2996. * @since unknown 
  2997. * @param array $data The entry data 
  2998. * @param int $entry_id ID of entry 
  2999. * @param array $form Form config 
  3000. */ 
  3001. $data = apply_filters( 'caldera_forms_get_entry', $data, $entry_id, $form ); 
  3002.  
  3003. return $data; 
  3004.  
  3005.  
  3006. /** 
  3007. * Load a Caldera Form in a modal. 
  3008. * @since unknown 
  3009. * @deprecated 1.5.0.7 
  3010. * @param string|array $atts Shortcode atts or form ID 
  3011. * @param string $content Content to use in trigger link. 
  3012. * @return string 
  3013. */ 
  3014. static public function render_modal_form( $atts, $content ) { 
  3015. _deprecated_function( __METHOD__, '1.5.0.7', 'Caldera_Forms_Render_Modals::render_modal_form' ); 
  3016. return Caldera_Forms_Render_Modals::modal_form( $atts, $content ); 
  3017.  
  3018. /** 
  3019. * Print modal content in footer. 
  3020. * @since unknown 
  3021. * @uses "wp_footer" 
  3022. */ 
  3023. static public function render_footer_modals() { 
  3024. _deprecated_function( __METHOD__, '1.5.0.7', 'Caldera_Forms_Render_Modals::render_footer_modals' ); 
  3025. Caldera_Forms_Render_Modals::render_footer_modals(); 
  3026.  
  3027. /** 
  3028. * Create HTML markup for a field. 
  3029. * @since unknown 
  3030. * @param array|string $field Form ID or shortcode atts or form config array 
  3031. * @param array|null $form Optional Form to load field from. Not necessary, but helps the filters out. 
  3032. * @param array $entry_data Optional. Entry data to populate field with. Null, the default, loads form for creating a new entry. 
  3033. * @return void|string HTML for form, if it was able to be loaded,  
  3034. */ 
  3035. static public function render_field( $field, $form = null, $entry_data = array(), $field_errors = array() ) { 
  3036. $current_form_count = Caldera_Forms_Render_Util::get_current_form_count(); 
  3037.  
  3038. $field_classes = Caldera_Forms_Field_Util::prepare_field_classes( $field, $form ); 
  3039.  
  3040. $field_wrapper_class = implode( ' ', $field_classes[ 'control_wrapper' ] ); 
  3041. $field_input_class = implode( ' ', $field_classes[ 'field_wrapper' ] ); 
  3042. $field_class = implode( ' ', $field_classes[ 'field' ] ); 
  3043.  
  3044. // add error class 
  3045. if ( ! empty( $field_errors ) ) { 
  3046. $field_wrapper_class .= " " . implode( ' ', $field_classes[ 'field_error' ] ); 
  3047.  
  3048. if ( ! empty( $field[ 'hide_label' ] ) ) { 
  3049. $field_classes[ 'field_label' ][] = 'screen-reader-text sr-only'; 
  3050.  
  3051. $field_structure = array( 
  3052. "field" => $field,  
  3053. "id" => $field[ 'ID' ], //'fld_' . $field['slug'],  
  3054. "name" => $field[ 'ID' ], //$field['slug'],  
  3055. "wrapper_before" => "<div role=\"field\" data-field-wrapper=\"" . $field[ 'ID' ] . "\" class=\"" . $field_wrapper_class . "\">\r\n",  
  3056. "field_before" => "<div class=\"" . $field_input_class . "\">\r\n",  
  3057. "label_before" => "<label id=\"" . $field[ 'ID' ] . "Label\" for=\"" . $field[ 'ID' ] . '_' . $current_form_count . "\" class=\"" . implode( ' ', $field_classes[ 'field_label' ] ) . "\">",  
  3058. "label" => $field[ 'label' ],  
  3059. "label_required" => ( empty( $field[ 'hide_label' ] ) ? ( ! empty( $field[ 'required' ] ) ? " <span aria-hidden=\"true\" role=\"presentation\" class=\"" . implode( ' ', $field_classes[ 'field_required_tag' ] ) . "\" style=\"color:#ee0000;\">*</span>" : "" ) : null ),  
  3060. "label_after" => "</label>",  
  3061. "field_placeholder" => ( ! empty( $field[ 'hide_label' ] ) ? 'placeholder="' . htmlentities( $field[ 'label' ] ) . '"' : null ),  
  3062. "field_required" => ( ! empty( $field[ 'required' ] ) ? 'required="required"' : null ),  
  3063. "field_value" => null,  
  3064. "field_caption" => ( ! empty( $field[ 'caption' ] ) ? "<span id=\"" . $field[ 'ID' ] . "Caption\" class=\"" . implode( ' ', $field_classes[ 'field_caption' ] ) . "\">" . $field[ 'caption' ] . "</span>\r\n" : "" ),  
  3065. "field_after" => "</div>\r\n",  
  3066. "wrapper_after" => "</div>\r\n",  
  3067. "aria" => array() 
  3068. ); 
  3069.  
  3070. $field_structure[ 'aria' ][ 'labelledby' ] = $field[ 'ID' ] . 'Label'; 
  3071.  
  3072. // if has caption 
  3073. if ( ! empty( $field[ 'caption' ] ) ) { 
  3074. $field_structure[ 'aria' ][ 'describedby' ] = $field[ 'ID' ] . 'Caption'; 
  3075.  
  3076. // add error 
  3077. if ( ! empty( $field_errors ) ) { 
  3078. if ( is_string( $field_errors ) ) { 
  3079. $field_errors = array( $field_errors ); 
  3080.  
  3081. foreach ( $field_errors as $error ) { 
  3082. $field_structure[ 'field_caption' ] = "<span class=\"" . implode( ' ', $field_classes[ 'field_caption' ] ) . "\">" . $error . "</span>\r\n"; 
  3083.  
  3084.  
  3085. // value 
  3086. if ( isset( $field[ 'config' ][ 'default' ] ) ) { 
  3087.  
  3088. $field_structure[ 'field_value' ] = self::do_magic_tags( $field[ 'config' ][ 'default' ] ); 
  3089.  
  3090. // transient data 
  3091. if ( isset( $entry_data[ $field[ 'ID' ] ] ) ) { 
  3092. $field_structure[ 'field_value' ] = $entry_data[ $field[ 'ID' ] ]; 
  3093.  
  3094. $field_structure = apply_filters( 'caldera_forms_render_field_structure', $field_structure, $form ); 
  3095. $field_structure = apply_filters( 'caldera_forms_render_field_structure_type-' . $field[ 'type' ], $field_structure, $form ); 
  3096. $field_structure = apply_filters( 'caldera_forms_render_field_structure_slug-' . $field[ 'slug' ], $field_structure, $form ); 
  3097.  
  3098. // compile aria tags 
  3099. if ( ! empty( $field_structure[ 'aria' ] ) ) { 
  3100. $aria_atts = null; 
  3101. foreach ( $field_structure[ 'aria' ] as $att => $att_val ) { 
  3102. $aria_atts .= ' aria-' . $att . '="' . esc_attr( $att_val ) . '"'; 
  3103. $field_structure[ 'aria' ] = $aria_atts; 
  3104.  
  3105. $field_name = $field_structure[ 'name' ]; 
  3106. $field_id = $field_structure[ 'id' ] . '_' . $current_form_count; 
  3107. $wrapper_before = $field_structure[ 'wrapper_before' ]; 
  3108. $field_before = $field_structure[ 'field_before' ]; 
  3109. $field_label = $field_structure[ 'label_before' ] . $field_structure[ 'label' ] . $field_structure[ 'label_required' ] . $field_structure[ 'label_after' ] . "\r\n"; 
  3110. $field_placeholder = $field_structure[ 'field_placeholder' ]; 
  3111. $field_required = $field_structure[ 'field_required' ]; 
  3112. $field_caption = $field_structure[ 'field_caption' ]; 
  3113. $field_after = $field_structure[ 'field_after' ]; 
  3114. $wrapper_after = $field_structure[ 'wrapper_after' ]; 
  3115. // blank default 
  3116. $field_value = $field_structure[ 'field_value' ]; 
  3117. // setup base instance ID 
  3118. $field_base_id = $field[ 'ID' ]; 
  3119.  
  3120. // register strings 
  3121. $form_field_strings[ $field_structure[ 'id' ] ] = array( 
  3122. 'id' => $field_structure[ 'id' ],  
  3123. 'instance' => $current_form_count,  
  3124. 'slug' => $field[ 'slug' ],  
  3125. 'label' => $field[ 'label' ] 
  3126. ); 
  3127.  
  3128. $field_types = Caldera_Forms_Fields::get_all(); 
  3129.  
  3130. $field_file = $field_types[ $field[ 'type' ] ][ 'file' ]; 
  3131. /** 
  3132. * Filter the file path to be used for a field's HTML in front-end 
  3133. * @since 1.3.4 
  3134. * @param string $field_file The path to the file 
  3135. * @param string $field_type The field type 
  3136. * @param string $field The field ID. 
  3137. * @param string $field_structure Data to be used in field 
  3138. * @param array $form Current form (NOTE: May be null) 
  3139. */ 
  3140. $field_file = apply_filters( 'caldera_forms_render_field_file', $field_file, $field[ 'type' ], $field[ 'ID' ], $field_file, $field_structure, $form ); 
  3141.  
  3142. ob_start(); 
  3143. include $field_file; 
  3144. $field_html = apply_filters( 'caldera_forms_render_field', ob_get_clean(), $form ); 
  3145. $field_html = apply_filters( 'caldera_forms_render_field_type-' . $field[ 'type' ], $field_html, $form ); 
  3146. $field_html = apply_filters( 'caldera_forms_render_field_slug-' . $field[ 'slug' ], $field_html, $form ); 
  3147.  
  3148. return $field_html; 
  3149.  
  3150. /** 
  3151. * Create HTML markup for a form. 
  3152. * @param array|string $atts Form ID or shortcode atts or form config array 
  3153. * @param null|int $entry_id Optional. Entry ID to load data from. Null, the default, loads form for creating a new entry. 
  3154. * @param null $shortcode No longer used. 
  3155. * @return void|string HTML for form, if it was able to be laoded 
  3156. */ 
  3157. static public function render_form( $atts, $entry_id = null, $shortcode = null ) { 
  3158.  
  3159. global $current_form_count, $form, $post; 
  3160.  
  3161. if ( empty( $atts ) ) { 
  3162. return; 
  3163.  
  3164. if ( is_string( $atts ) ) { 
  3165.  
  3166. $form = Caldera_Forms_Forms::get_form( $atts ); 
  3167. $atts = array(); 
  3168.  
  3169. } elseif ( is_array( $atts ) && isset( $atts[ 'ID' ] ) ) { 
  3170. $form = Caldera_Forms_Forms::get_form( $atts[ 'ID' ] ); 
  3171. } else { 
  3172.  
  3173. if ( empty( $atts[ 'id' ] ) ) { 
  3174. if ( ! empty( $atts[ 'name' ] ) ) { 
  3175. $form = Caldera_Forms_Forms::get_form( $atts[ 'name' ] ); 
  3176. } elseif ( ! empty( $atts[ 'id' ] ) ) { 
  3177. $form = Caldera_Forms_Forms::get_form( $atts[ 'id' ] ); 
  3178.  
  3179. if ( empty( $form ) ) { 
  3180. return; 
  3181.  
  3182.  
  3183. // is this form allowed to render ( check state ) 
  3184. if ( isset( $form[ 'form_draft' ] ) ) { 
  3185. if ( ! isset( $_GET[ 'cf_preview' ] ) || $_GET[ 'cf_preview' ] != $form[ 'ID' ] ) { 
  3186. if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'cf_get_form_preview' ) { 
  3187. echo '<p style="color: #cf0000;">' . __( 'Form is currently not active.', 'caldera-forms' ) . '</p>'; 
  3188. } else { 
  3189. return; 
  3190.  
  3191. } else { 
  3192. echo '<div class="caldera-grid"><p class="alert alert-error alert-danger">' . __( 'Form is currently not active.', 'caldera-forms' ) . '</p></div>'; 
  3193.  
  3194. Caldera_Forms_Render_Assets::optional_style_includes(); 
  3195.  
  3196. if ( isset( $atts[ 'ajax' ] ) ) { 
  3197. if ( ! empty( $atts[ 'ajax' ] ) ) { 
  3198. $form[ 'form_ajax' ] = 1; 
  3199. } else { 
  3200. $form[ 'form_ajax' ] = 0; 
  3201. // set entry edit 
  3202. if ( ! empty( $atts[ 'entry' ] ) ) { 
  3203. $entry_id = self::do_magic_tags( $atts[ 'entry' ] ); 
  3204.  
  3205. /** 
  3206. * Filter form settings, before rendering form. 
  3207. * @since unknown 
  3208. * @param int $entry_id The entry ID. 
  3209. * @param array $form Form config. 
  3210. */ 
  3211. $form = apply_filters( 'caldera_forms_render_get_form', $form ); 
  3212.  
  3213. /** 
  3214. * Set entry ID when loading form 
  3215. * @since 1.2.3 
  3216. * @param int $entry_id The entry ID. 
  3217. * @param array $form Form config. 
  3218. */ 
  3219. $entry_id = apply_filters( 'caldera_forms_render_entry_id', $entry_id, $form ); 
  3220.  
  3221. if ( empty( $form ) ) { 
  3222. return; 
  3223.  
  3224. /** 
  3225. * Runs after form is loaded and before rendering starts 
  3226. * NOTE: An excellent way to conditionally abort form loading. 
  3227. * @since 1.3.4 
  3228. * @param null|string $html By default, null. If string is returned, method will immediately return that string. 
  3229. * @param int $entry_id The entry ID. 
  3230. * @param array $form Form config. 
  3231. * @param array $atts Shortcode/function atts 
  3232. */ 
  3233. $html = apply_filters( 'caldera_forms_pre_render_form', null, $entry_id, $form, $atts ); 
  3234. if ( is_string( $html ) ) { 
  3235. return $html; 
  3236.  
  3237.  
  3238. if ( empty( $current_form_count ) ) { 
  3239. $current_form_count = 0; 
  3240. $current_form_count += 1; 
  3241.  
  3242. // set instance 
  3243. if ( ! empty( $atts[ 'instance' ] ) ) { 
  3244. $current_form_count = absint( $atts[ 'instance' ] ); 
  3245.  
  3246. $field_types = Caldera_Forms_Fields::get_all(); 
  3247.  
  3248. do_action( 'caldera_forms_render_start', $form ); 
  3249.  
  3250. $form_attributes = array( 
  3251. 'method' => 'POST',  
  3252. 'enctype' => 'multipart/form-data',  
  3253. 'role' => 'form',  
  3254. 'id' => $form['ID'] . '_' . $current_form_count 
  3255. ); 
  3256.  
  3257. //add extra attributes to make AJAX submissions JS do its thing 
  3258. if( ! empty( $form[ 'form_ajax'] ) ) { 
  3259. add_filter('caldera_forms_render_form_attributes', 'cf_ajax_setatts', 10, 2); 
  3260.  
  3261.  
  3262. /** 
  3263. * Modify HTML attributes applied to form element 
  3264. * @since unknown 
  3265. * @param array $form_attributes Array of HTML attributes 
  3266. * @param array $config Form config 
  3267. */ 
  3268. $form_attributes = apply_filters( 'caldera_forms_render_form_attributes', $form_attributes, $form); 
  3269.  
  3270.  
  3271. include_once CFCORE_PATH . "classes/caldera-grid.php"; 
  3272.  
  3273. $gridsize = 'sm'; 
  3274. if ( ! empty( $form[ 'settings' ][ 'responsive' ][ 'break_point' ] ) ) { 
  3275. $gridsize = $form[ 'settings' ][ 'responsive' ][ 'break_point' ]; 
  3276.  
  3277. /** 
  3278. * What size grid to use for the grid 
  3279. * @param string $size Size for grid. Default is "sm" 
  3280. */ 
  3281. $gridsize = apply_filters( 'caldera_forms_render_set_grid_size', $gridsize ); 
  3282.  
  3283. // set grid render engine 
  3284. $grid_settings = array( 
  3285. "first" => 'first_row',  
  3286. "last" => 'last_row',  
  3287. "single" => 'single',  
  3288. "before" => '<div %1$s class="row %2$s">',  
  3289. "after" => '</div>',  
  3290. "column_first" => 'first_col',  
  3291. "column_last" => 'last_col',  
  3292. "column_single" => 'single',  
  3293. "column_before" => '<div %1$s class="col-'.$gridsize.'-%2$d %3$s">',  
  3294. "column_after" => '</div>',  
  3295. 'form_id' => $form['ID'],  
  3296. 'form_id_attr' => $form_attributes[ 'id'] 
  3297.  
  3298. ); 
  3299.  
  3300. // filter settings 
  3301. $grid_settings = apply_filters( 'caldera_forms_render_grid_settings', $grid_settings, $form ); 
  3302.  
  3303. $form[ 'grid_object' ] = new Caldera_Form_Grid( $grid_settings ); 
  3304. if ( empty( $form[ 'layout_grid' ] ) ) { 
  3305. $form[ 'layout_grid' ] = array( 'structure' => '' ); 
  3306.  
  3307. // Build Pages Breaks 
  3308. if ( false !== strpos( $form[ 'layout_grid' ][ 'structure' ], '#' ) ) { 
  3309. // setup pages 
  3310. $pages = explode( '#', $form[ 'layout_grid' ][ 'structure' ] ); 
  3311. $page_breaks = array(); 
  3312. foreach ( $pages as $page_no => $page ) { 
  3313. $point = substr_count( $page, '|' ) + 1; 
  3314. if ( isset( $page_breaks[ $page_no - 1 ] ) ) { 
  3315. $point += $page_breaks[ $page_no - 1 ]; 
  3316. $page_breaks[ $page_no ] = $point; 
  3317. $form[ 'layout_grid' ][ 'structure' ] = str_replace( '#', '|', $form[ 'layout_grid' ][ 'structure' ] ); 
  3318.  
  3319. // setup notcies 
  3320. $notices = array(); 
  3321.  
  3322. $note_general_classes = Caldera_Forms_Render_Notices::get_note_general_classes( $form ); 
  3323.  
  3324. $note_classes = Caldera_Forms_Render_Notices::get_note_classes( $note_general_classes, $form ); 
  3325. $field_errors = array(); 
  3326.  
  3327. // edit entry from url 
  3328. if ( ! empty( $_GET[ 'cf_ee' ] ) ) { 
  3329. $entry_id = $_GET[ 'cf_ee' ]; 
  3330.  
  3331. // attr entry id 
  3332. if ( ! empty( $atts[ 'entry' ] ) ) { 
  3333. $entry_id = $atts[ 'entry' ]; 
  3334.  
  3335. if ( ! empty( $entry_id ) ) { 
  3336. //check user can edit this item. 
  3337. $user_id = get_current_user_id(); 
  3338. $details = self::get_entry_detail( $entry_id, $form ); 
  3339. if ( ! empty( $_GET[ 'cf_et' ] ) ) { 
  3340. // build token 
  3341. $validated = Caldera_Forms_Entry_Token::verify_token( trim( $_GET[ 'cf_et' ] ), $_GET[ 'cf_et' ], $form[ 'ID' ] ); 
  3342. if ( ! is_wp_error( $validated ) ) { 
  3343. $notices[ 'error' ][ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  3344. } else { 
  3345. $entry_id = (int) $details[ 'id' ]; 
  3346. $edit_token = Caldera_Forms_Entry_Token::create_entry_token( $entry_id, $form[ 'ID' ] ); 
  3347.  
  3348. } else { 
  3349.  
  3350. if ( ! empty( $user_id ) ) { 
  3351.  
  3352. if ( ! empty( $details ) ) { 
  3353. // check user can edit 
  3354. if ( current_user_can( 'edit_posts' ) || $details[ 'user_id' ] === $user_id ) { 
  3355. // can edit. 
  3356. $entry_id = (int) $details[ 'id' ]; 
  3357. } else { 
  3358. $notices[ 'error' ][ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  3359. } else { 
  3360. $notices[ 'error' ][ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  3361.  
  3362. } else { 
  3363. $notices[ 'error' ][ 'note' ] = __( 'Permission denied or entry does not exist.', 'caldera-forms' ); 
  3364.  
  3365. if ( ! empty( $notices[ 'error' ][ 'note' ] ) ) { 
  3366. $halt_render = true; 
  3367. $entry_id = false; 
  3368.  
  3369. // check for prev post 
  3370. $prev_data = apply_filters( 'caldera_forms_render_pre_get_entry', array(), $form, $entry_id ); 
  3371.  
  3372. // load requested data 
  3373. if ( ! empty( $entry_id ) ) { 
  3374. $prev_entry = self::get_entry( $entry_id, $form ); 
  3375. $prev_data = array(); 
  3376. self::set_field_data( '_entry_id', $entry_id, $form ); 
  3377. foreach ( $prev_entry[ 'data' ] as $field_id => $entry_data ) { 
  3378. $prev_data[ $field_id ] = $entry_data[ 'value' ]; 
  3379. $prev_data = apply_filters( 'caldera_forms_render_get_entry', $prev_data, $form, $entry_id ); 
  3380.  
  3381.  
  3382. if(!empty($_GET['cf_er'])) { 
  3383. $prev_post = Caldera_Forms_Transient::get_transient( $_GET['cf_er'] ); 
  3384. if(!empty($prev_post['transient'])) { 
  3385.  
  3386. if ( $prev_post[ 'transient' ] === $_GET[ 'cf_er' ] ) { 
  3387. foreach ( $prev_post[ 'data' ] as $field_id => $field_entry ) { 
  3388.  
  3389. if ( ! isset( $form[ 'fields' ][ $field_id ] ) ) { 
  3390. continue; // ignore non field data 
  3391.  
  3392. if ( ! is_wp_error( $field_entry ) ) { 
  3393. $prev_data[ $field_id ] = $field_entry; 
  3394. if ( ! empty( $prev_post[ 'type' ] ) && ! empty( $prev_post[ 'note' ] ) ) { 
  3395. $notices[ $prev_post[ 'type' ] ][ 'note' ] = $prev_post[ 'note' ]; 
  3396. if ( ! empty( $prev_post[ 'error' ] ) && ! empty( $prev_post[ 'note' ] ) ) { 
  3397. $notices[ 'error' ][ 'note' ] = $prev_post[ 'note' ]; 
  3398. if ( ! empty( $prev_post[ 'fields' ] ) ) { 
  3399. $field_errors = array(); 
  3400. foreach ( $prev_post[ 'fields' ] as $field_id => $field_error ) { 
  3401.  
  3402. if ( is_wp_error( $field_error ) ) { 
  3403. $field_errors[ $field_id ] = $field_error->get_error_message(); 
  3404. } else { 
  3405. $field_errors[ $field_id ] = $field_error; 
  3406. // filter transient 
  3407. $prev_post = apply_filters( 'caldera_forms_render_get_transient', $prev_post, $form ); 
  3408.  
  3409. if ( ! empty( $_GET[ 'cf_su' ] ) && $current_form_count == $_GET[ 'cf_su' ] ) { 
  3410. if ( empty( $notices[ 'success' ][ 'note' ] ) ) { 
  3411. $notices[ 'success' ][ 'note' ] = $form[ 'success' ]; 
  3412.  
  3413.  
  3414. // build grid & pages 
  3415. $form[ 'grid_object' ]->setLayout( $form[ 'layout_grid' ][ 'structure' ] ); 
  3416.  
  3417. // insert page breaks 
  3418. if ( ! empty( $page_breaks ) ) { 
  3419. $currentpage = 1; 
  3420. if ( isset( $_GET[ 'cf_pg' ] ) && ! isset( $prev_post[ 'page' ] ) ) { 
  3421. $currentpage = (int) $_GET[ 'cf_pg' ]; 
  3422. } elseif ( isset( $prev_post[ 'page' ] ) ) { 
  3423. $currentpage = (int) $prev_post[ 'page' ]; 
  3424. $display = 'none'; 
  3425. $hidden = 'true'; 
  3426. if ( $currentpage === 1 ) { 
  3427. $display = 'block'; 
  3428. $hidden = 'false'; 
  3429.  
  3430. $total_rows = substr_count( $form[ 'layout_grid' ][ 'structure' ], '|' ) + 1; 
  3431. $form[ 'grid_object' ]->before( '<div id="form_page_' . $current_form_count . '_pg_1" data-formpage="1" class="caldera-form-page" style="display:' . $display . ';" role="region" aria-labelledby="breadcrumb_' . $current_form_count . '_pg_1" aria-hidden="' . $hidden . '">', 1 ); 
  3432. $form[ 'grid_object' ]->after( '</div>', $total_rows ); 
  3433. foreach ( $page_breaks as $page => $break ) { 
  3434.  
  3435. $form[ 'grid_object' ]->after( '</div>', $break ); 
  3436.  
  3437. if ( $break + 1 <= $total_rows ) { 
  3438. $display = 'none'; 
  3439. $hidden = 'true'; 
  3440. if ( $page + 2 == $currentpage ) { 
  3441. $display = 'block'; 
  3442. $hidden = 'false'; 
  3443.  
  3444. $form[ 'grid_object' ]->before( '<div id="form_page_' . $current_form_count . '_pg_' . ( $page + 2 ) . '" data-formpage="' . ( $page + 2 ) . '" role="region" aria-labelledby="breadcrumb_' . $current_form_count . '_pg_' . ( $page + 2 ) . '" aria-hidden="' . $hidden . '" class="caldera-form-page" style="display:' . $display . ';">', $break + 1 ); 
  3445.  
  3446.  
  3447.  
  3448. // setup processor bound requieds 
  3449. if ( ! empty( $form[ 'processors' ] ) ) { 
  3450. $bound_fields = array(); 
  3451. foreach ( $form[ 'processors' ] as $processor_id => $processor ) { 
  3452. if ( ! empty( $processor[ 'config' ][ '_required_bounds' ] ) ) { 
  3453. foreach ( $processor[ 'config' ] as $slug => &$value ) { 
  3454. if ( $slug == '_required_bounds' ) { 
  3455. continue; 
  3456. if ( in_array( $slug, $processor[ 'config' ][ '_required_bounds' ] ) ) { 
  3457. $bound_fields = array_merge( $bound_fields, self::search_array_fields( $value, array_keys( $form[ 'fields' ] ) ) ); 
  3458. foreach ( $bound_fields as $bound ) { 
  3459. $form[ 'fields' ][ $bound ][ 'required' ] = 1; 
  3460.  
  3461. $conditions_templates = array(); 
  3462. $conditions_configs = array(); 
  3463. $used_slugs = array(); 
  3464. $form_field_strings = array(); 
  3465.  
  3466. if ( ! empty( $form[ 'fields' ] ) ) { 
  3467. // prepare fields 
  3468. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  3469. $field = apply_filters( 'caldera_forms_render_get_field', $field, $form ); 
  3470. $field = apply_filters( 'caldera_forms_render_get_field_type-' . $field[ 'type' ], $field, $form ); 
  3471. $field = apply_filters( 'caldera_forms_render_get_field_slug-' . $field[ 'slug' ], $field, $form ); 
  3472. $form[ 'fields' ][ $field_id ] = $field; 
  3473.  
  3474.  
  3475. if ( ! empty( $form[ 'layout_grid' ][ 'fields' ] ) ) { 
  3476.  
  3477. foreach ( $form[ 'layout_grid' ][ 'fields' ] as $field_base_id => $location ) { 
  3478.  
  3479. if ( isset( $form[ 'fields' ][ $field_base_id ] ) ) { 
  3480. $field = self::load_field( $form, $field_base_id ); 
  3481.  
  3482.  
  3483. if ( empty( $field ) || ! isset( $field_types[ $field[ 'type' ] ][ 'file' ] ) || ! file_exists( $field_types[ $field[ 'type' ] ][ 'file' ] ) ) { 
  3484. continue; 
  3485.  
  3486. $field[ 'grid_location' ] = $location; 
  3487. //$field[ 'page' ] = $cr 
  3488.  
  3489. Caldera_Forms_Render_Assets::enqueue_field_scripts( $field_types, $field ); 
  3490.  
  3491.  
  3492. $field_base_id = $field[ 'ID' ] . '_' . $current_form_count; 
  3493.  
  3494. $field_error = array(); 
  3495. if ( isset( $field_errors[ $field[ 'ID' ] ] ) ) { 
  3496. $field_error = $field_errors[ $field[ 'ID' ] ]; 
  3497.  
  3498. $field_html = self::render_field( $field, $form, $prev_data, $field_error ); 
  3499. // conditional wrapper 
  3500. if ( ! empty( $field[ 'conditions' ][ 'group' ] ) && ! empty( $field[ 'conditions' ][ 'type' ] ) ) { 
  3501.  
  3502. $conditions_configs[ $field_base_id ] = $field[ 'conditions' ]; 
  3503.  
  3504. if ( $field[ 'conditions' ][ 'type' ] !== 'disable' ) { 
  3505. // wrap it up 
  3506. $conditions_templates[ $field_base_id ] = "<script type=\"text/html\" id=\"conditional-" . $field_base_id . "-tmpl\">\r\n" . $field_html . "</script>\r\n"; 
  3507. // add in instance number 
  3508. if ( ! empty( $field[ 'conditions' ][ 'group' ] ) ) { 
  3509. foreach ( $field[ 'conditions' ][ 'group' ] as &$group_row ) { 
  3510. foreach ( $group_row as &$group_line ) { 
  3511. // add instance value 
  3512. $group_line[ 'instance' ] = $current_form_count; 
  3513.  
  3514. if ( $field[ 'conditions' ][ 'type' ] == 'show' || $field[ 'conditions' ][ 'type' ] == 'wdisable' ) { 
  3515. // show if indicates hidden by default until condition is matched. 
  3516. $field_html = null; 
  3517. // wrapp it up 
  3518. $field_html = '<span class="caldera-forms-conditional-field" role="region" aria-live="polite" id="conditional_' . $field_base_id . '" data-field-id="' . $field_base_id . '">' . $field_html . '</span>'; 
  3519.  
  3520. $form[ 'grid_object' ]->append( $field_html, $field[ 'grid_location' ] ); 
  3521.  
  3522.  
  3523.  
  3524. // form object strings 
  3525. wp_localize_script( 'cf-dynamic', $form[ 'ID' ] . '_' . $current_form_count, $form_field_strings ); 
  3526.  
  3527. // do grid 
  3528. $form[ 'grid_object' ] = apply_filters( 'caldera_forms_render_grid_structure', $form[ 'grid_object' ], $form ); 
  3529. // wrapper classes 
  3530. $form_wrapper_classes = array( 
  3531. "caldera-grid" 
  3532. ); 
  3533.  
  3534. /** 
  3535. * Change classes of elements wrapping the grid 
  3536. * @since unknown 
  3537. * @param array $form_wrapper_classes Array of classes 
  3538. * @param array $config Form config 
  3539. */ 
  3540. $form_wrapper_classes = apply_filters( 'caldera_forms_render_form_wrapper_classes', $form_wrapper_classes, $form ); 
  3541.  
  3542. $form_wrap_id = Caldera_Forms_Render_Util::form_id_attr( $current_form_count ); 
  3543. $out = sprintf( '<div class="%s" id="%s" data-cf-ver="%s" data-cf-form-id="%s">', esc_attr( implode( ' ', $form_wrapper_classes ) ), esc_attr( $form_wrap_id ), esc_attr( CFCORE_VER ), esc_attr( $form[ 'ID' ] ) ); 
  3544.  
  3545. $notices = Caldera_Forms_Render_Notices::prepare_notices( $notices, $form ); 
  3546.  
  3547. // set debug notice 
  3548. if ( ! empty( $form[ 'mailer' ][ 'enable_mailer' ] ) && ! empty( $form[ 'debug_mailer' ] ) ) { 
  3549. $notices[ 'error' ] = array( 'note' => __( 'WARNING: Form is in Mailer Debug mode. Disable before going live.', 'caldera-forms' ) ); 
  3550.  
  3551. $out .= '<div id="caldera_notices_' . $current_form_count . '" data-spinner="' . admin_url( 'images/spinner.gif' ) . '">'; 
  3552. if ( ! empty( $notices ) ) { 
  3553. // do notices 
  3554. // entry id 
  3555. if ( isset( $_GET[ 'cf_id' ] ) ) { 
  3556. $notice_entry_id = (int) $_GET[ 'cf_id' ]; 
  3557. } elseif ( ! empty( $entry_id ) ) { 
  3558. $notice_entry_id = $entry_id; 
  3559. } else { 
  3560. $notice_entry_id = null; 
  3561.  
  3562. foreach ( $notices as $note_type => $notice ) { 
  3563. if ( ! empty( $notice[ 'note' ] ) ) { 
  3564. $out .= '<div class=" ' . implode( ' ', $note_classes[ $note_type ] ) . '">' . self::do_magic_tags( $notice[ 'note' ], $notice_entry_id ) . '</div>'; 
  3565.  
  3566. $out .= '</div>'; 
  3567. if ( ( empty( $notices[ 'success' ] ) || empty( $form[ 'hide_form' ] ) ) && empty( $halt_render ) ) { 
  3568.  
  3569. $form_element = 'form'; 
  3570.  
  3571. $form_classes = array( 
  3572. $form[ 'ID' ],  
  3573. 'caldera_forms_form',  
  3574. ); 
  3575.  
  3576. /** 
  3577. * Change what type of element form is in. 
  3578. * Note: Using anything besides "form" here will make form cease to function as a form. 
  3579. * @since unknown 
  3580. * @param string $form_element Form element type. 
  3581. * @param array $config Form config 
  3582. */ 
  3583. $form_element = apply_filters( 'caldera_forms_render_form_element', $form_element, $form ); 
  3584.  
  3585. /** 
  3586. * Modify classes applied to form element 
  3587. * @since unknown 
  3588. * @param array $form_classes Array of classes 
  3589. * @param array $config Form config 
  3590. */ 
  3591. $form_classes = apply_filters( 'caldera_forms_render_form_classes', $form_classes, $form ); 
  3592.  
  3593.  
  3594. $attributes = array(); 
  3595. foreach ( $form_attributes as $attribute => $value ) { 
  3596. $attributes[] = $attribute . '="' . htmlentities( $value ) . '"'; 
  3597.  
  3598. // render only non success 
  3599. $out .= "<" . $form_element . " data-instance=\"" . $current_form_count . "\" class=\"" . implode( ' ', $form_classes ) . "\" " . implode( " ", $attributes ) . ">\r\n"; 
  3600. $out .= Caldera_Forms_Render_Nonce::nonce_field( $form[ 'ID' ] ); 
  3601. $out .= "<input type=\"hidden\" name=\"_cf_frm_id\" value=\"" . $form[ 'ID' ] . "\">\r\n"; 
  3602. $out .= "<input type=\"hidden\" name=\"_cf_frm_ct\" value=\"" . $current_form_count . "\">\r\n"; 
  3603. if ( ! empty( $form[ 'form_ajax' ] ) ) { 
  3604. $out .= "<input type=\"hidden\" name=\"cfajax\" value=\"" . $form[ 'ID' ] . "\">\r\n"; 
  3605. if ( is_object( $post ) ) { 
  3606. $out .= "<input type=\"hidden\" name=\"_cf_cr_pst\" value=\"" . $post->ID . "\">\r\n"; 
  3607.  
  3608. // user transient for continuation 
  3609. if ( ! empty( $prev_post[ 'transient' ] ) ) { 
  3610. $out .= "<input type=\"hidden\" name=\"_cf_frm_tr\" value=\"" . $prev_post[ 'transient' ] . "\">\r\n"; 
  3611. // is edit? 
  3612. if ( ! empty( $entry_id ) ) { 
  3613. $out .= "<input type=\"hidden\" name=\"_cf_frm_edt\" value=\"" . $entry_id . "\">\r\n"; 
  3614.  
  3615. // is edit via token? 
  3616. if ( ! empty( $edit_token ) ) { 
  3617. $out .= "<input type=\"hidden\" name=\"_cf_frm_edt_tkn\" value=\"" . $edit_token . "\">\r\n"; 
  3618.  
  3619. //setup fieldjs 
  3620. Caldera_Forms_Field_Localizer::add_form( $form, Caldera_Forms_Render_Util::get_current_form_count() ); 
  3621.  
  3622.  
  3623. // auto pagination 
  3624. if ( ! empty( $form[ 'auto_progress' ] ) && count( $form[ 'page_names' ] ) > 1 ) { 
  3625.  
  3626. // retain query string 
  3627. $qurystr = array(); 
  3628. parse_str( $_SERVER[ 'QUERY_STRING' ], $qurystr ); 
  3629. $out .= "<span class=\"caldera-grid\"><ol class=\"breadcrumb\" data-form=\"caldera_form_" . $current_form_count . "\">\r\n"; 
  3630. $current_page = 1; 
  3631. if ( ! empty( $_GET[ 'cf_pg' ] ) ) { 
  3632. $current_page = $_GET[ 'cf_pg' ]; 
  3633. foreach ( $form[ 'page_names' ] as $page_key => $page_name ) { 
  3634. $tabclass = null; 
  3635. $expanded = 'false'; 
  3636. if ( $current_page == $page_key + 1 ) { 
  3637. $tabclass = ' class="active"'; 
  3638. $expanded = 'true'; 
  3639.  
  3640. $qurystr[ 'cf_pg' ] = $page_key + 1; 
  3641. $out .= "<li" . $tabclass . "><a aria-controls=\"form_page_" . $current_form_count . "_pg_" . ( $page_key + 1 ) . "\" aria-expanded=\"" . $expanded . "\" id=\"breadcrumb_" . $current_form_count . "_pg_" . ( $page_key + 1 ) . "\" href=\"?" . http_build_query( $qurystr ) . "\" data-page=\"" . ( $page_key + 1 ) . "\" data-pagenav=\"caldera_form_" . $current_form_count . "\" title=\"" . sprintf( __( 'Navigate to %s', 'caldera-forms' ), $page_name ) . "\">" . $page_name . "</a></li>\r\n"; 
  3642. $out .= "</ol></span>\r\n"; 
  3643.  
  3644. // sticky sticky honey 
  3645. if ( isset( $form[ 'check_honey' ] ) ) { 
  3646. $out .= "<div class=\"hide\" style=\"display:none; overflow:hidden;height:0;width:0;\">\r\n"; 
  3647.  
  3648. /** 
  3649. * Change which words are used to form honey pot 
  3650. * @since unknown 
  3651. * @param array $words An array of words. 
  3652. */ 
  3653. $honey_words = apply_filters( 'caldera_forms_get_honey_words', array( 
  3654. 'web_site',  
  3655. 'url',  
  3656. 'email',  
  3657. 'company',  
  3658. 'name' 
  3659. ) ); 
  3660. $word = $honey_words[ rand( 0, count( $honey_words ) - 1 ) ]; 
  3661. $out .= "<label>" . ucwords( str_replace( '_', ' ', $word ) ) . "</label><input type=\"text\" name=\"" . $word . "\" value=\"\" autocomplete=\"off\">\r\n"; 
  3662. $out .= "</div>"; 
  3663.  
  3664. $out .= $form[ 'grid_object' ]->renderLayout(); 
  3665.  
  3666. $out .= "</" . $form_element . ">\r\n"; 
  3667.  
  3668. $out .= "</div>\r\n"; 
  3669.  
  3670. // output javascript conditions. 
  3671. if ( ! empty( $conditions_configs ) ) { 
  3672. // sortout magics 
  3673. foreach ( $conditions_configs as &$condition_field_conf ) { 
  3674. if ( ! empty( $condition_field_conf[ 'group' ] ) ) { 
  3675. foreach ( $condition_field_conf[ 'group' ] as &$condition_group ) { 
  3676. if ( ! empty( $condition_group ) ) { 
  3677. foreach ( $condition_group as &$condition_line ) { 
  3678.  
  3679. if ( isset( $form[ 'fields' ][ $condition_line[ 'field' ] ][ 'config' ][ 'option' ][ $condition_line[ 'value' ] ] ) ) { 
  3680. $condition_line[ 'label' ] = $form[ 'fields' ][ $condition_line[ 'field' ] ][ 'config' ][ 'option' ][ $condition_line[ 'value' ] ][ 'label' ]; 
  3681. $condition_line[ 'value' ] = $form[ 'fields' ][ $condition_line[ 'field' ] ][ 'config' ][ 'option' ][ $condition_line[ 'value' ] ][ 'value' ]; 
  3682. } else { 
  3683.  
  3684. if ( false !== strpos( $condition_line[ 'field' ], '{' ) && false !== strpos( $condition_line[ 'field' ], '}' ) ) { 
  3685. $condition_line[ 'field' ] = self::do_magic_tags( $condition_line[ 'field' ] ); 
  3686.  
  3687. //strip out fields 
  3688. $regex = "/%([a-zA-Z0-9_:]*)%/"; 
  3689. preg_match_all( $regex, $condition_line[ 'value' ], $matches ); 
  3690. if ( ! empty( $matches[ 1 ] ) ) { 
  3691. foreach ( $matches[ 1 ] as $field_slug ) { 
  3692. $value_field = self::get_field_by_slug( $field_slug, $form ); 
  3693. $condition_line[ 'selectors' ][ $value_field[ 'ID' ] ] = '[data-field="' . $value_field[ 'ID' ] . '"]'; 
  3694. $condition_line[ 'value' ] = str_replace( '%' . $field_slug . '%', $value_field[ 'ID' ], $condition_line[ 'value' ] ); 
  3695. } else { 
  3696. $condition_line[ 'value' ] = self::do_magic_tags( $condition_line[ 'value' ] ); 
  3697.  
  3698. $conditions_str = wp_json_encode( $conditions_configs ); 
  3699.  
  3700. $hastags = Caldera_Forms_Magic_Util::explode_field_magic( $conditions_str ); 
  3701. if ( ! empty( $hastags[ 1 ] ) ) { 
  3702.  
  3703. foreach ( $hastags[ 1 ] as $tag_key => $tag ) { 
  3704.  
  3705. foreach ( $form[ 'fields' ] as $field_id => $field ) { 
  3706. if ( $field[ 'slug' ] === $tag ) { 
  3707. $conditions_str = str_replace( '"' . $hastags[ 0 ][ $tag_key ] . '"', "function() { return jQuery('#" . $field[ 'ID' ] . '_' . $current_form_count . "').val(); }", $conditions_str ); 
  3708.  
  3709. $out .= "<script type=\"text/javascript\">\r\n"; 
  3710. $out .= 'if( typeof caldera_conditionals === "undefined" ) { var caldera_conditionals = {}; }'; 
  3711. $out .= "caldera_conditionals." . $form[ 'ID' ] . '_' . $current_form_count . " = " . $conditions_str . ";\r\n"; 
  3712. $out .= "</script>\r\n"; 
  3713. if ( ! empty( $conditions_templates ) ) { 
  3714. $out .= implode( "\r\n", $conditions_templates ); 
  3715.  
  3716. // enqueue conditionls app. 
  3717. Caldera_Forms_Render_Assets::enqueue_script( 'conditionals' ); 
  3718.  
  3719. /** 
  3720. * Runs after form is rendered 
  3721. * @since unknown 
  3722. * @param array $config Form config 
  3723. */ 
  3724. do_action( 'caldera_forms_render_end', $form ); 
  3725.  
  3726. Caldera_Forms_Render_Assets::enqueue_form_assets(); 
  3727.  
  3728. /** 
  3729. * Filter final HTML of form 
  3730. * @since unknow 
  3731. * @param string $out The HTML 
  3732. * @param array $config Form config 
  3733. */ 
  3734. return apply_filters( 'caldera_forms_render_form', $out, $form ); 
  3735.  
  3736.  
  3737. /** 
  3738. * Returns the capability to manage Caldera Forms 
  3739. * By default, returns "manage_options" can be filtered with "caldera_forms_manage_cap" 
  3740. * @since 1.3.1 
  3741. * @param string $context Optional. Context for checking capabilities. 
  3742. * @return mixed|void 
  3743. */ 
  3744. public static function get_manage_cap( $context = 'admin', $form = false ) { 
  3745. if ( is_string( $form ) ) { 
  3746. $form = Caldera_Forms_Forms::get_form( $form ); 
  3747.  
  3748. /** 
  3749. * Change capability for managing Caldera Forms 
  3750. * @since 1.3.1 
  3751. * @param string $cap A capability. By default "manage_options" 
  3752. * @param string $context Context to check in. 
  3753. * @param array|null $form Form config if it was passed. 
  3754. */ 
  3755. return apply_filters( 'caldera_forms_manage_cap', 'manage_options', $context, $form ); 
  3756.  
  3757.  
  3758. /** 
  3759. * Handler for shortcode 
  3760. * @since 1.3.1 
  3761. * @param array $atts Array of shortcode attributes 
  3762. * @param string $content Enclosed content 
  3763. * @param string $shortcode Shortcode type caldera_form|caldera_forms_modal 
  3764. * @return string|void 
  3765. */ 
  3766. public static function shortcode_handler( $atts, $content, $shortcode ) { 
  3767. if( ! in_array( $shortcode, array( 
  3768. 'caldera_form',  
  3769. 'caldera_form_modal' 
  3770.  
  3771. ) ) ) { 
  3772. return; 
  3773.  
  3774. $atts = shortcode_atts(array ( 
  3775. 'id' => null,  
  3776. 'width' => null,  
  3777. 'height' => null,  
  3778. 'type' => 'link',  
  3779. 'entry' => null,  
  3780. 'ID' => null,  
  3781. ), $atts, $shortcode ); 
  3782.  
  3783. if( ! empty( $atts[ 'ID' ] ) && empty( $atts[ 'id' ] )) { 
  3784. $atts[ 'id' ] = $atts[ 'ID' ]; 
  3785. if ( ! isset( $atts[ 'id' ] ) ) { 
  3786. return; 
  3787.  
  3788. if ( $shortcode === 'caldera_form_modal' || ( ! empty( $atts[ 'modal' ] ) && $atts[ 'modal' ] ) ) { 
  3789. return Caldera_Forms_Render_Modals::modal_form( $atts, $content ); 
  3790.  
  3791. $form = self::render_form( $atts ); 
  3792.  
  3793.  
  3794. return $form; 
  3795.  
  3796.  
  3797. /** 
  3798. * Convert time entry was submitted (as MySQL timestamp in UTC) to local display time 
  3799. * @since 1.4.0 
  3800. * @param string $submitted Timestamp 
  3801. * @return string 
  3802. */ 
  3803. public static function localize_time( $submitted ) { 
  3804.  
  3805.  
  3806. $format = self::time_format(); 
  3807. $time = get_date_from_gmt( $submitted, $format ); 
  3808.  
  3809. return $time; 
  3810.  
  3811. /** 
  3812. * Get time format 
  3813. * @since 1.5.0 
  3814. * @return string 
  3815. */ 
  3816. public static function time_format() { 
  3817. $dateformat = get_option( 'date_format' ); 
  3818. $timeformat = get_option( 'time_format' ); 
  3819.  
  3820. $format = $dateformat . ' ' . $timeformat; 
  3821. return $format; 
  3822.  
  3823. /** 
  3824. * Setup auto-population options for Easy Pods and Easy Queries 
  3825. * @since 1.4.3 
  3826. * @uses "caldera_forms_render_start" action 
  3827. */ 
  3828. public static function easy_pods_queries_setup() { 
  3829. if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { 
  3830. if ( function_exists( 'cep_get_easy_pod' ) || defined( 'CAEQ_PATH' ) ) { 
  3831. $setup = new Caldera_Forms_Render_AutoPopulation(); 
  3832. $setup->add_hooks(); 
  3833.  
  3834.  
  3835.  
  3836. /** 
  3837. * Load the Caldera Forms REST API 
  3838. * @since 1.4.4 
  3839. * @uses "rest_api_init" action 
  3840. */ 
  3841. public static function init_rest_api() { 
  3842.  
  3843. self::$api = new Caldera_Forms_API_Load( Caldera_Forms_API_Util::api_namespace() ); 
  3844.  
  3845. /** 
  3846. * Runs after REST API loader is initialized, but before routes are initialized. 
  3847. * Use this hook to register routes in add-ons 
  3848. * do_action( 'caldera_forms_rest_api_pre_init', function( $api ) { $api->add_route(... 
  3849. * @since 1.4.4 
  3850. * @param Caldera_Forms_API_Load $api API Load class 
  3851. */ 
  3852. do_action( 'caldera_forms_rest_api_pre_init', self::$api ); 
  3853.  
  3854. self::$api->add_route( new Caldera_Forms_API_Tokens() ); 
  3855. self::$api->add_route( new Caldera_Forms_API_Entries() ); 
  3856. self::$api->add_route( new Caldera_Forms_API_Forms() ); 
  3857. self::$api->add_route( new Caldera_Forms_API_Settings() ); 
  3858. self::$api->init_routes(); 
  3859.  
  3860. /** 
  3861. * Runs after Caldera Forms REST API is loaded 
  3862. * @since 1.4.4 
  3863. */ 
  3864. do_action( 'caldera_forms_rest_api_init' ); 
  3865.  
  3866.  
  3867. public static function should_send_mail( $form, $transadata = array() ) { 
  3868. $send = true; 
  3869. if ( empty( $transadata ) ) { 
  3870. global $transadata; 
  3871.  
  3872. if ( ! empty( $transdata[ 'edit' ] ) ) { 
  3873. // update 
  3874. if ( empty( $form[ 'mailer' ][ 'on_update' ] ) ) { 
  3875. $send = false; 
  3876. } else { 
  3877. // insert 
  3878. if ( empty( $form[ 'mailer' ][ 'enable_mailer' ] ) && empty( $form[ 'mailer' ][ 'on_insert' ] ) ) { 
  3879. $send = false; 
  3880.  
  3881. /** 
  3882. * Change programmed decision to send mailer or not 
  3883. * Useful for causing emails to send on entry edit, when they normally would not 
  3884. * @since 1.4.4 
  3885. * @param bool $send Whether to send or not 
  3886. * @param array $form Form config 
  3887. */ 
  3888. return apply_filters( 'caldera_forms_send_email', $send, $form ); 
  3889.  
  3890.  
  3891. /** 
  3892. * The one true handler for submissions via POST 
  3893. * Does nonce check and then processes and returns, else 400. 
  3894. * @since 1.5.0 
  3895. */ 
  3896.  
  3897. public static function process_form_via_post() { 
  3898. if (isset($_POST['_cf_frm_id'])) { 
  3899. if ( isset( $_POST[ '_cf_verify' ] ) && Caldera_Forms_Render_Nonce::verify_nonce( $_POST[ '_cf_verify' ], $_POST[ '_cf_frm_id' ] ) ) { 
  3900. $submission = Caldera_Forms::process_submission(); 
  3901. wp_send_json($submission); 
  3902. exit; 
  3903.  
  3904.  
  3905. status_header(400); 
  3906. $form = Caldera_Forms_Forms::get_form($_POST['_cf_frm_id']); 
  3907. $notices = array(); 
  3908. $notices['error']['note'] = __('Submission rejected, token invalid', 'caldera-forms'); 
  3909.  
  3910. $note_general_classes = Caldera_Forms_Render_Notices::get_note_general_classes($form); 
  3911.  
  3912. $note_classes = Caldera_Forms_Render_Notices::get_note_classes($note_general_classes, $form); 
  3913.  
  3914. $out = array( 
  3915. 'html' => Caldera_Forms_Render_Notices::html_from_notices($notices, $note_classes),  
  3916.  
  3917. ); 
  3918.  
  3919.  
  3920. wp_send_json_error($out); 
  3921. exit; 
  3922.  
  3923.  
  3924.  
  3925.  
  3926. /** 
  3927. * Apply wpautop to email message. 
  3928. * This was separated out from main email generation method in 1.4.7 so it would be removable, see: https://github.com/CalderaWP/Caldera-Forms/issues/1048 
  3929. * @since 1.4.7 
  3930. * @uses "caldera_forms_mailer" filter 
  3931. * @param array $mail 
  3932. * @return mixed 
  3933. */ 
  3934. public static function format_message( $mail ) { 
  3935. $mail[ 'message' ] = wpautop( $mail[ 'message' ] ); 
  3936. return $mail; 
  3937.  
  3938.