Skip to content

Commit

Permalink
Merge pull request #13 from zanderwar/fix/9/extension-pollution
Browse files Browse the repository at this point in the history
FIX over pollution of methods from extensions
  • Loading branch information
zanderwar committed Apr 17, 2018
2 parents 59d4295 + 4e02246 commit 503193b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 117 deletions.
4 changes: 3 additions & 1 deletion src/Analysis/WordCountAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vulcan\Seo\Analysis;

use Vulcan\Seo\Seo;

/**
* Class WordCountAnalysis
* @package Vulcan\Seo\Analysis
Expand Down Expand Up @@ -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()))));
}
}
26 changes: 4 additions & 22 deletions src/Extensions/PageHealthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
}
}
94 changes: 8 additions & 86 deletions src/Extensions/PageSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use SilverStripe\Security\Security;
use Vulcan\Seo\Builders\FacebookMetaGenerator;
use Vulcan\Seo\Builders\TwitterMetaGenerator;
use Vulcan\Seo\Seo;

/**
* Class PageSeoExtension
Expand Down Expand Up @@ -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('<meta property="article:published_time" content="%s" />', $published->Rfc3339()),
sprintf('<meta property="article:modified_time" content="%s" />', $modified->Rfc3339()),
];
}

/**
* Creates the canonical url link
*
* @return array
*/
public function getCanonicalUrlLink()
{
return [
sprintf('<link rel="canonical" href="%s"/>', $this->getOwner()->AbsoluteLink())
];
}

/**
* Added to improve IDE syntax highlighting
*
* @return \Page|static
*/
public function getOwner()
{
/** @var \Page $owner */
$owner = parent::getOwner();
return $owner;
}
}
121 changes: 121 additions & 0 deletions src/Seo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Vulcan\Seo;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\FieldType\DBDatetime;
use Vulcan\Seo\Builders\FacebookMetaGenerator;
use Vulcan\Seo\Builders\TwitterMetaGenerator;
use Vulcan\Seo\Extensions\PageHealthExtension;
use Vulcan\Seo\Extensions\PageSeoExtension;

/**
* Class Seo
* @package Vulcan\Seo
*/
class Seo
{
use Injectable, Configurable;

/**
* 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.
*
* @param \Page|PageHealthExtension $owner
*
* @return string
*/
public static function collateContentFields($owner)
{
$contentFields = $owner->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('<meta property="article:published_time" content="%s" />', $published->Rfc3339()),
sprintf('<meta property="article:modified_time" content="%s" />', $modified->Rfc3339()),
];
}

/**
* Creates the canonical url link
*
* @param \Page|PageSeoExtension|Object $owner
*
* @return array
*/
public static function getCanonicalUrlLink($owner)
{
return [
sprintf('<link rel="canonical" href="%s"/>', $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();
}
}
17 changes: 9 additions & 8 deletions tests/Extensions/PageSeoExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -29,29 +30,29 @@ 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]
);
}

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);
}
}

0 comments on commit 503193b

Please sign in to comment.