Ticket #47509: 45709.4.diff
File 45709.4.diff, 5.4 KB (added by , 5 years ago) |
---|
-
src/wp-includes/user.php
From 17d4add0774adfebaf25c485737f553529f55351 Mon Sep 17 00:00:00 2001 From: Paul Biron <paul@sparrowhawkcomputing.com> Date: Sat, 25 Jan 2020 13:34:44 -0700 Subject: [PATCH] add a filter in wp_user_personal_data_exporter() to make it easier to include additional user meta in a personal data export. Includes improved unit test. --- src/wp-includes/user.php | 49 ++++++++++++++++++++- tests/phpunit/tests/user.php | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index a1478679f6..f84bade569 100644
a b function wp_user_personal_data_exporter( $email_address ) { 3003 3003 } 3004 3004 } 3005 3005 3006 // get the list of reserved names. 3007 $reserved_names = array_values( $user_prop_to_export ); 3008 3009 /** 3010 * Filter to extend the Users personal data for the privacy exporter. 3011 * 3012 * @since 5.4.0 3013 * 3014 * @param array $additional_user_data { 3015 * An array of name-value pairs of additional user data items. Default: the empty array. 3016 * 3017 * @type string $name The user-facing name of an item name-value pair, e.g. 'IP Address'. 3018 * @type string $value The user-facing value of an item data pair, e.g. '50.60.70.0'. 3019 * } 3020 * @param WP_User $user The user whose data is being exported. 3021 * @param string[] $reserved_names An array of reserved names. Any item in 3022 * `$additional_user_data` that uses one of these 3023 * for it's `name` will not be included in the export. 3024 */ 3025 $_extra_data = apply_filters( 'wp_privacy_additional_user_data', array(), $user, $reserved_names ); 3026 3027 if ( is_array( $_extra_data ) && ! empty( $_extra_data ) ) { 3028 // remove items that use reserved names. 3029 $extra_data = array_filter( 3030 $_extra_data, 3031 function( $item ) use ( $reserved_names ) { 3032 return ! in_array( $item['name'], $reserved_names ); 3033 } 3034 ); 3035 3036 if ( count( $extra_data ) !== count( $_extra_data ) ) { 3037 _doing_it_wrong( 3038 __FUNCTION__, 3039 sprintf( 3040 /* translators: %s: wp_privacy_additional_user_data */ 3041 __( 'Filter %s returned items with reserved names.' ), 3042 '<code>wp_privacy_additional_user_data</code>' 3043 ), 3044 '5.4.0' 3045 ); 3046 } 3047 3048 if ( ! empty( $extra_data ) ) { 3049 $user_data_to_export = array_merge( $user_data_to_export, $extra_data ); 3050 } 3051 } 3052 3006 3053 $data_to_export[] = array( 3007 3054 'group_id' => 'user', 3008 3055 'group_label' => __( 'User' ), 3009 'group_description' => __( 'User’s profiledata.' ),3056 'group_description' => __( 'User’s meta data.' ), 3010 3057 'item_id' => "user-{$user->ID}", 3011 3058 'data' => $user_data_to_export, 3012 3059 ); -
tests/phpunit/tests/user.php
diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 74c533b69d..123d84afc7 100644
a b class Tests_User extends WP_UnitTestCase { 1671 1671 // Number of exported user properties. 1672 1672 $this->assertSame( 11, count( $actual['data'][0]['data'] ) ); 1673 1673 } 1674 1675 /** 1676 * Testing the `wp_privacy_additional_user_data` filter works. 1677 * 1678 * @since 5.4.0 1679 * 1680 * @ticket 47509 1681 */ 1682 function test_filter_wp_privacy_additional_user_data() { 1683 $test_user = new WP_User( self::$contrib_id ); 1684 1685 // _doing_wrong() should be called because the filter callback 1686 // adds a item with a 'name' that is the same as one generated by core. 1687 $this->setExpectedIncorrectUsage( 'wp_user_personal_data_exporter' ); 1688 add_filter( 'wp_privacy_additional_user_data', array( $this, 'export_additional_user_data' ) ); 1689 1690 $actual = wp_user_personal_data_exporter( $test_user->user_email ); 1691 1692 remove_filter( 'wp_privacy_additional_user_data', array( $this, 'export_additional_user_data' ) ); 1693 1694 $this->assertTrue( $actual['done'] ); 1695 1696 // Number of exported users. 1697 $this->assertSame( 1, count( $actual['data'] ) ); 1698 1699 // Number of exported user properties (the 11 core properties, 1700 // plus 1 additional from the filter). 1701 $this->assertSame( 12, count( $actual['data'][0]['data'] ) ); 1702 1703 // check that the duplicate 'name' => 'User ID' was stripped. 1704 $this->assertSame( 1705 1, 1706 count( 1707 wp_list_filter( 1708 $actual['data'][0]['data'], 1709 array( 1710 'name' => 'User ID', 1711 ) 1712 ) 1713 ) 1714 ); 1715 1716 // check that the item added by the filter was retained. 1717 $this->assertSame( 1718 1, 1719 count( 1720 wp_list_filter( 1721 $actual['data'][0]['data'], 1722 array( 1723 'name' => 'Test Additional Data Name', 1724 'value' => 'Test Additional Data Value' 1725 ) 1726 ) 1727 ) 1728 ); 1729 } 1730 1731 /** 1732 * Filter callback to add additional data to the User Group on Export Requests. 1733 * 1734 * This callback should generate a `_doing_it_wrong()`. 1735 * 1736 * @since 5.4.0 1737 * 1738 * @ticket 47509 1739 * 1740 * @return array $additional_data The additional User data. 1741 */ 1742 public function export_additional_user_data() { 1743 $additional_data = array( 1744 // this item should be stripped out by wp_user_personal_data_exporter() 1745 // because it's 'name' duplicates one exported by core. 1746 array( 1747 'name' => 'User ID', 1748 'value' => 'Some User ID', 1749 ), 1750 // this item should be retained and included in the export. 1751 array( 1752 'name' => 'Test Additional Data Name', 1753 'value' => 'Test Additional Data Value', 1754 ), 1755 ); 1756 1757 return $additional_data; 1758 } 1674 1759 }