Make WordPress Core

Changeset 43060


Ignore:
Timestamp:
05/01/2018 06:59:48 PM (8 years ago)
Author:
azaozz
Message:

Privacy: translate error messages, some fixes and improvements for the AJAX actions for exporting and erasing user data.

Props desrosj, birgire.
See #43438.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r43056 r43060  
    43354335 */
    43364336function wp_ajax_wp_privacy_export_personal_data() {
    4337         $request_id  = (int) $_POST['id'];
    4338 
    4339         if ( empty( $request_id ) ) {
    4340                 wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4337
     4338        if ( empty( $_POST['id'] ) ) {
     4339                wp_send_json_error( __( 'Missing request ID.' ) );
     4340        }
     4341        $request_id = (int) $_POST['id'];
     4342
     4343        if ( $request_id < 1 ) {
     4344                wp_send_json_error( __( 'Invalid request ID.' ) );
    43414345        }
    43424346
    43434347        if ( ! current_user_can( 'manage_options' ) ) {
    4344                 wp_send_json_error( __( 'Error: Invalid request.' ) );
     4348                wp_send_json_error( __( 'Invalid request.' ) );
    43454349        }
    43464350
     
    43514355
    43524356        if ( ! $request || 'export_personal_data' !== $request->action_name ) {
    4353                 wp_send_json_error( __( 'Error: Invalid request type.' ) );
     4357                wp_send_json_error( __( 'Invalid request type.' ) );
    43544358        }
    43554359
    43564360        $email_address = $request->email;
    43574361        if ( ! is_email( $email_address ) ) {
    4358                 wp_send_json_error( __( 'Error: A valid email address must be given.' ) );
    4359         }
    4360 
     4362                wp_send_json_error( __( 'A valid email address must be given.' ) );
     4363        }
     4364
     4365        if ( ! isset( $_POST['exporter'] ) ) {
     4366                wp_send_json_error( __( 'Missing exporter index.' ) );
     4367        }
    43614368        $exporter_index = (int) $_POST['exporter'];
    4362         $page           = (int) $_POST['page'];
    4363         $send_as_email  = isset( $_POST['sendAsEmail'] ) ? ( "true" === $_POST['sendAsEmail'] ) : false;
     4369
     4370        if ( ! isset( $_POST['page'] ) ) {
     4371                wp_send_json_error( __( 'Missing page index.' ) );
     4372        }
     4373        $page = (int) $_POST['page'];
     4374
     4375        $send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false;
    43644376
    43654377        /**
     
    43704382         * @param array $args {
    43714383         *     An array of callable exporters of personal data. Default empty array.
    4372          *     [
    4373          *         callback               string  Callable exporter that accepts an email address and
    4374          *                                        a page and returns an array of name => value
    4375          *                                        pairs of personal data.
    4376          *         exporter_friendly_name string  Translated user facing friendly name for the exporter.
    4377          *     ]
     4384         *
     4385         *     @type array {
     4386         *         Array of personal data exporters.
     4387         *
     4388         *         @type string $callback               Callable exporter function that accepts an
     4389         *                                              email address and a page and returns an array
     4390         *                                              of name => value pairs of personal data.
     4391         *         @type string $exporter_friendly_name Translated user facing friendly name for the
     4392         *                                              exporter.
     4393         *     }
    43784394         * }
    43794395         */
     
    43814397
    43824398        if ( ! is_array( $exporters ) ) {
    4383                 wp_send_json_error( 'An exporter has improperly used the registration filter.' );
     4399                wp_send_json_error( __( 'An exporter has improperly used the registration filter.' ) );
    43844400        }
    43854401
     
    43874403        if ( 0 < count( $exporters ) ) {
    43884404                if ( $exporter_index < 1 ) {
    4389                         wp_send_json_error( 'Exporter index cannot be negative.' );
     4405                        wp_send_json_error( __( 'Exporter index cannot be negative.' ) );
    43904406                }
    43914407
    43924408                if ( $exporter_index > count( $exporters ) ) {
    4393                         wp_send_json_error( 'Exporter index out of range.' );
     4409                        wp_send_json_error( __( 'Exporter index out of range.' ) );
    43944410                }
    43954411
     
    43974413
    43984414                if ( $page < 1 ) {
    4399                         wp_send_json_error( 'Page index cannot be less than one.' );
     4415                        wp_send_json_error( __( 'Page index cannot be less than one.' ) );
    44004416                }
    44014417
     
    44034419
    44044420                if ( ! is_array( $exporter ) ) {
    4405                         wp_send_json_error( "Expected an array describing the exporter at index {$exporter_index}." );
     4421                        wp_send_json_error(
     4422                                /* translators: %d: array index */
     4423                                sprintf( __( 'Expected an array describing the exporter at index %d.' ), $exporter_index )
     4424                        );
    44064425                }
    44074426                if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
    4408                         wp_send_json_error( "Exporter array at index {$exporter_index} does not include a friendly name." );
     4427                        wp_send_json_error(
     4428                                /* translators: %d: array index */
     4429                                sprintf( __( 'Exporter array at index %d does not include a friendly name.' ), $exporter_index )
     4430                        );
    44094431                }
    44104432                if ( ! array_key_exists( 'callback', $exporter ) ) {
    4411                         wp_send_json_error( "Exporter does not include a callback: {$exporter['exporter_friendly_name']}." );
     4433                        wp_send_json_error(
     4434                                /* translators: %s: exporter friendly name */
     4435                                sprintf( __( 'Exporter does not include a callback: %s.' ), esc_html( $exporter['exporter_friendly_name'] ) )
     4436                        );
    44124437                }
    44134438                if ( ! is_callable( $exporter['callback'] ) ) {
    4414                         wp_send_json_error( "Exporter callback is not a valid callback: {$exporter['exporter_friendly_name']}." );
     4439                        wp_send_json_error(
     4440                                /* translators: %s: exporter friendly name */
     4441                                sprintf( __( 'Exporter callback is not a valid callback: %s.' ), esc_html( $exporter['exporter_friendly_name'] ) )
     4442                        );
    44154443                }
    44164444
     
    44244452
    44254453                if ( ! is_array( $response ) ) {
    4426                         wp_send_json_error( "Expected response as an array from exporter: {$exporter_friendly_name}." );
     4454                        wp_send_json_error(
     4455                                /* translators: %s: exporter friendly name */
     4456                                sprintf( __( 'Expected response as an array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4457                        );
    44274458                }
    44284459                if ( ! array_key_exists( 'data', $response ) ) {
    4429                         wp_send_json_error( "Expected data in response array from exporter: {$exporter_friendly_name}." );
     4460                        wp_send_json_error(
     4461                                /* translators: %s: exporter friendly name */
     4462                                sprintf( __( 'Expected data in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4463                        );
    44304464                }
    44314465                if ( ! is_array( $response['data'] ) ) {
    4432                         wp_send_json_error( "Expected data array in response array from exporter: {$exporter_friendly_name}." );
     4466                        wp_send_json_error(
     4467                                /* translators: %s: exporter friendly name */
     4468                                sprintf( __( 'Expected data array in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4469                        );
    44334470                }
    44344471                if ( ! array_key_exists( 'done', $response ) ) {
    4435                         wp_send_json_error( "Expected done (boolean) in response array from exporter: {$exporter_friendly_name}." );
     4472                        wp_send_json_error(
     4473                                /* translators: %s: exporter friendly name */
     4474                                sprintf( __( 'Expected done (boolean) in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4475                        );
    44364476                }
    44374477        } else {
     
    44534493         * @param int    $exporter_index  The index of the exporter that provided this data.
    44544494         * @param string $email_address   The email address associated with this personal data.
    4455          * @param int    $page            The zero-based page for this response.
     4495         * @param int    $page            The page for this response.
    44564496         * @param int    $request_id      The privacy request post ID associated with this request.
    44574497         * @param bool   $send_as_email   Whether the final results of the export should be emailed to the user.
     
    44724512 */
    44734513function wp_ajax_wp_privacy_erase_personal_data() {
    4474         $request_id  = (int) $_POST['id'];
    4475 
    4476         if ( empty( $request_id ) ) {
    4477                 wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4514
     4515        if ( empty( $_POST['id'] ) ) {
     4516                wp_send_json_error( __( 'Missing request ID.' ) );
     4517        }
     4518
     4519        $request_id = (int) $_POST['id'];
     4520
     4521        if ( $request_id < 1 ) {
     4522                wp_send_json_error( __( 'Invalid request ID.' ) );
    44784523        }
    44794524
    44804525        if ( ! current_user_can( 'delete_users' ) ) {
    4481                 wp_send_json_error( __( 'Error: Invalid request.' ) );
     4526                wp_send_json_error( __( 'Invalid request.' ) );
    44824527        }
    44834528
     
    44884533
    44894534        if ( ! $request || 'remove_personal_data' !== $request->action_name ) {
    4490                 wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4535                wp_send_json_error( __( 'Invalid request ID.' ) );
    44914536        }
    44924537
     
    44944539
    44954540        if ( ! is_email( $email_address ) ) {
    4496                 wp_send_json_error( __( 'Error: Invalid email address in request.' ) );
     4541                wp_send_json_error( __( 'Invalid email address in request.' ) );
     4542        }
     4543
     4544        if ( ! isset( $_POST['eraser'] ) ) {
     4545                wp_send_json_error( __( 'Missing eraser index.' ) );
    44974546        }
    44984547
    44994548        $eraser_index = (int) $_POST['eraser'];
    4500         $page         = (int) $_POST['page'];
     4549
     4550        if ( ! isset( $_POST['page'] ) ) {
     4551                wp_send_json_error( __( 'Missing page index.' ) );
     4552        }
     4553
     4554        $page = (int) $_POST['page'];
    45014555
    45024556        /**
     
    45074561         * @param array $args {
    45084562         *     An array of callable erasers of personal data. Default empty array.
    4509          *     [
    4510          *         callback             string Callable eraser that accepts an email address and
    4511          *                                     a page and returns an array with the number of items
    4512          *                                     removed, the number of items retained and any messages
    4513          *                                     from the eraser, as well as if additional pages are
    4514          *                                     available.
    4515          *         eraser_friendly_name string Translated user facing friendly name for the eraser.
    4516          *     ]
     4563         *     @type array {
     4564         *         Array of personal data exporters.
     4565         *
     4566         *         @type string $callback               Callable eraser that accepts an email address and
     4567         *                                              a page and returns an array with the number of items
     4568         *                                              removed, the number of items retained and any messages
     4569         *                                              from the eraser, as well as if additional pages are
     4570         *                                              available.
     4571         *         @type string $exporter_friendly_name Translated user facing friendly name for the eraser.
     4572         *     }
    45174573         * }
    45184574         */
     
    45214577        // Do we have any registered erasers?
    45224578        if ( 0 < count( $erasers ) ) {
     4579
    45234580                if ( $eraser_index < 1 ) {
    4524                         wp_send_json_error( __( 'Error: Eraser index cannot be less than one.' ) );
     4581                        wp_send_json_error( __( 'Eraser index cannot be less than one.' ) );
    45254582                }
    45264583
    45274584                if ( $eraser_index > count( $erasers ) ) {
    4528                         wp_send_json_error( __( 'Error: Eraser index is out of range.' ) );
     4585                        wp_send_json_error( __( 'Eraser index is out of range.' ) );
    45294586                }
    45304587
    45314588                if ( $page < 1 ) {
    4532                         wp_send_json_error( __( 'Error: Page index cannot be less than one.' ) );
    4533                 }
    4534 
    4535                 $index = $eraser_index - 1; // Convert to zero based for eraser index
     4589                        wp_send_json_error( __( 'Page index cannot be less than one.' ) );
     4590                }
     4591
     4592                $index  = $eraser_index - 1; // Convert to zero based for eraser index.
    45364593                $eraser = $erasers[ $index ];
     4594
    45374595                if ( ! is_array( $eraser ) ) {
     4596                        /* translators: %d: array index */
     4597                        wp_send_json_error( sprintf( __( 'Expected an array describing the eraser at index %d.' ), $eraser_index ) );
     4598                }
     4599
     4600                if ( ! array_key_exists( 'callback', $eraser ) ) {
     4601                        /* translators: %d: array index */
     4602                        wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a callback.' ), $eraser_index ) );
     4603                }
     4604
     4605                if ( ! is_callable( $eraser['callback'] ) ) {
     4606                        /* translators: %d: array index */
     4607                        wp_send_json_error( sprintf( __( 'Eraser callback at index %d is not a valid callback.' ), $eraser_index ) );
     4608                }
     4609
     4610                if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
     4611                        /* translators: %d: array index */
     4612                        wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a friendly name.' ), $eraser_index ) );
     4613                }
     4614
     4615                $callback             = $erasers[ $index ]['callback'];
     4616                $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
     4617
     4618                $response = call_user_func( $callback, $email_address, $page );
     4619
     4620                if ( is_wp_error( $response ) ) {
     4621                        wp_send_json_error( $response );
     4622                }
     4623
     4624                if ( ! is_array( $response ) ) {
    45384625                        wp_send_json_error(
    45394626                                sprintf(
    4540                                         __( 'Error: Expected an array describing the eraser at index %d.' ),
     4627                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4628                                        __( 'Did not receive array from %1$s eraser (index %2$d).' ),
     4629                                        esc_html( $eraser_friendly_name ),
    45414630                                        $eraser_index
    45424631                                )
    45434632                        );
    45444633                }
    4545                 if ( ! array_key_exists( 'callback', $eraser ) ) {
     4634
     4635                if ( ! array_key_exists( 'num_items_removed', $response ) ) {
    45464636                        wp_send_json_error(
    45474637                                sprintf(
    4548                                         __( 'Error: Eraser array at index %d does not include a callback.' ),
     4638                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4639                                        __( 'Expected num_items_removed key in response array from %1$s eraser (index %2$d).' ),
     4640                                        esc_html( $eraser_friendly_name ),
    45494641                                        $eraser_index
    45504642                                )
    45514643                        );
    45524644                }
    4553                 if ( ! is_callable( $eraser['callback'] ) ) {
     4645
     4646                if ( ! array_key_exists( 'num_items_retained', $response ) ) {
    45544647                        wp_send_json_error(
    45554648                                sprintf(
    4556                                         __( 'Error: Eraser callback at index %d is not a valid callback.' ),
     4649                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4650                                        __( 'Expected num_items_retained key in response array from %1$s eraser (index %2$d).' ),
     4651                                        esc_html( $eraser_friendly_name ),
    45574652                                        $eraser_index
    45584653                                )
    45594654                        );
    45604655                }
    4561                 if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
     4656
     4657                if ( ! array_key_exists( 'messages', $response ) ) {
    45624658                        wp_send_json_error(
    45634659                                sprintf(
    4564                                         __( 'Error: Eraser array at index %d does not include a friendly name.' ),
     4660                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4661                                        __( 'Expected messages key in response array from %1$s eraser (index %2$d).' ),
     4662                                        esc_html( $eraser_friendly_name ),
    45654663                                        $eraser_index
    45664664                                )
     
    45684666                }
    45694667
    4570                 $callback = $erasers[ $index ]['callback'];
    4571                 $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
    4572 
    4573                 $response = call_user_func( $callback, $email_address, $page );
    4574                 if ( is_wp_error( $response ) ) {
    4575                         wp_send_json_error( $response );
    4576                 }
    4577 
    4578                 if ( ! is_array( $response ) ) {
     4668                if ( ! is_array( $response['messages'] ) ) {
    45794669                        wp_send_json_error(
    45804670                                sprintf(
    4581                                         __( 'Error: Did not receive array from %s eraser (index %d).' ),
    4582                                         $eraser_friendly_name,
     4671                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4672                                        __( 'Expected messages key to reference an array in response array from %1$s eraser (index %2$d).' ),
     4673                                        esc_html( $eraser_friendly_name ),
    45834674                                        $eraser_index
    45844675                                )
    45854676                        );
    45864677                }
    4587                 if ( ! array_key_exists( 'num_items_removed', $response ) ) {
     4678
     4679                if ( ! array_key_exists( 'done', $response ) ) {
    45884680                        wp_send_json_error(
    45894681                                sprintf(
    4590                                         __( 'Error: Expected num_items_removed key in response array from %s eraser (index %d).' ),
    4591                                         $eraser_friendly_name,
     4682                                        /* translators: %1$s: eraser friendly name, %2$d: array index */
     4683                                        __( 'Expected done flag in response array from %1$s eraser (index %2$d).' ),
     4684                                        esc_html( $eraser_friendly_name ),
    45924685                                        $eraser_index
    45934686                                )
    45944687                        );
    45954688                }
    4596                 if ( ! array_key_exists( 'num_items_retained', $response ) ) {
    4597                         wp_send_json_error(
    4598                                 sprintf(
    4599                                         __( 'Error: Expected num_items_retained key in response array from %s eraser (index %d).' ),
    4600                                         $eraser_friendly_name,
    4601                                         $eraser_index
    4602                                 )
    4603                         );
    4604                 }
    4605                 if ( ! array_key_exists( 'messages', $response ) ) {
    4606                         wp_send_json_error(
    4607                                 sprintf(
    4608                                         __( 'Error: Expected messages key in response array from %s eraser (index %d).' ),
    4609                                         $eraser_friendly_name,
    4610                                         $eraser_index
    4611                                 )
    4612                         );
    4613                 }
    4614                 if ( ! is_array( $response['messages'] ) ) {
    4615                         wp_send_json_error(
    4616                                 sprintf(
    4617                                         __( 'Error: Expected messages key to reference an array in response array from %s eraser (index %d).' ),
    4618                                         $eraser_friendly_name,
    4619                                         $eraser_index
    4620                                 )
    4621                         );
    4622                 }
    4623                 if ( ! array_key_exists( 'done', $response ) ) {
    4624                         wp_send_json_error(
    4625                                 sprintf(
    4626                                         __( 'Error: Expected done flag in response array from %s eraser (index %d).' ),
    4627                                         $eraser_friendly_name,
    4628                                         $eraser_index
    4629                                 )
    4630                         );
    4631                 }
    46324689        } else {
    4633                 // No erasers, so we're done
     4690                // No erasers, so we're done.
    46344691                $response = array(
    4635                         'num_items_removed' => 0,
     4692                        'num_items_removed'  => 0,
    46364693                        'num_items_retained' => 0,
    4637                         'messages' => array(),
    4638                         'done' => true,
     4694                        'messages'           => array(),
     4695                        'done'               => true,
    46394696                );
    46404697        }
     
    46504707         * @param int    $exporter_index  The index of the exporter that provided this data.
    46514708         * @param string $email_address   The email address associated with this personal data.
    4652          * @param int    $page            The zero-based page for this response.
     4709         * @param int    $page            The page for this response.
    46534710         * @param int    $request_id      The privacy request post ID associated with this request.
    46544711         */
    46554712        $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id );
     4713
    46564714        if ( is_wp_error( $response ) ) {
    46574715                wp_send_json_error( $response );
Note: See TracChangeset for help on using the changeset viewer.