diff --git a/src/Builders/FacebookMetaGenerator.php b/src/Builders/FacebookMetaGenerator.php index 7638c25..a1d3533 100644 --- a/src/Builders/FacebookMetaGenerator.php +++ b/src/Builders/FacebookMetaGenerator.php @@ -105,32 +105,32 @@ public function process() $tags = []; if ($this->getTitle()) { - $tags[] = sprintf('', htmlentities($this->getTitle())); + $tags['og:title'] = htmlentities($this->getTitle()); } if ($this->getDescription()) { - $tags[] = sprintf('', htmlentities($this->getDescription())); + $tags['og:description'] = htmlentities($this->getDescription()); } if ($this->getType()) { - $tags[] = sprintf('', $this->getType()); + $tags['og:type'] = $this->getType(); } if ($this->getUrl()) { - $tags[] = sprintf('', $this->getUrl()); + $tags['og:url'] = $this->getUrl(); } if ($this->getImageUrl()) { - $tags[] = sprintf('', $this->getImageUrl()); + $tags['og:image'] = $this->getImageUrl(); } if ($this->imageWidth && $this->imageHeight) { - $tags[] = sprintf('', $this->imageWidth); - $tags[] = sprintf('', $this->imageHeight); + $tags['og:image:width'] = $this->imageWidth; + $tags['og:image:height'] = $this->imageHeight; } - $tags[] = sprintf('', i18n::get_locale()); - $tags[] = sprintf('', SiteConfig::current_site_config()->Title); + $tags['og:locale'] = i18n::get_locale(); + $tags['og:site_name'] = SiteConfig::current_site_config()->Title; return $tags; } diff --git a/src/Builders/TwitterMetaGenerator.php b/src/Builders/TwitterMetaGenerator.php index fcaa503..78cc49e 100644 --- a/src/Builders/TwitterMetaGenerator.php +++ b/src/Builders/TwitterMetaGenerator.php @@ -93,24 +93,24 @@ public function process() $siteConfig = SiteConfig::current_site_config(); $tags = []; - $tags[] = ''; + $tags['twitter:card'] = 'summary'; if ($this->getTitle()) { - $tags[] = sprintf('', htmlentities($this->getTitle())); + $tags['twitter:title'] = htmlentities($this->getTitle()); } if ($this->getDescription()) { - $tags[] = sprintf('', htmlentities($this->getDescription())); + $tags['twitter:description'] = htmlentities($this->getDescription()); } if ($this->getImageUrl()) { - $tags[] = sprintf('', $this->getImageUrl()); + $tags['twitter:image'] = $this->getImageUrl(); } if ($this->getCreator()) { - $tags[] = sprintf('', $this->getCreator()); + $tags['twitter:creator'] = "@{$this->getCreator()}"; } if ($siteConfig->TwitterAccountName) { - $tags[] = sprintf('', $siteConfig->TwitterAccountName); + $tags['twitter:site'] = "@$siteConfig->TwitterAccountName"; } return $tags; diff --git a/src/Extensions/PageSeoExtension.php b/src/Extensions/PageSeoExtension.php index 4d028ee..9580fc8 100644 --- a/src/Extensions/PageSeoExtension.php +++ b/src/Extensions/PageSeoExtension.php @@ -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 * @@ -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() ); diff --git a/src/Seo.php b/src/Seo.php index 908878c..3428027 100644 --- a/src/Seo.php +++ b/src/Seo.php @@ -64,8 +64,8 @@ public static function getArticleTags($owner) $modified = $owner->dbObject('LastEdited'); return [ - sprintf('', $published->Rfc3339()), - sprintf('', $modified->Rfc3339()), + 'article:published_time' => $published->Rfc3339(), + 'article:modified_time' => $modified->Rfc3339(), ]; } @@ -92,9 +92,7 @@ public static function getCurrentAction() */ public static function getCanonicalUrlLink($owner) { - return [ - sprintf('', $owner->AbsoluteLink(static::getCurrentAction())) - ]; + return $owner->AbsoluteLink(static::getCurrentAction()); } /** diff --git a/tests/Extensions/PageSeoExtensionTest.php b/tests/Extensions/PageSeoExtensionTest.php index d4a5416..16f1284 100644 --- a/tests/Extensions/PageSeoExtensionTest.php +++ b/tests/Extensions/PageSeoExtensionTest.php @@ -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() @@ -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']); } }