2017-05-10 16:22:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles Batch CSV export.
|
|
|
|
*
|
|
|
|
* Based on https://pippinsplugins.com/batch-processing-for-big-data/
|
|
|
|
*
|
|
|
|
* @author Automattic
|
|
|
|
* @category Admin
|
|
|
|
* @package WooCommerce/Export
|
|
|
|
* @version 3.1.0
|
|
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include dependencies.
|
|
|
|
*/
|
|
|
|
if ( ! class_exists( 'WC_CSV_Exporter', false ) ) {
|
|
|
|
include_once( WC_ABSPATH . 'includes/export/abstract-wc-csv-exporter.php' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_CSV_Exporter Class.
|
|
|
|
*/
|
|
|
|
abstract class WC_CSV_Batch_Exporter extends WC_CSV_Exporter {
|
2017-05-12 16:00:14 +00:00
|
|
|
|
2017-05-10 16:22:08 +00:00
|
|
|
/**
|
|
|
|
* The file being exported to.
|
2017-05-12 19:15:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
protected $file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Page being exported
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $page = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$upload_dir = wp_upload_dir();
|
2017-05-12 19:15:08 +00:00
|
|
|
$this->file = trailingslashit( $upload_dir['basedir'] ) . $this->get_filename();
|
2017-05-10 16:22:08 +00:00
|
|
|
$this->column_names = $this->get_default_column_names();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file contents.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_file() {
|
|
|
|
$file = '';
|
|
|
|
if ( @file_exists( $this->file ) ) {
|
|
|
|
$file = @file_get_contents( $this->file );
|
|
|
|
} else {
|
|
|
|
@file_put_contents( $this->file, '' );
|
|
|
|
@chmod( $this->file, 0664 );
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-12 19:15:08 +00:00
|
|
|
* Serve the file and remove once sent to the client.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
public function export() {
|
|
|
|
$this->send_headers();
|
|
|
|
$this->send_content( $this->get_file() );
|
|
|
|
@unlink( $this->file );
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the CSV file.
|
2017-05-12 19:15:08 +00:00
|
|
|
*
|
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
public function generate_file() {
|
|
|
|
if ( 1 === $this->get_page() ) {
|
|
|
|
@unlink( $this->file );
|
|
|
|
}
|
|
|
|
$this->prepare_data_to_export();
|
|
|
|
$this->write_csv_data( $this->get_csv_data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data to the file.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @param string $data
|
|
|
|
*/
|
|
|
|
protected function write_csv_data( $data ) {
|
|
|
|
$file = $this->get_file();
|
|
|
|
|
|
|
|
// Add columns when finished.
|
|
|
|
if ( 100 === $this->get_percent_complete() ) {
|
|
|
|
$file = $this->export_column_headers() . $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file .= $data;
|
|
|
|
@file_put_contents( $this->file, $file );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get page.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_page() {
|
|
|
|
return $this->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set page.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @param int $page
|
|
|
|
*/
|
|
|
|
public function set_page( $page ) {
|
|
|
|
$this->page = absint( $page );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get count of records exported.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_total_exported() {
|
|
|
|
return ( $this->get_page() * $this->get_limit() ) + $this->exported_row_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get total % complete.
|
|
|
|
*
|
2017-05-12 19:15:08 +00:00
|
|
|
* @since 3.1.0
|
2017-05-10 16:22:08 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_percent_complete() {
|
|
|
|
return $this->total_rows ? floor( ( $this->get_total_exported() / $this->total_rows ) * 100 ) : 100;
|
|
|
|
}
|
|
|
|
}
|