DorsetDigital /
silverstripe-canonical
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace DorsetDigital\SilverStripeCanonical; |
||||
| 4 | |||||
| 5 | use SilverStripe\View\HTML; |
||||
| 6 | use SilverStripe\Forms\FieldList; |
||||
| 7 | use SilverStripe\Forms\TextField; |
||||
| 8 | use SilverStripe\Forms\LiteralField; |
||||
| 9 | use SilverStripe\SiteConfig\SiteConfig; |
||||
| 10 | use SilverStripe\CMS\Model\SiteTreeExtension; |
||||
| 11 | use SilverStripe\CMS\Model\VirtualPage; |
||||
| 12 | |||||
| 13 | class SiteTreeCanonicalExtension extends SiteTreeExtension |
||||
| 14 | { |
||||
| 15 | private static $db = [ |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 16 | 'CanonicalURL' => 'Text' |
||||
| 17 | ]; |
||||
| 18 | |||||
| 19 | public function updateCMSFields(FieldList $fields) |
||||
| 20 | { |
||||
| 21 | if ($MetaToggle = $fields->fieldByName('Root.Main.Metadata')) { |
||||
|
0 ignored issues
–
show
Are you sure the assignment to
$MetaToggle is correct as $fields->fieldByName('Root.Main.Metadata') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 22 | if ($url = $this->getorsetCanonicalURL()) { |
||||
|
0 ignored issues
–
show
Are you sure the assignment to
$url is correct as $this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 23 | $MetaToggle->push($MetaCanonical = TextField::create('CanonicalURL', _t(__CLASS__ . '.LinkOverride', "Override canonical URL"))); |
||||
| 24 | $MetaCanonical |
||||
| 25 | ->setAttribute('placeholder', $this->getorsetCanonicalURL()) |
||||
|
0 ignored issues
–
show
Are you sure the usage of
$this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 26 | ->setDescription(_t(__CLASS__ . '.LinkOverrideDesc', 'Only set this if another URL should count as the original (e.g. of reposting a blog post from another source).')); |
||||
| 27 | if ($this->owner->ClassName == VirtualPage::class) { |
||||
| 28 | $MetaCanonical |
||||
| 29 | ->setReadonly(true) |
||||
| 30 | ->setDescription(_t(__CLASS__ . '.LinkOverrideVirtualDesc', 'Linked page will be used.')); |
||||
| 31 | } |
||||
| 32 | } else { |
||||
| 33 | $MetaToggle->push($MetaCanonical = LiteralField::create("CanonicalURL", '<p class="form__field-label">' . _t(__CLASS__ . '.LinkFieldPlaceholder', 'Canonical-URLs needs a Canonical domain in <a href="/admin/settings">SiteConfig</a>') . '</p>')); |
||||
| 34 | } |
||||
| 35 | $MetaCanonical->setRightTitle(_t(__CLASS__ . '.LinkFieldRightTitle', 'Used to identify the original resource (URL) to prevent being considered as "duplicate content".')); |
||||
| 36 | } |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | function getorsetCanonicalURL() |
||||
| 40 | { |
||||
| 41 | $siteConfig = SiteConfig::current_site_config(); |
||||
| 42 | if (filter_var($siteConfig->CanonicalDomain, FILTER_VALIDATE_URL)) { |
||||
| 43 | $canonicalBase = trim($siteConfig->CanonicalDomain, '/'); |
||||
|
0 ignored issues
–
show
It seems like
$siteConfig->CanonicalDomain can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 44 | |||||
| 45 | // dynamic value |
||||
| 46 | if (method_exists($this->owner, 'CanonicalLink')) { |
||||
| 47 | $link = $this->owner->CanonicalLink(); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | // canonical link on Page |
||||
| 51 | if (isset($this->owner->CanonicalURL) && $this->owner->CanonicalURL != null) { |
||||
| 52 | $link = $this->owner->CanonicalURL; |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | // use CopyContentFrom()->Link() for VirtualPage |
||||
| 56 | if ($this->owner->ClassName == VirtualPage::class) { |
||||
| 57 | $link = $this->owner->CopyContentFrom()->Link(); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | // add canonicalBase if relative URL |
||||
| 61 | if (isset($link)) { |
||||
| 62 | $urlArray = parse_url($link); |
||||
| 63 | if (!isset($urlArray['host']) && !isset($urlArray['scheme'])) { |
||||
| 64 | $link = $canonicalBase . $link; |
||||
| 65 | } |
||||
| 66 | } else { |
||||
| 67 | // default link with base |
||||
| 68 | $link = $canonicalBase . $this->owner->Link(); |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | return $link; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 72 | } |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | public function MetaComponents(array &$tags) |
||||
| 76 | { |
||||
| 77 | if ($canonLink = $this->getorsetCanonicalURL()) { |
||||
|
0 ignored issues
–
show
Are you sure the assignment to
$canonLink is correct as $this->getorsetCanonicalURL() targeting DorsetDigital\SilverStri...:getorsetCanonicalURL() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 78 | $tags['canonical'] = [ |
||||
| 79 | 'tag' => 'link', |
||||
| 80 | 'attributes' => [ |
||||
| 81 | 'rel' => 'canonical', |
||||
| 82 | 'href' => $canonLink |
||||
| 83 | ] |
||||
| 84 | ]; |
||||
| 85 | } |
||||
| 86 | } |
||||
| 87 | } |
||||
| 88 |