Skip to content

Commit

Permalink
Add support for sharable story card
Browse files Browse the repository at this point in the history
  • Loading branch information
munirajun committed Feb 9, 2018
1 parent da23dd9 commit 5634b1d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Quintype/Seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function staticPage($title){
return new StaticPage($this->config, $title);
}

public function story($pageType, $story){
return new Story($this->config, $pageType, $story);
public function story($pageType, $story, $card = null){
return new Story($this->config, $pageType, $story, $card);
}

public function storyElement($pageType, $story, $element){
Expand Down
63 changes: 59 additions & 4 deletions src/Quintype/Seo/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

class Story extends Base
{
public function __construct($config, $pageType, $story)
public function __construct($config, $pageType, $story, $card = null)
{
parent::__construct($config, $pageType);
$this->story = $story;
$this->card = $card;
$this->cardSocialShare = (isset($card) && isset($card['metadata']['social-share']['shareable']))? $card['metadata']['social-share'] : [];

}

public function prepareTags()
Expand All @@ -19,8 +22,8 @@ public function prepareTags()
'keywords' => trim($this->getKeywords(["stories" => $this->story])),
'news_keywords' => trim($this->getKeywords(["stories" => $this->story])),
'image_src' => $this->getHeroImageUrl(),
'og' => $this->getOgAttributes(),
'twitter' => $this->getTwitterAttributes(),
'og' => !empty($this->cardSocialShare)? $this->getCardShareOgAttributes() : $this->getOgAttributes(),
'twitter' => !empty($this->cardSocialShare)? $this->getCardShareTwitterAttributes() :$this->getTwitterAttributes(),
'msvalidate.01' => $this->getBingId(),
'fb' => [
'app_id' => $this->getFacebookData('app-id'),
Expand Down Expand Up @@ -104,6 +107,48 @@ private function getOgAttributes()
return $attributes;
}

private function getCardShareOgAttributes()
{
$attributes = [
'title' => isset($this->cardSocialShare["title"])? $this->cardSocialShare["title"] : trim($this->getTitle()),
'type' => 'article',
'url' => "{$this->config['sketches-host']}/{$this->story['slug']}/{$card['id']}",
'site-name' => trim($this->config['title']),
'description' => isset($this->cardSocialShare["message"])? $this->cardSocialShare["message"] : trim($this->getSocialDescription()),
'image' => isset($this->cardSocialShare["image"])? $this->getCardImageUrl() : $this->getHeroImageUrl(),
];

if (isset($this->cardSocialShare['image']['metadata'])) {
$imageProperties = [];
if (isset($this->cardSocialShare['image']['metadata']['width'])) {
$imageProperties['image:width'] = $this->cardSocialShare['image']['metadata']['width'];
}
if (isset($this->cardSocialShare['image']['metadata']['height'])) {
$imageProperties['image:height'] = $this->cardSocialShare['image']['metadata']['height'];
}
$attributes = array_merge($attributes, $imageProperties);
}


return $attributes;
}

private function getCardShareTwitterAttributes()
{
$attributes = [
'title' => isset($this->cardSocialShare["title"])? $this->cardSocialShare["title"] : trim($this->getTitle()),
'description' => isset($this->cardSocialShare["message"])? $this->cardSocialShare["message"] : trim($this->getSocialDescription()),
'card' => 'summary_large_image',
'site' => $this->getTwitterSite(),
'creator' => $this->getTwitterCreator(),
'image' => [
'src' => isset($this->cardSocialShare["image"])? $this->getCardImageUrl() : $this->getHeroImageUrl(),
],
];

return $attributes;
}

private function getTwitterAttributes()
{
$attributes = [
Expand Down Expand Up @@ -165,10 +210,20 @@ private function getAndroidData($element)
}

private function getHeroImageUrl()
{
return $this->getImageCDNUrl($this->story['hero-image-s3-key']);
}

private function getCardImageUrl()
{
return $this->getImageCDNUrl($this->card['metadata']['image']['key']);
}

private function getImageCDNUrl($imageS3Key)
{
if (isset($this->config['cdn-name']) || isset($this->config['cdn-image'])) {
$cdn = isset($this->config['cdn-image']) ? "https://".$this->config['cdn-image'] : $this->config['cdn-name'];
$imageUrl = trim($cdn, '/').'/'.$this->story['hero-image-s3-key'];
$imageUrl = trim($cdn, '/').'/'.$imageS3Key;

return str_replace(' ', '%20', $imageUrl).'?w=700';
} else {
Expand Down

0 comments on commit 5634b1d

Please sign in to comment.