Make WordPress Core

Ticket #43546: 43546.diff

File 43546.diff, 6.3 KB (added by allendav, 7 years ago)

Work in progress

  • src/wp-admin/privacy.php

     
    1313        wp_die( __( 'Sorry, you are not allowed to manage privacy on this site.' ) );
    1414}
    1515
     16// Collect all the export verification requests from the options and usermeta
     17
     18function _wp_privacy_get_all_personal_data_export_requests() {
     19        global $wpdb;
     20
     21        $requests = array();
     22
     23        $registered_user_export_requests = $wpdb->get_results(
     24                "SELECT * FROM $wpdb->usermeta WHERE meta_key LIKE '_account_action_%'",
     25                ARRAY_A
     26        );
     27
     28        foreach ( (array) $registered_user_export_requests as $export_request ) {
     29                error_log( print_r( $export_request, true ) );
     30
     31                $user = get_user_by( 'id', $export_request['user_id'] );
     32                // TODO handle user not found
     33
     34                $email    = $user->user_email;
     35                $username = $user->user_login;
     36                $details  = explode( ':', $export_request['meta_value'] );
     37                // TODO handle malformed details
     38
     39                $requests[] = array(
     40                        'email'     => $email,
     41                        'requested' => $details[0],
     42                        'verified'  => ''
     43                );
     44        }
     45
     46        $email_only_export_requests = $wpdb->get_results(
     47                "SELECT * FROM $wpdb->options WHERE option_name LIKE '_account_action_%'",
     48                ARRAY_A
     49        );
     50
     51        foreach ( (array) $email_only_export_requests as $export_request ) {
     52                $details  = explode( ':', $export_request['option_value'] );
     53                // TODO handle malformed details
     54
     55                $requests[] = array(
     56                        'email' => $details[2],
     57                        'requested' => $details[0],
     58                        'verified' => ''
     59                );
     60        }
     61
     62        return $requests;
     63}
     64
     65global $export_requests;
     66$export_requests = _wp_privacy_get_all_personal_data_export_requests();
     67
     68if ( ! class_exists( 'WP_List_Table' ) ) {
     69        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
     70}
     71
     72// Build export request list table class here for now so we don't have to create new files
     73
     74class WP_Personal_Data_Export_Requests_Table extends WP_List_Table {
     75        function get_columns() {
     76                $columns = array(
     77                        'email'     => __( 'Email' ),
     78                        'requested' => __( 'Requested' ),
     79                        'verified'  => __( 'Verified' ),
     80                        'actions'   => __( 'Export File Actions' ),
     81                );
     82
     83                return $columns;
     84        }
     85
     86        function prepare_items() {
     87                global $export_requests;
     88
     89                $columns = $this->get_columns();
     90                $hidden = array();
     91                $sortable = array();
     92                $this->_column_headers = array( $columns, $hidden, $sortable );
     93                $this->items = $export_requests;
     94        }
     95
     96        function column_default( $item, $column_name ) {
     97                $cell_value = $item[ $column_name ];
     98
     99                if ( in_array( $column_name, array( 'requested', 'verified' ) ) ) {
     100                        if ( empty( $cell_value ) ) {
     101                                return '-';
     102                        }
     103
     104                        $time_diff = current_time( 'timestamp', true ) - $cell_value;
     105                        if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
     106                                return sprintf( __( '%s ago' ), human_time_diff( $cell_value ) );
     107                        }
     108
     109                        return
     110                                date( get_option( 'date_format' ), $cell_value ) .
     111                                '<br>' .
     112                                date( get_option( 'time_format' ), $cell_value );
     113                }
     114
     115                if ( 'actions' === $column_name ) {
     116                        return '<a href="#">Download</a> | <a href="#">Send via Email</a>';
     117                }
     118
     119                return $cell_value;
     120        }
     121
     122        function column_email( $item ) {
     123                // TODO links, nonces
     124
     125                $actions = array(
     126                        'resend'   => __( '<a href="#">Re-send verification email</a>' ),
     127                        'delete'   => __( '<a href="#">Delete</a>' ),
     128                );
     129
     130                return sprintf( '%1$s %2$s', $item['email'], $this->row_actions( $actions ) );
     131        }
     132}
     133
     134$personal_data_export_requests_table = new WP_Personal_Data_Export_Requests_Table();
     135
    16136// "Borrow" xfn.js for now so we don't have to create new files.
    17137// wp_enqueue_script( 'xfn' );
    18138
     139$doing_personal_data_export_for_email = '';
     140
    19141$action = isset( $_POST['action'] ) ? $_POST['action'] : '';
    20142
    21143if ( ! empty( $action ) ) {
     
    57179                                'updated'
    58180                        );
    59181                }
     182        } elseif ( 'add-export-request' === $action ) {
     183                $username_or_email_address = isset( $_POST['username_or_email_to_export'] ) ? $_POST['username_or_email_to_export'] : '';
     184                $username_or_email_address = sanitize_text_field( $username_or_email_address );
     185
     186                if ( ! is_email( $username_or_email_address ) ) {
     187                        $user = get_user_by( 'login', $username_or_email_address );
     188                        if ( ! $user instanceof WP_User ) {
     189                                add_settings_error(
     190                                        'username_or_email_to_export',
     191                                        'username_or_email_to_export',
     192                                        __( 'Unable to add export request. A valid email address or username must be supplied.' ),
     193                                        'error'
     194                                );
     195                        } else {
     196                                $doing_personal_data_export_for_email = $user->user_email;
     197                        }
     198                } else {
     199                        $doing_personal_data_export_for_email = $username_or_email_address;
     200                }
     201
     202                if ( ! empty( $doing_personal_data_export_for_email ) ) {
     203                        $result = send_confirm_account_action_email( 'export_personal_data', __( 'Export personal data' ), $doing_personal_data_export_for_email );
     204                        if ( is_wp_error( $result ) || ! $result ) {
     205                                add_settings_error(
     206                                        'username_or_email_to_export',
     207                                        'username_or_email_to_export',
     208                                        __( 'Unable to initiate export verification request.' ),
     209                                        'error'
     210                                );
     211                        } else {
     212                                add_settings_error(
     213                                        'username_or_email_to_export',
     214                                        'username_or_email_to_export',
     215                                        __( 'Export verification request initiated successfully.' ),
     216                                        'updated'
     217                                );
     218
     219                                // Re-fetch the requests since we just added one
     220                                global $export_requests;
     221                                $export_requests =_wp_privacy_get_all_personal_data_export_requests();
     222                        }
     223                }
    60224        }
    61225}
    62226
     
    195359
    196360                ?>
    197361        </table>
     362
     363        <h2><?php _e( 'Personal Data Export Requests' ); ?></h2>
     364        <?php
     365                $personal_data_export_requests_table->prepare_items();
     366                $personal_data_export_requests_table->display();
     367        ?>
     368        <h3><?php _e( 'Add New Request' ); ?></h3>
     369        <form method="post" action="">
     370                <input type="hidden" name="action" value="add-export-request" />
     371                <?php wp_nonce_field( 'add-export-request' ); ?>
     372                <fieldset>
     373                        <legend class="screen-reader-text"><span><?php _e( 'Enter the username or email address of the user whose personal data you wish to export.' ); ?></span></legend>
     374                        <label for="username_or_email_to_export">
     375                                <input type="text" class="regular-text" name="username_or_email_to_export" />
     376                        </label>
     377                        <p class="description"><?php _e( 'A verification email will be sent to the user at this email address, asking them to verify the request.' ); ?></p>
     378                </fieldset>
     379                <?php submit_button( __( 'Add New Request' ) ); ?>
     380        </form>
    198381</div>
    199382
    200383<?php