Skip to content

Commit

Permalink
Support passing ?site=<siteHandle> to sitemap URLs for headless sites
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jun 5, 2023
1 parent 67ce1f1 commit e97a94b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.7.5 - 2023-06-05
### Added
- Support passing `?site=<siteHandle>` to sitemap URLs for headless sites

## 3.7.4 - 2021-06-14
### Fixed
- Fix error when trying to fetch social image (Fixes #358, via @MDXDave)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ether/seo",
"license": "MIT",
"description": "SEO utilities including a unique field type, sitemap, & redirect manager",
"version": "3.7.4",
"version": "3.7.5",
"type": "craft-plugin",
"minimum-stability": "dev",
"require": {
Expand Down
11 changes: 10 additions & 1 deletion src/controllers/sitemap/XmlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class XmlController extends Controller

protected $allowAnonymous = true;

public function beforeAction($action)
{
$siteHandle = \Craft::$app->getRequest()->getQueryParam("site");
if (!empty($siteHandle))
\Craft::$app->sites->setCurrentSite($siteHandle);

return parent::beforeAction($action);
}

/**
* @throws \yii\base\Exception
*/
Expand Down Expand Up @@ -43,4 +52,4 @@ private function _asXml ($data)
return $response;
}

}
}
29 changes: 29 additions & 0 deletions src/gql/SitemapIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ether\seo\gql;

use craft\gql\GqlEntityRegistry;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

class SitemapIndex extends \craft\gql\base\ObjectType
{
public static function getName(): string
{
return 'Ether_' . (new \ReflectionClass(static::class))->getShortName();
}

public static function getType(): Type
{
if ($type = GqlEntityRegistry::getEntity(static::class))
return $type;

return GqlEntityRegistry::createEntity(static::class, new ObjectType([
'name' => static::getName(),
'fields' => [
'loc' => Type::string(),
'lastmod' => Type::string(),
],
]));
}
}
7 changes: 4 additions & 3 deletions src/services/SitemapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function core (array $variables)
$type = Category::instance();
$idHandle = 'groupId';
break;

case 'productTypes':
$type = \craft\commerce\elements\Product::instance();
$idHandle = 'typeId';
Expand Down Expand Up @@ -618,7 +618,7 @@ private function _generateIndex ($group, $id)
case 'productTypes':
$last = $this->_getUpdated(\craft\commerce\elements\Product::instance(), $id);
$pages = $this->_getPageCount(\craft\commerce\elements\Product::instance(), $id);
break;
break;

default:
$last = DateTimeHelper::currentUTCDateTime()->format('c');
Expand Down Expand Up @@ -652,12 +652,13 @@ private function _generateIndex ($group, $id)
private function _indexUrl ($group, $id, $page)
{
$sitemapName = Seo::$i->getSettings()->sitemapName;
$siteHandle = \Craft::$app->getRequest()->getQueryParam("site");

return UrlHelper::siteUrl(
$sitemapName . '_' . $group
. ($id > 0 ? '_' . $id : '')
. ($id > 0 ? '_' . $page : '')
. '.xml'
. '.xml' . (empty($siteHandle) ? '' : "?site=$siteHandle")
);
}

Expand Down

0 comments on commit e97a94b

Please sign in to comment.