2017-05-10 16:22:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handles Batch CSV export.
|
|
|
|
*
|
|
|
|
* Based on https://pippinsplugins.com/batch-processing-for-big-data/
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Export
|
2017-05-10 16:22:08 +00:00
|
|
|
* @version 3.1.0
|
|
|
|
*/
|
2018-03-06 05:52:03 +00:00
|
|
|
|
2019-10-31 14:58:54 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2017-05-10 16:22:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Include dependencies.
|
|
|
|
*/
|
|
|
|
if ( ! class_exists( 'WC_CSV_Exporter', false ) ) {
|
2018-03-06 05:52:03 +00:00
|
|
|
require_once WC_ABSPATH . 'includes/export/abstract-wc-csv-exporter.php';
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* Page being exported
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $page = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->column_names = $this->get_default_column_names();
|
|
|
|
}
|
|
|
|
|
2018-01-02 13:16:43 +00:00
|
|
|
/**
|
|
|
|
* Get file path to export to.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_file_path() {
|
|
|
|
$upload_dir = wp_upload_dir();
|
|
|
|
return trailingslashit( $upload_dir['basedir'] ) . $this->get_filename();
|
|
|
|
}
|
2021-04-23 09:34:23 +00:00
|
|
|
|
2021-04-23 09:20:45 +00:00
|
|
|
/**
|
|
|
|
* Get CSV headers row file path to export to.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_headers_row_file_path() {
|
2021-04-23 09:34:23 +00:00
|
|
|
return $this->get_file_path() . '.headers';
|
2021-04-23 09:20:45 +00:00
|
|
|
}
|
2021-04-23 09:34:23 +00:00
|
|
|
|
2021-04-23 09:20:45 +00:00
|
|
|
/**
|
|
|
|
* Get the contents of the CSV headers row file. Defaults to the original known headers.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_headers_row_file() {
|
|
|
|
|
2021-04-23 09:34:23 +00:00
|
|
|
$file = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers();
|
2021-04-23 09:20:45 +00:00
|
|
|
|
|
|
|
if ( @file_exists( $this->get_headers_row_file_path() ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
|
|
|
|
$file = @file_get_contents( $this->get_headers_row_file_path() ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
2018-01-02 13:16:43 +00:00
|
|
|
|
2017-05-10 16:22:08 +00:00
|
|
|
/**
|
|
|
|
* 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 = '';
|
2018-03-06 05:52:03 +00:00
|
|
|
if ( @file_exists( $this->get_file_path() ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
|
|
|
|
$file = @file_get_contents( $this->get_file_path() ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
|
2017-05-10 16:22:08 +00:00
|
|
|
} else {
|
2018-03-06 05:52:03 +00:00
|
|
|
@file_put_contents( $this->get_file_path(), '' ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_file_put_contents, Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
|
|
|
|
@chmod( $this->get_file_path(), 0664 ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.chmod_chmod, WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents, Generic.PHP.NoSilencedErrors.Discouraged
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
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();
|
2021-04-23 09:20:45 +00:00
|
|
|
$this->send_content( $this->get_headers_row_file() . $this->get_file() );
|
2018-03-06 05:52:03 +00:00
|
|
|
@unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged
|
2021-04-23 09:20:45 +00:00
|
|
|
@unlink( $this->get_headers_row_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged
|
2017-05-10 16:22:08 +00:00
|
|
|
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() ) {
|
2018-03-06 05:52:03 +00:00
|
|
|
@unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged,
|
2021-04-23 09:20:45 +00:00
|
|
|
|
2021-04-23 09:34:23 +00:00
|
|
|
// we need to initialize the file here
|
2021-04-23 09:20:45 +00:00
|
|
|
$this->get_file();
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
$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
|
2018-03-06 05:52:03 +00:00
|
|
|
* @param string $data Data.
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
protected function write_csv_data( $data ) {
|
2021-04-23 09:34:23 +00:00
|
|
|
|
|
|
|
if ( ! file_exists( $this->get_file_path() ) || ! is_writeable( $this->get_file_path() ) ) {
|
2021-04-23 09:20:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-10 16:22:08 +00:00
|
|
|
|
2021-04-23 09:34:23 +00:00
|
|
|
$fp = fopen( $this->get_file_path(), 'a+' );
|
|
|
|
|
|
|
|
if ( $fp ) {
|
2021-04-23 09:20:45 +00:00
|
|
|
fwrite( $fp, $data );
|
|
|
|
fclose( $fp );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all columns when finished.
|
2017-05-10 16:22:08 +00:00
|
|
|
if ( 100 === $this->get_percent_complete() ) {
|
2021-04-23 09:20:45 +00:00
|
|
|
$header = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers();
|
|
|
|
|
2021-04-23 09:34:23 +00:00
|
|
|
// We need to use a temporary file to store headers, this will make our life so much easier.
|
2021-04-23 09:23:18 +00:00
|
|
|
@file_put_contents( $this->get_headers_row_file_path(), $header ); //phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_file_put_contents, Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2018-03-06 05:52:03 +00:00
|
|
|
* @param int $page Page Nr.
|
2017-05-10 16:22:08 +00:00
|
|
|
*/
|
|
|
|
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() {
|
2017-12-12 19:33:48 +00:00
|
|
|
return ( ( $this->get_page() - 1 ) * $this->get_limit() ) + $this->exported_row_count;
|
2017-05-10 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|