← All changes
|
includes/admin/tools/export/class-batch-export.php
+32
-21
2.3.1
→
4.16.9
View file →
| @@ -4,12 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * This is the base class for all batch export methods. Each data export type (donors, payments, etc) extend this class. |
| 6 | 6 | * |
| 7 | 7 | * @package Give |
| 8 | - * @subpackage Admin/Export | |
| 8 | + * @since 1.5 | |
| 9 | 9 | * @copyright Copyright (c) 2016, GiveWP |
| 10 | 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | - * @since 1.5 | |
| 11 | + * @subpackage Admin/Export | |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | // Exit if accessed directly. |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -115,18 +115,29 @@ | ||
| 115 | 115 | |
| 116 | 116 | /** |
| 117 | 117 | * Give_Batch_Export constructor. |
| 118 | 118 | * |
| 119 | - * @param int $_step | |
| 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 | |
| 120 | 125 | */ |
| 121 | - public function __construct( $_step = 1 ) { | |
| 126 | + public function __construct( $_step = 1, $filename = null ) { | |
| 127 | + $upload_dir = wp_upload_dir(); | |
| 128 | + $this->filetype = '.csv'; | |
| 122 | 129 | |
| 123 | - $upload_dir = wp_upload_dir(); | |
| 124 | - $this->filetype = '.csv'; | |
| 125 | - $this->filename = 'give-' . $this->export_type . $this->filetype; | |
| 126 | - $this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename; | |
| 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 | + } | |
| 127 | 136 | |
| 128 | - if ( ! is_writeable( $upload_dir['basedir'] ) ) { | |
| 137 | + $this->file = trailingslashit( $upload_dir['basedir'] ) . $this->filename; | |
| 138 | + | |
| 139 | + if ( ! is_writable( $upload_dir['basedir'] ) ) { | |
| 129 | 140 | $this->is_writable = false; |
| 130 | 141 | } |
| 131 | 142 | |
| 132 | 143 | $this->step = $_step; |
| @@ -141,11 +152,15 @@ | ||
| 141 | 152 | */ |
| 142 | 153 | public function process_step() { |
| 143 | 154 | |
| 144 | 155 | if ( ! $this->can_export() ) { |
| 145 | - wp_die( esc_html__( 'You do not have permission to export data.', 'give' ), esc_html__( 'Error', 'give' ), array( | |
| 146 | - 'response' => 403, | |
| 147 | - ) ); | |
| 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 | + ); | |
| 148 | 163 | } |
| 149 | 164 | |
| 150 | 165 | if ( $this->step < 2 ) { |
| 151 | 166 | |
| @@ -153,15 +168,11 @@ | ||
| 153 | 168 | @unlink( $this->file ); |
| 154 | 169 | $this->print_csv_cols(); |
| 155 | 170 | } |
| 156 | 171 | |
| 157 | - $rows = $this->print_csv_rows(); | |
| 172 | + $this->print_csv_rows(); | |
| 158 | 173 | |
| 159 | - if ( $rows ) { | |
| 160 | - return true; | |
| 161 | - } else { | |
| 162 | - return false; | |
| 163 | - } | |
| 174 | + return 100 !== $this->get_percentage_complete(); | |
| 164 | 175 | } |
| 165 | 176 | |
| 166 | 177 | /** |
| 167 | 178 | * Output the CSV columns. |
| @@ -167,10 +178,10 @@ | ||
| 167 | 178 | * Output the CSV columns. |
| 168 | 179 | * |
| 169 | 180 | * @access public |
| 170 | 181 | * @since 1.5 |
| 182 | + * @return string | |
| 171 | 183 | * @uses Give_Export::get_csv_cols() |
| 172 | - * @return string | |
| 173 | 184 | */ |
| 174 | 185 | public function print_csv_cols() { |
| 175 | 186 | |
| 176 | 187 | $col_data = ''; |
| @@ -247,9 +258,9 @@ | ||
| 247 | 258 | $file = ''; |
| 248 | 259 | |
| 249 | 260 | if ( @file_exists( $this->file ) ) { |
| 250 | 261 | |
| 251 | - if ( ! is_writeable( $this->file ) ) { | |
| 262 | + if ( ! is_writable( $this->file ) ) { | |
| 252 | 263 | $this->is_writable = false; |
| 253 | 264 | } |
| 254 | 265 | |
| 255 | 266 | $file = @file_get_contents( $this->file ); |
| @@ -274,9 +285,9 @@ | ||
| 274 | 285 | * @return void |
| 275 | 286 | */ |
| 276 | 287 | protected function stash_step_data( $data = '' ) { |
| 277 | 288 | |
| 278 | - $file = $this->get_file(); | |
| 289 | + $file = $this->get_file(); | |
| 279 | 290 | $file .= $data; |
| 280 | 291 | @file_put_contents( $this->file, $file ); |
| 281 | 292 | |
| 282 | 293 | // If we have no rows after this step, mark it as an empty export. |