Make WordPress Core

Changeset 51801

09/10/2021 05:42:36 PM (21 months ago)

Code Modernization: Fix "passing null to non-nullable" deprecation notices in WP_Http::normalize_cookies().

The Requests_Cookie class expects valid - non-null - attributes to be passed, either as an array or as a Requests_Utility_CaseInsensitiveDictionary object.

However, the WP_Http_Cookie::get_attributes() explicitly sets the expires, path and domain index keys in an array with values which _may_ be null. This will cause strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated-like errors when the attributes are passed to the Requests_Cookie class.

Note: a null value for path would generate a similar deprecation notice, but for the preg_match() function.

Fixed by using array_filter() on the attributes to explicitly filter out null values before passing the attributes to Requests_Cookie.

Note: I'm choosing to explicitly only filter null values. Using array_filter() without a callback would filter out all "empty" values, but that may also remove values which are explicitly set to false or 0, which may be valid values.

Fixes two errors in the external-http group in the WordPress Core test suite:

1) Tests_HTTP_Functions::test_get_response_cookies_with_wp_http_cookie_object
strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated


2) Tests_HTTP_Functions::test_get_cookie_host_only
strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated


Follow-up to [38164], [45135], [51657].

Props jrf, hellofromTonya.
See #53635.

1 edited


  • trunk/src/wp-includes/class-http.php

    r51301 r51801  
    460460        foreach ( $cookies as $name => $value ) {
    461461            if ( $value instanceof WP_Http_Cookie ) {
    462                 $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $value->get_attributes(), array( 'host-only' => $value->host_only ) );
     462                $attributes                 = array_filter(
     463                    $value->get_attributes(),
     464                    static function( $attr ) {
     465                        return null !== $attr;
     466                    }
     467                );
     468                $cookie_jar[ $value->name ] = new Requests_Cookie( $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) );
    463469            } elseif ( is_scalar( $value ) ) {
    464470                $cookie_jar[ $name ] = new Requests_Cookie( $name, $value );
Note: See TracChangeset for help on using the changeset viewer.