Skip to:
Content

BuddyPress.org

Ticket #7826: 7826.02.patch

File 7826.02.patch, 16.8 KB (added by r-a-y, 7 years ago)

Fixes an issue with expired data export download files.

  • new file src/bp-settings/actions/data.php

    new file mode 100644
    - +  
     1<?php
     2/**
     3 * Settings: Data management action handler
     4 *
     5 * @package BuddyPress
     6 * @subpackage SettingsActions
     7 * @since 3.1.0
     8 */
     9
     10/**
     11 * Data export request handler.
     12 *
     13 * @since 3.1.0
     14 */
     15add_action( 'bp_actions', function() {
     16        if ( ! bp_is_post_request() || ! bp_displayed_user_id() || empty( $_POST['bp-data-export-nonce'] ) ) {
     17                return;
     18        }
     19
     20        // Nonce check.
     21        check_admin_referer( 'bp-data-export', 'bp-data-export-nonce' );
     22
     23        // Delete existing request if available.
     24        if ( ! empty( $_POST['bp-data-export-delete-request-nonce'] ) && wp_verify_nonce( $_POST['bp-data-export-delete-request-nonce'], 'bp-data-export-delete-request' ) ) {
     25                $existing = bp_settings_get_personal_data_request();
     26                if ( ! empty( $existing->ID ) ) {
     27                        wp_delete_post( $existing->ID, true );
     28                }
     29        }
     30
     31        // Create the user request.
     32        $request_id = wp_create_user_request( buddypress()->displayed_user->userdata->user_email, 'export_personal_data' );
     33
     34        $success = true;
     35        if ( is_wp_error( $request_id ) ) {
     36                $success = false;
     37                $message = $request_id->get_error_message();
     38        } elseif ( ! $request_id ) {
     39                $success = false;
     40                $message = __( 'We were unable to generate the data export request.', 'buddypress' );
     41        }
     42       
     43        /*
     44         * Auto-confirm the user request since the user already consented by
     45         * submitting our form.
     46         */
     47        if ( $success ) {
     48                /** This hook is documented in /wp-login.php */
     49                do_action( 'user_request_action_confirmed', $request_id );
     50
     51                $message = __( 'Data export request successfully created', 'buddypress' );
     52        }
     53
     54        bp_core_add_message( $message, $success ? 'success' : 'error' );
     55        bp_core_redirect( bp_get_requested_url() );
     56} );
     57 No newline at end of file
  • src/bp-settings/classes/class-bp-settings-component.php

     
    6565                        return;
    6666                }
    6767
    68                 $actions = array( 'notifications', 'capabilities', 'delete-account' );
     68                $actions = array( 'notifications', 'capabilities', 'data', 'delete-account' );
    6969
    7070                // Authenticated actions.
    7171                if ( is_user_logged_in() ) {
     
    182182                        );
    183183                }
    184184
     185                /**
     186                 * Filter whether the site should show the "Settings > Data" page.
     187                 *
     188                 * @since 3.1.0
     189                 *
     190                 * @param bool $show Defaults to true.
     191                 */
     192                $show_data_page = apply_filters( 'bp_settings_show_user_data_page', true );
     193
     194                // Data Management - only available for WP 4.9.6+
     195                if ( true === $show_data_page && function_exists( 'wp_register_user_personal_data_exporter' ) ) {
     196                        $sub_nav[] = array(
     197                                'name'            => __( 'Data Management', 'buddypress' ),
     198                                'slug'            => 'data',
     199                                'parent_url'      => $settings_link,
     200                                'parent_slug'     => $slug,
     201                                'screen_function' => 'bp_settings_screen_data',
     202                                'position'        => 89,
     203                                'user_has_access' => $access
     204                        );
     205                }
     206
    185207                // Add Delete Account nav item.
    186208                if ( ( ! bp_disable_account_deletion() && bp_is_my_profile() ) || bp_current_user_can( 'delete_users' ) ) {
    187209                        $sub_nav[] = array(
     
    241263                                );
    242264                        }
    243265
     266                        /** This filter is documented in bp-settings/classes/class-bp-settings-component.php */
     267                        $show_data_page = apply_filters( 'bp_settings_show_user_data_page', true );
     268
     269                        // Data Management.
     270                        if ( true === $show_data_page && function_exists( 'wp_register_user_personal_data_exporter' ) ) {
     271                                $wp_admin_nav[] = array(
     272                                        'parent'   => 'my-account-' . $this->id,
     273                                        'id'       => 'my-account-' . $this->id . '-data',
     274                                        'title'    => __( 'Data Management', 'buddypress' ),
     275                                        'href'     => trailingslashit( $settings_link . 'data' ),
     276                                        'position' => 89
     277                                );
     278                        }
     279
    244280                        // Delete Account
    245281                        if ( !bp_current_user_can( 'bp_moderate' ) && ! bp_core_get_root_option( 'bp-disable-account-deletion' ) ) {
    246282                                $wp_admin_nav[] = array(
  • new file src/bp-settings/screens/data.php

    new file mode 100644
    - +  
     1<?php
     2/**
     3 * Settings: User's "Settings > Data Management" screen handler
     4 *
     5 * @package BuddyPress
     6 * @subpackage SettingsScreens
     7 * @since 3.1.0
     8 */
     9
     10/**
     11 * Show the data settings template.
     12 *
     13 * @since 3.1.0
     14 */
     15function bp_settings_screen_data() {
     16        if ( bp_action_variables() ) {
     17                bp_do_404();
     18                return;
     19        }
     20
     21        /**
     22         * Filters the template file path to use for the data settings screen.
     23         *
     24         * @since 3.1.0
     25         *
     26         * @param string $value Directory path to look in for the template file.
     27         */
     28        bp_core_load_template( apply_filters( 'bp_settings_screen_data', 'members/single/settings/data' ) );
     29}
     30
     31/**
     32 * Fetches a user's personal data request.
     33 *
     34 * @since 3.1.0
     35 *
     36 * @param int WP user ID.
     37 * @return WP_User_Request|false WP_User_Request object on success, boolean false on failure.
     38 */
     39function bp_settings_get_personal_data_request( $user_id = 0 ) {
     40        if ( empty( $user_id ) ) {
     41                $user_id = bp_displayed_user_id();
     42        }
     43
     44        if ( empty( $user_id ) ) {
     45                return false;
     46        }
     47
     48        $user = get_userdata( $user_id );
     49        if ( empty( $user ) ) {
     50                return false;
     51        }
     52
     53        $query = new WP_Query( array(
     54                'author'        => (int) $user_id,
     55                'post_type'     => 'user_request',
     56                'post_status'   => 'any',
     57                'post_name__in' => array(
     58                        'export_personal_data'
     59                )
     60        ) );
     61
     62        if ( ! empty( $query->post ) ) {
     63                return wp_get_user_request_data( $query->post->ID );
     64        } else {
     65                return false;
     66        }
     67}
     68
     69/**
     70 * Fetches the expiration date for when a user request expires.
     71 *
     72 * @since 3.1.0
     73 *
     74 * @param WP_User_Request $request User request object.
     75 * @return string Formatted date.
     76 */
     77function bp_settings_get_personal_data_expiration_date( WP_User_Request $request ) {
     78        /** This filter is documented in wp-admin/includes/file.php */
     79        $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
     80
     81        return bp_format_time( $request->completed_timestamp + $expiration, true );
     82}
     83
     84/**
     85 * Fetches the confirmation date for a user request object.
     86 *
     87 * @since 3.1.0
     88 *
     89 * @param WP_User_Request $request User request object.
     90 * @return string Formatted date for the confirmation date.
     91 */
     92function bp_settings_get_personal_data_confirmation_date( WP_User_Request $request ) {
     93        return bp_format_time( $request->confirmed_timestamp, true );
     94}
     95
     96function bp_settings_get_personal_data_export_url( WP_User_Request $request ) {
     97        return get_post_meta( $request->ID, '_export_file_url', true );
     98}
     99
     100/**
     101 * Check if the generated data export file still exists or not.
     102 *
     103 * @since 3.1.0
     104 *
     105 * @param  WP_User_Request $request User request object.
     106 * @return bool
     107 */
     108function bp_settings_personal_data_export_exists( WP_User_Request $request ) {
     109        $file = get_post_meta( $request->ID, '_export_file_path', true );
     110        if ( file_exists( $file ) ) {
     111                return true;
     112        } else {
     113                return false;
     114        }
     115}
     116
     117/**
     118 * Tempalte tag to output a list of data exporter items.
     119 *
     120 * Piggybacks off of the 'wp_privacy_personal_data_exporters' filter and the
     121 * 'exporter_friendly_name' key, which is meant for the admin area.
     122 *
     123 * @todo We should look for a custom key like 'exporter_frontend_name' if available.
     124 *
     125 * @since 3.1.0
     126 */
     127function bp_settings_data_exporter_items() {
     128        /** This filter is documented in /wp-admin/includes/ajax-actions.php */
     129        $exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
     130
     131?>
     132        <ul>
     133        <?php foreach ( $exporters as $exporter => $data ) :
     134                /**
     135                 * Filters the data exporter name for display on the "Settings > Data" page.
     136                 *
     137                 * @since 3.1.0
     138                 *
     139                 * @param string $name     Data exporter friendly name.
     140                 * @param string $exporter Internal exporter name.
     141                 */
     142                $item = apply_filters( 'bp_settings_data_exporter_name', esc_html( $data['exporter_friendly_name'] ), $exporter );
     143        ?>
     144
     145                <li><?php echo $item; ?></li>
     146
     147        <?php endforeach; ?>
     148        </ul>
     149
     150<?php
     151}
     152
     153// This needs some polishing...
     154add_filter( 'bp_settings_data_exporter_name', function( $retval, $exporter ) {
     155        if ( 'wordpress-user' === $exporter ) {
     156                $retval = __( 'User profile', 'buddypress' );
     157
     158        // Strip 'WordPress' string for whitelisting.
     159        } elseif (  0 === strpos( $exporter, 'wordpress-' ) ) {
     160                $retval = str_replace( 'WordPress ', '', $retval );
     161        }
     162        return $retval;
     163}, 10, 2 );
     164 No newline at end of file
  • src/bp-templates/bp-legacy/buddypress/members/single/settings.php

     
    3737        case 'profile'        :
    3838                bp_get_template_part( 'members/single/settings/profile'        );
    3939                break;
     40        case 'data'           :
     41                bp_get_template_part( 'members/single/settings/data'           );
    4042        default:
    4143                bp_get_template_part( 'members/single/plugins'                 );
    4244                break;
  • new file src/bp-templates/bp-legacy/buddypress/members/single/settings/data.php

    new file mode 100644
    - +  
     1<?php
     2/**
     3 * BuddyPress - Members Settings Data
     4 *
     5 * @package BuddyPress
     6 * @subpackage bp-legacy
     7 * @version 3.1.0
     8 */
     9
     10/** This action is documented in bp-templates/bp-legacy/buddypress/members/single/settings/profile.php */
     11do_action( 'bp_before_member_settings_template' ); ?>
     12
     13<h2><?php _e( 'Data Export', 'buddypress' );?></h2>
     14
     15<?php $request = bp_settings_get_personal_data_request(); ?>
     16
     17<?php if ( $request ) : ?>
     18
     19        <?php if ( 'request-completed' === $request->status ) : ?>
     20
     21                <?php if ( bp_settings_personal_data_export_exists( $request ) ) : ?>
     22
     23                        <p><?php esc_html_e( 'Your request for an export of personal data has been completed.', 'buddypress' ); ?></p>
     24                        <p><?php printf( esc_html__( 'You may download your personal data by clicking on the link below. For privacy and security, we will automatically delete the file on %s, so please download it before then.', 'buddypress' ), bp_settings_get_personal_data_expiration_date( $request ) ); ?></p>
     25
     26                        <p><strong><?php printf( '<a href="%1$s">%2$s</a>', bp_settings_get_personal_data_export_url( $request ), esc_html__( 'Download personal data', 'buddypress' ) ); ?></strong></p>
     27
     28                <?php else : ?>
     29
     30                        <p><?php esc_html_e( 'Your previous request for an export of personal data has expired.', 'buddypress' ); ?></p>
     31                        <p><?php esc_html_e( 'Please click on the button below to make a new request.', 'buddypress' ); ?></p>
     32
     33                        <form id="bp-data-export" method="post">
     34                                <input type="hidden" name="bp-data-export-delete-request-nonce" value="<?php echo wp_create_nonce( 'bp-data-export-delete-request' ); ?>" />
     35                                <button type="submit" name="bp-data-export-nonce" value="<?php echo wp_create_nonce( 'bp-data-export' ); ?>"><?php esc_html_e( 'Request new data export', 'buddypress' ); ?></button>
     36                        </form>
     37
     38                <?php endif; ?>
     39
     40        <?php elseif ( 'request-confirmed' === $request->status ) : ?>
     41
     42                <p><?php printf( esc_html__( 'You previously requested an export of your personal data on %s.', 'buddypress' ), bp_settings_get_personal_data_confirmation_date( $request ) ); ?></p>
     43                <p><?php esc_html_e( 'You will receive a link to download your export via email once we are able to fulfill your request.', 'buddypress' ); ?></p>
     44
     45        <?php endif; ?>
     46
     47<?php else : ?>
     48
     49        <p><?php esc_html_e( 'You can request an export of your personal data, containing the following items if applicable:', 'buddypress' ); ?></p>
     50
     51        <?php bp_settings_data_exporter_items(); ?>
     52
     53        <p><?php esc_html_e( 'If you want to make a request, please click on the button below:', 'buddypress' ); ?></p>
     54
     55        <form id="bp-data-export" method="post">
     56                <button type="submit" name="bp-data-export-nonce" value="<?php echo wp_create_nonce( 'bp-data-export' ); ?>"><?php esc_html_e( 'Request personal data export', 'buddypress' ); ?></button>
     57        </form>
     58
     59<?php endif; ?>
     60
     61<!--
     62<h2 class="bp-screen-reader-text"><?php
     63        /* translators: accessibility text */
     64        _e( 'Data Erase', 'buddypress' );
     65?></h2>
     66
     67<p>You can make a request to erase the following type of data from the site:</p>
     68
     69<p>If you want to make a request, please click on the button below:</p>
     70
     71        <form id="bp-data-erase" method="post">
     72                <button type="submit" name="bp-data-erase-nonce" value="<?php echo wp_create_nonce( 'bp-data-erase' ); ?>">Request data erasure</button>
     73        </form>
     74-->
     75
     76<?php
     77
     78/** This action is documented in bp-templates/bp-legacy/buddypress/members/single/settings/profile.php */
     79do_action( 'bp_after_member_settings_template' );
  • src/bp-templates/bp-nouveau/buddypress/members/single/settings.php

     
    3939        case 'invites':
    4040                bp_get_template_part( 'members/single/settings/group-invites' );
    4141                break;
     42        case 'data':
     43                bp_get_template_part( 'members/single/settings/data' );
     44                break;
    4245        default:
    4346                bp_get_template_part( 'members/single/plugins' );
    4447                break;
  • new file src/bp-templates/bp-nouveau/buddypress/members/single/settings/data.php

    new file mode 100644
    - +  
     1<?php
     2/**
     3 * BuddyPress - Members Settings ( Data Management )
     4 *
     5 * @since 3.1.0
     6 * @version 3.1.0
     7 */
     8
     9bp_nouveau_member_hook( 'before', 'settings_template' ); ?>
     10
     11<h2 class="screen-heading data-settings-screen">
     12        <?php esc_html_e( 'Data Export', 'buddypress' ); ?>
     13</h2>
     14
     15<?php $request = bp_settings_get_personal_data_request(); ?>
     16
     17<?php if ( $request ) : ?>
     18
     19        <?php if ( 'request-completed' === $request->status ) : ?>
     20
     21                <?php if ( bp_settings_personal_data_export_exists( $request ) ) : ?>
     22
     23                        <p><?php esc_html_e( 'Your request for an export of personal data has been completed.', 'buddypress' ); ?></p>
     24                        <p><?php printf( esc_html__( 'You may download your personal data by clicking on the link below. For privacy and security, we will automatically delete the file on %s, so please download it before then.', 'buddypress' ), bp_settings_get_personal_data_expiration_date( $request ) ); ?></p>
     25
     26                        <p><strong><?php printf( '<a href="%1$s">%2$s</a>', bp_settings_get_personal_data_export_url( $request ), esc_html__( 'Download personal data', 'buddypress' ) ); ?></strong></p>
     27
     28                <?php else : ?>
     29
     30                        <p><?php esc_html_e( 'Your previous request for an export of personal data has expired.', 'buddypress' ); ?></p>
     31                        <p><?php esc_html_e( 'Please click on the button below to make a new request.', 'buddypress' ); ?></p>
     32
     33                        <form id="bp-data-export" method="post">
     34                                <input type="hidden" name="bp-data-export-delete-request-nonce" value="<?php echo wp_create_nonce( 'bp-data-export-delete-request' ); ?>" />
     35                                <button type="submit" name="bp-data-export-nonce" value="<?php echo wp_create_nonce( 'bp-data-export' ); ?>"><?php esc_html_e( 'Request new data export', 'buddypress' ); ?></button>
     36                        </form>
     37
     38                <?php endif; ?>
     39
     40        <?php elseif ( 'request-confirmed' === $request->status ) : ?>
     41
     42                <p><?php printf( esc_html__( 'You previously requested an export of your personal data on %s.', 'buddypress' ), bp_settings_get_personal_data_confirmation_date( $request ) ); ?></p>
     43                <p><?php esc_html_e( 'You will receive a link to download your export via email once we are able to fulfill your request.', 'buddypress' ); ?></p>
     44
     45        <?php endif; ?>
     46
     47<?php else : ?>
     48
     49        <p><?php esc_html_e( 'You can request an export of your personal data, containing the following items if applicable:', 'buddypress' ); ?></p>
     50
     51        <?php bp_settings_data_exporter_items(); ?>
     52
     53        <p><?php esc_html_e( 'If you want to make a request, please click on the button below:', 'buddypress' ); ?></p>
     54
     55        <form id="bp-data-export" method="post">
     56                <button type="submit" name="bp-data-export-nonce" value="<?php echo wp_create_nonce( 'bp-data-export' ); ?>"><?php esc_html_e( 'Request personal data export', 'buddypress' ); ?></button>
     57        </form>
     58
     59<?php endif; ?>
     60
     61<!--
     62<h2 class="screen-heading data-settings-screen">
     63        <?php esc_html_e( 'Data Erase', 'buddypress' ); ?>
     64</h2>
     65
     66<p>You can make a request to erase the following type of data from the site:</p>
     67
     68<p>If you want to make a request, please click on the button below:</p>
     69
     70        <form id="bp-data-erase" method="post">
     71                <button type="submit" name="bp-data-erase-nonce" value="<?php echo wp_create_nonce( 'bp-data-erase' ); ?>">Request data erasure</button>
     72        </form>
     73-->
     74
     75<?php
     76bp_nouveau_member_hook( 'after', 'settings_template' );