| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Controller; |
| 9 | 9 | |
| 10 | 10 | use Exception; |
| 11 | +use Generator; | |
| 11 | 12 | use WP_Defender\Event; |
| 12 | 13 | use Calotes\Component\Request; |
| 13 | 14 | use Calotes\Component\Response; |
| 14 | 15 | use WP_Defender\Traits\Setting; |
| @@ -13,8 +14,9 @@ | ||
| 13 | 14 | use Calotes\Component\Response; |
| 14 | 15 | use WP_Defender\Traits\Setting; |
| 15 | 16 | use WP_Defender\Model\Lockout_Log; |
| 16 | 17 | use WP_Defender\Model\Setting\User_Agent_Lockout; |
| 18 | +use WP_Defender\Component\Export\Csv; | |
| 17 | 19 | use WP_Defender\Component\Config\Config_Hub_Helper; |
| 18 | 20 | use WP_Defender\Component\User_Agent as User_Agent_Service; |
| 19 | 21 | |
| 20 | 22 | /** |
| @@ -345,12 +347,23 @@ | ||
| 345 | 347 | * @defender_route |
| 346 | 348 | * @since 2.6.0 |
| 347 | 349 | */ |
| 348 | 350 | public function export_ua(): void { |
| 349 | - $data = array(); | |
| 351 | + try { | |
| 352 | + Csv::to_browser( 'ua', $this->get_export_rows() ); | |
| 353 | + } catch ( \RuntimeException $e ) { | |
| 354 | + $this->log( $e->getMessage(), 'ua-lockout' ); | |
| 355 | + } | |
| 356 | + } | |
| 350 | 357 | |
| 358 | + /** | |
| 359 | + * Generates User Agent export rows. | |
| 360 | + * | |
| 361 | + * @return Generator | |
| 362 | + */ | |
| 363 | + private function get_export_rows(): Generator { | |
| 351 | 364 | foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) { |
| 352 | - $data[] = array( | |
| 365 | + yield array( | |
| 353 | 366 | 'ua' => $ua, |
| 354 | 367 | 'type' => 'allowlist', |
| 355 | 368 | ); |
| 356 | 369 | } |
| @@ -355,26 +368,13 @@ | ||
| 355 | 368 | ); |
| 356 | 369 | } |
| 357 | 370 | |
| 358 | 371 | foreach ( $this->model->get_all_selected_blocklist_ua( false ) as $ua ) { |
| 359 | - $data[] = array( | |
| 372 | + yield array( | |
| 360 | 373 | 'ua' => $ua, |
| 361 | 374 | 'type' => 'blocklist', |
| 362 | 375 | ); |
| 363 | 376 | } |
| 364 | - | |
| 365 | - // WP_Filesystem class doesn’t directly provide a function for opening a stream to php://memory with the 'w' mode. | |
| 366 | - $fp = fopen( 'php://memory', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 367 | - foreach ( $data as $fields ) { | |
| 368 | - fputcsv( $fp, $fields, ',', '"', '\\' ); | |
| 369 | - } | |
| 370 | - $filename = 'wdf-ua-export-' . wp_date( 'ymdHis' ) . '.csv'; | |
| 371 | - fseek( $fp, 0 ); | |
| 372 | - header( 'Content-Type: text/csv' ); | |
| 373 | - header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); | |
| 374 | - // Make php send the generated csv lines to the browser. | |
| 375 | - fpassthru( $fp ); | |
| 376 | - exit(); | |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | /** |
| 380 | 380 | * Importing UAs from exporter. |