Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

feat: enhance multilanguage handling #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ For a kirby3 site, this plugin (_omz13/xmlsitemap_) automatically generates an x
- Unpublished pages can be explicitly included based on their template; c.f. `includeUnlistedWhenTemplateIs` in _Configuration_.
- Pages made using certain templates can be excluded; c.f. `excludePageWhenTemplateIs` in _Configuration_.
- Pages with certain slugnames can be excluded; c.f. `excludePageWhenSlugIs` in _Configuration_.
- Pages of certain languages can be excluded; c.f. `excludePageWhenLanguageIs` in _Configuration_.
- Pages with a content field `excludefromxmlsitemap` that is `true` are excluded.
- Pages with a method `issunset` that returns `true` are excluded.
- Pages with a method `isunderembargo` that returns `true` are excluded.
Expand Down Expand Up @@ -71,6 +72,7 @@ The non-binding list of planned features and implementation notes are:
- [ ] `<image:license>`
- [x] Exclude image sitemap; c.f. `disableImages` **done 0.3**
- [x] Exclusion of individual pages – **done 0.2** c.f. `excludePageWhenSlugIs`
- [x] Exclusion of individual languages – **done 1.3** c.f. `excludePageWhenLanguageIs`
- [x] Exclusion of pages by template – **done 0.1** c.f. `excludePageWhenTemplateIs`
- [x] Better heuristics for `<lastmod>` (e.g. `modifiedat` field?)
- [ ] ~~Overriding of stylesheet~~
Expand Down Expand Up @@ -117,6 +119,7 @@ In your site's `site/config/config.php` the following entries prefixed with `omz
- `includeUnlistedWhenTemplateIs` : an array of template names whose pages are to be included if their status is unlisted.
- `excludePageWhenTemplateIs` : an array of templates names whose pages are to be excluded from the xml-sitemap.
- `excludePageWhenSlugIs` : an array of slug names whose pages are to be excluded from the xml-sitemap.
- `excludePageWhenLanguageIs` : an array of language codes to be excluded from the xml-sitemap.
- `excludeChildrenWhenTemplateIs` : an array of templates names whose children are to be ignored (but pages associated with the template is to be included); this is used for one-pagers (where the principal page will be included and all the 'virtual' children ignored).
- `disableImages` : a boolean which, if true, disables including data for images related to pages included in the xml-sitemap.
- `addPages` : a closure which, if present, returns a collection of `Pages` to be added. This is how you get virtual pages into the sitemap.
Expand All @@ -134,6 +137,7 @@ return [
'omz13.xmlsitemap.includeUnlistedWhenTemplateIs' => [ ],
'omz13.xmlsitemap.excludePageWhenTemplateIs' => [ 'contact','sandbox' ],
'omz13.xmlsitemap.excludePageWhenSlugIs' => [ 'form' ],
'omz13.xmlsitemap.excludePageWhenLanguageIs' => [ ],
'omz13.xmlsitemap.excludeChildrenWhenTemplateIs' => [ 'events','one-pager','shop','team','testimonials' ],
],
];
Expand All @@ -151,6 +155,7 @@ return [
'includeUnlistedWhenTemplateIs' => [ ],
'excludePageWhenTemplateIs' => ['contact','sandbox'],
'excludePageWhenSlugIs' => [ 'form' ],
'excludePageWhenLanguageIs' => [ ],
'excludeChildrenWhenTemplateIs' => [ 'events','one-pager','shop','team','testimonials' ],
'disableImages' => false,
],
Expand All @@ -173,6 +178,7 @@ return [
'omz13.xmlsitemap.excludeChildrenWhenTemplateIs' => ['events','one-pager','shop','team','testimonials'],
'omz13.xmlsitemap.excludePageWhenTemplateIs' => ['contact','sandbox'],
'omz13.xmlsitemap.excludePageWhenSlugIs' => [ 'form' ],
'omz13.xmlsitemap.excludePageWhenLanguageIs => [ ],
],
];
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"plugin",
"sitemap"
],
"version": "1.2.1",
"version": "1.3.0",
"license": "MIT",
"authors": [
{
Expand Down
1 change: 1 addition & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'includeUnlistedWhenTemplateIs' => [],
'excludePageWhenTemplateIs' => [],
'excludePageWhenSlugIs' => [],
'excludePageWhenLanguageIs' => [],
'excludeChildrenWhenTemplateIs' => [],
'disableImages' => false,
'hideuntranslated' => false,
Expand Down
31 changes: 28 additions & 3 deletions src/xmlsitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class XMLSitemap
private static $optionXCWTI; // exclude children when template is
private static $optionXPWTI; // exclude page when template is
private static $optionXPWSI; // exclude page when slug is
private static $optionXPWLI; // exclude page when language is
private static $optionNOTRA; // hide untranslated
private static $optionShimH;
public static $version = XMLSITEMAP_VERSION;
Expand Down Expand Up @@ -175,6 +176,7 @@ private static function pickupOptions() : void {
static::$optionXCWTI = static::getArrayConfigurationForKey( 'excludeChildrenWhenTemplateIs' );
static::$optionXPWTI = static::getArrayConfigurationForKey( 'excludePageWhenTemplateIs' );
static::$optionXPWSI = static::getArrayConfigurationForKey( 'excludePageWhenSlugIs' );
static::$optionXPWLI = static::getArrayConfigurationForKey( 'excludePageWhenLanguageIs' );
static::$optionNOTRA = static::getConfigurationForKey( 'hideuntranslated' );
static::$optionShimH = static::getConfigurationForKey( 'x-shimHomepage' );
}//end pickupOptions()
Expand Down Expand Up @@ -210,6 +212,7 @@ public static function getSitemap( Pages $p, bool $debug = false, ?bool $nocache
$ops .= '-' . json_encode( static::$optionIUWTI );
$ops .= '-' . json_encode( static::$optionXCWTI );
$ops .= '-' . json_encode( static::$optionXPWSI );
$ops .= '-' . json_encode( static::$optionXPWLI );
$ops .= '-' . json_encode( static::$optionXPWTI );
$ops .= '-' . json_encode( static::$optionNOTRA );
$ops .= '-' . json_encode( static::$optionShimH );
Expand Down Expand Up @@ -275,6 +278,7 @@ private static function generateSitemap( Pages $p, bool $debug = false ) : strin
$r .= '<!-- excludeChildrenWhenTemplateIs = ' . json_encode( static::$optionXCWTI ) . " -->\n";
$r .= '<!-- excludePageWhenTemplateIs = ' . json_encode( static::$optionXPWTI ) . " -->\n";
$r .= '<!-- excludePageWhenSlugIs = ' . json_encode( static::$optionXPWSI ) . " -->\n";
$r .= '<!-- excludePageWhenLanguageIs = ' . json_encode( static::$optionXPWLI ) . " -->\n";
$r .= '<!-- addPages = ' . ( static::getClosureForKey( 'addpages' ) != null ? "Closure" : "null" ) . " -->\n";
$r .= '<!-- x-shimHomepage = ' . json_encode( static::$optionShimH ) . " -->\n";
}
Expand All @@ -301,6 +305,11 @@ private static function generateSitemap( Pages $p, bool $debug = false ) : strin
$r .= ' <loc>' . kirby()->url( 'index' ) . '</loc>' . "\n";
$r .= ' <xhtml:link rel="alternate" hreflang="x-default" href="' . $homepage->urlForLanguage( kirby()->language()->code() ) . '" />' . "\n";
foreach ( kirby()->languages() as $lang ) {
if ( isset( static::$optionXPWLI ) && in_array( $lang->code(), static::$optionXPWLI ) ) {
static::addComment( $r, 'excluding because excludePageWhenLanguageIs (' . $lang->code() . ')' );
continue;
}

$r .= ' <xhtml:link rel="alternate" hreflang="' . static::getHreflangFromLocale( static::localeFromLang( $lang ) ) . '" href="' . $homepage->urlForLanguage( $lang->code() ) . '" />' . "\n";
}
$r .= '</url>' . "\n";
Expand Down Expand Up @@ -431,8 +440,19 @@ private static function addPagesToSitemap( Pages $pages, string &$r, ?string $la
continue;
}

// exclude because slug is in the exclusion list:
if ( isset( static::$optionXPWLI ) && in_array( $langcode, static::$optionXPWLI ) ) {
static::addComment( $r, 'excluding because excludePageWhenLanguageIs (' . $langcode . ')' );
continue;
}

// exclude because page content field 'excludefromxmlsitemap':
if ( $p->content()->excludefromxmlsitemap() == 'true' ) {
$excludefromxmlsitemap = $p->content()->excludefromxmlsitemap()->toBool();
if ( kirby()->languages()->findBy('code', $langcode) !== null ) {
$excludefromxmlsitemap = $p->content($langcode)->excludefromxmlsitemap()->toBool();
}

if ( $excludefromxmlsitemap ) {
static::addComment( $r, 'excluding because excludeFromXMLSitemap' );
continue;
}
Expand Down Expand Up @@ -469,6 +489,11 @@ private static function addPagesToSitemap( Pages $pages, string &$r, ?string $la
$r .= ' <xhtml:link rel="alternate" hreflang="x-default" href="' . $p->urlForLanguage( kirby()->language()->code() ) . '" />' . "\n";
// localized languages: <xhtml:link rel="alternate" hreflang="en" href="http://www.example.com/"/>
foreach ( kirby()->languages() as $l ) {
if ( isset( static::$optionXPWLI ) && in_array( $l->code(), static::$optionXPWLI ) ) {
static::addComment( $r, 'excluding because excludePageWhenLanguageIs (' . $l->code() . ')' );
continue;
}

if ( static::$optionNOTRA == true && ! $p->translation( $l->code() )->exists() ) {
$r .= ' <!-- no translation for hreflang="' . $l->code() . '" -->' . "\n";
} else {
Expand Down Expand Up @@ -582,12 +607,12 @@ private static function getHreflangFromLocale( string $locale ) : string {
// Clean (.whatever@whatever)
$x = strrpos( $locale, '.', 0 );
if ( $x != false ) {
$locale = substr( $locale, 0, -$x );
$locale = substr( $locale, 0, $x );
}
// More clean (just in case @whatever)
$y = strrpos( $locale, '@', 0 );
if ( $y != false ) {
$locale = substr( $locale, 0, -$y );
$locale = substr( $locale, 0, $y );
}
// Huzzah! $locale is now sanitized (which is not the same as canonicalization)
// Ensure hyphens not underscores
Expand Down