diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php
index 6028cb6a3b..b547468184 100644
--- a/src/wp-admin/includes/privacy-tools.php
+++ b/src/wp-admin/includes/privacy-tools.php
@@ -324,42 +324,20 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
$file_basename = 'wp-personal-data-file-' . $obscura;
$html_report_filename = wp_unique_filename( $exports_dir, $file_basename . '.html' );
$html_report_pathname = wp_normalize_path( $exports_dir . $html_report_filename );
- $file = fopen( $html_report_pathname, 'w' );
- if ( false === $file ) {
- wp_send_json_error( __( 'Unable to open export file (HTML report) for writing.' ) );
- }
+ $json_report_filename = $file_basename . '.json';
+ $json_report_pathname = wp_normalize_path( $exports_dir . $json_report_filename );
+ /*
+ * Gather general data needed.
+ */
+
+ // Title.
$title = sprintf(
/* translators: %s: User's email address. */
__( 'Personal Data Export for %s' ),
$email_address
);
- // Open HTML.
- fwrite( $file, "\n" );
- fwrite( $file, "\n" );
-
- // Head.
- fwrite( $file, "
\n" );
- fwrite( $file, "\n" );
- fwrite( $file, "' );
- fwrite( $file, '' );
- fwrite( $file, esc_html( $title ) );
- fwrite( $file, '' );
- fwrite( $file, "\n" );
-
- // Body.
- fwrite( $file, "\n" );
-
- // Heading.
- fwrite( $file, '' . esc_html__( 'Personal Data Export' ) . '
' );
-
// And now, all the Groups.
$groups = get_post_meta( $request_id, '_export_data_grouped', true );
@@ -394,14 +372,57 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
// Merge in the special about group.
$groups = array_merge( array( 'about' => $about_group ), $groups );
+ // Convert the groups to JSON format.
+ $groups_json = wp_json_encode( $groups );
+
+ /*
+ * Handle the JSON export.
+ */
+ $file = fopen( $json_report_pathname, 'w' );
+
+ if ( false === $file ) {
+ wp_send_json_error( __( 'Unable to open export file (JSON report) for writing.' ) );
+ }
+
+ fwrite( $file, '{' );
+ fwrite( $file, '"' . $title . '":' );
+ fwrite( $file, $groups_json );
+ fwrite( $file, '}' );
+ fclose( $file );
+
+ /*
+ * Handle the HTML export.
+ */
+ $file = fopen( $html_report_pathname, 'w' );
+
+ if ( false === $file ) {
+ wp_send_json_error( __( 'Unable to open export file (HTML report) for writing.' ) );
+ }
+
+ fwrite( $file, "\n" );
+ fwrite( $file, "\n" );
+ fwrite( $file, "\n" );
+ fwrite( $file, "\n" );
+ fwrite( $file, "' );
+ fwrite( $file, '' );
+ fwrite( $file, esc_html( $title ) );
+ fwrite( $file, '' );
+ fwrite( $file, "\n" );
+ fwrite( $file, "\n" );
+ fwrite( $file, '' . esc_html__( 'Personal Data Export' ) . '
' );
+
// Now, iterate over every group in $groups and have the formatter render it in HTML.
foreach ( (array) $groups as $group_id => $group_data ) {
fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
}
fwrite( $file, "\n" );
-
- // Close HTML.
fwrite( $file, "\n" );
fclose( $file );
@@ -431,8 +452,12 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
$zip = new ZipArchive;
if ( true === $zip->open( $archive_pathname, ZipArchive::CREATE ) ) {
+ if ( ! $zip->addFile( $json_report_pathname, 'export.json' ) ) {
+ $error = __( 'Unable to add data to JSON file.' );
+ }
+
if ( ! $zip->addFile( $html_report_pathname, 'index.html' ) ) {
- $error = __( 'Unable to add data to export file.' );
+ $error = __( 'Unable to add data to HTML file.' );
}
$zip->close();
@@ -448,13 +473,16 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) {
* @param string $html_report_pathname The full path to the personal data report on the filesystem.
* @param int $request_id The export request ID.
*/
- do_action( 'wp_privacy_personal_data_export_file_created', $archive_pathname, $archive_url, $html_report_pathname, $request_id );
+ do_action( 'wp_privacy_personal_data_export_file_created', $archive_pathname, $archive_url, $html_report_pathname, $request_id, $json_report_pathname );
}
} else {
$error = __( 'Unable to open export file (archive) for writing.' );
}
- // And remove the HTML file.
+ // Remove the JSON file.
+ unlink( $json_report_pathname );
+
+ // Remove the HTML file.
unlink( $html_report_pathname );
if ( $error ) {
diff --git a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
index 9ca05dadca..c38007f498 100644
--- a/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
+++ b/tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
@@ -237,11 +237,11 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
}
/**
- * Test the export file has all the expected parts.
+ * Test the export HTML file has all the expected parts.
*
* @ticket 44233
*/
- public function test_contents() {
+ public function test_html_contents() {
$this->expectOutputString( '' );
wp_privacy_generate_personal_data_export_file( self::$export_request_id );
$this->assertTrue( file_exists( $this->export_file_name ) );
@@ -264,4 +264,34 @@ class Tests_Privacy_WpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestC
$this->assertContains( 'About
', $report_contents );
$this->assertContains( $request->email, $report_contents );
}
+
+ /**
+ * Test the export JSON file has all the expected parts.
+ *
+ * @ticket 49029
+ */
+ public function test_json_contents() {
+ $this->expectOutputString( '' );
+ wp_privacy_generate_personal_data_export_file( self::$export_request_id );
+ $this->assertTrue( file_exists( $this->export_file_name ) );
+
+ $report_dir = trailingslashit( self::$exports_dir . 'test_contents' );
+ mkdir( $report_dir );
+
+ $zip = new ZipArchive();
+ $opened_zip = $zip->open( $this->export_file_name );
+ $this->assertTrue( $opened_zip );
+
+ $zip->extractTo( $report_dir );
+ $zip->close();
+
+ $request = wp_get_user_request_data( self::$export_request_id );
+
+ $this->assertTrue( file_exists( $report_dir . 'export.json' ) );
+
+ $report_contents_json = file_get_contents( $report_dir . 'export.json' );
+
+ $this->assertContains( '"Personal Data Export for ' . $request->email . '"', $report_contents_json );
+ $this->assertContains( '"about"', $report_contents_json );
+ }
}