/** * Add support sub menu page * * @since 1.3.5 * * @uses "admin_menu" hook */ public static function add_menu_page() { add_submenu_page('caldera-forms', __('Support', 'caldera-forms'), __('Support', 'caldera-forms'), Caldera_Forms::get_manage_cap('admin'), 'caldera-form-support', array(__CLASS__, 'page')); }
static function save_form() { /// check for form delete if (!empty($_GET['delete']) && !empty($_GET['cal_del']) && current_user_can(Caldera_Forms::get_manage_cap('save'))) { 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(Caldera_Forms::get_manage_cap('import'))) { 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(Caldera_Forms::get_manage_cap('export'))) { $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); if (empty($_GET['format']) || $_GET['format'] != 'php') { header("Content-Type: application/json"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name(strtolower($form['name'])) . "-export.json\";"); echo json_encode($form); } else { $form_id = sanitize_key($_GET['form_id']); if (!empty($_GET['pin_menu'])) { $form['pinned'] = 1; } header("Content-Type: application/php"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name(strtolower($form_id)) . "-include.php\";"); echo '<?php' . "\r\n"; echo "/**\r\n * Caldera Forms - PHP Export \r\n * {$form['name']} \r\n * @version " . CFCORE_VER . "\r\n * @license GPL-2.0+\r\n * \r\n */\r\n\r\n\r\n"; $structure = "/**\r\n * Filter admin forms to include custom form in admin\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param array all registered forms\r\n */\r\n"; $structure .= 'add_filter( "caldera_forms_get_forms", function( $forms ){' . "\r\n"; $structure .= "\t" . '$forms["' . $form_id . '"] = apply_filters( "caldera_forms_get_form-' . $form_id . '", array() );' . "\r\n"; $structure .= "\t" . 'return $forms;' . "\r\n"; $structure .= "} );\r\n\r\n"; $structure .= "/**\r\n * Filter form request to include form structure to be rendered\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param array form structure\r\n */\r\n"; $structure .= "add_filter( 'caldera_forms_get_form-{$form_id}', function(){\r\n return " . var_export($form, true) . ";\r\n" . '} );' . "\r\n"; // cleanups because I'm me $structure = str_replace('array (', 'array(', $structure); $structure = str_replace($form['ID'], $form_id, $structure); // switch field IDs if (!empty($_GET['convert_slugs'])) { if (!empty($form['fields'])) { foreach ($form['fields'] as $field_id => $field) { $structure = str_replace($field_id, $field['slug'], $structure); } } } echo $structure; } exit; } if (!empty($_GET['export']) && current_user_can(Caldera_Forms::get_manage_cap('export'))) { $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(Caldera_Forms::get_manage_cap('manage'))) { // 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']); } if (isset($forms[$data['ID']]['conditional_groups'])) { unset($forms[$data['ID']]['conditional_groups']); } 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']); // remove fields from conditions if (!empty($data['conditional_groups']['fields'])) { unset($data['conditional_groups']['fields']); } // remove magics ( yes, not used yet.) if (!empty($data['conditional_groups']['magic'])) { unset($data['conditional_groups']['magic']); } // 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; } }
<script type="text/html" id="bulk-actions-active-tmpl"> <option selected="selected" value=""><?php echo __('Bulk Actions'); ?> </option> <option value="export"><?php echo __('Export Selected'); ?> </option> <?php if (current_user_can(Caldera_Forms::get_manage_cap('manage'))) { ?> <option value="trash"><?php echo __('Move to Trash'); ?> </option><?php } ?> </script> <script type="text/html" id="cf-export-template"> <input type="hidden" name="export-form" value="{{formid}}"> <input type="hidden" name="cal_del" value="{{nonce}}"> <div class="caldera-config-group"> <label><?php echo __('Export Type', 'caldera-forms'); ?> </label> <div class="caldera-config-field"> <select class="form-export-type" name="format" style="width: 230px;">
static function save_form() { if (!isset($_GET['page']) || 'caldera-forms' != $_GET['page']) { return; } add_filter('caldera_forms_manage_cap', array(__CLASS__, 'save_form_cap_filter'), 9, 3); /// check for form delete if (!empty($_GET['delete']) && !empty($_GET['cal_del']) && current_user_can(Caldera_Forms::get_manage_cap('save'), strip_tags($_GET['delete']))) { 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 { $deleted = Caldera_Forms_Forms::delete_form(strip_tags($_GET['delete'])); if ($deleted) { wp_redirect('admin.php?page=caldera-forms'); exit; } else { wp_die(__('Sorry, please try again', 'caldera-forms'), __('Form could not be deleted.', 'caldera-forms')); } } } /** IMPORT */ if (isset($_POST['cfimporter']) && current_user_can(Caldera_Forms::get_manage_cap('import'))) { if (check_admin_referer('cf-import', 'cfimporter')) { if (isset($_FILES['import_file']) && !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 (!is_array($data)) { wp_die(esc_html__('File is not a valid Caldera Form Import', 'caldera-forms')); } if (!isset($_POST['name'])) { wp_die(esc_html__('Form must have a name.', 'caldera-forms')); } $data['name'] = strip_tags($_POST['name']); $new_form_id = Caldera_Forms_Forms::import_form($data); if (is_string($new_form_id)) { cf_redirect('admin.php?page=caldera-forms&edit=' . $new_form_id, 302); exit; } else { wp_die(esc_html__('Form could not be imported.', 'caldera-forms')); } } } else { wp_die(esc_html__('Sorry, File not uploaded.', 'caldera-forms'), esc_html__('Form Import Error', 'caldera-forms')); } } else { wp_die(esc_html__('Sorry, please try again', 'caldera-forms'), esc_html__('Form Import Error', 'caldera-forms')); } } if (!empty($_GET['export-form']) && current_user_can(Caldera_Forms::get_manage_cap('export', strip_tags($_GET['export-form'])))) { $form = Caldera_Forms_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); if (empty($_GET['format']) || $_GET['format'] != 'php') { header("Content-Type: application/json"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name(strtolower($form['name'])) . "-export.json\";"); echo json_encode($form); } else { $form_id = sanitize_key($_GET['form_id']); if (!empty($_GET['pin_menu'])) { $form['pinned'] = 1; } header("Content-Type: application/php"); header("Content-Disposition: attachment; filename=\"" . sanitize_file_name(strtolower($form_id)) . "-include.php\";"); echo '<?php' . "\r\n"; echo "/**\r\n * Caldera Forms - PHP Export \r\n * {$form['name']} \r\n * @version " . CFCORE_VER . "\r\n * @license GPL-2.0+\r\n * \r\n */\r\n\r\n\r\n"; $structure = "/**\r\n * Filter admin forms to include custom form in admin\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param array \$forms All registered forms\r\n */\r\n"; $structure .= 'add_filter( "caldera_forms_get_forms", function( $forms ){' . "\r\n"; $structure .= "\t" . '$forms["' . $form_id . '"] = apply_filters( "caldera_forms_get_form-' . $form_id . '", array() );' . "\r\n"; $structure .= "\t" . 'return $forms;' . "\r\n"; $structure .= "} );\r\n\r\n"; $structure .= "/**\r\n * Filter form request to include form structure to be rendered\r\n *\r\n * @since 1.3.1\r\n *\r\n * @param \$form array form structure\r\n */\r\n"; $structure .= "add_filter( 'caldera_forms_get_form-{$form_id}', function( \$form ){\r\n return " . var_export($form, true) . ";\r\n" . '} );' . "\r\n"; // cleanups because I'm me $structure = str_replace('array (', 'array(', $structure); $structure = str_replace($form['ID'], $form_id, $structure); // switch field IDs if (!empty($_GET['convert_slugs'])) { if (!empty($form['fields'])) { foreach ($form['fields'] as $field_id => $field) { $structure = str_replace($field_id, $field['slug'], $structure); } } } echo $structure; } exit; } if (!empty($_GET['export']) && current_user_can(Caldera_Forms::get_manage_cap('export', strip_tags($_GET['export'])))) { $form = Caldera_Forms_Forms::get_form($_GET['export']); global $wpdb; //build labels $labels = array(); $structure = array(); $field_types = apply_filters('caldera_forms_get_field_types', array()); $headers = 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; } } if (empty($headers)) { wp_die(esc_html_e('Could not process export. This is most likely due to a problem with the form configuration.', '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: 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])) { $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(Caldera_Forms::get_manage_cap('manage'))) { // 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); self::save_a_form($data); if (!empty($_POST['sender'])) { exit; } wp_redirect('admin.php?page=caldera-forms'); die; } return; } }