WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/11/2020 09:26:25 PM (10 months ago)
Author:
SergeyBiryukov
Message:

Privacy: Add a table of contents to Personal Data Export report for easier navigation.

Props xkon, garrett-eclipse, birgire, karmatosed.
Fixes #46894.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/privacy-tools.php

    r47245 r47278  
    217217 *
    218218 * @since 4.9.6
     219 * @since 5.4.0 Added the `$group_id` and `$groups_count` parameters.
    219220 *
    220221 * @param array $group_data {
     
    233234 *     }
    234235 * }
    235  * @return string The HTML for this group and its items.
    236  */
    237 function wp_privacy_generate_personal_data_export_group_html( $group_data ) {
    238     $group_html  = '<h2>';
     236 * @param string  $group_id     The group identifier.
     237 * @param int     $groups_count The number of all groups
     238 * @return string $group_html   The HTML for this group and its items.
     239 */
     240function wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id = '', $groups_count = 1 ) {
     241    $group_id_attr = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id );
     242
     243    $group_html  = '<h2 id="' . esc_attr( $group_id_attr ) . '">';
    239244    $group_html .= esc_html( $group_data['group_label'] );
    240245
     
    271276        $group_html .= '</tbody>';
    272277        $group_html .= '</table>';
     278    }
     279
     280    if ( 1 < $groups_count ) {
     281        $group_html .= '<div class="return_to_top">';
     282        $group_html .= '<a href="#top">' . esc_html__( '&uarr; Return to top' ) . '</a>';
     283        $group_html .= '</div>';
    273284    }
    274285
     
    374385    $groups = array_merge( array( 'about' => $about_group ), $groups );
    375386
     387    $groups_count = count( $groups );
     388
    376389    // Convert the groups to JSON format.
    377390    $groups_json = wp_json_encode( $groups );
     
    411424    fwrite( $file, 'td { padding: 5px; }' );
    412425    fwrite( $file, 'tr:nth-child(odd) { background-color: #fafafa; }' );
     426    fwrite( $file, '.return_to_top { text-align:right; }' );
    413427    fwrite( $file, '</style>' );
    414428    fwrite( $file, '<title>' );
     
    417431    fwrite( $file, "</head>\n" );
    418432    fwrite( $file, "<body>\n" );
    419     fwrite( $file, '<h1>' . esc_html__( 'Personal Data Export' ) . '</h1>' );
     433    fwrite( $file, '<h1 id="top">' . esc_html__( 'Personal Data Export' ) . '</h1>' );
     434
     435    // Create TOC.
     436    if ( 1 < $groups_count ) {
     437        fwrite( $file, '<div id="table_of_contents">' );
     438        fwrite( $file, '<h2>' . esc_html__( 'Table of Contents' ) . '</h2>' );
     439        fwrite( $file, '<ul>' );
     440        foreach ( (array) $groups as $group_id => $group_data ) {
     441            $group_label       = esc_html( $group_data['group_label'] );
     442            $group_id_attr     = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id );
     443            $group_items_count = count( (array) $group_data['items'] );
     444            if ( $group_items_count > 1 ) {
     445                $group_label .= sprintf( ' <span class="count">(%d)</span>', $group_items_count );
     446            }
     447            fwrite( $file, '<li>' );
     448            fwrite( $file, '<a href="#' . esc_attr( $group_id_attr ) . '">' . $group_label . '</a>' );
     449            fwrite( $file, '</li>' );
     450        }
     451        fwrite( $file, '</ul>' );
     452        fwrite( $file, '</div>' );
     453    }
    420454
    421455    // Now, iterate over every group in $groups and have the formatter render it in HTML.
    422456    foreach ( (array) $groups as $group_id => $group_data ) {
    423         fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data ) );
     457        fwrite( $file, wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id, $groups_count ) );
    424458    }
    425459
Note: See TracChangeset for help on using the changeset viewer.