Make WordPress Core


Ignore:
Timestamp:
05/02/2018 12:02:01 AM (7 years ago)
Author:
SergeyBiryukov
Message:

Privacy: add means to erase personal data by username or email address. First run.

Props allendav, coreymckrill, ericdaams, azaozz.
Merges [42986] to the 4.9 branch.
See #43637, #43602.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43074 r43075  
    40204020
    40214021function wp_ajax_wp_privacy_export_personal_data() {
    4022 //  check_ajax_referer( 'wp-privacy-export-personal-data', 'security' );
     4022    check_ajax_referer( 'wp-privacy-export-personal-data', 'security' );
    40234023
    40244024    if ( ! current_user_can( 'manage_options' ) ) {
    4025         wp_send_json_error( 'access denied' );
     4025        wp_send_json_error( __( 'Error: Invalid request.' ) );
    40264026    }
    40274027
     
    40334033     * Filters the array of exporter callbacks.
    40344034     *
    4035      * @since 4.9.5.
     4035     * @since 4.9.6
    40364036     *
    40374037     * @param array $args {
     
    41214121     * Allows the export response to be consumed by destinations in addition to Ajax.
    41224122     *
    4123      * @since 4.9.5
     4123     * @since 4.9.6
    41244124     *
    41254125     * @param array  $response        The personal data for the given exporter and page.
     
    41354135    wp_send_json_success( $response );
    41364136}
     4137
     4138/**
     4139 * Ajax handler for erasing personal data.
     4140 *
     4141 * @since 4.9.6
     4142 */
     4143function wp_ajax_wp_privacy_erase_personal_data() {
     4144    $request_id  = (int) $_POST['id'];
     4145
     4146    if ( empty( $request_id ) ) {
     4147        wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4148    }
     4149
     4150    if ( ! current_user_can( 'delete_users' ) ) {
     4151        wp_send_json_error( __( 'Error: Invalid request.' ) );
     4152    }
     4153
     4154    check_ajax_referer( 'wp-privacy-erase-personal-data-' . $request_id, 'security' );
     4155
     4156    // Find the request CPT
     4157    $request = get_post( $request_id );
     4158    if ( 'user_remove_request' !== $request->post_type ) {
     4159        wp_send_json_error( __( 'Error: Invalid request ID.' ) );
     4160    }
     4161
     4162    $email_address = get_post_meta( $request_id, '_user_email', true );
     4163
     4164    if ( ! is_email( $email_address ) ) {
     4165        wp_send_json_error( __( 'Error: Invalid email address in request.' ) );
     4166    }
     4167
     4168    $eraser_index = (int) $_POST['eraser'];
     4169    $page         = (int) $_POST['page'];
     4170
     4171    /**
     4172     * Filters the array of personal data eraser callbacks.
     4173     *
     4174     * @since 4.9.6
     4175     *
     4176     * @param array $args {
     4177     *     An array of callable erasers of personal data. Default empty array.
     4178     *     [
     4179     *         callback             string Callable eraser that accepts an email address and
     4180     *                                     a page and returns an array with the number of items
     4181     *                                     removed, the number of items retained and any messages
     4182     *                                     from the eraser, as well as if additional pages are
     4183     *                                     available.
     4184     *         eraser_friendly_name string Translated user facing friendly name for the eraser.
     4185     *     ]
     4186     * }
     4187     */
     4188    $erasers = apply_filters( 'wp_privacy_personal_data_erasers', array() );
     4189
     4190    // Do we have any registered erasers?
     4191    if ( 0 < count( $erasers ) ) {
     4192        if ( $eraser_index < 1 ) {
     4193            wp_send_json_error( __( 'Error: Eraser index cannot be less than one.' ) );
     4194        }
     4195
     4196        if ( $eraser_index > count( $erasers ) ) {
     4197            wp_send_json_error( __( 'Error: Eraser index is out of range.' ) );
     4198        }
     4199
     4200        if ( $page < 1 ) {
     4201            wp_send_json_error( __( 'Error: Page index cannot be less than one.' ) );
     4202        }
     4203
     4204        $index = $eraser_index - 1; // Convert to zero based for eraser index
     4205        $eraser = $erasers[ $index ];
     4206        if ( ! is_array( $eraser ) ) {
     4207            wp_send_json_error(
     4208                sprintf(
     4209                    __( 'Error: Expected an array describing the eraser at index %d.' ),
     4210                    $eraser_index
     4211                )
     4212            );
     4213        }
     4214        if ( ! array_key_exists( 'callback', $eraser ) ) {
     4215            wp_send_json_error(
     4216                sprintf(
     4217                    __( 'Error: Eraser array at index %d does not include a callback.' ),
     4218                    $eraser_index
     4219                )
     4220            );
     4221        }
     4222        if ( ! is_callable( $eraser['callback'] ) ) {
     4223            wp_send_json_error(
     4224                sprintf(
     4225                    __( 'Error: Eraser callback at index %d is not a valid callback.' ),
     4226                    $eraser_index
     4227                )
     4228            );
     4229        }
     4230        if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) {
     4231            wp_send_json_error(
     4232                sprintf(
     4233                    __( 'Error: Eraser array at index %d does not include a friendly name.' ),
     4234                    $eraser_index
     4235                )
     4236            );
     4237        }
     4238
     4239        $callback = $erasers[ $index ]['callback'];
     4240        $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
     4241
     4242        $response = call_user_func( $callback, $email_address, $page );
     4243        if ( is_wp_error( $response ) ) {
     4244            wp_send_json_error( $response );
     4245        }
     4246
     4247        if ( ! is_array( $response ) ) {
     4248            wp_send_json_error(
     4249                sprintf(
     4250                    __( 'Error: Did not receive array from %s eraser (index %d).' ),
     4251                    $eraser_friendly_name,
     4252                    $eraser_index
     4253                )
     4254            );
     4255        }
     4256        if ( ! array_key_exists( 'num_items_removed', $response ) ) {
     4257            wp_send_json_error(
     4258                sprintf(
     4259                    __( 'Error: Expected num_items_removed key in response array from %s eraser (index %d).' ),
     4260                    $eraser_friendly_name,
     4261                    $eraser_index
     4262                )
     4263            );
     4264        }
     4265        if ( ! array_key_exists( 'num_items_retained', $response ) ) {
     4266            wp_send_json_error(
     4267                sprintf(
     4268                    __( 'Error: Expected num_items_retained key in response array from %s eraser (index %d).' ),
     4269                    $eraser_friendly_name,
     4270                    $eraser_index
     4271                )
     4272            );
     4273        }
     4274        if ( ! array_key_exists( 'messages', $response ) ) {
     4275            wp_send_json_error(
     4276                sprintf(
     4277                    __( 'Error: Expected messages key in response array from %s eraser (index %d).' ),
     4278                    $eraser_friendly_name,
     4279                    $eraser_index
     4280                )
     4281            );
     4282        }
     4283        if ( ! is_array( $response['messages'] ) ) {
     4284            wp_send_json_error(
     4285                sprintf(
     4286                    __( 'Error: Expected messages key to reference an array in response array from %s eraser (index %d).' ),
     4287                    $eraser_friendly_name,
     4288                    $eraser_index
     4289                )
     4290            );
     4291        }
     4292        if ( ! array_key_exists( 'done', $response ) ) {
     4293            wp_send_json_error(
     4294                sprintf(
     4295                    __( 'Error: Expected done flag in response array from %s eraser (index %d).' ),
     4296                    $eraser_friendly_name,
     4297                    $eraser_index
     4298                )
     4299            );
     4300        }
     4301    } else {
     4302        // No erasers, so we're done
     4303        $response = array(
     4304            'num_items_removed' => 0,
     4305            'num_items_retained' => 0,
     4306            'messages' => array(),
     4307            'done' => true,
     4308        );
     4309    }
     4310
     4311    /**
     4312     * Filters a page of personal data eraser data.
     4313     *
     4314     * Allows the erasure response to be consumed by destinations in addition to Ajax.
     4315     *
     4316     * @since 4.9.6
     4317     *
     4318     * @param array  $response        The personal data for the given exporter and page.
     4319     * @param int    $exporter_index  The index of the exporter that provided this data.
     4320     * @param string $email_address   The email address associated with this personal data.
     4321     * @param int    $page            The zero-based page for this response.
     4322     * @param int    $request_id      The privacy request post ID associated with this request.
     4323     */
     4324    $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id );
     4325    if ( is_wp_error( $response ) ) {
     4326        wp_send_json_error( $response );
     4327    }
     4328
     4329    wp_send_json_success( $response );
     4330}
Note: See TracChangeset for help on using the changeset viewer.