SEO: structured data and correct meta tags for news articles
- NewsArticle + BreadcrumbList JSON-LD (headline, summary, images, ISO dates, authors, GOOI&meer as NewsMediaOrganization publisher, section) - Per-article meta description instead of the generic site text - article:published_time / modified_time in ISO 8601 (was og:article:* in RFC 2822), article:section / article:tag, og:image:alt, og:locale nl_NL - Canonical URL, twitter:card, robots max-image-preview:large - <html lang="nl">, title without leading whitespace, no keywords meta - Article wrapped in <article>, dates in <time datetime> - Metadata is rebuilt after the controller fills in API v2 data (dates, author, content blocks were not reflected before) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab3499e365
commit
dbbdca504e
9 files changed
+224
-24
No files matched your search
+35
-7
@@ -1,35 +1,63 @@
|
||||
<?php
|
||||
namespace Model;
|
||||
|
||||
/**
|
||||
* Open Graph / article meta tags for a page.
|
||||
*
|
||||
* Properties are stored without prefix and rendered as og:<prop>, except
|
||||
* namespaced ones (e.g. article:published_time), which are rendered as-is.
|
||||
* A value can be an array to render the same property multiple times
|
||||
* (e.g. article:tag).
|
||||
*/
|
||||
class MetaData {
|
||||
private $data = ['type' => 'article', 'locale' => 'nl-nl'];
|
||||
private $data = ['type' => 'article', 'locale' => 'nl_NL'];
|
||||
|
||||
public function set($prop, $val) {
|
||||
if($val) {
|
||||
$this->data[$prop] = $val;
|
||||
$this->data[$prop] = self::clean($val);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get($prop, $default = null) {
|
||||
return $this->data[$prop] ?? $default;
|
||||
}
|
||||
|
||||
public function append($prop, $val) {
|
||||
if(array_key_exists($prop, $this->data)) {
|
||||
if(array_key_exists($prop, $this->data) && is_string($this->data[$prop])) {
|
||||
$this->data[$prop] .= $val;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefix a relative value (e.g. an image path) with a base URL.
|
||||
* Absolute URLs are left alone.
|
||||
*/
|
||||
public function prepend($prop, $val) {
|
||||
if(array_key_exists($prop, $this->data)) {
|
||||
if(array_key_exists($prop, $this->data) && is_string($this->data[$prop])
|
||||
&& !preg_match('#^(https?:)?//#i', $this->data[$prop])) {
|
||||
$this->data[$prop] = $val . $this->data[$prop];
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function metaTags() {
|
||||
foreach($this->data as $prop => $val) {
|
||||
if($val) {
|
||||
echo "<meta property=\"og:$prop\" content=\"" . htmlentities($val) . "\" />\n";
|
||||
foreach($this->data as $prop => $values) {
|
||||
$name = strpos($prop, ':') !== false && strpos($prop, 'image:') !== 0 ? $prop : "og:$prop";
|
||||
foreach((array)$values as $val) {
|
||||
if($val) {
|
||||
echo "<meta property=\"$name\" content=\"" . htmlspecialchars($val, ENT_QUOTES, 'UTF-8') . "\" />\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Collapse whitespace (newlines, tabs) in text values. */
|
||||
private static function clean($val) {
|
||||
if(is_array($val)) {
|
||||
return array_values(array_filter(array_map([self::class, 'clean'], $val)));
|
||||
}
|
||||
return is_string($val) ? trim(preg_replace('/\s+/u', ' ', $val)) : $val;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user