From 69a7063a49c355c18c920c7f924f5b5d5b5e7da8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 29 Mar 2021 19:37:04 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@50613 git-svn-id: http://core.svn.wordpress.org/trunk@50226 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/privacy-tools.php | 24 ++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/wp-admin/includes/privacy-tools.php b/wp-admin/includes/privacy-tools.php index 472c3de9ba..3f69366020 100644 --- a/wp-admin/includes/privacy-tools.php +++ b/wp-admin/includes/privacy-tools.php @@ -362,9 +362,6 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) { $email_address ); - // And now, all the Groups. - $groups = get_post_meta( $request_id, '_export_data_grouped', true ); - // First, build an "About" group on the fly for this report. $about_group = array( /* translators: Header for the About section in a personal data export. */ @@ -393,10 +390,25 @@ function wp_privacy_generate_personal_data_export_file( $request_id ) { ), ); - // Merge in the special about group. - $groups = array_merge( array( 'about' => $about_group ), $groups ); + // And now, all the Groups. + $groups = get_post_meta( $request_id, '_export_data_grouped', true ); + if ( is_array( $groups ) ) { + // Merge in the special "About" group. + $groups = array_merge( array( 'about' => $about_group ), $groups ); + $groups_count = count( $groups ); + } else { + if ( false !== $groups ) { + _doing_it_wrong( + __FUNCTION__, + /* translators: %s: Post meta key. */ + sprintf( __( 'The %s post meta must be an array.' ), '_export_data_grouped' ), + '5.8.0' + ); + } - $groups_count = count( $groups ); + $groups = null; + $groups_count = 0; + } // Convert the groups to JSON format. $groups_json = wp_json_encode( $groups ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 84b1e260a0..75d422b9cf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50612'; +$wp_version = '5.8-alpha-50613'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.