WordPress.org

Make WordPress Core

Changeset 42889


Ignore:
Timestamp:
03/28/2018 07:27:59 PM (15 months ago)
Author:
azaozz
Message:

Privacy: add support for exporting multiple pages of personal data.

Props allendav.
See #43438.

Location:
trunk/src/wp-admin
Files:
2 edited

Legend:

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

    r42349 r42889  
    130130    'get-community-events',
    131131    'edit-theme-plugin-file',
     132    'wp-privacy-export-personal-data',
    132133);
    133134
  • trunk/src/wp-admin/includes/ajax-actions.php

    r42777 r42889  
    43274327    }
    43284328}
     4329
     4330function wp_ajax_wp_privacy_export_personal_data() {
     4331//  check_ajax_referer( 'wp-privacy-export-personal-data', 'security' );
     4332
     4333    if ( ! current_user_can( 'manage_options' ) ) {
     4334        wp_send_json_error( 'access denied' );
     4335    }
     4336
     4337    $email_address  = sanitize_text_field( $_POST['email'] );
     4338    $exporter_index = (int) $_POST['exporter'];
     4339    $page           = (int) $_POST['page'];
     4340
     4341    /**
     4342     * Filters the array of exporter callbacks.
     4343     *
     4344     * @since 4.9.5.
     4345     *
     4346     * @param array $args {
     4347     *     An array of callable exporters of personal data. Default empty array.
     4348     *     [
     4349     *         callback               string  Callable exporter that accepts an email address and
     4350     *                                        a page and returns an array of name => value
     4351     *                                        pairs of personal data
     4352     *         exporter_friendly_name string  Translated user facing friendly name for the exporter
     4353     *     ]
     4354     * }
     4355     */
     4356    $exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
     4357
     4358    if ( ! is_array( $exporters ) ) {
     4359        wp_send_json_error( 'An exporter has improperly used the registration filter.' );
     4360    }
     4361
     4362    // Do we have any registered exporters?
     4363    if ( 0 < count( $exporters ) ) {
     4364        if ( $exporter_index < 1 ) {
     4365            wp_send_json_error( 'Exporter index cannot be negative.' );
     4366        }
     4367
     4368        if ( $exporter_index > count( $exporters ) ) {
     4369            wp_send_json_error( 'Exporter index out of range.' );
     4370        }
     4371
     4372        $index = $exporter_index - 1;
     4373
     4374        if ( $page < 1 ) {
     4375            wp_send_json_error( 'Page index cannot be less than one.' );
     4376        }
     4377
     4378        // Surprisingly, email addresses can contain mutli-byte characters now
     4379        $email_address = trim( mb_strtolower( $email_address ) );
     4380
     4381        if ( ! is_email( $email_address ) ) {
     4382            wp_send_json_error( 'A valid email address must be given.' );
     4383        }
     4384
     4385        $exporter = $exporters[ $index ];
     4386        if ( ! is_array( $exporter ) ) {
     4387            wp_send_json_error( "Expected an array describing the exporter at index {$exporter_index}." );
     4388        }
     4389        if ( ! array_key_exists( 'callback', $exporter ) ) {
     4390            wp_send_json_error( "Exporter array at index {$exporter_index} does not include a callback." );
     4391        }
     4392        if ( ! is_callable( $exporter['callback'] ) ) {
     4393            wp_send_json_error( "Exporter callback at index {$exporter_index} is not a valid callback." );
     4394        }
     4395        if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
     4396            wp_send_json_error( "Exporter array at index {$exporter_index} does not include a friendly name." );
     4397        }
     4398
     4399        $callback = $exporters[ $index ]['callback'];
     4400        $exporter_friendly_name = $exporters[ $index ]['exporter_friendly_name'];
     4401
     4402        $response = call_user_func( $callback, $email_address, $page );
     4403        if ( is_wp_error( $response ) ) {
     4404            wp_send_json_error( $response );
     4405        }
     4406
     4407        if ( ! is_array( $response ) ) {
     4408            wp_send_json_error( "Expected response as an array from exporter: {$exporter_friendly_name}." );
     4409        }
     4410        if ( ! array_key_exists( 'data', $response ) ) {
     4411            wp_send_json_error( "Expected data in response array from exporter: {$exporter_friendly_name}." );
     4412        }
     4413        if ( ! is_array( $response['data'] ) ) {
     4414            wp_send_json_error( "Expected data array in response array from exporter: {$exporter_friendly_name}." );
     4415        }
     4416        if ( ! array_key_exists( 'done', $response ) ) {
     4417            wp_send_json_error( "Expected done (boolean) in response array from exporter: {$exporter_friendly_name}." );
     4418        }
     4419    } else {
     4420        // No exporters, so we're done
     4421        $response = array(
     4422            'data' => array(),
     4423            'done' => true,
     4424        );
     4425    }
     4426
     4427    /**
     4428     * Filters a page of personal data exporter data. Used to build the export report.
     4429     *
     4430     * Allows the export response to be consumed by destinations in addition to Ajax.
     4431     *
     4432     * @since 4.9.5
     4433     *
     4434     * @param array  $response        The personal data for the given exporter and page.
     4435     * @param int    $exporter_index  The index of the exporter that provided this data.
     4436     * @param string $email_address   The email address associated with this personal data.
     4437     * @param int    $page            The zero-based page for this response.
     4438     */
     4439    $response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page );
     4440    if ( is_wp_error( $response ) ) {
     4441        wp_send_json_error( $response );
     4442    }
     4443
     4444    wp_send_json_success( $response );
     4445}
Note: See TracChangeset for help on using the changeset viewer.