Make WordPress Core


Ignore:
Timestamp:
05/02/2018 03:18:19 AM (7 years ago)
Author:
SergeyBiryukov
Message:

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

Props desrosj, birgire.
Merges [43060] to the 4.9 branch.
See #43438.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-admin/includes/ajax-actions.php

    r43092 r43105  
    40254025 */
    40264026function wp_ajax_wp_privacy_export_personal_data() {
    4027     $request_id  = (int) $_POST['id'];
    4028 
    4029     if ( empty( $request_id ) ) {
    4030         wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4027
     4028    if ( empty( $_POST['id'] ) ) {
     4029        wp_send_json_error( __( 'Missing request ID.' ) );
     4030    }
     4031    $request_id = (int) $_POST['id'];
     4032
     4033    if ( $request_id < 1 ) {
     4034        wp_send_json_error( __( 'Invalid request ID.' ) );
    40314035    }
    40324036
    40334037    if ( ! current_user_can( 'manage_options' ) ) {
    4034         wp_send_json_error( __( 'Error: Invalid request.' ) );
     4038        wp_send_json_error( __( 'Invalid request.' ) );
    40354039    }
    40364040
     
    40414045
    40424046    if ( ! $request || 'export_personal_data' !== $request->action_name ) {
    4043         wp_send_json_error( __( 'Error: Invalid request type.' ) );
     4047        wp_send_json_error( __( 'Invalid request type.' ) );
    40444048    }
    40454049
    40464050    $email_address = $request->email;
    40474051    if ( ! is_email( $email_address ) ) {
    4048         wp_send_json_error( __( 'Error: A valid email address must be given.' ) );
    4049     }
    4050 
     4052        wp_send_json_error( __( 'A valid email address must be given.' ) );
     4053    }
     4054
     4055    if ( ! isset( $_POST['exporter'] ) ) {
     4056        wp_send_json_error( __( 'Missing exporter index.' ) );
     4057    }
    40514058    $exporter_index = (int) $_POST['exporter'];
    4052     $page           = (int) $_POST['page'];
    4053     $send_as_email  = isset( $_POST['sendAsEmail'] ) ? ( "true" === $_POST['sendAsEmail'] ) : false;
     4059
     4060    if ( ! isset( $_POST['page'] ) ) {
     4061        wp_send_json_error( __( 'Missing page index.' ) );
     4062    }
     4063    $page = (int) $_POST['page'];
     4064
     4065    $send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false;
    40544066
    40554067    /**
     
    40604072     * @param array $args {
    40614073     *     An array of callable exporters of personal data. Default empty array.
    4062      *     [
    4063      *         callback               string  Callable exporter that accepts an email address and
    4064      *                                        a page and returns an array of name => value
    4065      *                                        pairs of personal data.
    4066      *         exporter_friendly_name string  Translated user facing friendly name for the exporter.
    4067      *     ]
     4074     *
     4075     *     @type array {
     4076     *         Array of personal data exporters.
     4077     *
     4078     *         @type string $callback               Callable exporter function that accepts an
     4079     *                                              email address and a page and returns an array
     4080     *                                              of name => value pairs of personal data.
     4081     *         @type string $exporter_friendly_name Translated user facing friendly name for the
     4082     *                                              exporter.
     4083     *     }
    40684084     * }
    40694085     */
     
    40714087
    40724088    if ( ! is_array( $exporters ) ) {
    4073         wp_send_json_error( 'An exporter has improperly used the registration filter.' );
     4089        wp_send_json_error( __( 'An exporter has improperly used the registration filter.' ) );
    40744090    }
    40754091
     
    40774093    if ( 0 < count( $exporters ) ) {
    40784094        if ( $exporter_index < 1 ) {
    4079             wp_send_json_error( 'Exporter index cannot be negative.' );
     4095            wp_send_json_error( __( 'Exporter index cannot be negative.' ) );
    40804096        }
    40814097
    40824098        if ( $exporter_index > count( $exporters ) ) {
    4083             wp_send_json_error( 'Exporter index out of range.' );
     4099            wp_send_json_error( __( 'Exporter index out of range.' ) );
    40844100        }
    40854101
     
    40874103
    40884104        if ( $page < 1 ) {
    4089             wp_send_json_error( 'Page index cannot be less than one.' );
     4105            wp_send_json_error( __( 'Page index cannot be less than one.' ) );
    40904106        }
    40914107
     
    40934109
    40944110        if ( ! is_array( $exporter ) ) {
    4095             wp_send_json_error( "Expected an array describing the exporter at index {$exporter_index}." );
     4111            wp_send_json_error(
     4112                /* translators: %d: array index */
     4113                sprintf( __( 'Expected an array describing the exporter at index %d.' ), $exporter_index )
     4114            );
    40964115        }
    40974116        if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
    4098             wp_send_json_error( "Exporter array at index {$exporter_index} does not include a friendly name." );
     4117            wp_send_json_error(
     4118                /* translators: %d: array index */
     4119                sprintf( __( 'Exporter array at index %d does not include a friendly name.' ), $exporter_index )
     4120            );
    40994121        }
    41004122        if ( ! array_key_exists( 'callback', $exporter ) ) {
    4101             wp_send_json_error( "Exporter does not include a callback: {$exporter['exporter_friendly_name']}." );
     4123            wp_send_json_error(
     4124                /* translators: %s: exporter friendly name */
     4125                sprintf( __( 'Exporter does not include a callback: %s.' ), esc_html( $exporter['exporter_friendly_name'] ) )
     4126            );
    41024127        }
    41034128        if ( ! is_callable( $exporter['callback'] ) ) {
    4104             wp_send_json_error( "Exporter callback is not a valid callback: {$exporter['exporter_friendly_name']}." );
     4129            wp_send_json_error(
     4130                /* translators: %s: exporter friendly name */
     4131                sprintf( __( 'Exporter callback is not a valid callback: %s.' ), esc_html( $exporter['exporter_friendly_name'] ) )
     4132            );
    41054133        }
    41064134
     
    41144142
    41154143        if ( ! is_array( $response ) ) {
    4116             wp_send_json_error( "Expected response as an array from exporter: {$exporter_friendly_name}." );
     4144            wp_send_json_error(
     4145                /* translators: %s: exporter friendly name */
     4146                sprintf( __( 'Expected response as an array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4147            );
    41174148        }
    41184149        if ( ! array_key_exists( 'data', $response ) ) {
    4119             wp_send_json_error( "Expected data in response array from exporter: {$exporter_friendly_name}." );
     4150            wp_send_json_error(
     4151                /* translators: %s: exporter friendly name */
     4152                sprintf( __( 'Expected data in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4153            );
    41204154        }
    41214155        if ( ! is_array( $response['data'] ) ) {
    4122             wp_send_json_error( "Expected data array in response array from exporter: {$exporter_friendly_name}." );
     4156            wp_send_json_error(
     4157                /* translators: %s: exporter friendly name */
     4158                sprintf( __( 'Expected data array in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4159            );
    41234160        }
    41244161        if ( ! array_key_exists( 'done', $response ) ) {
    4125             wp_send_json_error( "Expected done (boolean) in response array from exporter: {$exporter_friendly_name}." );
     4162            wp_send_json_error(
     4163                /* translators: %s: exporter friendly name */
     4164                sprintf( __( 'Expected done (boolean) in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) )
     4165            );
    41264166        }
    41274167    } else {
     
    41434183     * @param int    $exporter_index  The index of the exporter that provided this data.
    41444184     * @param string $email_address   The email address associated with this personal data.
    4145      * @param int    $page            The zero-based page for this response.
     4185     * @param int    $page            The page for this response.
    41464186     * @param int    $request_id      The privacy request post ID associated with this request.
    41474187     * @param bool   $send_as_email   Whether the final results of the export should be emailed to the user.
     
    41624202 */
    41634203function wp_ajax_wp_privacy_erase_personal_data() {
    4164     $request_id  = (int) $_POST['id'];
    4165 
    4166     if ( empty( $request_id ) ) {
    4167         wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4204
     4205    if ( empty( $_POST['id'] ) ) {
     4206        wp_send_json_error( __( 'Missing request ID.' ) );
     4207    }
     4208
     4209    $request_id = (int) $_POST['id'];
     4210
     4211    if ( $request_id < 1 ) {
     4212        wp_send_json_error( __( 'Invalid request ID.' ) );
    41684213    }
    41694214
    41704215    if ( ! current_user_can( 'delete_users' ) ) {
    4171         wp_send_json_error( __( 'Error: Invalid request.' ) );
     4216        wp_send_json_error( __( 'Invalid request.' ) );
    41724217    }
    41734218
     
    41784223
    41794224    if ( ! $request || 'remove_personal_data' !== $request->action_name ) {
    4180         wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4225        wp_send_json_error( __( 'Invalid request ID.' ) );
    41814226    }
    41824227
     
    41844229
    41854230    if ( ! is_email( $email_address ) ) {
    4186         wp_send_json_error( __( 'Error: Invalid email address in request.' ) );
     4231        wp_send_json_error( __( 'Invalid email address in request.' ) );
     4232    }
     4233
     4234    if ( ! isset( $_POST['eraser'] ) ) {
     4235        wp_send_json_error( __( 'Missing eraser index.' ) );
    41874236    }
    41884237
    41894238    $eraser_index = (int) $_POST['eraser'];
    4190     $page         = (int) $_POST['page'];
     4239
     4240    if ( ! isset( $_POST['page'] ) ) {
     4241        wp_send_json_error( __( 'Missing page index.' ) );
     4242    }
     4243
     4244    $page = (int) $_POST['page'];
    41914245
    41924246    /**
     
    41974251     * @param array $args {
    41984252     *     An array of callable erasers of personal data. Default empty array.
    4199      *     [
    4200      *         callback             string Callable eraser that accepts an email address and
    4201      *                                     a page and returns an array with the number of items
    4202      *                                     removed, the number of items retained and any messages
    4203      *                                     from the eraser, as well as if additional pages are
    4204      *                                     available.
    4205      *         eraser_friendly_name string Translated user facing friendly name for the eraser.
    4206      *     ]
     4253     *     @type array {
     4254     *         Array of personal data exporters.
     4255     *
     4256     *         @type string $callback               Callable eraser that accepts an email address and
     4257     *                                              a page and returns an array with the number of items
     4258     *                                              removed, the number of items retained and any messages
     4259     *                                              from the eraser, as well as if additional pages are
     4260     *                                              available.
     4261     *         @type string $exporter_friendly_name Translated user facing friendly name for the eraser.
     4262     *     }
    42074263     * }
    42084264     */
     
    42114267    // Do we have any registered erasers?
    42124268    if ( 0 < count( $erasers ) ) {
     4269
    42134270        if ( $eraser_index < 1 ) {
    4214             wp_send_json_error( __( 'Error: Eraser index cannot be less than one.' ) );
     4271            wp_send_json_error( __( 'Eraser index cannot be less than one.' ) );
    42154272        }
    42164273
    42174274        if ( $eraser_index > count( $erasers ) ) {
    4218             wp_send_json_error( __( 'Error: Eraser index is out of range.' ) );
     4275            wp_send_json_error( __( 'Eraser index is out of range.' ) );
    42194276        }
    42204277
    42214278        if ( $page < 1 ) {
    4222             wp_send_json_error( __( 'Error: Page index cannot be less than one.' ) );
    4223         }
    4224 
    4225         $index = $eraser_index - 1; // Convert to zero based for eraser index
     4279            wp_send_json_error( __( 'Page index cannot be less than one.' ) );
     4280        }
     4281
     4282        $index  = $eraser_index - 1; // Convert to zero based for eraser index.
    42264283        $eraser = $erasers[ $index ];
     4284
    42274285        if ( ! is_array( $eraser ) ) {
     4286            /* translators: %d: array index */
     4287            wp_send_json_error( sprintf( __( 'Expected an array describing the eraser at index %d.' ), $eraser_index ) );
     4288        }
     4289
     4290        if ( ! array_key_exists( 'callback', $eraser ) ) {
     4291            /* translators: %d: array index */
     4292            wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a callback.' ), $eraser_index ) );
     4293        }
     4294
     4295        if ( ! is_callable( $eraser['callback'] ) ) {
     4296            /* translators: %d: array index */
     4297            wp_send_json_error( sprintf( __( 'Eraser callback at index %d is not a valid callback.' ), $eraser_index ) );
     4298        }
     4299
     4300        if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
     4301            /* translators: %d: array index */
     4302            wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a friendly name.' ), $eraser_index ) );
     4303        }
     4304
     4305        $callback             = $erasers[ $index ]['callback'];
     4306        $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
     4307
     4308        $response = call_user_func( $callback, $email_address, $page );
     4309
     4310        if ( is_wp_error( $response ) ) {
     4311            wp_send_json_error( $response );
     4312        }
     4313
     4314        if ( ! is_array( $response ) ) {
    42284315            wp_send_json_error(
    42294316                sprintf(
    4230                     __( 'Error: Expected an array describing the eraser at index %d.' ),
     4317                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4318                    __( 'Did not receive array from %1$s eraser (index %2$d).' ),
     4319                    esc_html( $eraser_friendly_name ),
    42314320                    $eraser_index
    42324321                )
    42334322            );
    42344323        }
    4235         if ( ! array_key_exists( 'callback', $eraser ) ) {
     4324
     4325        if ( ! array_key_exists( 'num_items_removed', $response ) ) {
    42364326            wp_send_json_error(
    42374327                sprintf(
    4238                     __( 'Error: Eraser array at index %d does not include a callback.' ),
     4328                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4329                    __( 'Expected num_items_removed key in response array from %1$s eraser (index %2$d).' ),
     4330                    esc_html( $eraser_friendly_name ),
    42394331                    $eraser_index
    42404332                )
    42414333            );
    42424334        }
    4243         if ( ! is_callable( $eraser['callback'] ) ) {
     4335
     4336        if ( ! array_key_exists( 'num_items_retained', $response ) ) {
    42444337            wp_send_json_error(
    42454338                sprintf(
    4246                     __( 'Error: Eraser callback at index %d is not a valid callback.' ),
     4339                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4340                    __( 'Expected num_items_retained key in response array from %1$s eraser (index %2$d).' ),
     4341                    esc_html( $eraser_friendly_name ),
    42474342                    $eraser_index
    42484343                )
    42494344            );
    42504345        }
    4251         if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
     4346
     4347        if ( ! array_key_exists( 'messages', $response ) ) {
    42524348            wp_send_json_error(
    42534349                sprintf(
    4254                     __( 'Error: Eraser array at index %d does not include a friendly name.' ),
     4350                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4351                    __( 'Expected messages key in response array from %1$s eraser (index %2$d).' ),
     4352                    esc_html( $eraser_friendly_name ),
    42554353                    $eraser_index
    42564354                )
     
    42584356        }
    42594357
    4260         $callback = $erasers[ $index ]['callback'];
    4261         $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
    4262 
    4263         $response = call_user_func( $callback, $email_address, $page );
    4264         if ( is_wp_error( $response ) ) {
    4265             wp_send_json_error( $response );
    4266         }
    4267 
    4268         if ( ! is_array( $response ) ) {
     4358        if ( ! is_array( $response['messages'] ) ) {
    42694359            wp_send_json_error(
    42704360                sprintf(
    4271                     __( 'Error: Did not receive array from %s eraser (index %d).' ),
    4272                     $eraser_friendly_name,
     4361                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4362                    __( 'Expected messages key to reference an array in response array from %1$s eraser (index %2$d).' ),
     4363                    esc_html( $eraser_friendly_name ),
    42734364                    $eraser_index
    42744365                )
    42754366            );
    42764367        }
    4277         if ( ! array_key_exists( 'num_items_removed', $response ) ) {
     4368
     4369        if ( ! array_key_exists( 'done', $response ) ) {
    42784370            wp_send_json_error(
    42794371                sprintf(
    4280                     __( 'Error: Expected num_items_removed key in response array from %s eraser (index %d).' ),
    4281                     $eraser_friendly_name,
     4372                    /* translators: %1$s: eraser friendly name, %2$d: array index */
     4373                    __( 'Expected done flag in response array from %1$s eraser (index %2$d).' ),
     4374                    esc_html( $eraser_friendly_name ),
    42824375                    $eraser_index
    42834376                )
    42844377            );
    42854378        }
    4286         if ( ! array_key_exists( 'num_items_retained', $response ) ) {
    4287             wp_send_json_error(
    4288                 sprintf(
    4289                     __( 'Error: Expected num_items_retained key in response array from %s eraser (index %d).' ),
    4290                     $eraser_friendly_name,
    4291                     $eraser_index
    4292                 )
    4293             );
    4294         }
    4295         if ( ! array_key_exists( 'messages', $response ) ) {
    4296             wp_send_json_error(
    4297                 sprintf(
    4298                     __( 'Error: Expected messages key in response array from %s eraser (index %d).' ),
    4299                     $eraser_friendly_name,
    4300                     $eraser_index
    4301                 )
    4302             );
    4303         }
    4304         if ( ! is_array( $response['messages'] ) ) {
    4305             wp_send_json_error(
    4306                 sprintf(
    4307                     __( 'Error: Expected messages key to reference an array in response array from %s eraser (index %d).' ),
    4308                     $eraser_friendly_name,
    4309                     $eraser_index
    4310                 )
    4311             );
    4312         }
    4313         if ( ! array_key_exists( 'done', $response ) ) {
    4314             wp_send_json_error(
    4315                 sprintf(
    4316                     __( 'Error: Expected done flag in response array from %s eraser (index %d).' ),
    4317                     $eraser_friendly_name,
    4318                     $eraser_index
    4319                 )
    4320             );
    4321         }
    43224379    } else {
    4323         // No erasers, so we're done
     4380        // No erasers, so we're done.
    43244381        $response = array(
    4325             'num_items_removed' => 0,
     4382            'num_items_removed'  => 0,
    43264383            'num_items_retained' => 0,
    4327             'messages' => array(),
    4328             'done' => true,
     4384            'messages'           => array(),
     4385            'done'               => true,
    43294386        );
    43304387    }
     
    43404397     * @param int    $exporter_index  The index of the exporter that provided this data.
    43414398     * @param string $email_address   The email address associated with this personal data.
    4342      * @param int    $page            The zero-based page for this response.
     4399     * @param int    $page            The page for this response.
    43434400     * @param int    $request_id      The privacy request post ID associated with this request.
    43444401     */
    43454402    $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id );
     4403
    43464404    if ( is_wp_error( $response ) ) {
    43474405        wp_send_json_error( $response );
Note: See TracChangeset for help on using the changeset viewer.