class-evf-entry-csv-exporter.php
237 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles entry CSV export. |
| 4 | * |
| 5 | * @package EverestForms\Export |
| 6 | * @since 1.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Include dependencies. |
| 13 | */ |
| 14 | if ( ! class_exists( 'EVF_CSV_Exporter', false ) ) { |
| 15 | require_once EVF_ABSPATH . 'includes/export/abstract-evf-csv-exporter.php'; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * EVF_Entry_CSV_Exporter Class. |
| 20 | */ |
| 21 | class EVF_Entry_CSV_Exporter extends EVF_CSV_Exporter { |
| 22 | |
| 23 | /** |
| 24 | * Form ID. |
| 25 | * |
| 26 | * @var int|mixed |
| 27 | */ |
| 28 | public $form_id; |
| 29 | |
| 30 | /** |
| 31 | * Entry ID. |
| 32 | * |
| 33 | * @var int|mixed |
| 34 | */ |
| 35 | public $entry_id; |
| 36 | |
| 37 | /** |
| 38 | * Type of export used in filter names. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $export_type = 'entry'; |
| 43 | |
| 44 | /** |
| 45 | * Constructor. |
| 46 | * |
| 47 | * @param int $form_id Form ID. |
| 48 | * @param int $entry_id Entry ID. |
| 49 | */ |
| 50 | public function __construct( $form_id = '', $entry_id = '' ) { |
| 51 | $this->form_id = absint( $form_id ); |
| 52 | $this->entry_id = absint( $entry_id ); |
| 53 | $this->column_names = $this->get_default_column_names(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return an array of columns to export. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | public function get_default_column_names() { |
| 62 | $columns = array(); |
| 63 | $form_obj = evf()->form->get( $this->form_id ); |
| 64 | $form_data = ! empty( $form_obj->post_content ) ? evf_decode( $form_obj->post_content ) : ''; |
| 65 | |
| 66 | // Set Entry ID at first. |
| 67 | $columns['entry_id'] = esc_html__( 'ID', 'everest-forms' ); |
| 68 | |
| 69 | // Add whitelisted fields to export columns. |
| 70 | if ( ! empty( $form_data['form_fields'] ) ) { |
| 71 | foreach ( $form_data['form_fields'] as $field ) { |
| 72 | if ( ! in_array( $field['type'], array( 'html', 'title', 'captcha' ), true ) ) { |
| 73 | $columns[ $field['meta-key'] ] = evf_clean( $field['label'] ); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Set the default columns. |
| 79 | $columns['status'] = esc_html__( 'Status', 'everest-forms' ); |
| 80 | $columns['date_created'] = esc_html__( 'Date Created', 'everest-forms' ); |
| 81 | $columns['date_created_gmt'] = esc_html__( 'Date Created GMT', 'everest-forms' ); |
| 82 | |
| 83 | // If user details are disabled globally discard the IP and UA. |
| 84 | if ( 'yes' !== get_option( 'everest_forms_disable_user_details' ) ) { |
| 85 | $columns['user_device'] = esc_html__( 'User Device', 'everest-forms' ); |
| 86 | $columns['user_ip_address'] = esc_html__( 'User IP Address', 'everest-forms' ); |
| 87 | } |
| 88 | |
| 89 | return apply_filters( "everest_forms_export_{$this->export_type}_default_columns", $columns ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Prepare data for export. |
| 94 | * |
| 95 | * @since 1.6.0 |
| 96 | */ |
| 97 | public function prepare_data_to_export() { |
| 98 | $this->row_data = array(); |
| 99 | |
| 100 | if ( $this->entry_id ) { |
| 101 | $entry = evf_get_entry( $this->entry_id ); |
| 102 | $this->row_data[] = $this->generate_row_data( $entry ); |
| 103 | } else { |
| 104 | $entry_ids = evf_search_entries( |
| 105 | array( |
| 106 | 'limit' => -1, |
| 107 | 'order' => 'ASC', |
| 108 | 'form_id' => $this->form_id, |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | // Get the entries. |
| 113 | $entries = array_map( 'evf_get_entry', $entry_ids ); |
| 114 | |
| 115 | foreach ( $entries as $entry ) { |
| 116 | $this->row_data[] = $this->generate_row_data( $entry ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $this->row_data; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Take a entry id and generate row data from it for export. |
| 125 | * |
| 126 | * @param object $entry Entry object. |
| 127 | * @return array |
| 128 | */ |
| 129 | protected function generate_row_data( $entry ) { |
| 130 | $columns = $this->get_column_names(); |
| 131 | $row = array(); |
| 132 | foreach ( $columns as $column_id => $column_name ) { |
| 133 | $column_id = strstr( $column_id, ':' ) ? current( explode( ':', $column_id ) ) : $column_id; |
| 134 | $value = ''; |
| 135 | |
| 136 | if ( isset( $entry->meta[ $column_id ] ) ) { |
| 137 | // Filter for entry meta data. |
| 138 | $value = $entry->meta[ $column_id ]; |
| 139 | |
| 140 | if ( is_serialized( $value ) ) { |
| 141 | $value = $this->implode_values( maybe_unserialize( $value ) ); |
| 142 | } |
| 143 | |
| 144 | $value = apply_filters( 'everest_forms_html_field_value', $value, $entry->meta[ $column_id ], $entry, 'export-csv' ); |
| 145 | |
| 146 | } elseif ( is_callable( array( $this, "get_column_value_{$column_id}" ) ) ) { |
| 147 | // Handle special columns which don't map 1:1 to entry data. |
| 148 | $value = $this->{"get_column_value_{$column_id}"}( $entry ); |
| 149 | } |
| 150 | |
| 151 | $row[ $column_id ] = sanitize_text_field( $value ); |
| 152 | } |
| 153 | |
| 154 | return apply_filters( 'everest_forms_entry_export_row_data', $row, $entry ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get entry id value. |
| 159 | * |
| 160 | * @param object $entry Entry being exported. |
| 161 | * @return int |
| 162 | */ |
| 163 | protected function get_column_value_entry_id( $entry ) { |
| 164 | return absint( $entry->entry_id ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get entry status value. |
| 169 | * |
| 170 | * @param object $entry Entry being exported. |
| 171 | * @return string |
| 172 | */ |
| 173 | protected function get_column_value_status( $entry ) { |
| 174 | $statuses = evf_get_entry_statuses(); |
| 175 | |
| 176 | if ( isset( $statuses[ $entry->status ] ) ) { |
| 177 | return $statuses[ $entry->status ]; |
| 178 | } |
| 179 | |
| 180 | return $entry->status; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get date created value. |
| 185 | * |
| 186 | * @param object $entry Entry being exported. |
| 187 | * @return string |
| 188 | */ |
| 189 | protected function get_column_value_date_created( $entry ) { |
| 190 | $timestamp = false; |
| 191 | |
| 192 | if ( isset( $entry->date_created ) ) { |
| 193 | $timestamp = strtotime( $entry->date_created ); |
| 194 | } |
| 195 | |
| 196 | /* translators: 1: entry date 2: entry time */ |
| 197 | return sprintf( esc_html__( '%1$s %2$s', 'everest-forms' ), date_i18n( evf_date_format(), $timestamp ), date_i18n( evf_time_format(), $timestamp ) ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get GMT date created value. |
| 202 | * |
| 203 | * @param object $entry Entry being exported. |
| 204 | * @return string |
| 205 | */ |
| 206 | protected function get_column_value_date_created_gmt( $entry ) { |
| 207 | $timestamp = false; |
| 208 | |
| 209 | if ( isset( $entry->date_created ) ) { |
| 210 | $timestamp = strtotime( $entry->date_created ) + ( get_option( 'gmt_offset' ) * 3600 ); |
| 211 | } |
| 212 | |
| 213 | /* translators: 1: entry date 2: entry time */ |
| 214 | return sprintf( esc_html__( '%1$s %2$s', 'everest-forms' ), date_i18n( evf_date_format(), $timestamp ), date_i18n( evf_time_format(), $timestamp ) ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Get entry user device value. |
| 219 | * |
| 220 | * @param object $entry Entry being exported. |
| 221 | * @return string |
| 222 | */ |
| 223 | protected function get_column_value_user_device( $entry ) { |
| 224 | return sanitize_text_field( $entry->user_device ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get entry user IP address value. |
| 229 | * |
| 230 | * @param object $entry Entry being exported. |
| 231 | * @return string |
| 232 | */ |
| 233 | protected function get_column_value_user_ip_address( $entry ) { |
| 234 | return sanitize_text_field( $entry->user_ip_address ); |
| 235 | } |
| 236 | } |
| 237 |