-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMetaTags.php
100 lines (71 loc) · 2.48 KB
/
MetaTags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Automad Meta Tags
*
* An Automad meta tags extension.
*
* @author Marc Anton Dahmen
* @copyright Copyright (C) 2018-2020 Marc Anton Dahmen - <https://marcdahmen.de>
* @license MIT license
*/
namespace Automad;
defined('AUTOMAD') or die('Direct access not permitted!');
class MetaTags {
/**
* The main function.
*
* @param array $options
* @param object $Automad
* @return string the output of the extension
*/
public function MetaTags($options, $Automad) {
$Page = $Automad->Context->get();
$defaults = array(
'charset' => 'utf-8',
'viewport' => 'width=device-width, initial-scale=1, shrink-to-fit=no',
'description' => false,
'ogTitle' => $Automad->Shared->get('sitename') . ' / ' . $Page->get('title'),
'ogDescription' => false,
'ogType' => 'website',
'ogImage' => false,
'twitterCard' => 'summary_large_image'
);
$options = array_merge($defaults, $options);
$host = getenv('HTTP_HOST');
$protocol = 'http';
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$protocol = 'https';
}
$baseUrl = $protocol . '://' . $host . AM_BASE_URL;
$baseIndex = $protocol . '://' . $host . AM_BASE_INDEX;
$html = '';
$html .= '<meta charset="' . $options['charset'] . '" />';
$html .= '<meta name="viewport" content="' . $options['viewport'] . '" />';
if ($options['description']) {
$html .= '<meta name="description" content="' . htmlspecialchars(Core\Str::shorten($options['description'], 160)) . '" />';
}
if ($options['ogTitle']) {
$html .= '<meta property="og:title" content="' . $options['ogTitle'] . '" />';
}
if ($options['ogDescription']) {
$html .= '<meta property="og:description" content="' . htmlspecialchars(Core\Str::shorten($options['ogDescription'], 320)) . '" />';
}
$html .= '<meta property="og:type" content="' . $options['ogType'] . '" />' .
'<meta property="og:url" content="' . $baseIndex . $Page->url . '" />';
if ($options['ogImage']) {
$files = Core\Parse::fileDeclaration($options['ogImage'], $Page);
if ($files) {
$file = reset($files);
$Image = new Core\Image($file);
$imageUrl = $baseUrl . $Image->file;
} else {
$imageUrl = $options['ogImage'];
}
$html .= '<meta property="og:image" content="' . $imageUrl . '" />';
}
if ($options['twitterCard']) {
$html .= '<meta name="twitter:card" content="' . $options['twitterCard'] . '" />';
}
return $html;
}
}