| 1 |
<?php |
| 2 |
/** |
| 3 |
* Batch Export Class |
| 4 |
* |
| 5 |
* This is the base class for all batch export methods. Each data export type (donors, payments, etc) extend this class. |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @since 1.5 |
| 9 |
* @copyright Copyright (c) 2016, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @subpackage Admin/Export |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Give_Batch_Export Class |
| 21 |
* |
| 22 |
* @since 1.5 |
| 23 |
*/ |
| 24 |
class Give_Batch_Export extends Give_Export { |
| 25 |
|
| 26 |
/** |
| 27 |
* The file the data is stored in. |
| 28 |
* |
| 29 |
* @since 1.5 |
| 30 |
*/ |
| 31 |
private $file; |
| 32 |
|
| 33 |
/** |
| 34 |
* The name of the file the data is stored in. |
| 35 |
* |
| 36 |
* @since 1.5 |
| 37 |
*/ |
| 38 |
public $filename; |
| 39 |
|
| 40 |
/** |
| 41 |
* The file type, typically .csv |
| 42 |
* |
| 43 |
* @since 1.5 |
| 44 |
*/ |
| 45 |
public $filetype; |
| 46 |
|
| 47 |
/** |
| 48 |
* The current step being processed. |
| 49 |
* |
| 50 |
* @since 1.5 |
| 51 |
*/ |
| 52 |
public $step; |
| 53 |
|
| 54 |
/** |
| 55 |
* Start date, Y-m-d H:i:s |
| 56 |
* |
| 57 |
* @since 1.5 |
| 58 |
*/ |
| 59 |
public $start; |
| 60 |
|
| 61 |
/** |
| 62 |
* End date, Y-m-d H:i:s |
| 63 |
* |
| 64 |
* @since 1.5 |
| 65 |
*/ |
| 66 |
public $end; |
| 67 |
|
| 68 |
/** |
| 69 |
* Status to export. |
| 70 |
* |
| 71 |
* @since 1.5 |
| 72 |
*/ |
| 73 |
public $status; |
| 74 |
|
| 75 |
/** |
| 76 |
* Form to export data for. |
| 77 |
* |
| 78 |
* @since 1.5 |
| 79 |
*/ |
| 80 |
public $form = null; |
| 81 |
|
| 82 |
/** |
| 83 |
* Form Price ID to export data for. |
| 84 |
* |
| 85 |
* @since 1.5 |
| 86 |
*/ |
| 87 |
public $price_id = null; |
| 88 |
|
| 89 |
/** |
| 90 |
* Is the export file writable. |
| 91 |
* |
| 92 |
* @since 1.5 |
| 93 |
*/ |
| 94 |
public $is_writable = true; |
| 95 |
|
| 96 |
/** |
| 97 |
* Is the export file empty. |
| 98 |
* |
| 99 |
* @since 1.5 |
| 100 |
*/ |
| 101 |
public $is_empty = false; |
| 102 |
|
| 103 |
/** |
| 104 |
* |
| 105 |
* @since 1.8.9 |
| 106 |
*/ |
| 107 |
public $is_void = false; |
| 108 |
|
| 109 |
/** |
| 110 |
* Is the export file complete. |
| 111 |
* |
| 112 |
* @since 1.8.9 |
| 113 |
*/ |
| 114 |
public $done = false; |
| 115 |
|
| 116 |
/** |
| 117 |
* Give_Batch_Export constructor. |
| 118 |
* |
| 119 |
* @since 2.21.0 Create only csv file. |
| 120 |
* @since 2.9.0 add hash to filename to avoid collisions |
| 121 |
* @since 1.5 |
| 122 |
* |
| 123 |
* @param int $_step |
| 124 |
* @param string|null $filename |
| 125 |
*/ |
| 126 |
public function __construct( $_step = 1, $filename = null ) { |
| 127 |
$upload_dir = wp_upload_dir(); |
| 128 |
$this->filetype = '.csv'; |
| 129 |
|
| 130 |
if ( null === $filename ) { |
| 131 |
$hash = uniqid(); |
| 132 |
$this->filename = "give-{$hash}-{$this->export_type}{$this->filetype}"; |
| 133 |
} else { |
| 134 |
$this->filename = "{$filename}{$this->filetype}"; |
| 135 |
} |
| 136 |
|
| 137 |
$this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename; |
| 138 |
|
| 139 |
if ( ! is_writable( $upload_dir['basedir'] ) ) { |
| 140 |
$this->is_writable = false; |
| 141 |
} |
| 142 |
|
| 143 |
$this->step = $_step; |
| 144 |
$this->done = false; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Process a step. |
| 149 |
* |
| 150 |
* @since 1.5 |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public function process_step() { |
| 154 |
|
| 155 |
if ( ! $this->can_export() ) { |
| 156 |
wp_die( |
| 157 |
esc_html__( 'You do not have permission to export data.', 'give' ), |
| 158 |
esc_html__( 'Error', 'give' ), |
| 159 |
[ |
| 160 |
'response' => 403, |
| 161 |
] |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
if ( $this->step < 2 ) { |
| 166 |
|
| 167 |
// Make sure we start with a fresh file on step 1. |
| 168 |
@unlink( $this->file ); |
| 169 |
$this->print_csv_cols(); |
| 170 |
} |
| 171 |
|
| 172 |
$this->print_csv_rows(); |
| 173 |
|
| 174 |
return 100 !== $this->get_percentage_complete(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Output the CSV columns. |
| 179 |
* |
| 180 |
* @access public |
| 181 |
* @since 1.5 |
| 182 |
* @return string |
| 183 |
* @uses Give_Export::get_csv_cols() |
| 184 |
*/ |
| 185 |
public function print_csv_cols() { |
| 186 |
|
| 187 |
$col_data = ''; |
| 188 |
$cols = $this->get_csv_cols(); |
| 189 |
$i = 1; |
| 190 |
foreach ( $cols as $col_id => $column ) { |
| 191 |
$col_data .= '"' . addslashes( $column ) . '"'; |
| 192 |
$col_data .= $i == count( $cols ) ? '' : ','; |
| 193 |
$i ++; |
| 194 |
} |
| 195 |
$col_data .= "\r\n"; |
| 196 |
|
| 197 |
$this->stash_step_data( $col_data ); |
| 198 |
|
| 199 |
return $col_data; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Print the CSV rows for the current step. |
| 205 |
* |
| 206 |
* @access public |
| 207 |
* @since 1.5 |
| 208 |
* @return string|false |
| 209 |
*/ |
| 210 |
public function print_csv_rows() { |
| 211 |
|
| 212 |
$row_data = ''; |
| 213 |
$data = $this->get_data(); |
| 214 |
$cols = $this->get_csv_cols(); |
| 215 |
|
| 216 |
if ( $data ) { |
| 217 |
|
| 218 |
// Output each row |
| 219 |
foreach ( $data as $row ) { |
| 220 |
$i = 1; |
| 221 |
foreach ( $row as $col_id => $column ) { |
| 222 |
// Make sure the column is valid |
| 223 |
if ( array_key_exists( $col_id, $cols ) ) { |
| 224 |
$row_data .= '"' . addslashes( preg_replace( '/"/', "'", $column ) ) . '"'; |
| 225 |
$row_data .= $i == count( $cols ) ? '' : ','; |
| 226 |
$i ++; |
| 227 |
} |
| 228 |
} |
| 229 |
$row_data .= "\r\n"; |
| 230 |
} |
| 231 |
|
| 232 |
$this->stash_step_data( $row_data ); |
| 233 |
|
| 234 |
return $row_data; |
| 235 |
} |
| 236 |
|
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Return the calculated completion percentage. |
| 242 |
* |
| 243 |
* @since 1.5 |
| 244 |
* @return int |
| 245 |
*/ |
| 246 |
public function get_percentage_complete() { |
| 247 |
return 100; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Retrieve the file data is written to. |
| 252 |
* |
| 253 |
* @since 1.5 |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
protected function get_file() { |
| 257 |
|
| 258 |
$file = ''; |
| 259 |
|
| 260 |
if ( @file_exists( $this->file ) ) { |
| 261 |
|
| 262 |
if ( ! is_writable( $this->file ) ) { |
| 263 |
$this->is_writable = false; |
| 264 |
} |
| 265 |
|
| 266 |
$file = @file_get_contents( $this->file ); |
| 267 |
|
| 268 |
} else { |
| 269 |
|
| 270 |
@file_put_contents( $this->file, '' ); |
| 271 |
@chmod( $this->file, 0664 ); |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
return $file; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Append data to export file. |
| 280 |
* |
| 281 |
* @since 1.5 |
| 282 |
* |
| 283 |
* @param $data string The data to add to the file. |
| 284 |
* |
| 285 |
* @return void |
| 286 |
*/ |
| 287 |
protected function stash_step_data( $data = '' ) { |
| 288 |
|
| 289 |
$file = $this->get_file(); |
| 290 |
$file .= $data; |
| 291 |
@file_put_contents( $this->file, $file ); |
| 292 |
|
| 293 |
// If we have no rows after this step, mark it as an empty export. |
| 294 |
$file_rows = file( $this->file, FILE_SKIP_EMPTY_LINES ); |
| 295 |
$default_cols = $this->get_csv_cols(); |
| 296 |
$default_cols = empty( $default_cols ) ? 0 : 1; |
| 297 |
|
| 298 |
$this->is_empty = count( $file_rows ) == $default_cols ? true : false; |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Perform the export. |
| 304 |
* |
| 305 |
* @access public |
| 306 |
* @since 1.5 |
| 307 |
* @return void |
| 308 |
*/ |
| 309 |
public function export() { |
| 310 |
|
| 311 |
// Set headers |
| 312 |
$this->headers(); |
| 313 |
|
| 314 |
$file = $this->get_file(); |
| 315 |
|
| 316 |
@unlink( $this->file ); |
| 317 |
|
| 318 |
echo $file; |
| 319 |
|
| 320 |
/** |
| 321 |
* Fire action after file output. |
| 322 |
* |
| 323 |
* @since 1.8 |
| 324 |
*/ |
| 325 |
do_action( 'give_file_export_complete', $_REQUEST ); |
| 326 |
|
| 327 |
give_die(); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Set the properties specific to the export. |
| 332 |
* |
| 333 |
* @since 1.5 |
| 334 |
* |
| 335 |
* @param array $request The Form Data passed into the batch processing. |
| 336 |
*/ |
| 337 |
public function set_properties( $request ) { |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Unset the properties specific to the export. |
| 342 |
* |
| 343 |
* @since 1.8.9 |
| 344 |
* |
| 345 |
* @param array $request The Form Data passed into the batch processing. |
| 346 |
* @param Give_Batch_Export $export |
| 347 |
*/ |
| 348 |
public function unset_properties( $request, $export ) { |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Allow for pre-fetching of data for the remainder of the exporter. |
| 353 |
* |
| 354 |
* @access public |
| 355 |
* @since 1.5 |
| 356 |
* @return void |
| 357 |
*/ |
| 358 |
public function pre_fetch() { |
| 359 |
} |
| 360 |
|
| 361 |
} |
| 362 |
|