diff --git a/projects/plugins/jetpack/changelog/change-response-export-field-names b/projects/plugins/jetpack/changelog/change-response-export-field-names new file mode 100644 index 000000000000..9f722e3ba7dc --- /dev/null +++ b/projects/plugins/jetpack/changelog/change-response-export-field-names @@ -0,0 +1,4 @@ +Significance: minor +Type: enhancement + +Contact Form: update the name of the columns used when exporting Contact Form responses to CSV. The file will now use clear and detailed column names. 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 a170e193bbd0..9de0180838b3 100644 --- a/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php +++ b/projects/plugins/jetpack/modules/contact-form/grunion-contact-form.php @@ -1318,18 +1318,18 @@ public function get_post_content_for_csv_export( $post_id ) { * @return mixed */ public function get_post_meta_for_csv_export( $post_id ) { - $md = get_post_meta( $post_id, '_feedback_extra_fields', true ); - $md['feedback_date'] = get_the_date( DATE_RFC3339, $post_id ); - $content_fields = self::parse_fields_from_content( $post_id ); - $md['feedback_ip'] = ( isset( $content_fields['_feedback_ip'] ) ) ? $content_fields['_feedback_ip'] : 0; + $md = get_post_meta( $post_id, '_feedback_extra_fields', true ); + $md[ __( 'Date', 'jetpack' ) ] = get_the_date( DATE_RFC3339, $post_id ); + $content_fields = self::parse_fields_from_content( $post_id ); + $md[ __( 'IP Address', 'jetpack' ) ] = ( isset( $content_fields['_feedback_ip'] ) ) ? $content_fields['_feedback_ip'] : 0; // add the email_marketing_consent to the post meta. - $md['email_marketing_consent'] = 0; + $md[ _x( 'Consent', 'noun', 'jetpack' ) ] = 0; if ( isset( $content_fields['_feedback_all_fields'] ) ) { $all_fields = $content_fields['_feedback_all_fields']; // check if the email_marketing_consent field exists. if ( isset( $all_fields['email_marketing_consent'] ) ) { - $md['email_marketing_consent'] = $all_fields['email_marketing_consent']; + $md[ _x( 'Consent', 'noun', 'jetpack' ) ] = $all_fields['email_marketing_consent']; } } @@ -1364,12 +1364,13 @@ public function map_parsed_field_contents_of_post_to_field_names( $parsed_post_c $mapped_fields = array(); $field_mapping = array( - '_feedback_subject' => __( 'Contact Form', 'jetpack' ), + // TODO: Commented out since we'll be re-introducing this after some other changes + // '_feedback_subject' => __( 'Contact Form', 'jetpack' ), '_feedback_author' => '1_Name', '_feedback_author_email' => '2_Email', '_feedback_author_url' => '3_Website', '_feedback_main_comment' => '4_Comment', - '_feedback_author_ip' => '5_IP', + '_feedback_ip' => __( 'IP Address', 'jetpack' ), ); foreach ( $field_mapping as $parsed_field_name => $field_name ) { @@ -1765,13 +1766,17 @@ public function get_export_data_for_posts( $post_ids ) { * If it is not - add an empty string, which is just a placeholder in the CSV. */ foreach ( $field_names as $single_field_name ) { + /** + * Remove the numeral prefix 1_, 2_, etc, only for export results + */ + $renamed_field = preg_replace( '/^(\d{1,2}_)/', '', $single_field_name ); if ( isset( $single_post_data[ $single_field_name ] ) && ! empty( $single_post_data[ $single_field_name ] ) ) { - $result[ $single_field_name ][] = trim( $single_post_data[ $single_field_name ] ); + $result[ $renamed_field ][] = trim( $single_post_data[ $single_field_name ] ); } else { - $result[ $single_field_name ][] = ''; + $result[ $renamed_field ][] = ''; } } } @@ -1921,6 +1926,7 @@ public function download_feedback_as_csv() { } fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose + exit(); } /** diff --git a/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php b/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php index b3ca938ded07..75a39cbdf328 100644 --- a/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php +++ b/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php @@ -1240,7 +1240,7 @@ public function test_get_export_data_for_posts_fully_valid_data() { 'key4' => array( 'value4', 'value4' ), 'key5' => array( '', 'value5' ), 'key6' => array( '', 'value6' ), - '4_Comment' => array( 'This is my test 15', 'This is my test 16' ), + 'Comment' => array( 'This is my test 15', 'This is my test 16' ), ); $this->assertEquals( $expected_result, $result ); @@ -1301,7 +1301,7 @@ public function test_get_export_data_for_posts_invalid_single_entry_meta() { ), array( 'Contact Form' => 'subj1', - '4_Comment' => 'This is my test 15', + 'Comment' => 'This is my test 15', ), ), array( @@ -1311,7 +1311,7 @@ public function test_get_export_data_for_posts_invalid_single_entry_meta() { ), array( 'Contact Form' => 'subj2', - '4_Comment' => 'This is my test 16', + 'Comment' => 'This is my test 16', ), ), ); @@ -1342,7 +1342,7 @@ public function test_get_export_data_for_posts_invalid_single_entry_meta() { 'key4' => array( '', 'value4' ), 'key5' => array( '', 'value5' ), 'key6' => array( '', 'value6' ), - '4_Comment' => array( 'This is my test 15', 'This is my test 16' ), + 'Comment' => array( 'This is my test 15', 'This is my test 16' ), ); $this->assertEquals( $expected_result, $result ); @@ -1395,7 +1395,7 @@ public function test_get_export_data_for_posts_invalid_all_entries_meta() { ), array( 'Contact Form' => 'subj1', - '4_Comment' => 'This is my test 15', + 'Comment' => 'This is my test 15', ), ), array( @@ -1405,7 +1405,7 @@ public function test_get_export_data_for_posts_invalid_all_entries_meta() { ), array( 'Contact Form' => 'subj2', - '4_Comment' => 'This is my test 16', + 'Comment' => 'This is my test 16', ), ), ); @@ -1430,7 +1430,7 @@ public function test_get_export_data_for_posts_invalid_all_entries_meta() { $expected_result = array( 'Contact Form' => array( 'subj1', 'subj2' ), - '4_Comment' => array( 'This is my test 15', 'This is my test 16' ), + 'Comment' => array( 'This is my test 15', 'This is my test 16' ), ); $this->assertEquals( $expected_result, $result ); @@ -1500,7 +1500,7 @@ public function test_get_export_data_for_posts_single_invalid_entry_for_parse_fi ), array( 'Contact Form' => 'subj1', - '4_Comment' => 'This is my test 15', + 'Comment' => 'This is my test 15', ), ), array( @@ -1510,7 +1510,7 @@ public function test_get_export_data_for_posts_single_invalid_entry_for_parse_fi ), array( 'Contact Form' => 'subj2', - '4_Comment' => 'This is my test 16', + 'Comment' => 'This is my test 16', ), ), ); @@ -1539,7 +1539,7 @@ public function test_get_export_data_for_posts_single_invalid_entry_for_parse_fi 'key4' => array( 'value4' ), 'key5' => array( 'value5' ), 'key6' => array( 'value6' ), - '4_Comment' => array( 'This is my test 16' ), + 'Comment' => array( 'This is my test 16' ), ); $this->assertEquals( $expected_result, $result ); @@ -1611,10 +1611,9 @@ public function test_map_parsed_field_contents_of_post_to_field_names() { $result = $plugin->map_parsed_field_contents_of_post_to_field_names( $input_data ); $expected_result = array( - 'Contact Form' => 'This is my form', - '1_Name' => 'John Smith', - '3_Website' => 'http://example.com', - '4_Comment' => 'This is my comment!', + '1_Name' => 'John Smith', + '3_Website' => 'http://example.com', + '4_Comment' => 'This is my comment!', ); $this->assertEquals( $expected_result, $result ); @@ -1666,7 +1665,7 @@ public function test_personal_data_exporter() { $this->assertSame( 'feedback', $data['group_id'], 'group_id matches' ); $this->assertSame( 'Feedback', $data['group_label'], 'group_label matches' ); $this->assertSame( true, ! empty( $data['item_id'] ), 'has item_id key' ); - $this->assertCount( 9, $data['data'], 'has total expected data keys' ); + $this->assertCount( 8, $data['data'], 'has total expected data keys' ); } }