Skip to content

Commit

Permalink
PHP 8.1: ms_cookie_constants(): prevent "passing null to non-nullable…
Browse files Browse the repository at this point in the history
…" notice

It is entirely possible for the `siteurl` option to not have a "path" component.

In PHP 8.1, this would lead to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` deprecation notice.

Changing the logic around and adding validation for the return type value of `parse_url()` prevents that.

Unfortunately, as this function is declaring global constants, adding tests for this change is not really an option without potentially affecting other tests.

Also note, that it is possible, though unlikely, for the option to not be available.
In that case though, the `get_option()` method returns `false` and while passing that to `parse_url()` does not deserve any prices for clean code, it also is not problematic (at this point in time).
  • Loading branch information
jrfnl committed Aug 13, 2021
1 parent 6e72d46 commit 9ea16c3
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/wp-includes/ms-default-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function ms_cookie_constants() {
* @since 2.6.0
*/
if ( ! defined( 'ADMIN_COOKIE_PATH' ) ) {
if ( ! is_subdomain_install() || trim( parse_url( get_option( 'siteurl' ), PHP_URL_PATH ), '/' ) ) {
$site_path = parse_url( get_option( 'siteurl' ), PHP_URL_PATH );
if ( ! is_subdomain_install() || ( isset( $site_path ) && trim( $site_path, '/' ) ) ) {
define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH );
} else {
define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . 'wp-admin' );
Expand Down

0 comments on commit 9ea16c3

Please sign in to comment.