google / google/site-kit-wp

Creating a Tag Manager container raises a PHP 8.1 deprecation notice when the container name isn't a string

Open
#7,809 11 comments 0 reactions 0 assignees View on GitHub
Next Up P1 PHP Type: Bug
Dominant language
JavaScript
Stars
1.4k
Forks
383
Avg merge
4d 12h
Merged PRs (30d)
80

Description

## Bug Description

https://github.com/google/site-kit-wp/blob/6b5733abdc43beb0e319f93aebf80e255d9ac435/includes/Modules/Tag_Manager.php#L253-L257

`sanitize_container_name()` receives the container name from `get_bloginfo( 'name' ) ?: URL::parse( $this->context->get_reference_site_url(), PHP_URL_HOST )`. `URL::parse()` returns `null` for a URL with no host. On a site with an empty **Site Title**, the name reaches `trim()` as `null`, and PHP 8.1 throws a deprecation notice:

```
Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated
```

`trim()` returns a string, so the `ltrim()` call below it receives an empty string and throws nothing.

`sanitize_container_name()` should check that the value is a string before it calls `trim()`.

## Steps to reproduce

1. On PHP 8.1, set `WP_DEBUG` and `WP_DEBUG_LOG` to `true` in `wp-config.php`.
2. Clear the **Site Title** field in **Settings > General** and save.
3. Add a `googlesitekit_site_url` filter that returns a value with no host, such as `example.com`.
4. Empty `wp-content/debug.log`.
5. Connect Tag Manager and create a new web container.
6. Read `wp-content/debug.log`.

Expected: Site Kit creates the container, and no deprecation notice names the Tag Manager module.

## Additional Context

- PHP Version: 8.1.29
- Plugin Version: 1.185.0

---------------

_Do not alter or remove anything below. The following sections will be managed by moderators only._

## Acceptance criteria

* On PHP 8.1 or later, Site Kit raises no PHP deprecation notice when a Tag Manager container name isn't a string.
* Following the **steps to reproduce** creates the Tag Manager container, and the PHP error log holds no deprecation notice from Site Kit.
* A Tag Manager container name that's already a string comes out exactly as it does today. Site Kit still cuts surrounding whitespace, removes leading underscores and accents, decodes HTML entities, collapses repeated spaces, and keeps only the characters `a-zA-Z0-9_., -`.

## Implementation Brief

* [ ]

### Test Coverage

*

## QA Brief

*

## Changelog entry

*

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.