diff --git a/README.md b/README.md index b50beb7..ed520bb 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,44 @@ # quintype-seo-php -A composer package for seo for all quintype projects +A composer package for SEO for all quintype projects ###Important : If any change is made to the package, do the following. * Create a new release. * Update the package in [Packagist](https://packagist.org/). -* To use the new version of the package in any project, change the version number in composer.json file and run +* To use the new version of the package in any project, change the version number in composer.json file and run ``` $ composer update ``` Note : We are making use of a package called Meta (https://github.com/quintype/meta) forked from https://github.com/RyanNielson/meta for dynamically adding the meta tags into the pages. Instructions to include the package into a project. -### In composer.json, add a repository pointing to the forked meta repository. +### In composer.json, require Meta and SEO packages. ```sh -"repositories": [ +"require": { ... ... - { - "type": "vcs", - "url": "https://github.com/quintype/meta" - } - ], + "quintype/seo":"2.0.0", + "quintype/meta":"2.0.0" + }, ``` -### In composer.json, require the original Meta package and this seo package. +### Install or update the composer packages. ```sh -"require": { - ... - ... - "quintype/seo":"0.0.5", - "ryannielson/meta":"dev-master" - }, +$ composer install +or +$ composer update ``` -### In the Laravel config/app.php file, include Meta provider and give an alias to it. + +### In the Laravel config/app.php file, add aliases to the packages for convenience. ```sh -'providers' => [ - ... - ... - RyanNielson\Meta\MetaServiceProvider::class - ], - 'aliases' => [ ... ... - 'Meta' => RyanNielson\Meta\Meta::class + 'Meta' => Quintype\Meta\Meta::class, + 'Seo' => Quintype\Seo\Seo::class ], ``` -### Add an attribute called 'title' in config/quintype.php file as a fall-back value if it is not recieved from the meta data. +### Add an attribute called 'title' in config/quintype.php file as a fall-back value if it is not received from the meta data. ```sh return [ ... @@ -56,31 +47,28 @@ return [ ]; ``` -### Install or update the composer packages. -```sh -$ composer install -or -$ composer update -``` ### Include both Meta and SEO classes in the necessary controllers. ```sh use Meta; -use Quintype\Seo; +use Seo; ``` -### Create a constructor function to initialize the Meta object and config variable if necessary. + +### Create a constructor function to initialize the Meta and SEO objects. Note: Do not forget to pass the config(merge local config and config from API response) to SEO package. ```sh public function __construct(){ - parent::__construct(); - $this->meta = new Meta; - $this->config = $this->client->config(); + $this->client = new Api(config("quintype.api-host")); + $this->config = array_merge($this->client->config(), config('quintype')); + $this->meta = new Meta(); + $this->seo = new Seo($this->config); } ``` ### Prepare the data required for meta tags. ```sh -// Setting Seo meta tags -$page = ["type" => "home"];//Type of the page. In this case, it is the home page. -$home = new Seo\Home(array_merge($this->config, config('quintype')), $page["type"]);//Since it is the home page, initialize the Home object. -$this->meta->set($home->tags());//Set the tags received in the above step. + +$page = ["type" => PAGE_TYPE];//eg. home, section, story etc. +//Set SEO meta tags. +$setSeo = $this->seo->FUNCTION_CORRESPONDING_TO_PAGE(ARGUMENTS);//eg. home($arguments), section($arguments), story($arguments) etc. +$this->meta->set($setSeo->prepareTags()); ``` ### In the function to render the view, include the meta data. diff --git a/src/Quintype/Seo.php b/src/Quintype/Seo.php new file mode 100644 index 0000000..d961646 --- /dev/null +++ b/src/Quintype/Seo.php @@ -0,0 +1,37 @@ +config = $config; + } + public function home($pageType){ + return new Home($this->config, $pageType); + } + + public function search($query){ + return new Search($query); + } + + public function section($pageType, $sectionName, $sectionId){ + return new Section($this->config, $pageType, $sectionName, $sectionId); + } + + public function staticPage($title){ + return new StaticPage($title); + } + + public function story($pageType, $story){ + return new Story($this->config, $pageType, $story); + } + + public function storyElement($pageType, $story, $element){ + return new StoryElement($this->config, $pageType, $story, $element); + } + + public function tag($tag){ + return new Tag($this->config, $tag); + } +} diff --git a/src/Quintype/Seo/Base.php b/src/Quintype/Seo/Base.php index 07c0b7f..7d955bf 100644 --- a/src/Quintype/Seo/Base.php +++ b/src/Quintype/Seo/Base.php @@ -3,35 +3,34 @@ namespace Quintype\Seo; class Base { - function __construct($config, $pageType, $section_id=''){ + function __construct($config, $pageType, $sectionId = ''){ $this->config = $config; $this->pageType = $pageType; - $this->section_id = $section_id; + $this->sectionId = $sectionId; $this->seoMetadata = $this->getSeoMetadata(); } private function getSeoMetadata() { - if(sizeof($this->config['seo-metadata'])>0){ $key = 'owner-type'; $found = 0; if($this->pageType == 'section'){//If it is a section page. foreach ($this->config['seo-metadata'] as $metadata) { - if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType && $metadata['owner-id']==$this->section_id) { - $found = 1; - return $metadata['data']; - } + if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType && $metadata['owner-id']==$this->sectionId) { + $found = 1; + return $metadata['data']; + } } if(!$found){ return array(); } } else { foreach ($this->config['seo-metadata'] as $metadata) { - if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType) { - $found = 1; - return $metadata['data']; - } + if (array_key_exists($key, $metadata) && $metadata[$key]==$this->pageType) { + $found = 1; + return $metadata['data']; + } } if(!$found){ return array(); @@ -101,4 +100,4 @@ protected function getCanonicalUrl(){ return $this->config['sketches-host'] . "/". $this->story['slug']; } } -} \ No newline at end of file +} diff --git a/src/Quintype/Seo/Home.php b/src/Quintype/Seo/Home.php index eb9ed33..8c3797d 100644 --- a/src/Quintype/Seo/Home.php +++ b/src/Quintype/Seo/Home.php @@ -2,40 +2,35 @@ namespace Quintype\Seo; -require "Base.php"; - -class Home extends Base { - - function tags() { +class Home extends Base +{ + function prepareTags() { if (sizeof($this->seoMetadata)>0){ - return [ 'title' => trim($this->getPageTitle()), - 'description' => trim($this->getDescription()), - 'keywords' => trim($this->getKeywords()), - 'og' => [ - 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()) - ], - 'twitter' => [ - 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()) - ], - 'msvalidate.01' => $this->getBingId(), - 'fb' => [ - 'app_id' => $this->getFacebookData('app-id'), - 'pages' => $this->getFacebookData('pages') - ], - 'alternate' => [ - 'href' => '/feed', - 'type' => 'application/atom+xml', - 'title' => trim($this->getPageTitle()) . " ATOM Feed" - ] - ]; + 'description' => trim($this->getDescription()), + 'keywords' => trim($this->getKeywords()), + 'og' => [ + 'title' => trim($this->getTitle()), + 'description' => trim($this->getDescription()) + ], + 'twitter' => [ + 'title' => trim($this->getTitle()), + 'description' => trim($this->getDescription()) + ], + 'msvalidate.01' => $this->getBingId(), + 'fb' => [ + 'app_id' => $this->getFacebookData('app-id'), + 'pages' => $this->getFacebookData('pages') + ], + 'alternate' => [ + 'href' => '/feed', + 'type' => 'application/atom+xml', + 'title' => trim($this->getPageTitle()) . " ATOM Feed" + ] + ]; } else { return ['title' => trim($this->getPageTitle())]; } } - - -} \ No newline at end of file +} diff --git a/src/Quintype/Seo/Search.php b/src/Quintype/Seo/Search.php index dee18ea..11ddbb3 100644 --- a/src/Quintype/Seo/Search.php +++ b/src/Quintype/Seo/Search.php @@ -2,16 +2,13 @@ namespace Quintype\Seo; -require "Base.php"; - -class Search extends Base { - - function __construct($config, $pageType, $query){ +class Search +{ + function __construct($query){ $this->query = $query; } - function tags() { - return ['title' => trim($this->query) ." - Search Results"]; + function prepareTags() { + return ['title' => trim($this->query) ." - Search Results"]; } - } diff --git a/src/Quintype/Seo/Section.php b/src/Quintype/Seo/Section.php index 07716f3..0ebb657 100644 --- a/src/Quintype/Seo/Section.php +++ b/src/Quintype/Seo/Section.php @@ -2,31 +2,28 @@ namespace Quintype\Seo; -require "Base.php"; - -class Section extends Base { - - function __construct($config, $pageType, $section, $section_id = ''){ - parent::__construct($config, $pageType, $section_id); - $this->section = $section; +class Section extends Base +{ + function __construct($config, $pageType, $sectionName, $sectionId){ + parent::__construct($config, $pageType, $sectionId); + $this->sectionName = $sectionName; } - function tags() { + function prepareTags() { if (sizeof($this->seoMetadata)>0){ - return [ 'title' => trim($this->getPageTitle()), - 'description' => trim($this->getDescription()), - 'keywords' => trim($this->getKeywords()), - 'og' => [ - 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()) - ], - 'twitter' => [ - 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()) - ] - ]; + 'description' => trim($this->getDescription()), + 'keywords' => trim($this->getKeywords()), + 'og' => [ + 'title' => trim($this->getTitle()), + 'description' => trim($this->getDescription()) + ], + 'twitter' => [ + 'title' => trim($this->getTitle()), + 'description' => trim($this->getDescription()) + ] + ]; } else { return ['title' => trim($this->getPageTitle())]; } @@ -35,14 +32,12 @@ function tags() { protected function getPageTitle(){ if(isset($this->seoMetadata['page-title'])){ if($this->seoMetadata['page-title']==''){ - return $this->section . " - " . $this->config['title']; + return $this->sectionName . " - " . $this->config['title']; }else{ return $this->seoMetadata['page-title']; } } else { - return $this->section . " - " . $this->config['title']; + return $this->sectionName . " - " . $this->config['title']; } } - - -} \ No newline at end of file +} diff --git a/src/Quintype/Seo/StaticPage.php b/src/Quintype/Seo/StaticPage.php index 86d40c1..6cd338e 100644 --- a/src/Quintype/Seo/StaticPage.php +++ b/src/Quintype/Seo/StaticPage.php @@ -2,16 +2,13 @@ namespace Quintype\Seo; -require "Base.php"; - -class StaticPage extends Base { - +class StaticPage extends Base +{ function __construct($title){ $this->title = $title; } - function tags() { - return ['title' => trim($this->title)]; + function prepareTags() { + return ['title' => trim($this->title)]; } - } diff --git a/src/Quintype/Seo/Story.php b/src/Quintype/Seo/Story.php index c5c9ee8..970344c 100644 --- a/src/Quintype/Seo/Story.php +++ b/src/Quintype/Seo/Story.php @@ -2,38 +2,34 @@ namespace Quintype\Seo; -require "Base.php"; - -class Story extends Base { - +class Story extends Base +{ function __construct($config, $pageType, $story){ parent::__construct($config, $pageType); $this->story = $story; } - function tags() { - + function prepareTags() { if (sizeof($this->story)>0){ - return [ 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()), - 'keywords' => trim($this->getKeywords()), - 'og' => $this->getOgAttributes(), - 'twitter' => $this->getTwitterAttributes(), - 'msvalidate.01' => $this->getBingId(), - 'fb' => [ - 'app_id' => $this->getFacebookData('app-id'), - 'pages' => $this->getFacebookData('pages') - ], - 'article' => [ - 'publisher' => $this->getPublisher() - ], - 'rel:canonical' => $this->getCanonicalUrl(), - 'al:android:package' => $this->getAndroidData('al:android:package'), - 'al:android:app-name' => $this->getAndroidData('al:android:app-name'), - 'al:android:url' => "quintypefb://" . $this->config['sketches-host'] . "/". $this->story['slug'] - ]; + 'description' => trim($this->getDescription()), + 'keywords' => trim($this->getKeywords()), + 'og' => $this->getOgAttributes(), + 'twitter' => $this->getTwitterAttributes(), + 'msvalidate.01' => $this->getBingId(), + 'fb' => [ + 'app_id' => $this->getFacebookData('app-id'), + 'pages' => $this->getFacebookData('pages') + ], + 'article' => [ + 'publisher' => $this->getPublisher() + ], + 'rel:canonical' => $this->getCanonicalUrl(), + 'al:android:package' => $this->getAndroidData('al:android:package'), + 'al:android:app-name' => $this->getAndroidData('al:android:app-name'), + 'al:android:url' => "quintypefb://" . $this->config['sketches-host'] . "/". $this->story['slug'] + ]; } else { return ['title' => $this->getPageTitle()]; } @@ -58,42 +54,36 @@ protected function getTitle(){ private function getOgAttributes(){ $attributes = [ 'title' => trim($this->getTitle()), - 'type' => 'article', - 'url' => $this->getCanonicalUrl(), - 'site-name' => trim($this->config['title']), - 'description' => trim($this->getDescription()), - 'image' => $this->getHeroImageUrl() - ]; - - if(isset($this->story['hero-image-metadata'])){ - - $imageProperties=[]; - - if(isset($this->story['hero-image-metadata']['width'])){ - $imageProperties['image:width']=$this->story['hero-image-metadata']['width']; - } - - if(isset($this->story['hero-image-metadata']['height'])){ - $imageProperties['image:height']=$this->story['hero-image-metadata']['height']; - } - - $attributes = array_merge($attributes, $imageProperties); - } - - return $attributes; + 'type' => 'article', + 'url' => $this->getCanonicalUrl(), + 'site-name' => trim($this->config['title']), + 'description' => trim($this->getDescription()), + 'image' => $this->getHeroImageUrl() + ]; + + if(isset($this->story['hero-image-metadata'])){ + $imageProperties=[]; + if(isset($this->story['hero-image-metadata']['width'])){ + $imageProperties['image:width']=$this->story['hero-image-metadata']['width']; + } + if(isset($this->story['hero-image-metadata']['height'])){ + $imageProperties['image:height']=$this->story['hero-image-metadata']['height']; + } + $attributes = array_merge($attributes, $imageProperties); + } + return $attributes; } private function getTwitterAttributes(){ $attributes = [ 'title' => trim($this->getTitle()), - 'description' => trim($this->getDescription()), - 'card' => 'summary_large_image', - 'site' => $this->getTwitterSite(), - 'image' => [ - 'src' => $this->getHeroImageUrl() - ] + 'description' => trim($this->getDescription()), + 'card' => 'summary_large_image', + 'site' => $this->getTwitterSite(), + 'image' => [ + 'src' => $this->getHeroImageUrl() + ] ]; - return $attributes; } @@ -131,7 +121,4 @@ private function getHeroImageUrl(){ return ''; } } - - - } diff --git a/src/Quintype/Seo/StoryElement.php b/src/Quintype/Seo/StoryElement.php index 7aa0711..0afb35e 100644 --- a/src/Quintype/Seo/StoryElement.php +++ b/src/Quintype/Seo/StoryElement.php @@ -4,18 +4,17 @@ require "Base.php"; -class StoryElement extends Base { - +class StoryElement extends Base +{ function __construct($config, $pageType, $story, $element){ $this->story = $story; $this->element = $element; } - function tags() { - return [ - 'title' => trim($this->story['headline']) . " - " . $this->config['title'], - 'rel:canonical' => $this->getCanonicalUrl() - ]; + function prepareTags() { + return [ + 'title' => trim($this->story['headline']) . " - " . $this->config['title'], + 'rel:canonical' => $this->getCanonicalUrl() + ]; } - } diff --git a/src/Quintype/Seo/Tag.php b/src/Quintype/Seo/Tag.php index fc804a0..b2669df 100644 --- a/src/Quintype/Seo/Tag.php +++ b/src/Quintype/Seo/Tag.php @@ -2,17 +2,14 @@ namespace Quintype\Seo; -require "Base.php"; - -class Tag extends Base { - - function __construct($config, $pageType, $tag){ +class Tag +{ + function __construct($config, $tag){ $this->config = $config; $this->tag = $tag; } - function tags() { - return ['title'=>trim($this->tag) . " - " . $this->config['title']]; + function prepareTags() { + return ['title'=>trim($this->tag) . " - " . $this->config['title']]; } - }