abstract-wc-csv-batch-exporter.php
3 years ago
abstract-wc-csv-exporter.php
2 years ago
class-wc-product-csv-exporter.php
3 years ago
abstract-wc-csv-batch-exporter.php
208 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles Batch CSV export. |
| 4 | * |
| 5 | * Based on https://pippinsplugins.com/batch-processing-for-big-data/ |
| 6 | * |
| 7 | * @package WooCommerce\Export |
| 8 | * @version 3.1.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Include dependencies. |
| 15 | */ |
| 16 | if ( ! class_exists( 'WC_CSV_Exporter', false ) ) { |
| 17 | require_once WC_ABSPATH . 'includes/export/abstract-wc-csv-exporter.php'; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * WC_CSV_Exporter Class. |
| 22 | */ |
| 23 | abstract class WC_CSV_Batch_Exporter extends WC_CSV_Exporter { |
| 24 | |
| 25 | /** |
| 26 | * Page being exported |
| 27 | * |
| 28 | * @var integer |
| 29 | */ |
| 30 | protected $page = 1; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | */ |
| 35 | public function __construct() { |
| 36 | $this->column_names = $this->get_default_column_names(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get file path to export to. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | protected function get_file_path() { |
| 45 | $upload_dir = wp_upload_dir(); |
| 46 | return trailingslashit( $upload_dir['basedir'] ) . $this->get_filename(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get CSV headers row file path to export to. |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | protected function get_headers_row_file_path() { |
| 55 | return $this->get_file_path() . '.headers'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the contents of the CSV headers row file. Defaults to the original known headers. |
| 60 | * |
| 61 | * @since 3.1.0 |
| 62 | * @return string |
| 63 | */ |
| 64 | public function get_headers_row_file() { |
| 65 | |
| 66 | $file = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers(); |
| 67 | |
| 68 | if ( @file_exists( $this->get_headers_row_file_path() ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 69 | $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 |
| 70 | } |
| 71 | |
| 72 | return $file; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the file contents. |
| 77 | * |
| 78 | * @since 3.1.0 |
| 79 | * @return string |
| 80 | */ |
| 81 | public function get_file() { |
| 82 | $file = ''; |
| 83 | if ( @file_exists( $this->get_file_path() ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 84 | $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 |
| 85 | } else { |
| 86 | @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 |
| 87 | @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 |
| 88 | } |
| 89 | return $file; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Serve the file and remove once sent to the client. |
| 94 | * |
| 95 | * @since 3.1.0 |
| 96 | */ |
| 97 | public function export() { |
| 98 | $this->send_headers(); |
| 99 | $this->send_content( $this->get_headers_row_file() . $this->get_file() ); |
| 100 | @unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged |
| 101 | @unlink( $this->get_headers_row_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged |
| 102 | die(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Generate the CSV file. |
| 107 | * |
| 108 | * @since 3.1.0 |
| 109 | */ |
| 110 | public function generate_file() { |
| 111 | if ( 1 === $this->get_page() ) { |
| 112 | @unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged, |
| 113 | |
| 114 | // We need to initialize the file here. |
| 115 | $this->get_file(); |
| 116 | } |
| 117 | $this->prepare_data_to_export(); |
| 118 | $this->write_csv_data( $this->get_csv_data() ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Write data to the file. |
| 123 | * |
| 124 | * @since 3.1.0 |
| 125 | * @param string $data Data. |
| 126 | */ |
| 127 | protected function write_csv_data( $data ) { |
| 128 | |
| 129 | if ( ! file_exists( $this->get_file_path() ) || ! is_writeable( $this->get_file_path() ) ) { |
| 130 | wc_get_logger()->error( |
| 131 | sprintf( |
| 132 | /* translators: %s is file path. */ |
| 133 | __( 'Unable to create or write to %s during CSV export. Please check file permissions.', 'woocommerce' ), |
| 134 | esc_html( $this->get_file_path() ) |
| 135 | ) |
| 136 | ); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Filters the mode parameter which specifies the type of access you require to the stream (used during file |
| 142 | * writing for CSV exports). Defaults to 'a+' (which supports both reading and writing, and places the file |
| 143 | * pointer at the end of the file). |
| 144 | * |
| 145 | * @see https://www.php.net/manual/en/function.fopen.php |
| 146 | * @since 6.8.0 |
| 147 | * |
| 148 | * @param string $fopen_mode, either (r, r+, w, w+, a, a+, x, x+, c, c+, e) |
| 149 | */ |
| 150 | $fopen_mode = apply_filters( 'woocommerce_csv_exporter_fopen_mode', 'a+' ); |
| 151 | $fp = fopen( $this->get_file_path(), $fopen_mode ); |
| 152 | |
| 153 | if ( $fp ) { |
| 154 | fwrite( $fp, $data ); |
| 155 | fclose( $fp ); |
| 156 | } |
| 157 | |
| 158 | // Add all columns when finished. |
| 159 | if ( 100 === $this->get_percent_complete() ) { |
| 160 | $header = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers(); |
| 161 | |
| 162 | // We need to use a temporary file to store headers, this will make our life so much easier. |
| 163 | @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 |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get page. |
| 170 | * |
| 171 | * @since 3.1.0 |
| 172 | * @return int |
| 173 | */ |
| 174 | public function get_page() { |
| 175 | return $this->page; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Set page. |
| 180 | * |
| 181 | * @since 3.1.0 |
| 182 | * @param int $page Page Nr. |
| 183 | */ |
| 184 | public function set_page( $page ) { |
| 185 | $this->page = absint( $page ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get count of records exported. |
| 190 | * |
| 191 | * @since 3.1.0 |
| 192 | * @return int |
| 193 | */ |
| 194 | public function get_total_exported() { |
| 195 | return ( ( $this->get_page() - 1 ) * $this->get_limit() ) + $this->exported_row_count; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get total % complete. |
| 200 | * |
| 201 | * @since 3.1.0 |
| 202 | * @return int |
| 203 | */ |
| 204 | public function get_percent_complete() { |
| 205 | return $this->total_rows ? (int) floor( ( $this->get_total_exported() / $this->total_rows ) * 100 ) : 100; |
| 206 | } |
| 207 | } |
| 208 |