plausible / wordpress

Plausible Analytics WordPress plugin
https://plausible.io/wordpress-analytics-plugin
MIT License
50 stars 26 forks source link

Invalid handling of non-root home url #199

Closed FlyingDR closed 3 months ago

FlyingDR commented 4 months ago

At this moment plugin incorrectly handles domain name extraction in case a non-root path is used as a home. \Plausible\Analytics\WP\Helpers::get_domain() for some reason tries to use a regular expression instead of using the standard parse_url() function:

https://github.com/plausible/wordpress/blob/18bc986f75baffe97fdf7868afce7193b0ef056e/src/Helpers.php#L261

Regular expression completely ignores the fact that there may be something after the domain name itself, effectively generating the wrong result. The most common scenario is use of the multilanguage plugins like WPML, see #160 for examples.

Since Plausible analytics strictly expects hostname for the domain name it is currently not possible to use Plausible analytics for multilingual sites. Any registration attempt results in "Oops! The API token you used is invalid" notification and attempt to use "create an API token" link causes issue because it takes invalid URL for Plausible because the value for domain is taken from the form.

Correct implementation would be:

return preg_replace('/^www\./i', '', parse_url($url, PHP_URL_HOST));
Dan0sz commented 3 months ago

Thanks for your thorough report!

I've investigated the issue, and the actual issue was that the API client would always use the hosted domain stored in the DB, resulting in the API token invalid error message. In this commit any submitted options in POST are grabbed when get_settings() is called.

Also, Plausible has supported domain names with a subdirectory structure (i.e. multilingual sites) for a while now. So parsing just the hostname is no longer needed.