| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Exporter Class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Base; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Exporter Class |
| 15 |
*/ |
| 16 |
abstract class Exporter { |
| 17 |
|
| 18 |
/** |
| 19 |
* Raw data to export |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $row_data = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Column name to export |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $colunms = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Store file name |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $file_name = ''; |
| 38 |
|
| 39 |
/** |
| 40 |
* Store file format |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
protected $format = ''; |
| 45 |
|
| 46 |
/** |
| 47 |
* Store file type |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $file_type = ''; |
| 52 |
|
| 53 |
/** |
| 54 |
* Store selected IDs for selective export |
| 55 |
* |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
protected $selected_ids = []; |
| 59 |
|
| 60 |
/** |
| 61 |
* Prepare data that will be exported |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
abstract protected function prepare_data(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Set file name that will be exported |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
protected function set_file_name( $file_name ) { |
| 73 |
$this->file_name = $file_name; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set file format that will be exported |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
protected function set_format( $format ) { |
| 82 |
$this->format = $format; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get column names |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
protected function get_columns() { |
| 91 |
return []; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Set content type |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
protected function send_headers() { |
| 100 |
header( 'Content-Type: application/' . $this->format . '; charset=utf-8' ); |
| 101 |
header( 'Content-Disposition: attachment; filename=' . $this->file_name ); |
| 102 |
header( 'Pragma: no-cache' ); |
| 103 |
header( 'Expires: 0' ); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Get data to export |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
protected function get_data() { |
| 111 |
return $this->row_data; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get columns as csv |
| 116 |
* |
| 117 |
* @return string |
| 118 |
*/ |
| 119 |
protected function export_columns() { |
| 120 |
$colunms = $this->get_columns(); |
| 121 |
ob_clean(); |
| 122 |
$output = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- php://output is a stream wrapper, not a filesystem operation |
| 123 |
ob_start(); |
| 124 |
|
| 125 |
fputcsv( $output, $colunms ); |
| 126 |
|
| 127 |
fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Closing php://output stream |
| 128 |
|
| 129 |
return ob_get_clean(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Export rows |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
protected function export_rows() { |
| 138 |
$data = $this->get_data(); |
| 139 |
ob_clean(); |
| 140 |
$buffer = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- php://output is a stream wrapper, not a filesystem operation |
| 141 |
ob_start(); |
| 142 |
|
| 143 |
array_walk( $data, array( $this, 'export_row' ), $buffer ); |
| 144 |
|
| 145 |
fclose( $buffer ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- Closing php://output stream |
| 146 |
|
| 147 |
return ob_get_clean(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Export row |
| 152 |
* |
| 153 |
* @param array $row_data [$row_data description] |
| 154 |
* @param [type] $key [$key description] |
| 155 |
* @param file $buffer [$buffer description] |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
protected function export_row( $row_data, $key, $buffer ) { |
| 160 |
$colunms = $this->get_columns(); |
| 161 |
$export_row = []; |
| 162 |
|
| 163 |
foreach ( $colunms as $colunm ) { |
| 164 |
if ( ! empty( $row_data[$colunm] ) ) { |
| 165 |
$export_row[] = $row_data[$colunm]; |
| 166 |
} else { |
| 167 |
$export_row[] = ''; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
fputcsv( $buffer, $export_row ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Print the content that will be exported |
| 176 |
* |
| 177 |
* @param string $content |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
protected function send_content( $content ) { |
| 182 |
echo wp_kses( $content, 'post' ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Export data to csv format |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function export_csv() { |
| 191 |
$this->set_file_name( "export-{$this->file_type}.csv" ); |
| 192 |
$this->set_format( 'csv' ); |
| 193 |
$this->prepare_data(); |
| 194 |
$this->send_headers(); |
| 195 |
$this->send_content( $this->export_columns() . $this->export_rows() ); |
| 196 |
die(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Export data to JSON format |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public function export_json() { |
| 205 |
$this->set_file_name( "export-{$this->file_type}.json" ); |
| 206 |
$this->set_format( 'json' ); |
| 207 |
$this->prepare_data(); |
| 208 |
$this->send_headers(); |
| 209 |
$data = $this->get_data(); |
| 210 |
|
| 211 |
echo wp_json_encode( $data, JSON_PRETTY_PRINT ); |
| 212 |
die(); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Export file |
| 217 |
* |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public function export() { |
| 221 |
$format = $this->format; |
| 222 |
|
| 223 |
switch ( $format ) { |
| 224 |
case 'csv': |
| 225 |
$this->export_csv(); |
| 226 |
break; |
| 227 |
case 'json': |
| 228 |
$this->export_json(); |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|