From 4e02246e169efc47f583ed104220982425d5fe9f Mon Sep 17 00:00:00 2001 From: zanderwar Date: Tue, 17 Apr 2018 20:57:40 +1000 Subject: [PATCH] FIX over pollution --- src/Analysis/WordCountAnalysis.php | 4 +- src/Extensions/PageHealthExtension.php | 26 +---- src/Extensions/PageSeoExtension.php | 94 ++--------------- src/Seo.php | 121 ++++++++++++++++++++++ tests/Extensions/PageSeoExtensionTest.php | 17 +-- 5 files changed, 145 insertions(+), 117 deletions(-) create mode 100644 src/Seo.php diff --git a/src/Analysis/WordCountAnalysis.php b/src/Analysis/WordCountAnalysis.php index d6f5007..7526fb7 100644 --- a/src/Analysis/WordCountAnalysis.php +++ b/src/Analysis/WordCountAnalysis.php @@ -2,6 +2,8 @@ namespace Vulcan\Seo\Analysis; +use Vulcan\Seo\Seo; + /** * Class WordCountAnalysis * @package Vulcan\Seo\Analysis @@ -50,6 +52,6 @@ public function responses() */ public function getWordCount() { - return count(array_filter(explode(' ', $this->getPage()->collateContentFields()))); + return count(array_filter(explode(' ', Seo::collateContentFields($this->getPage())))); } } diff --git a/src/Extensions/PageHealthExtension.php b/src/Extensions/PageHealthExtension.php index 9c2bf8f..2a92ade 100644 --- a/src/Extensions/PageHealthExtension.php +++ b/src/Extensions/PageHealthExtension.php @@ -36,8 +36,6 @@ public function updateCMSFields(FieldList $fields) { parent::updateCMSFields($fields); - $this->renderedHtml = file_get_contents($this->getOwner()->AbsoluteLink()); - $fields->addFieldsToTab('Root.Main', [ ToggleCompositeField::create(null, 'SEO Health Analysis', [ GoogleSearchPreview::create('GoogleSearchPreview', 'Search Preview', $this->getOwner(), $this->getRenderedHtmlDomParser()), @@ -54,6 +52,10 @@ public function updateCMSFields(FieldList $fields) */ public function getRenderedHtml() { + if (!$this->renderedHtml) { + $this->renderedHtml = file_get_contents($this->getOwner()->AbsoluteLink()); + } + return $this->renderedHtml; } @@ -89,24 +91,4 @@ public function seoContentFields() 'Content' ]; } - - /** - * Collates all content fields from {@link seoContentFields()} into a single string. Which makes it very important - * that the seoContentFields array is in the correct order as to which they display. - * - * @return string - */ - public function collateContentFields() - { - $contentFields = $this->getOwner()->seoContentFields(); - - $content = []; - foreach ($contentFields as $field) { - $content[] = $this->getOwner()->relObject($field)->forTemplate(); - } - - $content = implode(' ', $content); - - return strtolower(strip_tags($content)); - } } diff --git a/src/Extensions/PageSeoExtension.php b/src/Extensions/PageSeoExtension.php index cc7a269..8c19037 100644 --- a/src/Extensions/PageSeoExtension.php +++ b/src/Extensions/PageSeoExtension.php @@ -16,6 +16,7 @@ use SilverStripe\Security\Security; use Vulcan\Seo\Builders\FacebookMetaGenerator; use Vulcan\Seo\Builders\TwitterMetaGenerator; +use Vulcan\Seo\Seo; /** * Class PageSeoExtension @@ -105,92 +106,13 @@ public function updateCMSFields(FieldList $fields) public function MetaTags(&$tags) { $tags = explode(PHP_EOL, $tags); - $tags = array_merge($tags, $this->getCanonicalUrlLink(), $this->getFacebookMetaTags(), $this->getTwitterMetaTags(), $this->getArticleTags()); + $tags = array_merge( + $tags, + Seo::getCanonicalUrlLink($this->getOwner()), + Seo::getFacebookMetaTags($this->getOwner()), + Seo::getTwitterMetaTags($this->getOwner()), + Seo::getArticleTags($this->getOwner()) + ); $tags = implode(PHP_EOL, $tags); } - - /** - * Creates the Facebook/OpenGraph meta tags - * - * @return array - */ - public function getFacebookMetaTags() - { - $owner = $this->getOwner(); - $imageWidth = $owner->FacebookPageImage()->exists() ? $owner->FacebookPageImage()->getWidth() : null; - $imageHeight = $owner->FacebookPageImage()->exists() ? $owner->FacebookPageImage()->getHeight() : null; - - $generator = FacebookMetaGenerator::create(); - $generator->setTitle($owner->FacebookPageTitle ?: $owner->Title); - $generator->setDescription($owner->FacebookPageDescription ?: $owner->MetaDescription ?: $owner->Content); - $generator->setImageUrl(($owner->FacebookPageImage()->exists()) ? $owner->FacebookPageImage()->AbsoluteLink() : null); - $generator->setImageDimensions($imageWidth, $imageHeight); - $generator->setType($owner->FacebookPageType ?: 'website'); - $generator->setUrl($owner->AbsoluteLink()); - - return $generator->process(); - } - - /** - * Creates the twitter meta tags - * - * @return array - */ - public function getTwitterMetaTags() - { - $owner = $this->getOwner(); - $generator = TwitterMetaGenerator::create(); - $generator->setTitle($owner->FacebookPageTitle ?: $owner->Title); - $generator->setDescription($owner->FacebookPageDescription ?: $owner->MetaDescription ?: $owner->Content); - $generator->setImageUrl(($owner->FacebookPageImage()->exists()) ? $owner->FacebookPageImage()->AbsoluteLink() : null); - - if ($this->config()->get('enable_creator_tag') && $owner->Creator()->exists() && $owner->Creator()->TwitterAccountName) { - $generator->setCreator($owner->Creator()->TwitterAccountName); - } - - return $generator->process(); - } - - /** - * Creates article:published_time and article:modified_time tags - * - * @return array - */ - public function getArticleTags() - { - /** @var DBDatetime $published */ - $published = $this->getOwner()->dbObject('Created'); - - /** @var DBDatetime $modified */ - $modified = $this->getOwner()->dbObject('LastEdited'); - - return [ - sprintf('', $published->Rfc3339()), - sprintf('', $modified->Rfc3339()), - ]; - } - - /** - * Creates the canonical url link - * - * @return array - */ - public function getCanonicalUrlLink() - { - return [ - sprintf('', $this->getOwner()->AbsoluteLink()) - ]; - } - - /** - * Added to improve IDE syntax highlighting - * - * @return \Page|static - */ - public function getOwner() - { - /** @var \Page $owner */ - $owner = parent::getOwner(); - return $owner; - } } diff --git a/src/Seo.php b/src/Seo.php new file mode 100644 index 0000000..168dfe3 --- /dev/null +++ b/src/Seo.php @@ -0,0 +1,121 @@ +seoContentFields(); + + $content = []; + foreach ($contentFields as $field) { + $content[] = $owner->relObject($field)->forTemplate(); + } + + $content = implode(' ', $content); + + return strtolower(strip_tags($content)); + } + + /** + * Creates article:published_time and article:modified_time tags + * + * @param \Page|PageSeoExtension|Object $owner + * + * @return array + */ + public static function getArticleTags($owner) + { + /** @var DBDatetime $published */ + $published = $owner->dbObject('Created'); + + /** @var DBDatetime $modified */ + $modified = $owner->dbObject('LastEdited'); + + return [ + sprintf('', $published->Rfc3339()), + sprintf('', $modified->Rfc3339()), + ]; + } + + /** + * Creates the canonical url link + * + * @param \Page|PageSeoExtension|Object $owner + * + * @return array + */ + public static function getCanonicalUrlLink($owner) + { + return [ + sprintf('', $owner->AbsoluteLink()) + ]; + } + + /** + * Creates the twitter meta tags + * + * @param \Page|PageSeoExtension|Object $owner + * + * @return array + */ + public static function getTwitterMetaTags($owner) + { + $generator = TwitterMetaGenerator::create(); + $generator->setTitle($owner->FacebookPageTitle ?: $owner->Title); + $generator->setDescription($owner->FacebookPageDescription ?: $owner->MetaDescription ?: $owner->Content); + $generator->setImageUrl(($owner->FacebookPageImage()->exists()) ? $owner->FacebookPageImage()->AbsoluteLink() : null); + + if (PageSeoExtension::config()->get('enable_creator_tag') && $owner->Creator()->exists() && $owner->Creator()->TwitterAccountName) { + $generator->setCreator($owner->Creator()->TwitterAccountName); + } + + return $generator->process(); + } + + /** + * Creates the Facebook/OpenGraph meta tags + * + * @param \Page|PageSeoExtension|Object $owner + * + * @return array + */ + public static function getFacebookMetaTags($owner) + { + $imageWidth = $owner->FacebookPageImage()->exists() ? $owner->FacebookPageImage()->getWidth() : null; + $imageHeight = $owner->FacebookPageImage()->exists() ? $owner->FacebookPageImage()->getHeight() : null; + + $generator = FacebookMetaGenerator::create(); + $generator->setTitle($owner->FacebookPageTitle ?: $owner->Title); + $generator->setDescription($owner->FacebookPageDescription ?: $owner->MetaDescription ?: $owner->Content); + $generator->setImageUrl(($owner->FacebookPageImage()->exists()) ? $owner->FacebookPageImage()->AbsoluteLink() : null); + $generator->setImageDimensions($imageWidth, $imageHeight); + $generator->setType($owner->FacebookPageType ?: 'website'); + $generator->setUrl($owner->AbsoluteLink()); + + return $generator->process(); + } +} diff --git a/tests/Extensions/PageSeoExtensionTest.php b/tests/Extensions/PageSeoExtensionTest.php index a266472..0f3cd40 100644 --- a/tests/Extensions/PageSeoExtensionTest.php +++ b/tests/Extensions/PageSeoExtensionTest.php @@ -5,6 +5,7 @@ use SilverStripe\Control\Director; use SilverStripe\Dev\FunctionalTest; use Vulcan\Seo\Extensions\PageSeoExtension; +use Vulcan\Seo\Seo; class PageSeoExtensionTest extends FunctionalTest { @@ -29,18 +30,18 @@ public function testPageHasExtension() public function testCanonicalLink() { - $this->assertContains(Director::absoluteBaseURL(), $this->page->getCanonicalUrlLink()[0]); + $this->assertContains(Director::absoluteBaseURL(), Seo::getCanonicalUrlLink($this->page)[0]); } public function testArticleTags() { $this->assertContains( $this->page->dbObject('Created')->Rfc3339(), - $this->page->getArticleTags()[0] + Seo::getArticleTags($this->page)[0] ); $this->assertContains( $this->page->dbObject('LastEdited')->Rfc3339(), - $this->page->getArticleTags()[1] + Seo::getArticleTags($this->page)[1] ); } @@ -48,10 +49,10 @@ public function testMetaTags() { $tags = $this->page->MetaTags(false); - $this->assertContains($this->page->getCanonicalUrlLink()[0], $tags); - $this->assertContains($this->page->getArticleTags()[0], $tags); - $this->assertContains($this->page->getArticleTags()[1], $tags); - $this->assertContains(implode(PHP_EOL, $this->page->getFacebookMetaTags()), $tags); - $this->assertContains(implode(PHP_EOL, $this->page->getTwitterMetaTags()), $tags); + $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); } }