abstract-wc-csv-batch-exporter.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if ( ! defined( 'ABSPATH' ) ) {
  11. 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. * WC_CSV_Exporter Class.
  21. */
  22. abstract class WC_CSV_Batch_Exporter extends WC_CSV_Exporter {
  23. /**
  24. * Page being exported
  25. *
  26. * @var integer
  27. */
  28. protected $page = 1;
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct() {
  33. $this->column_names = $this->get_default_column_names();
  34. }
  35. /**
  36. * Get file path to export to.
  37. *
  38. * @return string
  39. */
  40. protected function get_file_path() {
  41. $upload_dir = wp_upload_dir();
  42. return trailingslashit( $upload_dir['basedir'] ) . $this->get_filename();
  43. }
  44. /**
  45. * Get the file contents.
  46. *
  47. * @since 3.1.0
  48. * @return string
  49. */
  50. public function get_file() {
  51. $file = '';
  52. if ( @file_exists( $this->get_file_path() ) ) { // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
  53. $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
  54. } else {
  55. @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
  56. @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
  57. }
  58. return $file;
  59. }
  60. /**
  61. * Serve the file and remove once sent to the client.
  62. *
  63. * @since 3.1.0
  64. */
  65. public function export() {
  66. $this->send_headers();
  67. $this->send_content( $this->get_file() );
  68. @unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged
  69. die();
  70. }
  71. /**
  72. * Generate the CSV file.
  73. *
  74. * @since 3.1.0
  75. */
  76. public function generate_file() {
  77. if ( 1 === $this->get_page() ) {
  78. @unlink( $this->get_file_path() ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink, Generic.PHP.NoSilencedErrors.Discouraged,
  79. }
  80. $this->prepare_data_to_export();
  81. $this->write_csv_data( $this->get_csv_data() );
  82. }
  83. /**
  84. * Write data to the file.
  85. *
  86. * @since 3.1.0
  87. * @param string $data Data.
  88. */
  89. protected function write_csv_data( $data ) {
  90. $file = $this->get_file();
  91. // Add columns when finished.
  92. if ( 100 === $this->get_percent_complete() ) {
  93. $file = chr( 239 ) . chr( 187 ) . chr( 191 ) . $this->export_column_headers() . $file;
  94. }
  95. $file .= $data;
  96. @file_put_contents( $this->get_file_path(), $file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_file_put_contents, Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
  97. }
  98. /**
  99. * Get page.
  100. *
  101. * @since 3.1.0
  102. * @return int
  103. */
  104. public function get_page() {
  105. return $this->page;
  106. }
  107. /**
  108. * Set page.
  109. *
  110. * @since 3.1.0
  111. * @param int $page Page Nr.
  112. */
  113. public function set_page( $page ) {
  114. $this->page = absint( $page );
  115. }
  116. /**
  117. * Get count of records exported.
  118. *
  119. * @since 3.1.0
  120. * @return int
  121. */
  122. public function get_total_exported() {
  123. return ( ( $this->get_page() - 1 ) * $this->get_limit() ) + $this->exported_row_count;
  124. }
  125. /**
  126. * Get total % complete.
  127. *
  128. * @since 3.1.0
  129. * @return int
  130. */
  131. public function get_percent_complete() {
  132. return $this->total_rows ? floor( ( $this->get_total_exported() / $this->total_rows ) * 100 ) : 100;
  133. }
  134. }