Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR use MetaComponents() method allowing for better extensibility #64

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Builders/FacebookMetaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,32 @@ public function process()
$tags = [];

if ($this->getTitle()) {
$tags[] = sprintf('<meta property="og:title" content="%s"/>', htmlentities($this->getTitle()));
$tags['og:title'] = htmlentities($this->getTitle());
}

if ($this->getDescription()) {
$tags[] = sprintf('<meta property="og:description" content="%s"/>', htmlentities($this->getDescription()));
$tags['og:description'] = htmlentities($this->getDescription());
}

if ($this->getType()) {
$tags[] = sprintf('<meta property="og:type" content="%s"/>', $this->getType());
$tags['og:type'] = $this->getType();
}

if ($this->getUrl()) {
$tags[] = sprintf('<meta property="og:url" content="%s"/>', $this->getUrl());
$tags['og:url'] = $this->getUrl();
}

if ($this->getImageUrl()) {
$tags[] = sprintf('<meta property="og:image" content="%s"/>', $this->getImageUrl());
$tags['og:image'] = $this->getImageUrl();
}

if ($this->imageWidth && $this->imageHeight) {
$tags[] = sprintf('<meta property="og:image:width" content="%s" />', $this->imageWidth);
$tags[] = sprintf('<meta property="og:image:height" content="%s" />', $this->imageHeight);
$tags['og:image:width'] = $this->imageWidth;
$tags['og:image:height'] = $this->imageHeight;
}

$tags[] = sprintf('<meta property="og:locale" content="%s" />', i18n::get_locale());
$tags[] = sprintf('<meta property="og:site_name" content="%s" />', SiteConfig::current_site_config()->Title);
$tags['og:locale'] = i18n::get_locale();
$tags['og:site_name'] = SiteConfig::current_site_config()->Title;

return $tags;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Builders/TwitterMetaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,24 @@ public function process()
$siteConfig = SiteConfig::current_site_config();
$tags = [];

$tags[] = '<meta name="twitter:card" content="summary"/>';
$tags['twitter:card'] = 'summary';
if ($this->getTitle()) {
$tags[] = sprintf('<meta name="twitter:title" content="%s"/>', htmlentities($this->getTitle()));
$tags['twitter:title'] = htmlentities($this->getTitle());
}

if ($this->getDescription()) {
$tags[] = sprintf('<meta name="twitter:description" content="%s"/>', htmlentities($this->getDescription()));
$tags['twitter:description'] = htmlentities($this->getDescription());
}

if ($this->getImageUrl()) {
$tags[] = sprintf('<meta name="twitter:image" content="%s"/>', $this->getImageUrl());
$tags['twitter:image'] = $this->getImageUrl();
}
if ($this->getCreator()) {
$tags[] = sprintf('<meta name="twitter:creator" content="@%s"/>', $this->getCreator());
$tags['twitter:creator'] = "@{$this->getCreator()}";
}

if ($siteConfig->TwitterAccountName) {
$tags[] = sprintf('<meta name="twitter:site" content="@%s" />', $siteConfig->TwitterAccountName);
$tags['twitter:site'] = "@$siteConfig->TwitterAccountName";
}

return $tags;
Expand Down
33 changes: 29 additions & 4 deletions src/Extensions/PageSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ class PageSeoExtension extends DataExtension
'TwitterPageImage'
];

/**
* @param $tags
*/
public function MetaComponents(&$tags)
{
$addTag = function ($tagName, $tag, $type = 'rel') use (&$tags) {
$tags[$tagName] = [
'attributes' => [
$type => $tagName,
'content' => $tag,
],
];
};

$addTag('canonical', Seo::getCanonicalUrlLink($this->getOwner()));

foreach (Seo::getFacebookMetaTags($this->getOwner()) as $tagName => $tag) {
$addTag($tagName, $tag, 'property');
}

foreach (Seo::getTwitterMetaTags($this->getOwner()) as $tagName => $tag) {
$addTag($tagName, $tag, 'name');
}

foreach (Seo::getArticleTags($this->getOwner()) as $tagName => $tag) {
$addTag($tagName, $tag);
}
}

