/** * Get one form * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_form($request) { $form_id = $request; if ($request instanceof \WP_REST_Request) { $form_id = $request->get_param('id'); } $form = \Caldera_Forms::get_form($form_id); if (null === $form) { return new \WP_Error('invalid_form_id', __('Invalid Form ID', 'caldera-forms')); } return new \WP_REST_Response($form, 200); }
/** * Get Caldera Forms * * Includes backwards compat for pre-Caldera Forms 1.3.4 * * @since 2.0.5 * * @param string $id_name ID or name of form * * @return array|void */ function cf_custom_fields_get_form($id_name) { if (class_exists('Caldera_Forms_Forms')) { $form = Caldera_Forms_Forms::get_form($id_name); } else { $form = Caldera_Forms::get_form($id_name); } if (isset($form['ID']) && !isset($form['id'])) { $form['id'] = $form['ID']; } return $form; }
/** * Constructor for class * * @since 1.0.0 */ public function __construct() { // add admin page add_action('admin_menu', array($this, 'add_settings_pages'), 25); // save config add_action('wp_ajax_frmwks_save_config', array($this, 'save_config')); // exporter add_action('init', array($this, 'check_exporter')); // get forms list add_filter('formworks_get_forms', array($this, 'get_forms')); add_action('wp_ajax_frmwks_module_data', array($this, 'module_data_loader')); if (current_user_can('manage_options')) { add_action('wp_ajax_frmwks_rebuild_database', array($this, 'rebuild_database')); //add_action( 'wp_ajax_frmwks_reset_form_stats', array( $this, 'reset_form_stats') ); } // create new add_action('wp_ajax_frmwks_create_formworks', array($this, 'create_new_formworks')); // delete add_action('wp_ajax_frmwks_delete_formworks', array($this, 'delete_formworks')); add_filter('formworks_stats_field_name', function ($field, $form_prefix, $form_id) { switch ($form_prefix) { case 'caldera': if (false !== strpos($field, '[')) { $field = strtok($field, '['); } // is CF $form = \Caldera_Forms::get_form($form_id); if (empty($form)) { continue; } if (!empty($form['fields'][$field]['label'])) { $field = $form['fields'][$field]['label']; } break; case 'gform': //get gravity form if (!class_exists('RGFormsModel')) { continue; } $form_info = \RGFormsModel::get_form($form_id); break; case 'ninja': //get ninja form if (!function_exists('Ninja_Forms')) { continue; } $form_name = Ninja_Forms()->form($form_id)->get_setting('form_title'); $form_id = $form_id; break; case 'cf7': //get contact form 7 if (!class_exists('WPCF7_ContactForm')) { continue; } $cf7form = \WPCF7_ContactForm::get_instance($form_id); $form_name = $cf7form->title(); $form_id = $cf7form->id(); break; case 'frmid': if (!class_exists('FrmForm')) { continue; } $field_id = (int) strtok(str_replace('item_meta[', '', $field), ']'); $form_field = \FrmField::getOne($field_id); $field = $form_field->name; if (!empty($form_field->description) && $form_field->description != $form_field->name) { $field .= ':' . $form_field->description; } break; case 'jp': $form_post = get_post($form_id); if (empty($form_post)) { continue; } $field = ucwords(str_replace('g' . $form_id . '-', '', $field)); break; default: //no idea what this is or the form plugin was disabled. break; } return $field; }, 10, 3); }
static function save_form() { /// check for form delete if (!empty($_GET['delete']) && !empty($_GET['cal_del']) && current_user_can('manage_options')) { if (!wp_verify_nonce($_GET['cal_del'], 'cf_del_frm')) { // This nonce is not valid. wp_die(__('Sorry, please try again', 'caldera-forms'), __('Form Delete Error', 'caldera-forms')); } else { // ok to delete // get form registry $forms = Caldera_Forms::get_forms(true); if (isset($forms[$_GET['delete']])) { unset($forms[$_GET['delete']]); $form = Caldera_Forms::get_form($_GET['delete']); if (empty($form)) { do_action('caldera_forms_delete_form', $_GET['delete']); update_option('_caldera_forms', $forms); } else { if (delete_option($_GET['delete'])) { do_action('caldera_forms_delete_form', $_GET['delete']); update_option('_caldera_forms', $forms); } } } wp_redirect('admin.php?page=caldera-forms'); exit; } } if (isset($_POST['cfimporter']) && current_user_can('manage_options')) { if (check_admin_referer('cf-import', 'cfimporter')) { if (!empty($_FILES['import_file']['size'])) { $loc = wp_upload_dir(); if (move_uploaded_file($_FILES['import_file']['tmp_name'], $loc['path'] . '/cf-form-import.json')) { $data = json_decode(file_get_contents($loc['path'] . '/cf-form-import.json'), true); if (isset($data['ID']) && isset($data['name'])) { // generate a new ID $data['ID'] = uniqid('CF'); $data['name'] = $_POST['name']; // rebuild field IDS /* if( !empty( $data['fields'] ) ){ $old_fields = array(); $fields = $data['fields']; $layout_fields = $data['layout_grid']['fields']; $data['layout_grid']['fields'] = $data['fields'] = array(); foreach( $fields as $field ){ $field_id = uniqid('fld_'); $old_fields[$field['ID']] = $field_id; $data['layout_grid']['fields'][$field_id] = $layout_fields[$field['ID']]; $field['ID'] = $field_id; $data['fields'][$field_id] = $field; } foreach( $data['fields'] as $field ){ // rebind conditions if( !empty( $field['conditions']['group'] ) ){ foreach( $field['conditions']['group'] as $group_id=>$group ){ foreach( $group as $group_line_id=>$group_line ){ $data['fields'][$field['ID']]['conditions']['group'][$group_id][$group_line_id]['field'] = $old_fields[$group_line['field']]; } } } } } // rebuild processor IDS if( !empty( $data['processors'] ) ){ $processors = $data['processors']; $data['processors'] = array(); $old_processors = array(); foreach( $processors as $processor ){ $processor_id = uniqid('fp_'); $old_processors[$processor['ID']] = $processor_id; $processor['ID'] = $processor_id; // fix binding if( !empty( $processor['config'] ) && !empty( $data['fields'] ) ){ foreach( $processor['config'] as $config_key => &$config_value ){ if( is_string($config_value) ){ foreach( $old_fields as $old_field=>$new_field ){ $config_value = str_replace( $old_field, $new_field, $config_value ); } } } } $data['processors'][$processor_id] = $processor; } // fix processor bindings foreach( $data['processors'] as &$processor ){ if( !empty( $processor['config'] ) ){ foreach( $processor['config'] as $config_key => &$config_value ){ if( is_string($config_value) ){ foreach( $old_processors as $old_proc=>$new_proc ){ $config_value = str_replace( $old_proc, $new_proc, $config_value ); } } } } } // fix field - processor bindings if( !empty( $data['fields'] ) ){ foreach( $data['fields'] as &$field ){ if( !empty( $field['config'] ) ){ foreach( $field['config'] as $config_key => &$config_value ){ if( is_string($config_value) ){ foreach( $old_processors as $old_proc=>$new_proc ){ $config_value = str_replace( $old_proc, $new_proc, $config_value ); } } } } } } } */ // get form registry $forms = Caldera_Forms::get_forms(true); if (empty($forms)) { $forms = array(); } // add form to registry $forms[$data['ID']] = $data; // remove undeeded settings for registry if (isset($forms[$data['ID']]['layout_grid'])) { unset($forms[$data['ID']]['layout_grid']); } if (isset($forms[$data['ID']]['fields'])) { unset($forms[$data['ID']]['fields']); } if (isset($forms[$data['ID']]['processors'])) { unset($forms[$data['ID']]['processors']); } if (isset($forms[$data['ID']]['settings'])) { unset($forms[$data['ID']]['settings']); } // add from to list update_option($data['ID'], $data); do_action('caldera_forms_import_form', $data); update_option('_caldera_forms', $forms); do_action('caldera_forms_save_form_register', $data); wp_redirect('admin.php?page=caldera-forms&edit=' . $data['ID']); exit; } else { wp_die(__('Sorry, File is not valid.', 'caldera-forms'), __('Form Import Error', 'caldera-forms')); } } } else { wp_die(__('Sorry, File not uploaded.', 'caldera-forms'), __('Form Import Error', 'caldera-forms')); } } else { wp_die(__('Sorry, please try again', 'caldera-forms'), __('Form Import Error', 'caldera-forms')); } } if (!empty($_GET['export-form']) && current_user_can('manage_options')) { $form = Caldera_Forms::get_form($_GET['export-form']); if (empty($form)) { wp_die(__('Form does not exist.', 'caldera-forms')); } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: application/json"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name(strtolower($form['name'])) . "-export.json\";"); echo json_encode($form); exit; } if (!empty($_GET['export']) && current_user_can('manage_options')) { $form = Caldera_Forms::get_form($_GET['export']); global $wpdb; //build labels $labels = array(); $structure = array(); $field_types = apply_filters('caldera_forms_get_field_types', array()); if (!empty($form['fields'])) { $headers['date_submitted'] = 'Submitted'; foreach ($form['fields'] as $field_id => $field) { if (isset($field_types[$field['type']]['capture']) && false === $field_types[$field['type']]['capture']) { continue; } $headers[$field['slug']] = $field['label']; $structure[$field['slug']] = $field_id; } } $filter = null; // export set - transient if (!empty($_GET['tid'])) { $items = get_transient($_GET['tid']); if (!empty($items)) { $filter = ' AND `entry`.`id` IN (' . implode(',', $items) . ') '; } else { wp_die(__('Export selection has expired', 'caldera-forms'), __('Export Expired', 'caldera-forms')); } } $rawdata = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT\n\t\t\t\t`entry`.`id` as `_entryid`,\n\t\t\t\t`entry`.`form_id` AS `_form_id`,\n\t\t\t\t`entry`.`datestamp` AS `_date_submitted`,\n\t\t\t\t`entry`.`user_id` AS `_user_id`\n\n\t\t\tFROM `" . $wpdb->prefix . "cf_form_entries` AS `entry`\n\t\t\t\n\n\t\t\tWHERE `entry`.`form_id` = %s\n\t\t\t" . $filter . "\n\t\t\tAND `entry`.`status` = 'active'\n\t\t\tORDER BY `entry`.`datestamp` DESC;", $_GET['export'])); $data = array(); foreach ($rawdata as $entry) { $submission = Caldera_Forms::get_entry($entry->_entryid, $form); $data[$entry->_entryid]['date_submitted'] = $entry->_date_submitted; foreach ($structure as $slug => $field_id) { $data[$entry->_entryid][$slug] = isset($submission['data'][$field_id]['value']) ? $submission['data'][$field_id]['value'] : null; } //dump($data); } header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: text/csv charset=utf-8;"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name($form['name']) . ".csv\";"); header("Content-Transfer-Encoding: binary"); $df = fopen("php://output", 'w'); fputcsv($df, $headers); foreach ($data as $row) { $csvrow = array(); foreach ($headers as $key => $label) { if (!isset($row[$key])) { $row[$key] = null; } else { if (is_array($row[$key]) && isset($row[$key]['label'])) { $row[$key] = $row[$key]['value']; } elseif (is_array($row[$key]) && count($row[$key]) === 1) { $row[$key] = $row[$key][0]; } elseif (is_array($row[$key])) { $subs = array(); foreach ($row[$key] as $row_part) { if (is_array($row_part) && isset($row_part['label'])) { $subs[] = $row_part['value']; } else { $subs[] = $row_part; } } $row[$key] = implode(', ', $subs); } } $csvrow[] = $row[$key]; } fputcsv($df, $row); } fclose($df); exit; } if (isset($_POST['config']) && isset($_POST['cf_edit_nonce']) && current_user_can('manage_options')) { // if this fails, check_admin_referer() will automatically print a "failed" page and die. if (check_admin_referer('cf_edit_element', 'cf_edit_nonce')) { // strip slashes $data = json_decode(stripslashes_deep($_POST['config']), ARRAY_A); // get form registry $forms = Caldera_Forms::get_forms(true); if (empty($forms)) { $forms = array(); } // option value labels if (!empty($data['fields'])) { foreach ($data['fields'] as &$field) { if (!empty($field['config']['option']) && is_array($field['config']['option'])) { foreach ($field['config']['option'] as &$option) { if (!isset($option['value'])) { $option['value'] = $option['label']; } } } } } // add form to registry $forms[$data['ID']] = $data; // remove undeeded settings for registry if (isset($forms[$data['ID']]['layout_grid'])) { unset($forms[$data['ID']]['layout_grid']); } if (isset($forms[$data['ID']]['fields'])) { unset($forms[$data['ID']]['fields']); } if (isset($forms[$data['ID']]['processors'])) { unset($forms[$data['ID']]['processors']); } if (isset($forms[$data['ID']]['settings'])) { unset($forms[$data['ID']]['settings']); } foreach ($forms as $form_id => $form_config) { if (empty($form_config)) { unset($forms[$form_id]); } } // combine structure pages $data['layout_grid']['structure'] = implode('#', $data['layout_grid']['structure']); // add from to list update_option($data['ID'], $data); do_action('caldera_forms_save_form', $data); update_option('_caldera_forms', $forms); do_action('caldera_forms_save_form_register', $data); if (!empty($_POST['sender'])) { exit; } wp_redirect('admin.php?page=caldera-forms'); die; } return; } }
/** * Get The entries from a Caldera Form * * @since 1.0.0 * * @param string $form_id form ID * @param int $page. Optional. Page number for results. Default is 1 * @param int $perpage Optional. Results per page. Default is 1000 * @param array $get_fields Optional. Array of fields to get. Default is an empty array, which returns all fields. * * @return array entries returned */ public static function get_cf_entries($form_id, $page = 1, $perpage = 1000, $get_fields = array()) { if (!class_exists('\\Caldera_Forms')) { return; } $form = \Caldera_Forms::get_form($form_id); if (isset($form['ID'])) { $form_id = $form['ID']; } else { return; } global $wpdb; $field_labels = array(); $backup_labels = array(); $selects = array(); // get all fieldtype $field_types = \Caldera_Forms::get_field_types(); $fields = array(); if (!empty($form['fields'])) { foreach ($form['fields'] as $fid => $field) { $fields[$field['slug']] = $field; $selects[] = "'" . $field['slug'] . "'"; if (empty($get_fields) || in_array($field['ID'], $get_fields)) { $field_labels[$field['slug']] = $field['label']; } $has_vars = array(); if (!empty($form['variables']['types'])) { $has_vars = $form['variables']['types']; } } } if (empty($field_labels)) { $field_labels = $backup_labels; } $data = array(); $filter = null; $status = 'active'; $data['active'] = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(`id`) AS `total` FROM `" . $wpdb->prefix . "cf_form_entries` WHERE `form_id` = %s AND `status` = 'active';", $form_id)); // set current total $data['total'] = $data['active']; if (!empty($perpage)) { $data['pages'] = ceil($data['total'] / $perpage); } else { $data['pages'] = 1; } if (!empty($page)) { $page = abs($page); if ($page > $data['pages']) { $page = $data['pages']; } } $data['current_page'] = $page; $gmt_offset = get_option('gmt_offset'); if ($data['total'] > 0) { $data['form'] = $form_id; $data['label'] = $field_labels; $offset = ($page - 1) * $perpage; $limit = null; if (!empty($perpage)) { $limit = "LIMIT " . $offset . ',' . $perpage; } $rawdata = $wpdb->get_results($wpdb->prepare("\n\t\t\tSELECT\n\t\t\t\t`id`,\n\t\t\t\t`form_id`\n\t\t\tFROM `" . $wpdb->prefix . "cf_form_entries`\n\n\t\t\tWHERE `form_id` = %s AND `status` = %s ORDER BY `datestamp` DESC " . $limit . ";", $form_id, $status)); if (!empty($rawdata)) { $ids = array(); foreach ($rawdata as $row) { $ids[] = $row->id; } $rawdata = $wpdb->get_results("\n\t\t\t\tSELECT\n\t\t\t\t\t`entry`.`id` as `_entryid`,\n\t\t\t\t\t`entry`.`form_id` AS `_form_id`,\n\t\t\t\t\t`entry`.`datestamp` AS `_date_submitted`,\n\t\t\t\t\t`entry`.`user_id` AS `_user_id`,\n\t\t\t\t\t`value`.*\n\n\t\t\t\tFROM `" . $wpdb->prefix . "cf_form_entries` AS `entry`\n\t\t\t\tLEFT JOIN `" . $wpdb->prefix . "cf_form_entry_values` AS `value` ON (`entry`.`id` = `value`.`entry_id`)\n\n\t\t\t\tWHERE `entry`.`id` IN (" . implode(',', $ids) . ")\n\t\t\t\t" . $filter . "\n\t\t\t\tORDER BY `entry`.`datestamp` DESC;"); $data['entry'] = array(); $dateformat = get_option('date_format'); $timeformat = get_option('time_format'); foreach ($rawdata as $row) { if (!empty($row->_user_id)) { $user = get_userdata($row->_user_id); if (!empty($user)) { $data['entry']['E' . $row->_entryid]['user']['ID'] = $user->ID; $data['entry']['E' . $row->_entryid]['user']['name'] = $user->data->display_name; $data['entry']['E' . $row->_entryid]['user']['email'] = $user->data->user_email; $data['entry']['E' . $row->_entryid]['user']['avatar'] = get_avatar($user->ID, 64); } } $data['entry']['E' . $row->_entryid]['_entry_id'] = $row->_entryid; $data['entry']['E' . $row->_entryid]['_date'] = date_i18n($dateformat . ' ' . $timeformat, get_date_from_gmt($row->_date_submitted, 'U')); // setup default data array if (!isset($data['entry']['E' . $row->_entryid]['data'])) { if (isset($field_labels)) { foreach ($field_labels as $slug => $label) { // setup labels ordering $data['entry']['E' . $row->_entryid]['data'][$slug] = null; } } } if (!empty($field_labels[$row->slug])) { $label = $field_labels[$row->slug]; // check view handler $field = $fields[$row->slug]; // filter the field to get field data $field = apply_filters('caldera_forms_render_get_field', $field, $form); $field = apply_filters('caldera_forms_render_get_field_type-' . $field['type'], $field, $form); $field = apply_filters('caldera_forms_render_get_field_slug-' . $field['slug'], $field, $form); // maybe json? $is_json = json_decode($row->value, ARRAY_A); if (!empty($is_json)) { $row->value = $is_json; } if (is_string($row->value)) { $row->value = esc_html(stripslashes_deep($row->value)); } else { $row->value = stripslashes_deep(\Caldera_Forms_Sanitize::sanitize($row->value)); } $row->value = apply_filters('caldera_forms_view_field_' . $field['type'], $row->value, $field, $form); if (isset($data['entry']['E' . $row->_entryid]['data'][$row->slug])) { // array based - add another entry if (!is_array($data['entry']['E' . $row->_entryid]['data'][$row->slug])) { $tmp = $data['entry']['E' . $row->_entryid]['data'][$row->slug]; $data['entry']['E' . $row->_entryid]['data'][$row->slug] = array($tmp); } $data['entry']['E' . $row->_entryid]['data'][$row->slug][] = $row->value; } else { $data['entry']['E' . $row->_entryid]['data'][$row->slug] = $row->value; } } if (!empty($form['variables']['types'])) { foreach ($form['variables']['types'] as $var_key => $var_type) { if ($var_type == 'entryitem') { $data['label'][$form['variables']['keys'][$var_key]] = ucwords(str_replace('_', ' ', $form['variables']['keys'][$var_key])); $data['entry']['E' . $row->_entryid]['data'][$form['variables']['keys'][$var_key]] = \Caldera_Forms::do_magic_tags($form['variables']['values'][$var_key], $row->_entryid); } } } } } } return $data; }
/** * Shortcode callback for cf_view callback * * @since 0.0.2 * * @param array $atts * * @return string|void */ function cf_view_shortcode($atts) { $atts = shortcode_atts(array('id' => null, 'fields' => array(), 'editor_id' => 0), $atts, 'cf_view'); if (is_null($atts['id'])) { return; } $maybe_form = Caldera_Forms::get_form($atts['id']); if (is_array($maybe_form)) { $form_id = $atts['id']; } else { return; } if (!empty($atts['fields'])) { $fields = explode(',', $atts['fields']); } else { $fields = $atts['fields']; } if (0 < absint($atts['editor_id'])) { $maybe_post = get_post($atts['editor_id']); if (!is_object($maybe_post)) { $editor_id = null; } else { $editor_id = $atts['editor_id']; } } else { $editor_id = null; } return cf_view($form_id, $fields, $editor_id); }
<?php // Connection builder $forms = \Caldera_Forms::get_forms(); foreach ($forms as $form_id => $form) { if (!empty($form['is_connected_form'])) { unset($forms[$form_id]); continue; } $forms[$form_id] = \Caldera_Forms::get_form($form_id); } if (!empty($element['condition_points']['conditions'])) { foreach ($element['condition_points']['conditions'] as $condition_point) { if (empty($element['node'][$condition_point['connect']])) { // dont output points for nonexistant forms continue; } if ($condition_point['connect'] == $condition_point['parent']) { unset($element['node'][$condition_point['connect']]); continue; } ?> <span class="condition-point" data-src="<?php echo $condition_point['id']; ?> " data-name="<?php echo $condition_point['name']; ?> " data-from="<?php echo $condition_point['parent'];
function lsx_is_form_enabled($slug = false) { if (false == $slug) { return false; } $match = false; $forms = get_option('_caldera_forms', false); if (false !== $forms) { foreach ($forms as $form_id => $form_maybe) { if (trim(strtolower($slug)) == strtolower($form_maybe['name'])) { $match = $form_id; break; } } } if (false === $match) { $is_form = Caldera_Forms::get_form(strtolower($slug)); if (!empty($is_form)) { return strtolower($slug); } } return $match; }
/** * Change form to be rendered * * @since 0.1.0 * * @uses "caldera_forms_render_get_form" filter * * @param array $form The form config * * @return array */ function cf_form_connector_change_form($form) { if (isset($_GET['cf_con']) && isset($_GET['cf_con_form_id']) && isset($_GET['cf_con_nonce']) && $_GET['cf_con'] && wp_verify_nonce($_GET['cf_con_nonce'], 'cf_con_nonce')) { remove_filter('caldera_forms_render_get_form', 'cf_form_connector_change_form'); $_form = Caldera_Forms::get_form(Caldera_Forms_Sanitize::sanitize($_GET['cf_con_form_id'])); if (is_array($_form)) { if (isset($_GET['cf_id']) && 0 < absint($_GET['cf_id'])) { add_filter('caldera_forms_render_entry_id', function ($entry_id) { return (int) $_GET['cf_id']; }); } $form = $_form; } } return $form; }
<?php // Connection builder $all_forms = \Caldera_Forms::get_forms(); $forms = array(); foreach ($all_forms as $form_id => $form_details) { $form = \Caldera_Forms::get_form($form_id); if (!empty($form['is_connected_form'])) { continue; } if (!empty($form['fields'])) { foreach ($form['fields'] as $field_id => $field) { if ($field['type'] == 'html') { $form['fields'][$field_id]['config']['default'] = ''; } } } $forms[$form_id] = $form; } if (!empty($element['condition_points']['conditions'])) { foreach ($element['condition_points']['conditions'] as $condition_point) { if (empty($element['node'][$condition_point['connect']])) { // dont output points for nonexistant forms continue; } if ($condition_point['connect'] == $condition_point['parent']) { unset($element['node'][$condition_point['connect']]); continue; } ?>