Make WordPress Core


Ignore:
Timestamp:
03/29/2021 07:35:36 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Check if the _export_data_grouped post meta is an array when generating a personal data export file.

This avoids a fatal error on PHP 8 in wp_privacy_generate_personal_data_export_file() if the _export_data_grouped post meta exists but is not an array.

Additionally, refactor unit tests for the function to:

  • Reduce redundant code
  • Switch to data provider
  • Test on the full HTML output instead of select pieces of the output
  • Expand unhappy path coverage

Follow-up to [43012], [44786], [47146], [47278].

Props hellofromTonya, jrf, xknown.
See #51423.

File:
1 edited

Legend:

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

    r50230 r50613  
    362362        $email_address
    363363    );
    364 
    365     // And now, all the Groups.
    366     $groups = get_post_meta( $request_id, '_export_data_grouped', true );
    367364
    368365    // First, build an "About" group on the fly for this report.
     
    394391    );
    395392
    396     // Merge in the special about group.
    397     $groups = array_merge( array( 'about' => $about_group ), $groups );
    398 
    399     $groups_count = count( $groups );
     393    // And now, all the Groups.
     394    $groups = get_post_meta( $request_id, '_export_data_grouped', true );
     395    if ( is_array( $groups ) ) {
     396        // Merge in the special "About" group.
     397        $groups       = array_merge( array( 'about' => $about_group ), $groups );
     398        $groups_count = count( $groups );
     399    } else {
     400        if ( false !== $groups ) {
     401            _doing_it_wrong(
     402                __FUNCTION__,
     403                /* translators: %s: Post meta key. */
     404                sprintf( __( 'The %s post meta must be an array.' ), '<code>_export_data_grouped</code>' ),
     405                '5.8.0'
     406            );
     407        }
     408
     409        $groups       = null;
     410        $groups_count = 0;
     411    }
    400412
    401413    // Convert the groups to JSON format.
Note: See TracChangeset for help on using the changeset viewer.