Make WordPress Core

Ticket #49029: 49029.diff

File 49029.diff, 5.0 KB (added by xkon, 5 years ago)
  • src/wp-admin/includes/privacy-tools.php

    diff --git a/src/wp-admin/includes/privacy-tools.php b/src/wp-admin/includes/privacy-tools.php
    index efe46cce32..ca336532f7 100644
    a b function wp_privacy_generate_personal_data_export_file( $request_id ) { 
    326326        $file_basename        = 'wp-personal-data-file-' . $stripped_email . '-' . $obscura;
    327327        $html_report_filename = $file_basename . '.html';
    328328        $html_report_pathname = wp_normalize_path( $exports_dir . $html_report_filename );
    329         $file                 = fopen( $html_report_pathname, 'w' );
    330         if ( false === $file ) {
    331                 wp_send_json_error( __( 'Unable to open export file (HTML report) for writing.' ) );
    332         }
     329        $json_report_filename = $file_basename . '.json';
     330        $json_report_pathname = wp_normalize_path( $exports_dir . $json_report_filename );
     331
     332        // Gather general data needed.
    333333
     334        // Title.
    334335        $title = sprintf(
    335336                /* translators: %s: User's email address. */
    336337                __( 'Personal Data Export for %s' ),
    337338                $email_address
    338339        );
    339340
    340         // Open HTML.
    341         fwrite( $file, "<!DOCTYPE html>\n" );
    342         fwrite( $file, "<html>\n" );
    343 
    344         // Head.
    345         fwrite( $file, "<head>\n" );
    346         fwrite( $file, "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />\n" );
    347         fwrite( $file, "<style type='text/css'>" );
    348         fwrite( $file, 'body { color: black; font-family: Arial, sans-serif; font-size: 11pt; margin: 15px auto; width: 860px; }' );
    349         fwrite( $file, 'table { background: #f0f0f0; border: 1px solid #ddd; margin-bottom: 20px; width: 100%; }' );
    350         fwrite( $file, 'th { padding: 5px; text-align: left; width: 20%; }' );
    351         fwrite( $file, 'td { padding: 5px; }' );
    352         fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' );
    353         fwrite( $file, '</style>' );
    354         fwrite( $file, '<title>' );
    355         fwrite( $file, esc_html( $title ) );
    356         fwrite( $file, '</title>' );
    357         fwrite( $file, "</head>\n" );
    358 
    359         // Body.
    360         fwrite( $file, "<body>\n" );
    361 
    362         // Heading.
    363         fwrite( $file, '<h1>' . esc_html__( 'Personal Data Export' ) . '</h1>' );
    364 
    365341        // And now, all the Groups.
    366342        $groups = get_post_meta( $request_id, '_export_data_grouped', true );
    367343
    function wp_privacy_generate_personal_data_export_file( $request_id ) { 
    396372        // Merge in the special about group.
    397373        $groups = array_merge( array( 'about' => $about_group ), $groups );
    398374
     375        // Convert the groups to JSON format.
     376        $groups_json = wp_json_encode( $groups );
     377
     378        /**
     379         * Handle the JSON export.
     380         */
     381        $file = fopen( $json_report_pathname, 'w' );
     382
     383        if ( false === $file ) {
     384                wp_send_json_error( __( 'Unable to open export file (JSON report) for writing.' ) );
     385        }
     386
     387        fwrite( $file, '{' );
     388        fwrite( $file, '"' . $title . '":' );
     389        fwrite( $file, $groups_json );
     390        fwrite( $file, '}' );
     391        fclose( $file );
     392
     393        /**
     394         * Handle the HTML export.
     395         */
     396        $file = fopen( $html_report_pathname, 'w' );
     397
     398        if ( false === $file ) {
     399                wp_send_json_error( __( 'Unable to open export file (HTML report) for writing.' ) );
     400        }
     401
     402        fwrite( $file, "<!DOCTYPE html>\n" );
     403        fwrite( $file, "<html>\n" );
     404        fwrite( $file, "<head>\n" );
     405        fwrite( $file, "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />\n" );
     406        fwrite( $file, "<style type='text/css'>" );
     407        fwrite( $file, 'body { color: black; font-family: Arial, sans-serif; font-size: 11pt; margin: 15px auto; width: 860px; }' );
     408        fwrite( $file, 'table { background: #f0f0f0; border: 1px solid #ddd; margin-bottom: 20px; width: 100%; }' );
     409        fwrite( $file, 'th { padding: 5px; text-align: left; width: 20%; }' );
     410        fwrite( $file, 'td { padding: 5px; }' );
     411        fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' );
     412        fwrite( $file, '</style>' );
     413        fwrite( $file, '<title>' );
     414        fwrite( $file, esc_html( $title ) );
     415        fwrite( $file, '</title>' );
     416        fwrite( $file, "</head>\n" );
     417        fwrite( $file, "<body>\n" );
     418        fwrite( $file, '<h1>' . esc_html__( 'Personal Data Export' ) . '</h1>' );
     419
    399420        // Now, iterate over every group in $groups and have the formatter render it in HTML.
    400421        foreach ( (array) $groups as $group_id => $group_data ) {
    401422                fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
    402423        }
    403424
    404425        fwrite( $file, "</body>\n" );
    405 
    406         // Close HTML.
    407426        fwrite( $file, "</html>\n" );
    408427        fclose( $file );
    409428
    function wp_privacy_generate_personal_data_export_file( $request_id ) { 
    433452
    434453        $zip = new ZipArchive;
    435454        if ( true === $zip->open( $archive_pathname, ZipArchive::CREATE ) ) {
     455                if ( ! $zip->addFile( $json_report_pathname, 'export.json' ) ) {
     456                        $error = __( 'Unable to add data to JSON file.' );
     457                }
     458
    436459                if ( ! $zip->addFile( $html_report_pathname, 'index.html' ) ) {
    437                         $error = __( 'Unable to add data to export file.' );
     460                        $error = __( 'Unable to add data to HTML file.' );
    438461                }
    439462
    440463                $zip->close();
    function wp_privacy_generate_personal_data_export_file( $request_id ) { 
    456479                $error = __( 'Unable to open export file (archive) for writing.' );
    457480        }
    458481
    459         // And remove the HTML file.
     482        // Remove the JSON file.
     483        unlink( $json_report_pathname );
     484
     485        // Remove the HTML file.
    460486        unlink( $html_report_pathname );
    461487
    462488        if ( $error ) {