/** * Export form entries */ function iphorm_export_entries() { if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['iphorm_do_entries_export']) && $_POST['iphorm_do_entries_export'] == 1) { if (isset($_POST['form_id']) && iphorm_form_exists($_POST['form_id'])) { $config = iphorm_get_form_config($_POST['form_id']); $id = $config['id']; $filenameFilter = new iPhorm_Filter_Filename(); $filename = $filenameFilter->filter($config['name']); // Send headers header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename=' . $filename . '-' . date('Y-m-d') . '.csv'); global $wpdb; $elementsCache = array(); // Build the query $sql = "SELECT `entries`.*"; if (isset($config['elements']) && is_array($config['elements'])) { foreach ($config['elements'] as $element) { if (isset($element['save_to_database']) && $element['save_to_database']) { $elementId = absint($element['id']); $sql .= ", GROUP_CONCAT(if (`data`.`element_id` = {$elementId}, value, NULL)) AS `element_{$elementId}`"; $elementsCache[$elementId] = iphorm_get_element_config($elementId, $config); } } } if (isset($_POST['from'], $_POST['to'])) { $pattern = '/^\\d{4}-\\d{2}-\\d{2}$/'; if (preg_match($pattern, $_POST['from']) && preg_match($pattern, $_POST['to'])) { $from = iphorm_local_to_utc($_POST['from'] . ' 00:00:00'); $to = iphorm_local_to_utc($_POST['to'] . ' 23:59:59'); $dateSql = $wpdb->prepare(' AND (`entries`.`date_added` >= %s AND `entries`.`date_added` <= %s)', array($from, $to)); } } $sql .= "\r\n FROM `" . iphorm_get_form_entries_table_name() . "` `entries`\r\n LEFT JOIN `" . iphorm_get_form_entry_data_table_name() . "` `data` ON `data`.`entry_id` = `entries`.`id`\r\n WHERE `entries`.`form_id` = {$id}"; if (isset($dateSql)) { $sql .= $dateSql; } $sql .= "\r\n GROUP BY `entries`.`id`;"; $wpdb->query('SET @@GROUP_CONCAT_MAX_LEN = 65535'); $entries = $wpdb->get_results($sql, ARRAY_A); $validFields = array('id' => 'Entry ID', 'date_added' => 'Date', 'ip' => 'IP address', 'form_url' => 'Form URL', 'referring_url' => 'Referring URL', 'post_id' => 'Post / page ID', 'post_title' => 'Post / page title', 'user_display_name' => 'User WordPress display name', 'user_email' => 'User WordPress email', 'user_login' => 'User WordPress login'); // Sanitize chosen fields $validFields = iphorm_get_valid_entry_fields(); $fields = array(); if (isset($_POST['export_fields']) && is_array($_POST['export_fields'])) { // Check which fields have been chosen for export and get their labels foreach ($_POST['export_fields'] as $field) { if (array_key_exists($field, $validFields)) { // It's a default column, get the label $fields[$field] = $validFields[$field]; } elseif (preg_match('/element_(\\d+)/', $field, $matches)) { // It's an element column, so get the element label $elementId = absint($matches[1]); if (isset($elementsCache[$elementId])) { $label = iphorm_get_element_admin_label($elementsCache[$elementId]); } else { $label = ''; } $fields[$field] = $label; } } } $fh = fopen('php://output', 'w'); // Write column headings row fputcsv($fh, $fields); // Write each entry if (is_array($entries)) { foreach ($entries as $entry) { $row = array(); foreach ($fields as $field => $label) { $row[$field] = isset($entry[$field]) ? $entry[$field] : ''; if (strlen($row[$field]) && strpos($field, 'element_') !== false) { $elementId = absint(str_replace('element_', '', $field)); if (isset($elementsCache[$elementId])) { // Per element modifications to the output if (isset($elementsCache[$elementId]['type'])) { switch ($elementsCache[$elementId]['type']) { // Remove <br /> from textarea newlines case 'text': case 'textarea': case 'password': case 'hidden': $row[$field] = htmlspecialchars_decode(preg_replace('/<br\\s*?\\/>/', '', $row[$field]), ENT_QUOTES); break; case 'email': // Email elements: remove <a> tag $row[$field] = trim(strip_tags($row[$field])); break; case 'checkbox': case 'radio': // Multiple elements: replace <br /> with new line $row[$field] = trim(preg_replace('/<br\\s*?\\/>/', "\n", $row[$field])); break; case 'file': // File uploads: replace <br /> with newline, remove anchor tag, use href attr as value $result = preg_match_all('/href=([\'"])?((?(1).+?|[^\\s>]+))(?(1)\\1)/is', $row[$field], $uploads); if ($result > 0) { $row[$field] = join("\n", $uploads[2]); } else { $row[$field] = trim(preg_replace('/<br\\s*?\\/>/', "\n", $row[$field])); } break; } } } } // Format the date to include the WordPress Timezone offset if ($field === 'date_added') { $row[$field] = iphorm_format_date($row[$field]); } } fputcsv($fh, $row); } } fclose($fh); exit; } } }
/** * Get the form object with the given ID * * @param int $id * @param string $uid * @return iPhorm */ function iphorm_get_form($id, $uid = '', $values = '') { $config = iphorm_get_form_config($id); if ($config !== null) { if (strlen($uid)) { $config['uniq_id'] = preg_replace('/[^A-Za-z0-9]/', '', $uid); } $config['dynamic_values'] = $values; $form = new iPhorm($config); return $form; } else { return null; } }
/** * Display the widget settings form * * @param array $instance The current settings for this widget instance */ public function form($instance) { $formRows = iphorm_get_all_form_rows(); if (!isset($instance['title'])) { $instance['title'] = ''; } if (!isset($instance['form_id'])) { $instance['form_id'] = 0; } if (!isset($instance['content'])) { $instance['content'] = ''; } if (!isset($instance['options'])) { $instance['options'] = ''; } ?> <?php if (count($formRows)) { ?> <div> <label for="<?php echo $this->get_field_id('title'); ?> "><?php esc_html_e('Title (optional)', 'iphorm'); ?> </label> <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?> " name="<?php echo $this->get_field_name('title'); ?> " value="<?php echo esc_attr($instance['title']); ?> " /> </div> <div style="margin-top: 10px;"> <label for="<?php echo $this->get_field_id('form_id'); ?> "><?php esc_html_e('Select a form', 'iphorm'); ?> </label> <select id="<?php echo $this->get_field_id('form_id'); ?> " name="<?php echo $this->get_field_name('form_id'); ?> "> <?php foreach ($formRows as $formRow) { ?> <?php $config = iphorm_get_form_config($formRow->id); ?> <option value="<?php echo absint($config['id']); ?> " <?php selected($instance['form_id'], $config['id']); ?> ><?php echo esc_html($config['name']); ?> </option> <?php } ?> </select> </div> <div style="margin-top: 10px;"> <label for="<?php echo $this->get_field_id('content'); ?> "><?php esc_html_e('Text or HTML to trigger the popup', 'iphorm'); ?> </label> <textarea id="<?php echo $this->get_field_id('content'); ?> " name="<?php echo $this->get_field_name('content'); ?> " class="widefat"><?php echo esc_attr($instance['content']); ?> </textarea> </div> <div style="margin-top: 10px;"> <label for="<?php echo $this->get_field_id('options'); ?> "><?php esc_html_e('Fancybox options (advanced)', 'iphorm'); ?> </label> <input type="text" class="widefat" name="<?php echo $this->get_field_name('options'); ?> " id="<?php echo $this->get_field_id('options'); ?> " value="<?php echo esc_attr($instance['options']); ?> " /> <p class="description" style="margin-bottom: 3px;"><?php printf(esc_html__('Enter any Fancybox options as a JSON formatted string, %sexample%s.', 'iphorm'), '<a href="' . admin_url('admin.php?page=iphorm_help&section=faq#website-lightbox-widget-options') . '" onclick="window.open(this.href); return false;">', '</a>'); ?> </p> </div> <?php } else { ?> <?php printf(esc_html__('You have not created a form yet, %sclick here to create one%s.', 'iphorm'), '<a href="' . admin_url('admin.php?page=iphorm_form_builder') . '">', '</a>'); ?> <?php } ?> <?php }