Example #1
0
 /**
  * Perform redirect
  *
  * @param string $type Type of redirect being performed.
  * @param string $url URL to redirect to.
  * @param array $form Form config.
  * @param string $processid Process ID for process calling the redirect.
  */
 public static function form_redirect($type, $url, $form, $processid)
 {
     $url = apply_filters('caldera_forms_redirect_url', $url, $form, $processid);
     $url = apply_filters('caldera_forms_redirect_url_' . $type, $url, $form, $processid);
     if (headers_sent()) {
         remove_action('caldera_forms_redirect', 'cf_ajax_redirect', 10);
     }
     do_action('caldera_forms_redirect', $type, $url, $form, $processid);
     do_action('caldera_forms_redirect_' . $type, $url, $form, $processid);
     if (!empty($url)) {
         cf_redirect($url, 302);
         exit;
     }
 }
Example #2
0
 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;
     }
 }