This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanonicalurl.php
76 lines (65 loc) · 2.2 KB
/
canonicalurl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* @package Canonical URL
* @copyright Copyright (c) 2014 Hans Kuijpers - HKweb
* @license GNU General Public License version 3 or later
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class plgSystemCanonicalurl extends JPlugin
{
/**
* Load the language file on instantiation. Note this is only available in Joomla 3.1 and higher.
* If you want to support 3.0 series you must override the constructor
*
* @var boolean
* @since 3.1
*/
protected $autoloadLanguage = true;
/**
* Replace Canonical URL set by Joomla!
* This plugin should be loaded after Joomla! SEF plugin
* Order can be set in Joomla! backend
*
* Using trigger onContentBeforeDisplay because it provides the needed $article and $params information
*/
public function onContentBeforeDisplay($article, $params, $limitstart)
{
$app = JFactory::getApplication();
$jinput = $app->input;
$option = $jinput->get('option', null);
$view = $jinput->get('view', null);
$doc = JFactory::getDocument();
// stop this plugin when you are in admin
if ($app->isAdmin())
{
return;
}
// stop this plugin when you are not in one of these components
$allowedcomponents = array('com_content');
if(!in_array($option,$allowedcomponents))
{
return;
}
if($article == 'com_content.article' && ($option == "com_content" && $view == "article"))
{
// remove existing Canonical URL
$this->removeCanonical();
// create new Canonical URL
$canonicalurl = JRoute::_( ContentHelperRoute::getArticleRoute( (int)$params->id, $params->catslug),false,-1);
// set new Canonical URL
$doc->addHeadLink(htmlspecialchars($canonicalurl), 'canonical');
}
}
private function removeCanonical()
{
$doc = JFactory::getDocument();
foreach ( $doc->_links as $k => $array )
{
if ( $array['relation'] == 'canonical' )
{
unset($doc->_links[$k]);
}
}
}
}