Dolphiq / craft3-plugin-sitemap

A craft 3 plugin that provides an easy way to enable and manage an XML sitemap for search engines like Google
MIT License
28 stars 18 forks source link

Invalid site ID: 2 #61

Open amityweb opened 3 years ago

amityweb commented 3 years ago

We have disabled "Enable on the front end" for a site, because it is not live whilst waiting for the translations. It is site ID 2.

But the xml sitemap plugin is still trying to generate a sitemap for it, and as such, returns an error due to the UrlHelper::siteUrl in your plugin returning an invalid site id.

I assume your code is not checking if a site is enabled on front end or not.

Is it possible this can be fixed, because we are unable to generate a sitemap now, not until we enable the other site.

Thanks

amityweb commented 3 years ago

I would say in function getUrl in dolphiq/sitemap/src/controllers/SitemapController.php in the following function check UrlHelper::siteUrl is not null and is valid before returning it?

    private function getUrl($uri, $siteId)
    {
        if ($uri !== null) {
            $path = ($uri === '__home__') ? '' : $uri;
            return UrlHelper::siteUrl($path, null, null, $siteId);
        }

        return null;
    }

OR maybe its a problem in SitemapController.php getUrl() because in fact if getUrl() which is $alternateLoc === null you have continue instead of stop.

            $dateUpdated = strtotime($item['dateUpdated']);
            $url->appendChild($dom->createElement('lastmod', date('Y-m-d\TH:i:sP', $dateUpdated)));
            if ($item['alternateLinkCount'] > 1) {
                $alternateLinks = $this->_createAlternateSectionQuery($item['elementId'])->all();
                if (count($alternateLinks) > 0) {
                    foreach ($alternateLinks as $alternateItem) {
                        $alternateLoc = $this->getUrl($alternateItem['uri'], $alternateItem['siteId']);
                        if ($alternateLoc === null) {
                            continue;
                        }

                        $alternateLink = $dom->createElementNS('http://www.w3.org/1999/xhtml', 'xhtml:link');
                        $alternateLink->setAttribute('rel', 'alternate')
amityweb commented 3 years ago

Actually its even before the continue... Craft reports invalid siteId in URL helper, before the continue, so your plugin cant even call UrlHelper::siteUrl if the siteId is invalid

amityweb commented 3 years ago

This is how I fixed it. In function getUrl in SitemapController line 184:

private function getUrl($uri, $siteId)
{
    $site = Craft::$app->sites->getSiteById($siteId);
    if($site)
    {          
        if ($uri !== null)
        {
            $path = ($uri === '__home__') ? '' : $uri;
            return UrlHelper::siteUrl($path, null, null, $siteId);
        }
    }
    return null;
}