Make WordPress Core

Changeset 47270


Ignore:
Timestamp:
02/11/2020 06:57:51 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Privacy: Introduce wp_privacy_additional_user_data filter to make it easier to include additional user meta in a personal data export.

Props pbiron, xkon, garrett-eclipse, azaozz.
Fixes #47509.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r47245 r47270  
    30073007        }
    30083008
     3009        // Get the list of reserved names.
     3010        $reserved_names = array_values( $user_props_to_export );
     3011
     3012        /**
     3013         * Filter to extend the Users profile data for the privacy exporter.
     3014         *
     3015         * @since 5.4.0
     3016         *
     3017         * @param array    $additional_user_profile_data {
     3018         *     An array of name-value pairs of additional user data items. Default empty array.
     3019         *
     3020         *     @type string $name  The user-facing name of an item name-value pair,e.g. 'IP Address'.
     3021         *     @type string $value The user-facing value of an item data pair, e.g. '50.60.70.0'.
     3022         * }
     3023         * @param WP_User  $user           The user whose data is being exported.
     3024         * @param string[] $reserved_names An array of reserved names. Any item in `$additional_user_data`
     3025         *                                 that uses one of these for its `name` will not be included in the export.
     3026         */
     3027        $_extra_data = apply_filters( 'wp_privacy_additional_user_profile_data', array(), $user, $reserved_names );
     3028
     3029        if ( is_array( $_extra_data ) && ! empty( $_extra_data ) ) {
     3030                // Remove items that use reserved names.
     3031                $extra_data = array_filter(
     3032                        $_extra_data,
     3033                        function( $item ) use ( $reserved_names ) {
     3034                                return ! in_array( $item['name'], $reserved_names );
     3035                        }
     3036                );
     3037
     3038                if ( count( $extra_data ) !== count( $_extra_data ) ) {
     3039                        _doing_it_wrong(
     3040                                __FUNCTION__,
     3041                                sprintf(
     3042                                        /* translators: %s: wp_privacy_additional_user_profile_data */
     3043                                        __( 'Filter %s returned items with reserved names.' ),
     3044                                        '<code>wp_privacy_additional_user_profile_data</code>'
     3045                                ),
     3046                                '5.4.0'
     3047                        );
     3048                }
     3049
     3050                if ( ! empty( $extra_data ) ) {
     3051                        $user_data_to_export = array_merge( $user_data_to_export, $extra_data );
     3052                }
     3053        }
     3054
    30093055        $data_to_export[] = array(
    30103056                'group_id'          => 'user',
  • trunk/tests/phpunit/tests/user.php

    r47237 r47270  
    17951795                $this->assertEquals( 'January 29, 2020 09:13 AM', $actual['data'][1]['data'][3]['value'] );
    17961796        }
     1797
     1798        /**
     1799         * Testing the `wp_privacy_additional_user_profile_data` filter works.
     1800         *
     1801         * @ticket 47509
     1802         */
     1803        function test_filter_wp_privacy_additional_user_profile_data() {
     1804                $test_user = new WP_User( self::$contrib_id );
     1805
     1806                add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1807
     1808                $actual = wp_user_personal_data_exporter( $test_user->user_email );
     1809
     1810                remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1811
     1812                $this->assertTrue( $actual['done'] );
     1813
     1814                // Number of exported users.
     1815                $this->assertSame( 1, count( $actual['data'] ) );
     1816
     1817                // Number of exported user properties (the 11 core properties,
     1818                // plus 1 additional from the filter).
     1819                $this->assertSame( 12, count( $actual['data'][0]['data'] ) );
     1820
     1821                // Check that the item added by the filter was retained.
     1822                $this->assertSame(
     1823                        1,
     1824                        count(
     1825                                wp_list_filter(
     1826                                        $actual['data'][0]['data'],
     1827                                        array(
     1828                                                'name'  => 'Test Additional Data Name',
     1829                                                'value' => 'Test Additional Data Value',
     1830                                        )
     1831                                )
     1832                        )
     1833                );
     1834
     1835                // _doing_wrong() should be called because the filter callback
     1836                // adds a item with a 'name' that is the same as one generated by core.
     1837                $this->setExpectedIncorrectUsage( 'wp_user_personal_data_exporter' );
     1838                add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data_with_dup_name' ) );
     1839
     1840                $actual = wp_user_personal_data_exporter( $test_user->user_email );
     1841
     1842                remove_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'export_additional_user_profile_data' ) );
     1843
     1844                $this->assertTrue( $actual['done'] );
     1845
     1846                // Number of exported users.
     1847                $this->assertSame( 1, count( $actual['data'] ) );
     1848
     1849                // Number of exported user properties
     1850                // (the 11 core properties, plus 1 additional from the filter).
     1851                $this->assertSame( 12, count( $actual['data'][0]['data'] ) );
     1852
     1853                // Check that the duplicate 'name' => 'User ID' was stripped.
     1854                $this->assertSame(
     1855                        1,
     1856                        count(
     1857                                wp_list_filter(
     1858                                        $actual['data'][0]['data'],
     1859                                        array(
     1860                                                'name' => 'User ID',
     1861                                        )
     1862                                )
     1863                        )
     1864                );
     1865
     1866                // Check that the item added by the filter was retained.
     1867                $this->assertSame(
     1868                        1,
     1869                        count(
     1870                                wp_list_filter(
     1871                                        $actual['data'][0]['data'],
     1872                                        array(
     1873                                                'name'  => 'Test Additional Data Name',
     1874                                                'value' => 'Test Additional Data Value',
     1875                                        )
     1876                                )
     1877                        )
     1878                );
     1879        }
     1880
     1881        /**
     1882         * Filter callback to add additional profile data to the User Group on Export Requests.
     1883         *
     1884         * @ticket 47509
     1885         *
     1886         * @return array $additional_profile_data The additional user data.
     1887         */
     1888        public function export_additional_user_profile_data() {
     1889                $additional_profile_data = array(
     1890                        // This item should be retained and included in the export.
     1891                        array(
     1892                                'name'  => 'Test Additional Data Name',
     1893                                'value' => 'Test Additional Data Value',
     1894                        ),
     1895                );
     1896
     1897                return $additional_profile_data;
     1898        }
     1899
     1900        /**
     1901         * Filter callback to add additional profile data to the User Group on Export Requests.
     1902         *
     1903         * This callback should generate a `_doing_it_wrong()`.
     1904         *
     1905         * @ticket 47509
     1906         *
     1907         * @return array $additional_profile_data The additional user data.
     1908         */
     1909        public function export_additional_user_profile_data_with_dup_name() {
     1910                $additional_profile_data = array(
     1911                        // This item should be stripped out by wp_user_personal_data_exporter()
     1912                        // because it's 'name' duplicates one exported by core.
     1913                        array(
     1914                                'name'  => 'User ID',
     1915                                'value' => 'Some User ID',
     1916                        ),
     1917                        // This item should be retained and included in the export.
     1918                        array(
     1919                                'name'  => 'Test Additional Data Name',
     1920                                'value' => 'Test Additional Data Value',
     1921                        ),
     1922                );
     1923
     1924                return $additional_profile_data;
     1925        }
    17971926}
Note: See TracChangeset for help on using the changeset viewer.