Skip to content

Commit

Permalink
Tidying, tweaking, doc-blocking :(
Browse files Browse the repository at this point in the history
  • Loading branch information
4t4r1 committed Nov 8, 2016
1 parent a8e2263 commit 21664dc
Showing 1 changed file with 19 additions and 35 deletions.
54 changes: 19 additions & 35 deletions code/SEO_Metadata_SiteTree_DataExtension.php
@@ -1,21 +1,27 @@
<?php

/**
* Extends SiteTree with basic metadata fields, as well as the main `Metadata()` method.
*
* @package silverstripe-seo
* @subpackage metadata
* @author Andrew Gerber <atari@graphiquesdigitale.net>
* @version 1.0.0
*
*/

/**
* Class SEO_Metadata_SiteTree_DataExtension
*/
class SEO_Metadata_SiteTree_DataExtension extends DataExtension
{


/* Overload Model
------------------------------------------------------------------------------*/

/**
* Database attributes.
*
* @var array $db
*/
private static $db = array(
'MetaTitle' => 'Varchar(128)',
'MetaDescription' => 'Text', // redundant, but included for backwards-compatibility
Expand All @@ -26,10 +32,9 @@ class SEO_Metadata_SiteTree_DataExtension extends DataExtension
/* Overload Methods
------------------------------------------------------------------------------*/

// CMS Fields
// @todo @inheritdoc ?? or does it happen automagically as promised?
public function updateCMSFields(FieldList $fields)
{

// Variables
$config = SiteConfig::current_site_config();
$owner = $this->owner;
Expand All @@ -38,6 +43,7 @@ public function updateCMSFields(FieldList $fields)
$fields->removeByName(array('Metadata'));

//// Metadata

$tab = 'Root.Metadata.SEO';

// Canonical
Expand Down Expand Up @@ -77,7 +83,6 @@ public function updateCMSFields(FieldList $fields)
LiteralField::create('HeaderMetadata', '<pre class="bold">$Metadata()</pre>'),
LiteralField::create('LiteralMetadata', '<pre>' . nl2br(htmlentities(trim($owner->Metadata()), ENT_QUOTES)) . '</pre>')
));

}

/**
Expand All @@ -89,7 +94,6 @@ public function updateCMSFields(FieldList $fields)
*/
public function Metadata()
{

// variables
$config = SiteConfig::current_site_config();
$owner = $this->owner;
Expand All @@ -112,36 +116,31 @@ public function Metadata()

// title
if ($config->TitleEnabled()) {

// ternary operation
// @todo Check what is going here ?!
// ternary setter
$title = ($owner->MetaTitle) ? $owner->MetaTitle : $owner->GenerateTitle();
//
// safe output
$metadata .= '<title>' . htmlentities($title, ENT_QUOTES, $config->Charset()) . '</title>' . PHP_EOL;

}

// description
$metadata .= $owner->MarkupMeta('description', $owner->GenerateDescription(), true, $config->Charset());

//// ExtraMeta

// extra metadata
if ($config->ExtraMetaEnabled()) {
if ($extraMeta = $owner->ExtraMeta != '') {
$metadata .= $owner->MarkupComment('Extra Metadata');
$metadata .= $owner->ExtraMeta . PHP_EOL;
}
}

//// extension update hook
// extension update hook
$owner->extend('updateMetadata', $config, $owner, $metadata);

// end
$metadata .= $owner->MarkupComment('END SEO');

// return
return $metadata;

}


Expand All @@ -157,10 +156,7 @@ public function Metadata()
*/
public function MarkupComment($comment)
{

// return
return '<!-- ' . $comment . ' -->' . PHP_EOL;

}

/**
Expand All @@ -174,10 +170,7 @@ public function MarkupComment($comment)
*/
public function MarkupMeta($name, $content, $encode = false)
{

// return
return '<meta name="' . $name . '" content="' . $this->encodeContent($content, $encode) . '" />' . PHP_EOL;

}

/**
Expand Down Expand Up @@ -215,30 +208,25 @@ public function MarkupLink($rel, $href, $type = '', $sizes = '')
/**
* Generates HTML title based on configuration settings.
*
* @return bool|string
* @return string|null
*/
public function GenerateTitle()
{

// return SEO title or false
return SiteConfig::current_site_config()->GenerateTitle($this->owner->Title);

}

/**
* Returns description from the page `MetaDescription`, or the first paragraph of the `Content` attribute.
*
* @return bool|string
* @return string|null
*/
public function GenerateDescription()
{

if ($this->owner->MetaDescription) {
return $this->owner->MetaDescription;
} else {
return $this->owner->GenerateDescriptionFromContent();
}

}

/**
Expand All @@ -248,27 +236,23 @@ public function GenerateDescription()
*/
public function GenerateDescriptionFromContent()
{

// check for content
if ($content = trim($this->owner->Content)) {

// pillage first paragraph from page content
if (preg_match('/<p>(.*?)<\/p>/i', $content, $match)) {
// is HTML
$content = $match[0];
} else {
// is plain text
$content = explode("\n", $content);
$content = explode(PHP_EOL, $content);
$content = $content[0];
}

// decode (no harm done) & return
return trim(html_entity_decode(strip_tags($content)));

} else {
// none
return false;
}

}

/**
Expand Down

0 comments on commit 21664dc

Please sign in to comment.