From 4c0c8b703dcc5e6649dd4de3fbfe5fb7431d9e2d Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:03:39 +0200 Subject: [PATCH 1/8] Added support for protocol relative URLs --- .../model/googlesitemap/googlesitemap.class.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index 2a935f4..d5159d2 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -60,6 +60,7 @@ function __construct(modX &$modx,array $config = array()) { 'excludeChildrenOf' => '', 'showHidden' => false, 'priorityTV' => 0, + 'includeUrlProto' => false ),$config); } @@ -100,7 +101,7 @@ public function run($currentParent,$selfId = -1,$depth = 0) { } if ($canParse) { - $url = $this->modx->makeUrl($id,'','','full'); + $url = $this->makeUrl($id); $date = $child->get('editedon') ? $child->get('editedon') : $child->get('createdon'); $date = date("Y-m-d", strtotime($date)); @@ -274,6 +275,18 @@ private function _getTplChunk($name,$postFix = '.chunk.tpl') { } return $chunk; } + + private function makeUrl($id) { + $url = $this->modx->makeUrl($id, '', '', 'full'); + if ($this->config['includeUrlProto'] && strpos($url, '//') === 0) { + $proto = 'http:'; + if ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS'])) || $_SERVER['SERVER_PORT'] == 443) { + $proto = 'https:'; + } + $url = $proto . $url; + } + return $url; + } } From 77e01ccf78ab5066285182b007080fa36a9729dd Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:08:32 +0200 Subject: [PATCH 2/8] Added support for Babel alternate-hreflang links --- .../elements/chunks/gcontainer.chunk.tpl | 2 +- .../elements/chunks/gitem.chunk.tpl | 1 + .../googlesitemap/googlesitemap.class.php | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/core/components/googlesitemap/elements/chunks/gcontainer.chunk.tpl b/core/components/googlesitemap/elements/chunks/gcontainer.chunk.tpl index b192006..fa30fae 100644 --- a/core/components/googlesitemap/elements/chunks/gcontainer.chunk.tpl +++ b/core/components/googlesitemap/elements/chunks/gcontainer.chunk.tpl @@ -1,3 +1,3 @@ - + [[+items]] \ No newline at end of file diff --git a/core/components/googlesitemap/elements/chunks/gitem.chunk.tpl b/core/components/googlesitemap/elements/chunks/gitem.chunk.tpl index 7826883..2690476 100644 --- a/core/components/googlesitemap/elements/chunks/gitem.chunk.tpl +++ b/core/components/googlesitemap/elements/chunks/gitem.chunk.tpl @@ -3,4 +3,5 @@ [[+date]] [[+update]] [[+priority]] + [[+links]] \ No newline at end of file diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index d5159d2..3a420da 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -127,12 +127,17 @@ public function run($currentParent,$selfId = -1,$depth = 0) { $priority = $priorityTV; } } + $links = ''; + if ($this->config['includeBabelLinks']) { + $links = $this->getBabelLinks($child); + } /* add item to output */ $output .= $this->getChunk($this->config['itemTpl'],array( 'url' => $url, 'date' => $date, 'update' => $update, 'priority' => $priority, + 'links' => $links )).$this->config['itemSeparator']; } @@ -287,6 +292,43 @@ private function makeUrl($id) { } return $url; } + + private function getBabelTvName() { + $babelTvName = null; + $babelNamespace = $this->modx->getObject('modNamespace', array('name' => 'babel')); + if (!empty($babelNamespace)) { + $babelTvName = $this->modx->getOption('babel.babelTvName'); + } + return $babelTvName; + } + + private function getBabelLinks($doc) { + $output = ''; + $babelTvName = $this->getBabelTvName(); + if (!empty($babelTvName)) { + $babelLinks = $doc->getTVValue($babelTvName); + if (!empty($babelLinks)) { + $output = array(); + $babelLinks = explode(';', $babelLinks); + foreach ($babelLinks as $babelLink) { + list($babelLinkContext, $babelLinkId) = explode(':', $babelLink); + // // prevent link loops + // if ($doc->id == $babelLinkId) { + // continue; + // } + $babelLinkContext = $this->modx->getContext($babelLinkContext); + if (!empty($babelLinkContext)) { + $babelLinkLang = $babelLinkContext->getOption('cultureKey'); + $babelLinkUrl = $this->makeUrl($babelLinkId); + $babelLinkXml = ''; + $output[] = sprintf($babelLinkXml, $babelLinkLang, $babelLinkUrl); + } + } + $output = (!empty($output) ? implode("\n\t", $output) : ''); + } + } + return $output; + } } From 6c2db6921824a3e3669ea73c562c577d1a733d4a Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:09:36 +0200 Subject: [PATCH 3/8] Removed comment --- .../googlesitemap/model/googlesitemap/googlesitemap.class.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index 3a420da..9b19bd6 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -312,10 +312,6 @@ private function getBabelLinks($doc) { $babelLinks = explode(';', $babelLinks); foreach ($babelLinks as $babelLink) { list($babelLinkContext, $babelLinkId) = explode(':', $babelLink); - // // prevent link loops - // if ($doc->id == $babelLinkId) { - // continue; - // } $babelLinkContext = $this->modx->getContext($babelLinkContext); if (!empty($babelLinkContext)) { $babelLinkLang = $babelLinkContext->getOption('cultureKey'); From 09ab2fae70d7cdf657229e47ace40b412e64b82b Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:20:12 +0200 Subject: [PATCH 4/8] Refactored alternate-hreflang XML into chunk --- .../googlesitemap/elements/chunks/gitemlink.chunk.tpl | 1 + .../model/googlesitemap/googlesitemap.class.php | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl diff --git a/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl new file mode 100644 index 0000000..3bbd14e --- /dev/null +++ b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index 9b19bd6..c3e7551 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -53,6 +53,7 @@ function __construct(modX &$modx,array $config = array()) { 'templateFilter' => 'id', 'itemSeparator' => "\n", 'itemTpl' => 'gItem', + 'itemLinkTpl' => 'gItemLink', 'chunksPath' => $corePath.'elements/chunks/', 'maxDepth' => 0, 'excludeResources' => '', @@ -315,9 +316,11 @@ private function getBabelLinks($doc) { $babelLinkContext = $this->modx->getContext($babelLinkContext); if (!empty($babelLinkContext)) { $babelLinkLang = $babelLinkContext->getOption('cultureKey'); - $babelLinkUrl = $this->makeUrl($babelLinkId); - $babelLinkXml = ''; - $output[] = sprintf($babelLinkXml, $babelLinkLang, $babelLinkUrl); + $babelLinkUrl = $this->makeUrl($babelLinkId); + $output[] = $this->getChunk($this->config['itemLinkTpl'], array( + 'hreflang' => $babelLinkLang, + 'href' => $babelLinkUrl + )); } } $output = (!empty($output) ? implode("\n\t", $output) : ''); From bd21b94c8cb6b1343744860ed233f5c9652d55a4 Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:21:22 +0200 Subject: [PATCH 5/8] Added newline --- .../googlesitemap/elements/chunks/gitemlink.chunk.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl index 3bbd14e..6b68334 100644 --- a/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl +++ b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl @@ -1 +1 @@ - \ No newline at end of file + From b67ece11a0b1e096da8fc38ad54c19f444b0c500 Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:22:28 +0200 Subject: [PATCH 6/8] Converted tabs to spaces --- .../model/googlesitemap/googlesitemap.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index c3e7551..37df43e 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -53,7 +53,7 @@ function __construct(modX &$modx,array $config = array()) { 'templateFilter' => 'id', 'itemSeparator' => "\n", 'itemTpl' => 'gItem', - 'itemLinkTpl' => 'gItemLink', + 'itemLinkTpl' => 'gItemLink', 'chunksPath' => $corePath.'elements/chunks/', 'maxDepth' => 0, 'excludeResources' => '', @@ -316,11 +316,11 @@ private function getBabelLinks($doc) { $babelLinkContext = $this->modx->getContext($babelLinkContext); if (!empty($babelLinkContext)) { $babelLinkLang = $babelLinkContext->getOption('cultureKey'); - $babelLinkUrl = $this->makeUrl($babelLinkId); - $output[] = $this->getChunk($this->config['itemLinkTpl'], array( - 'hreflang' => $babelLinkLang, - 'href' => $babelLinkUrl - )); + $babelLinkUrl = $this->makeUrl($babelLinkId); + $output[] = $this->getChunk($this->config['itemLinkTpl'], array( + 'hreflang' => $babelLinkLang, + 'href' => $babelLinkUrl + )); } } $output = (!empty($output) ? implode("\n\t", $output) : ''); From f7f7b99bf46fac71593fc149e83a61947dc15f38 Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 13:32:25 +0200 Subject: [PATCH 7/8] Removed newline --- .../googlesitemap/elements/chunks/gitemlink.chunk.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl index 6b68334..3bbd14e 100644 --- a/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl +++ b/core/components/googlesitemap/elements/chunks/gitemlink.chunk.tpl @@ -1 +1 @@ - + \ No newline at end of file From fd20270e026da42ea85114b93bb1be849e6a317b Mon Sep 17 00:00:00 2001 From: Matthew Bowman Date: Mon, 28 Apr 2014 15:15:17 +0200 Subject: [PATCH 8/8] Added default config option includeBabelLinks --- .../model/googlesitemap/googlesitemap.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php index 37df43e..5f76126 100755 --- a/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php +++ b/core/components/googlesitemap/model/googlesitemap/googlesitemap.class.php @@ -61,7 +61,8 @@ function __construct(modX &$modx,array $config = array()) { 'excludeChildrenOf' => '', 'showHidden' => false, 'priorityTV' => 0, - 'includeUrlProto' => false + 'includeUrlProto' => false, + 'includeBabelLinks' => false, ),$config); } @@ -319,7 +320,7 @@ private function getBabelLinks($doc) { $babelLinkUrl = $this->makeUrl($babelLinkId); $output[] = $this->getChunk($this->config['itemLinkTpl'], array( 'hreflang' => $babelLinkLang, - 'href' => $babelLinkUrl + 'href' => $babelLinkUrl, )); } }