From 958f7c6189f0acbee9571c950e7b1666ed2fd33f Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Thu, 24 Mar 2016 11:16:11 +0000 Subject: [PATCH 1/9] solve sef canocical issues --- plugins/system/sef/sef.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index 3fa5a85457ebb..2780434ea4e30 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -31,28 +31,26 @@ class PlgSystemSef extends JPlugin * * @since 3.5 */ - public function onAfterDispatch() + public function onAfterRoute() { - $doc = $this->app->getDocument(); + $doc = JFactory::getDocument(); if (!$this->app->isSite() || $doc->getType() !== 'html') { return; } - $uri = JUri::getInstance(); - $domain = $this->params->get('domain'); + $domain = $this->params->get('domain', ''); - if ($domain === false || $domain === '') + if ($domain !== '') { - $domain = $uri->toString(array('scheme', 'host', 'port')); - } - - $link = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + $uri = JUri::getInstance(); + $link = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); - if (rawurldecode($uri->toString()) !== $link) - { - $doc->addHeadLink(htmlspecialchars($link), 'canonical'); + if (rawurldecode($uri->toString()) !== $link) + { + $doc->addHeadLink(htmlspecialchars($link), 'canonical'); + } } } From 20c9d0ea9db146122ccd33121b404d3cbda39d8b Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Thu, 24 Mar 2016 12:45:31 +0000 Subject: [PATCH 2/9] Update sef.php --- plugins/system/sef/sef.php | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index 2780434ea4e30..a8ada4eeab20c 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -31,27 +31,43 @@ class PlgSystemSef extends JPlugin * * @since 3.5 */ - public function onAfterRoute() + public function onAfterDispatch() { - $doc = JFactory::getDocument(); + $doc = $this->app->getDocument(); if (!$this->app->isSite() || $doc->getType() !== 'html') { return; } - $domain = $this->params->get('domain', ''); - - if ($domain !== '') + // Check if canonical already exists (for instance, added by a component) so we don't override it. + $exists = false; + foreach ($doc->_links as $link) { - $uri = JUri::getInstance(); - $link = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); - - if (rawurldecode($uri->toString()) !== $link) + if (isset($link['relation']) && $link['relation'] === 'canonical') { - $doc->addHeadLink(htmlspecialchars($link), 'canonical'); + $exists = true; + break; } } + + $domain = $this->params->get('domain', ''); + + // If a canonical html tag already exists, don't do anything. + if ($exists) + { + return; + } + + // Add the canonical link if it's different from the current URI. + $uri = JUri::getInstance(); + $domain = (empty($domain)) ? $uri->toString(array('scheme', 'host', 'port')) : $domain; + $canonical = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + + if (rawurldecode($uri->toString()) !== $canonical) + { + $doc->addHeadLink(htmlspecialchars($canonical), 'canonical'); + } } /** From 9312a219142e7fc74b05de4551b0dcdab58b794c Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Thu, 24 Mar 2016 12:58:08 +0000 Subject: [PATCH 3/9] minor change --- plugins/system/sef/sef.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index a8ada4eeab20c..bbc0555a8d2c1 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -51,8 +51,6 @@ public function onAfterDispatch() } } - $domain = $this->params->get('domain', ''); - // If a canonical html tag already exists, don't do anything. if ($exists) { @@ -60,6 +58,7 @@ public function onAfterDispatch() } // Add the canonical link if it's different from the current URI. + $domain = $this->params->get('domain', ''); $uri = JUri::getInstance(); $domain = (empty($domain)) ? $uri->toString(array('scheme', 'host', 'port')) : $domain; $canonical = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); From d00b99c5b03c3cb18bfcc24b2632ec38b1f77680 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Thu, 24 Mar 2016 14:37:47 +0000 Subject: [PATCH 4/9] better sef canonical --- plugins/system/sef/sef.php | 41 +++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index bbc0555a8d2c1..a83fbef6bab93 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -41,32 +41,49 @@ public function onAfterDispatch() } // Check if canonical already exists (for instance, added by a component) so we don't override it. - $exists = false; - foreach ($doc->_links as $link) + $canonical = ''; + foreach ($doc->_links as $linkUrl => $link) { if (isset($link['relation']) && $link['relation'] === 'canonical') { - $exists = true; + $canonical = $linkUrl; break; } } - // If a canonical html tag already exists, don't do anything. - if ($exists) + $domain = $this->params->get('domain', ''); + + // If a canonical html tag already exists and we don't override it on SEF with a custom domain, don't do anything. + if (!empty($canonical) && empty($domain)) { return; } - // Add the canonical link if it's different from the current URI. - $domain = $this->params->get('domain', ''); - $uri = JUri::getInstance(); - $domain = (empty($domain)) ? $uri->toString(array('scheme', 'host', 'port')) : $domain; - $canonical = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + $uri = JUri::getInstance(); + $domain = (empty($domain)) ? $uri->toString(array('scheme', 'host', 'port')) : $domain; - if (rawurldecode($uri->toString()) !== $canonical) + // If a canonical html tag already exists and we override it on SEF with a custom domain, get the new canonical. + if (!empty($canonical) && !empty($domain)) { - $doc->addHeadLink(htmlspecialchars($canonical), 'canonical'); + // Remove current canonical link. + unset($doc->_links[$canonical]); + + // Set the current canonical link but use the SEF system plugin domain field. + $canonical = $domain . JUri::getInstance($canonical)->toString(array('path', 'query', 'fragment')); } + // If a canonical html doesn't exists already. + else + { + // Set the new canonical link, but only if it uses the SEF system plugin domain field or the canonical uri it's different from the current uri. + $canonical = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + if (rawurldecode($uri->toString()) === $canonical) + { + return; + } + } + + // Add the canonical link. + $doc->addHeadLink(htmlspecialchars($canonical), 'canonical'); } /** From 8daa095b669cad6bd95d6cb9fca2a3a7856aaebe Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Fri, 25 Mar 2016 11:42:54 +0000 Subject: [PATCH 5/9] add only canonical if the sef plugin domain field is entered --- plugins/system/sef/sef.php | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index a83fbef6bab93..7c0a745e26eea 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -40,7 +40,15 @@ public function onAfterDispatch() return; } - // Check if canonical already exists (for instance, added by a component) so we don't override it. + $sefDomain = $this->params->get('domain', ''); + + // Don't add a canonical id no alternative domain has added in SEF plugin domain field. + if (empty($sefDomain)) + { + return; + } + + // Check if canonical already exists (for instance, added by a component). $canonical = ''; foreach ($doc->_links as $linkUrl => $link) { @@ -51,35 +59,19 @@ public function onAfterDispatch() } } - $domain = $this->params->get('domain', ''); - - // If a canonical html tag already exists and we don't override it on SEF with a custom domain, don't do anything. - if (!empty($canonical) && empty($domain)) - { - return; - } - - $uri = JUri::getInstance(); - $domain = (empty($domain)) ? $uri->toString(array('scheme', 'host', 'port')) : $domain; - - // If a canonical html tag already exists and we override it on SEF with a custom domain, get the new canonical. - if (!empty($canonical) && !empty($domain)) + // If a canonical html tag already exists get the canonical and change it to use the SEF plugin domain field. + if (!empty($canonical)) { // Remove current canonical link. unset($doc->_links[$canonical]); // Set the current canonical link but use the SEF system plugin domain field. - $canonical = $domain . JUri::getInstance($canonical)->toString(array('path', 'query', 'fragment')); + $canonical = $sefDomain . JUri::getInstance($canonical)->toString(array('path', 'query', 'fragment')); } - // If a canonical html doesn't exists already. + // If a canonical html doesn't exists already add a canonical using the SEF plugin domain field. else { - // Set the new canonical link, but only if it uses the SEF system plugin domain field or the canonical uri it's different from the current uri. - $canonical = $domain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); - if (rawurldecode($uri->toString()) === $canonical) - { - return; - } + $canonical = $sefDomain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); } // Add the canonical link. From 4453a239153185d3618a548c64d842eee6ad1237 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Fri, 25 Mar 2016 11:44:01 +0000 Subject: [PATCH 6/9] add hint to field --- plugins/system/sef/sef.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml index c28b3b0afa04d..3715e9fef6c83 100644 --- a/plugins/system/sef/sef.xml +++ b/plugins/system/sef/sef.xml @@ -22,6 +22,7 @@ From 1c34b7b83d93d657e2c45742145441405b764dbc Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Fri, 25 Mar 2016 11:48:00 +0000 Subject: [PATCH 7/9] just better comments --- plugins/system/sef/sef.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index 7c0a745e26eea..7824d53776094 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -42,13 +42,13 @@ public function onAfterDispatch() $sefDomain = $this->params->get('domain', ''); - // Don't add a canonical id no alternative domain has added in SEF plugin domain field. + // Don't add a canonical html tag if no alternative domain has added in SEF plugin domain field. if (empty($sefDomain)) { return; } - // Check if canonical already exists (for instance, added by a component). + // Check if a canonical html tag already exists (for instance, added by a component). $canonical = ''; foreach ($doc->_links as $linkUrl => $link) { @@ -68,7 +68,7 @@ public function onAfterDispatch() // Set the current canonical link but use the SEF system plugin domain field. $canonical = $sefDomain . JUri::getInstance($canonical)->toString(array('path', 'query', 'fragment')); } - // If a canonical html doesn't exists already add a canonical using the SEF plugin domain field. + // If a canonical html doesn't exists already add a canonical html tag using the SEF plugin domain field. else { $canonical = $sefDomain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); From 86f63a650f43c26c965f577e6025e55e53c3e3c1 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Fri, 25 Mar 2016 12:21:21 +0000 Subject: [PATCH 8/9] use juri instead of router to avoid wrong canonicals --- plugins/system/sef/sef.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/sef/sef.php b/plugins/system/sef/sef.php index 7824d53776094..c4f8c3c0109bb 100644 --- a/plugins/system/sef/sef.php +++ b/plugins/system/sef/sef.php @@ -71,7 +71,7 @@ public function onAfterDispatch() // If a canonical html doesn't exists already add a canonical html tag using the SEF plugin domain field. else { - $canonical = $sefDomain . JRoute::_('index.php?' . http_build_query($this->app->getRouter()->getVars()), false); + $canonical = $sefDomain . JUri::getInstance()->toString(array('path', 'query', 'fragment')); } // Add the canonical link. From 99b82a7807b6792db93548c8d26f05d083211051 Mon Sep 17 00:00:00 2001 From: andrepereiradasilva Date: Fri, 25 Mar 2016 22:26:29 +0000 Subject: [PATCH 9/9] make george happy --- plugins/system/sef/sef.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/system/sef/sef.xml b/plugins/system/sef/sef.xml index 3715e9fef6c83..107f30dae7348 100644 --- a/plugins/system/sef/sef.xml +++ b/plugins/system/sef/sef.xml @@ -22,7 +22,7 @@