wordpress icon indicating copy to clipboard operation
wordpress copied to clipboard

Invalid handling of non-root home url

Open FlyingDR opened this issue 1 year ago • 0 comments

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));

FlyingDR avatar Apr 12 '24 14:04 FlyingDR