From 2a748c6de686a58fb35ad1d1eda11e1269617044 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 19 Aug 2019 17:52:16 -0300 Subject: [PATCH] better headers in csv exposer. add filename --- src/exposers/class-tainacan-csv.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/exposers/class-tainacan-csv.php b/src/exposers/class-tainacan-csv.php index 0bcafbc70..abfba23dd 100644 --- a/src/exposers/class-tainacan-csv.php +++ b/src/exposers/class-tainacan-csv.php @@ -22,9 +22,29 @@ class Csv extends Exposer { */ public function rest_request_after_callbacks( $response, $handler, $request ) { $response->set_headers( [ - 'Content-Type: text/csv; charset=' . get_option( 'blog_charset' ), - 'Content-disposition: attachment;filename=tainacan.csv'] // TODO filter/optional - ); + 'Content-Type: text/csv; charset=' . get_option( 'blog_charset' ) + ]); + + if (isset($request['collection_id'])) { + $collection = \Tainacan\Repositories\Collections::get_instance()->fetch( (int) $request['collection_id'] ); + if ($collection) { + $filename = sanitize_title($collection->get_name()); + } + } else { + $filename = sanitize_title(get_option('blogname')); + } + + if (empty($filename)) { + $filename = sanitize_title( __('items', 'tainacan') ); + } + + if ( isset($request['paged']) ) { + $filename .= '-' . $request['paged']; + } + + $filename .= '.csv'; + + header('Content-Disposition: attachment; filename="'.$filename.'"'); $items = $response->get_data(); $items = is_array($items) && isset($items['items']) ? $items['items'] : [];