Make WordPress Core

Ticket #44133: 44133.10.diff

File 44133.10.diff, 12.8 KB (added by garrett-eclipse, 6 years ago)

Additional improvements and fixed unit tests

  • src/wp-admin/includes/admin-filters.php

     
    135135// Privacy hooks
    136136add_filter( 'wp_privacy_personal_data_erasure_page', 'wp_privacy_process_personal_data_erasure_page', 10, 5 );
    137137add_filter( 'wp_privacy_personal_data_export_page', 'wp_privacy_process_personal_data_export_page', 10, 7 );
    138 add_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 );
     138add_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10, 3 );
    139139add_action( 'wp_privacy_personal_data_erased', '_wp_privacy_send_erasure_fulfillment_notification', 10 );
    140140
    141141// Privacy policy text changes check.
  • src/wp-admin/includes/file.php

     
    21552155 * Generate the personal data export file.
    21562156 *
    21572157 * @since 4.9.6
     2158 * @since 5.2.0 Added the $request parameter.
     2159 * @since 5.2.0 Added the $has_export_data parameter.
    21582160 *
    2159  * @param int $request_id The export request ID.
     2161 * @param int             $request_id      The export request ID.
     2162 *
     2163 * @param WP_User_Request $request         The export request.
     2164 * @param bool            $has_export_data Wether there is personal data to export.
    21602165 */
    2161 function wp_privacy_generate_personal_data_export_file( $request_id ) {
     2166function wp_privacy_generate_personal_data_export_file( $request_id, $request, $has_export_data ) {
    21622167        if ( ! class_exists( 'ZipArchive' ) ) {
    21632168                wp_send_json_error( __( 'Unable to generate export file. ZipArchive not available.' ) );
    21642169        }
    21652170
    2166         // Get the request data.
    2167         $request = wp_get_user_request_data( $request_id );
     2171        if ( ! $request ) {
     2172                // Get the request data.
     2173                $request = wp_get_user_request_data( $request_id );
     2174        }
    21682175
    21692176        if ( ! $request || 'export_personal_data' !== $request->action_name ) {
    21702177                wp_send_json_error( __( 'Invalid request ID when generating export file.' ) );
     
    22742281                fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
    22752282        }
    22762283
     2284        if ( ! $has_export_data ) {
     2285                fwrite( $file, '<strong>' . esc_html__( 'No personal data found.' ) . '</strong>' );
     2286        }
     2287
    22772288        fwrite( $file, "</body>\n" );
    22782289
    22792290        // Close HTML.
     
    23402351/**
    23412352 * Send an email to the user with a link to the personal data export file
    23422353 *
     2354 * The link to the export file is only included in the email content when there
     2355 * is personal data to export.
     2356 *
    23432357 * @since 4.9.6
     2358 * @since 5.2.0 Added the $has_export_data parameter.
    23442359 *
    2345  * @param int $request_id The request ID for this personal data export.
     2360 * @param int  $request_id      The request ID for this personal data export.
     2361 * @param bool $has_export_data Whether there is personal data to export.
     2362 *
    23462363 * @return true|WP_Error True on success or `WP_Error` on failure.
    23472364 */
    2348 function wp_privacy_send_personal_data_export_email( $request_id ) {
     2365function wp_privacy_send_personal_data_export_email( $request_id, $has_export_data = true ) {
    23492366        // Get the request data.
    23502367        $request = wp_get_user_request_data( $request_id );
    23512368
     
    23582375        $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
    23592376
    23602377        /* translators: Do not translate EXPIRATION, LINK, SITENAME, SITEURL: those are placeholders. */
    2361         $email_text = __(
    2362                 'Howdy,
     2378        if ( $has_export_data ) {
     2379                $email_text = __( 'Howdy,
    23632380
    23642381Your request for an export of personal data has been completed. You may
    23652382download your personal data by clicking on the link below. For privacy
     
    23702387
    23712388Regards,
    23722389All at ###SITENAME###
    2373 ###SITEURL###'
    2374         );
     2390###SITEURL###' );
    23752391
    2376         /**
    2377          * Filters the text of the email sent with a personal data export file.
    2378          *
    2379          * The following strings have a special meaning and will get replaced dynamically:
    2380          * ###EXPIRATION###         The date when the URL will be automatically deleted.
    2381          * ###LINK###               URL of the personal data export file for the user.
    2382          * ###SITENAME###           The name of the site.
    2383          * ###SITEURL###            The URL to the site.
    2384          *
    2385          * @since 4.9.6
    2386          *
    2387          * @param string $email_text     Text in the email.
    2388          * @param int    $request_id     The request ID for this personal data export.
    2389          */
    2390         $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id );
     2392                /**
     2393                 * Filters the text of the email sent when personal data exists to export.
     2394                 *
     2395                 * The following strings have a special meaning and will get replaced dynamically:
     2396                 * ###EXPIRATION###         The date when the URL will be automatically deleted.
     2397                 * ###LINK###               URL of the personal data export file for the user.
     2398                 * ###SITENAME###           The name of the site.
     2399                 * ###SITEURL###            The URL to the site.
     2400                 *
     2401                 * @since 4.9.6
     2402                 * @since 5.3.0 $has_export_data argument added.
     2403                 *
     2404                 * @param string $email_text      Text in the email.
     2405                 * @param int    $request_id      The request ID for this personal data export.
     2406                 * @param bool   $has_export_data Whether there is personal data to export.
     2407                 */
     2408                $content = apply_filters( 'wp_privacy_personal_data_email_content', $email_text, $request_id, $has_export_data );
     2409        } else {
     2410                /* translators: Do not translate EXPIRATION, LINK, SITENAME, SITEURL: those are placeholders. */
     2411                $email_text = __( 'Howdy,
     2412Your request for an export of personal data has been completed.
    23912413
     2414###SITENAME### has identified no personal data associated with this email address.
     2415
     2416You may download your personal data by clicking on the link below. For privacy
     2417and security, we will automatically delete the file on ###EXPIRATION###,
     2418so please download it before then.
     2419
     2420###LINK###
     2421
     2422Regards,
     2423All at ###SITENAME###
     2424###SITEURL###' );
     2425
     2426                /**
     2427                 * Filters the text of the email sent when no personal data exists to export.
     2428                 *
     2429                 * The following strings have a special meaning and will get replaced dynamically:
     2430                 * ###EXPIRATION###         The date when the URL will be automatically deleted.
     2431                 * ###LINK###               URL of the personal data export file for the user.
     2432                 * ###SITENAME###           The name of the site.
     2433                 * ###SITEURL###            The URL to the site.
     2434                 *
     2435                 * @since 5.3.0
     2436                 *
     2437                 * @param string $email_text      Text in the email.
     2438                 * @param int    $request_id      The request ID for this personal data export.
     2439                 * @param bool   $has_export_data Whether there is personal data to export.
     2440                 */
     2441                $content = apply_filters( 'wp_privacy_personal_data_email_content_no_data', $email_text, $request_id, $has_export_data );
     2442        }
     2443
    23922444        $email_address   = $request->email;
    23932445        $export_file_url = get_post_meta( $request_id, '_export_file_url', true );
    23942446        $site_name       = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     
    23962448
    23972449        $content = str_replace( '###EXPIRATION###', $expiration_date, $content );
    23982450        $content = str_replace( '###LINK###', esc_url_raw( $export_file_url ), $content );
    2399         $content = str_replace( '###EMAIL###', $email_address, $content );
    24002451        $content = str_replace( '###SITENAME###', $site_name, $content );
    24012452        $content = str_replace( '###SITEURL###', esc_url_raw( $site_url ), $content );
    24022453
     
    25082559        delete_post_meta( $request_id, '_export_data_raw' );
    25092560        update_post_meta( $request_id, '_export_data_grouped', $groups );
    25102561
     2562        $has_export_data = ! empty( $groups );
     2563
    25112564        /**
    25122565         * Generate the export file from the collected, grouped personal data.
    25132566         *
    25142567         * @since 4.9.6
     2568         * @since 5.3.0 Added the $request parameter.
     2569         * @since 5.3.0 Added the $has_export_data parameter.
    25152570         *
    2516          * @param int $request_id The export request ID.
     2571         * @param int             $request_id      The export request ID.
     2572         * @param WP_User_Request $request         The export request.
     2573         * @param bool            $has_export_data Whether there is personal data to export.
    25172574         */
    2518         do_action( 'wp_privacy_personal_data_export_file', $request_id );
     2575        do_action( 'wp_privacy_personal_data_export_file', $request_id, $request, $has_export_data );
    25192576
    25202577        // Clear the grouped data now that it is no longer needed.
    25212578        delete_post_meta( $request_id, '_export_data_grouped' );
    25222579
    25232580        // If the destination is email, send it now.
    25242581        if ( $send_as_email ) {
    2525                 $mail_success = wp_privacy_send_personal_data_export_email( $request_id );
     2582                $mail_success = wp_privacy_send_personal_data_export_email( $request_id, $has_export_data );
    25262583                if ( is_wp_error( $mail_success ) ) {
    25272584                        wp_send_json_error( $mail_success->get_error_message() );
    25282585                }
  • tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php

     
    2626        protected static $export_request_id;
    2727
    2828        /**
     29         * An Export Request
     30         *
     31         * @since 5.3.0
     32         *
     33         * @var int $export_request
     34         */
     35        protected static $export_request;
     36
     37        /**
    2938         * The full path to the export file for the current test method.
    3039         *
    3140         * @since 5.2.0
     
    5261         */
    5362        public static function wpSetUpBeforeClass( $factory ) {
    5463                self::$export_request_id = wp_create_user_request( 'export-requester@example.com', 'export_personal_data' );
     64                self::$export_request = wp_get_user_request_data( self::$export_request_id );
    5565                update_post_meta( self::$export_request_id, '_export_data_grouped', array() );
    5666                self::$exports_dir = wp_privacy_exports_dir();
    5767        }
     
    162172         */
    163173        public function test_rejects_remove_requests() {
    164174                $request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
     175                $request    = wp_get_user_request_data( $request_id );
    165176
    166177                $this->setExpectedException( 'WPDieException' );
    167178                $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
    168                 wp_privacy_generate_personal_data_export_file( $request_id );
     179                wp_privacy_generate_personal_data_export_file( $request_id, $request, false );
    169180        }
    170181
    171182        /**
     
    176187        public function test_invalid_request_id() {
    177188                $this->setExpectedException( 'WPDieException' );
    178189                $this->expectOutputString( '{"success":false,"data":"Invalid request ID when generating export file."}' );
    179                 wp_privacy_generate_personal_data_export_file( 123456789 );
     190                wp_privacy_generate_personal_data_export_file( 123456789, null, false );
    180191        }
    181192
    182193        /**
     
    194205                        )
    195206                );
    196207
     208                $request = wp_get_user_request_data( $request_id );
     209
    197210                $this->setExpectedException( 'WPDieException' );
    198211                $this->expectOutputString( '{"success":false,"data":"Invalid email address when generating export file."}' );
    199                 wp_privacy_generate_personal_data_export_file( $request_id );
     212                wp_privacy_generate_personal_data_export_file( $request_id, $request, false );
    200213        }
    201214
    202215        /**
     
    210223
    211224                $this->setExpectedException( 'WPDieException' );
    212225                $this->expectOutputString( '{"success":false,"data":"Unable to create export folder."}' );
    213                 wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     226                wp_privacy_generate_personal_data_export_file( self::$export_request_id, self::$export_request, false );
    214227        }
    215228
    216229        /**
    217230         * Test that an index.html file can be added to the export directory.
    218231         *
    219232         * @ticket 44233
     233         * @ticket 44133 Supplied new params for request and has_export_data
    220234         */
    221235        public function test_creates_index_in_export_folder() {
    222236                $this->expectOutputString( '' );
    223                 wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     237                wp_privacy_generate_personal_data_export_file( self::$export_request_id, self::$export_request, false );
    224238
    225239                $this->assertTrue( file_exists( self::$exports_dir . 'index.html' ) );
    226240        }
     
    229243         * Test that an export file is successfully created.
    230244         *
    231245         * @ticket 44233
     246         * @ticket 44133 Supplied new params for request and has_export_data
    232247         */
    233248        public function test_can_succeed() {
    234                 wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     249                wp_privacy_generate_personal_data_export_file( self::$export_request_id, self::$export_request, false );
    235250
    236251                $this->assertTrue( file_exists( $this->export_file_name ) );
    237252        }
     
    240255         * Test the export file has all the expected parts.
    241256         *
    242257         * @ticket 44233
     258         * @ticket 44133 Supplied new params for request and has_export_data
    243259         */
    244260        public function test_contents() {
    245261                $this->expectOutputString( '' );
    246                 wp_privacy_generate_personal_data_export_file( self::$export_request_id );
     262                wp_privacy_generate_personal_data_export_file( self::$export_request_id, self::$export_request, false );
    247263                $this->assertTrue( file_exists( $this->export_file_name ) );
    248264
    249265                $report_dir = trailingslashit( self::$exports_dir . 'test_contents' );