From 372f675bae3e1869c046024cc2858e50f9d65151 Mon Sep 17 00:00:00 2001 From: Christian Gastrell Date: Mon, 16 Jan 2023 21:16:49 -0300 Subject: [PATCH 1/3] add helper function to get export filename --- .../jetpack/modules/contact-form/admin.php | 35 +++++++++++++++++-- .../contact-form/grunion-contact-form.php | 24 ++++++++----- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/projects/plugins/jetpack/modules/contact-form/admin.php b/projects/plugins/jetpack/modules/contact-form/admin.php index 8718ea4940416..32d99b2736547 100644 --- a/projects/plugins/jetpack/modules/contact-form/admin.php +++ b/projects/plugins/jetpack/modules/contact-form/admin.php @@ -1288,8 +1288,18 @@ public function export_to_gdrive() { $sheet_data[] = $current_row; } - $user_id = (int) get_current_user_id(); - $spreadsheet_title = sprintf( '%s - %s', __( 'Responses', 'jetpack' ), gmdate( 'Y-m-d H:i' ) ); + $user_id = (int) get_current_user_id(); + + if ( ! empty( $post_data['post'] ) && $post_data['post'] !== 'all' ) { + $spreadsheet_title = sprintf( + '%1$s - %2$s', + $this->get_export_filename( get_the_title( (int) $post_data['post'] ) ), + gmdate( 'Y-m-d H:i' ) + ); + } else { + $spreadsheet_title = sprintf( '%s - %s', $this->get_export_filename(), gmdate( 'Y-m-d H:i' ) ); + } + require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-google-drive-helper.php'; $sheet = Jetpack_Google_Drive_Helper::create_sheet( $user_id, $spreadsheet_title, $sheet_data ); @@ -1456,6 +1466,27 @@ public function get_gdrive_export_button_markup() { array( 'data-nonce-name' => $this->export_nonce_field_gdrive ) ); } + + /** + * Get a filename for export tasks + * + * @param string $source The filtered source for exported data. + * @return string The filename without source nor date suffix. + */ + public function get_export_filename( $source = '' ) { + return $source === '' + ? sprintf( + /* translators: Site title, used to craft the export filename, eg "MySite - Jetpack Form Responses" */ + __( '%s - Jetpack Form Responses', 'jetpack' ), + sanitize_file_name( get_bloginfo( 'name' ) ) + ) + : sprintf( + /* translators: 1: Site title; 2: post title. Used to craft the export filename, eg "MySite - Jetpack Form Responses - Contact" */ + __( '%1$s - Jetpack Form Responses - %2$s', 'jetpack' ), + sanitize_file_name( get_bloginfo( 'name' ) ), + sanitize_file_name( $source ) + ); + } } Grunion_admin::init(); diff --git a/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php b/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php index b99fd857bd6ad..a170e193bbd09 100644 --- a/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php +++ b/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php @@ -1854,23 +1854,29 @@ function ( $selected ) { * Download exported data as CSV */ public function download_feedback_as_csv() { - $data = $this->get_feedback_entries_from_post(); + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verification is done on get_feedback_entries_from_post function + $post_data = wp_unslash( $_POST ); + $data = $this->get_feedback_entries_from_post(); if ( empty( $data ) ) { return; } - $filename = gmdate( 'Y-m-d' ) . '-feedback-export.csv'; - // Check if we want to download all the feedbacks or just a certain contact form - // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check is done on get_feedback_entries_from_post - if ( ! empty( $_POST['post'] ) && $_POST['post'] !== 'all' ) { - // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check is done on get_feedback_entries_from_post - $filename = gmdate( 'Y-m-d' ) . '-' . str_replace( ' ', '-', get_the_title( (int) $_POST['post'] ) ) . '.csv'; + if ( ! empty( $post_data['post'] ) && $post_data['post'] !== 'all' ) { + $filename = sprintf( + '%s - %s.csv', + Grunion_Admin::init()->get_export_filename( get_the_title( (int) $post_data['post'] ) ), + gmdate( 'Y-m-d H:i' ) + ); + } else { + $filename = sprintf( + '%s - %s.csv', + Grunion_Admin::init()->get_export_filename(), + gmdate( 'Y-m-d H:i' ) + ); } - $filename = sanitize_file_name( $filename ); - /** * Extract field names from `$data` for later use. */ From f542caee33693a8d5fd308bbbeb9d07ca7ceeff3 Mon Sep 17 00:00:00 2001 From: Christian Gastrell Date: Mon, 16 Jan 2023 21:17:35 -0300 Subject: [PATCH 2/3] use filename from response headers (crafted on backend) --- .../jetpack/modules/contact-form/js/grunion-admin.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/plugins/jetpack/modules/contact-form/js/grunion-admin.js b/projects/plugins/jetpack/modules/contact-form/js/grunion-admin.js index 7462e4a67132b..a2e9722c8269a 100644 --- a/projects/plugins/jetpack/modules/contact-form/js/grunion-admin.js +++ b/projects/plugins/jetpack/modules/contact-form/js/grunion-admin.js @@ -286,12 +286,15 @@ jQuery( function ( $ ) { selected: selected, [ nonceName ]: nonce, }, - function ( response ) { + function ( response, status, xhr ) { var blob = new Blob( [ response ], { type: 'application/octetstream' } ); - var a = document.createElement( 'a' ); a.href = window.URL.createObjectURL( blob ); - a.download = 'feedback.csv'; + + // Get filename from backend headers + var contentDispositionHeader = xhr.getResponseHeader( 'content-disposition' ); + a.download = + contentDispositionHeader.split( 'filename=' )[ 1 ] || 'Jetpack Form Responses.csv'; document.body.appendChild( a ); a.click(); From e28c309cfda4975adf2cfcce9c9005e955427365 Mon Sep 17 00:00:00 2001 From: Christian Gastrell Date: Mon, 16 Jan 2023 21:19:00 -0300 Subject: [PATCH 3/3] changelog --- projects/plugins/jetpack/changelog/change-export-file-name | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/plugins/jetpack/changelog/change-export-file-name diff --git a/projects/plugins/jetpack/changelog/change-export-file-name b/projects/plugins/jetpack/changelog/change-export-file-name new file mode 100644 index 0000000000000..76229f90155fe --- /dev/null +++ b/projects/plugins/jetpack/changelog/change-export-file-name @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Improve file/spreadsheet naming when exporting form responses. [Site name] - Jetpack Form Responses - [form source] - [date]