From 1b7527cccfcafd14a185ef953568f86d9a344ad1 Mon Sep 17 00:00:00 2001 From: Mischa Spelt Date: Wed, 5 Feb 2020 00:40:55 +0100 Subject: [PATCH] Meta data (OpenGraph) toegevoegd. --- common/classes/CalendarEvent.php | 7 +++++++ common/classes/MetaData.php | 35 ++++++++++++++++++++++++++++++++ common/classes/NewsItem.php | 11 ++++++++++ common/classes/Podcast.php | 10 ++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 common/classes/MetaData.php diff --git a/common/classes/CalendarEvent.php b/common/classes/CalendarEvent.php index 80576ab..768cf43 100644 --- a/common/classes/CalendarEvent.php +++ b/common/classes/CalendarEvent.php @@ -14,6 +14,7 @@ class CalendarEvent extends Model { public $video; public $keywords; public $url; + public $metadata; public function __construct($data, $images = null, $podcast = null) { parent::__construct($data); @@ -46,6 +47,12 @@ class CalendarEvent extends Model { } $this->url = "/agenda/{$this->id}/" . parent::url_slug($this->title); + $this->metadata = (new MetaData()) + ->set('title', $this->title) + ->set('description', strip_tags($this->excerpt())) + ->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null ) + ->set('audio', isset($this->podcast) ? $this->podcast->url : null) + ; } public function excerpt() { diff --git a/common/classes/MetaData.php b/common/classes/MetaData.php new file mode 100644 index 0000000..de36d80 --- /dev/null +++ b/common/classes/MetaData.php @@ -0,0 +1,35 @@ + 'article', 'locale' => 'nl-nl']; + + public function set($prop, $val) { + if($val) { + $this->data[$prop] = $val; + } + return $this; + } + + public function append($prop, $val) { + if(array_key_exists($prop, $this->data)) { + $this->data[$prop] .= $val; + } + return $this; + } + + public function prepend($prop, $val) { + if(array_key_exists($prop, $this->data)) { + $this->data[$prop] = $val . $this->data[$prop]; + } + return $this; + } + + public function metaTags() { + foreach($this->data as $prop => $val) { + if($val) { + echo "\n"; + } + } + } +} diff --git a/common/classes/NewsItem.php b/common/classes/NewsItem.php index a53c664..a969396 100644 --- a/common/classes/NewsItem.php +++ b/common/classes/NewsItem.php @@ -4,6 +4,7 @@ namespace Model; require_once "NewsImage.php"; require_once "NewsSource.php"; +require_once "MetaData.php"; class NewsItem extends Model { public $id; @@ -24,6 +25,7 @@ class NewsItem extends Model { public $video; public $url; + public $metadata; public function __construct($data, $images = null, $podcast = null) { parent::__construct($data); @@ -88,6 +90,15 @@ class NewsItem extends Model { } $this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title); + $this->metadata = (new MetaData()) + ->set('title', $this->title) + ->set('description', strip_tags($this->excerpt())) + ->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null ) + ->set('video', isset($this->video) ? $this->video : null) + ->set('audio', isset($this->podcast) ? $this->podcast->url : null) + ->set('article:published_time', $this->published->format('r')) + ->set('article:modified_time', $this->edited ? $this->edited->format('r') : null) + ; } public function excerpt() { diff --git a/common/classes/Podcast.php b/common/classes/Podcast.php index 961c92b..06dc91f 100644 --- a/common/classes/Podcast.php +++ b/common/classes/Podcast.php @@ -16,13 +16,14 @@ class Podcast extends Model { public $download; public $duration; public $image; + public $metadata; private $key; public function __construct($data) { parent::__construct($data); parent::ConvertToDateTime($this->created); - $this->url = $this->id . '/' . parent::url_slug($this->title); + $this->url = '/' . $this->id . '/' . parent::url_slug($this->title); if($this->soundfilename) { // Only generate when not constructing from a JSON object $this->key = sha1($this->id . ':' . date('Y-m-d') . ':' . $this->soundfilename); @@ -45,6 +46,13 @@ class Podcast extends Model { $imagedata->title = $data->imagecaption; $this->image = new NewsImage($imagedata, '/img/podcast/'); } + + $this->metadata = (new MetaData()) + ->set('title', $this->title) + ->set('description', strip_tags($this->excerpt())) + ->set('image', isset($this->images) && count($this->images) ? $this->images[0]->url : null ) + ->set('audio', $this->url) + ; } public function titleWithoutProgram() {