Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change responses export field names #28425

Merged
merged 12 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ][] = '';
}
}
}
Expand Down Expand Up @@ -1921,6 +1926,7 @@ public function download_feedback_as_csv() {
}

fclose( $output ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
exit();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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(
Expand All @@ -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',
),
),
);
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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(
Expand All @@ -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',
),
),
);
Expand All @@ -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 );
Expand Down Expand Up @@ -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(
Expand All @@ -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',
),
),
);
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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' );
}
}

Expand Down