/**
* Extension point for SiteTree to merge all tags with the standard meta tags
*
Expand All @@ -77,10 +106,6 @@ public function MetaTags(&$tags)
$tags = explode(PHP_EOL, $tags);
$tags = array_merge(
$tags,
Seo::getCanonicalUrlLink($this->getOwner()),
Seo::getFacebookMetaTags($this->getOwner()),
Seo::getTwitterMetaTags($this->getOwner()),
Seo::getArticleTags($this->getOwner()),
Seo::getGoogleAnalytics(),
Seo::getPixels()
);
Expand Down
8 changes: 3 additions & 5 deletions src/Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public static function getArticleTags($owner)
$modified = $owner->dbObject('LastEdited');

return [
sprintf('<meta property="article:published_time" content="%s" />', $published->Rfc3339()),
sprintf('<meta property="article:modified_time" content="%s" />', $modified->Rfc3339()),
'article:published_time' => $published->Rfc3339(),
'article:modified_time' => $modified->Rfc3339(),
];
}

Expand All @@ -92,9 +92,7 @@ public static function getCurrentAction()
*/
public static function getCanonicalUrlLink($owner)
{
return [
sprintf('<link rel="canonical" href="%s"/>', $owner->AbsoluteLink(static::getCurrentAction()))
];
return $owner->AbsoluteLink(static::getCurrentAction());
}

/**
Expand Down
30 changes: 20 additions & 10 deletions tests/Extensions/PageSeoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testPageHasExtension()

public function testCanonicalLink()
{
$this->assertContains(Director::absoluteBaseURL(), Seo::getCanonicalUrlLink($this->page)[0]);
$this->assertContains(Director::absoluteBaseURL(), Seo::getCanonicalUrlLink($this->page));
}

public function testArticleTags()
Expand All @@ -44,22 +44,32 @@ public function testArticleTags()

$this->assertContains(
$created->Rfc3339(),
Seo::getArticleTags($this->page)[0]
Seo::getArticleTags($this->page)
);
$this->assertContains(
$lastEdited->Rfc3339(),
Seo::getArticleTags($this->page)[1]
Seo::getArticleTags($this->page)
);
}

public function testMetaTags()
public function testMetaComponents()
{
$tags = $this->page->MetaTags(false);
$tags = $this->page->MetaComponents();

$this->assertContains(Seo::getCanonicalUrlLink($this->page)[0], $tags);
$this->assertContains(Seo::getArticleTags($this->page)[0], $tags);
$this->assertContains(Seo::getArticleTags($this->page)[1], $tags);
$this->assertContains(implode(PHP_EOL, Seo::getFacebookMetaTags($this->page)), $tags);
$this->assertContains(implode(PHP_EOL, Seo::getTwitterMetaTags($this->page)), $tags);
$this->assertContains(Seo::getCanonicalUrlLink($this->page), $tags['canonical']['attributes']);

$this->assertContains(Seo::getArticleTags($this->page)['article:published_time'], $tags['article:published_time']['attributes']);
$this->assertContains(Seo::getArticleTags($this->page)['article:modified_time'], $tags['article:modified_time']['attributes']);

$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:title'], $tags['og:title']['attributes']);
$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:description'], $tags['og:description']['attributes']);
$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:type'], $tags['og:type']['attributes']);
$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:url'], $tags['og:url']['attributes']);
$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:locale'], $tags['og:locale']['attributes']);
$this->assertContains(Seo::getFacebookMetaTags($this->page)['og:site_name'], $tags['og:site_name']['attributes']);

$this->assertContains(Seo::getTwitterMetaTags($this->page)['twitter:card'], $tags['twitter:card']['attributes']);
$this->assertContains(Seo::getTwitterMetaTags($this->page)['twitter:title'], $tags['twitter:title']['attributes']);
$this->assertContains(Seo::getTwitterMetaTags($this->page)['twitter:description'], $tags['twitter:description']['attributes']);
}
}