-
Notifications
You must be signed in to change notification settings - Fork 6
/
social-meta-tags.php
188 lines (157 loc) · 7.64 KB
/
social-meta-tags.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
/**
* Class SocialMetaTagsPlugin
* @package Grav\Plugin
*/
class SocialMetaTagsPlugin extends Plugin
{
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
if ( !$this->isAdmin()
and $this->config->get('plugins.social-meta-tags.enabled')
) {
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
}
public function onPageInitialized(Event $e)
{
$page = $this->grav['page'];
$meta = $page->metadata(null);
$meta = $this->getTwitterCardMetatags($meta);
$meta = $this->getFacebookMetatags($meta);
$page->metadata($meta);
}
private function getTwitterCardMetatags($meta){
if($this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.twitter.enabled')) {
if (!isset($meta['twitter:card'])) {
$meta['twitter:card']['name'] = 'twitter:card';
$meta['twitter:card']['property'] = 'twitter:card';
$meta['twitter:card']['content'] = $this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.twitter.type');
}
if (!isset($meta['twitter:title'])) {
$meta['twitter:title']['name'] = 'twitter:title';
$meta['twitter:title']['property'] = 'twitter:title';
$meta['twitter:title']['content'] = $this->sanitizeMarkdowns($this->grav['page']->title());
}
if (!isset($meta['twitter:description'])) {
$meta['twitter:description']['name'] = 'twitter:description';
$meta['twitter:description']['property'] = 'twitter:description';
$meta['twitter:description']['content'] = $this->sanitizeMarkdowns(strip_tags($this->grav['page']->summary()));
}
if (!isset($meta['twitter:image'])) {
if (!empty($this->grav['page']->value('media.image'))) {
$images = $this->grav['page']->media()->images();
$image = array_shift($images);
$meta['twitter:image']['name'] = 'twitter:image';
$meta['twitter:image']['property'] = 'twitter:image';
$meta['twitter:image']['content'] = $this->grav['uri']->base() . $image->url();
}
}
if (!isset($meta['twitter:site'])) {
//Use AboutMe plugin configuration
if ($this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.twitter.aboutme'))
{
if ($this->grav['config']->get('plugins.aboutme.social_pages.enabled')
and $this->grav['config']->get('plugins.aboutme.social_pages.pages.twitter.url'))
{
$user = preg_replace('((http|https)://twitter.com/)', '@', $this->grav['config']->get('plugins.aboutme.social_pages.pages.twitter.url'));
}
else
{
$user = "";
}
}
//Use plugin self-configuration
else
{
$user = "@".$this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.twitter.username');
}
//Update data
$meta['twitter:site']['name'] = 'twitter:site';
$meta['twitter:site']['property'] = 'twitter:site';
$meta['twitter:site']['content'] = $user;
}
}
return $meta;
}
private function getFacebookMetatags($meta){
if($this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.facebook.enabled')){
$meta['og:sitename']['name'] = 'og:sitename';
$meta['og:sitename']['property'] = 'og:sitename';
$meta['og:sitename']['content'] = $this->grav['page']->value('name');
$meta['og:title']['name'] = 'og:title';
$meta['og:title']['property'] = 'og:title';
$meta['og:title']['content'] = $this->sanitizeMarkdowns($this->grav['page']->title());
$meta['og:description']['name'] = 'og:description';
$meta['og:description']['property'] = 'og:description';
$meta['og:description']['content'] = $this->sanitizeMarkdowns(strip_tags($this->grav['page']->summary()));
$meta['og:type']['name'] = 'og:type';
$meta['og:type']['property'] = 'og:type';
$meta['og:type']['content'] = 'article';
$meta['og:url']['name'] = 'og:url';
$meta['og:url']['property'] = 'og:url';
$meta['og:url']['content'] = $this->grav['uri']->url(true);
if (!empty($this->grav['page']->value('media.image'))) {
$images = $this->grav['page']->media()->images();
$image = array_shift($images);
$meta['og:image']['name'] = 'og:image';
$meta['og:image']['property'] = 'og:image';
$meta['og:image']['content'] = $this->grav['uri']->base() . $image->url();
}
$meta['fb:app_id']['name'] = 'fb:app_id';
$meta['fb:app_id']['property'] = 'fb:app_id';
$meta['fb:app_id']['content'] = $this->grav['config']->get('plugins.social-meta-tags.social_pages.pages.facebook.appid');
}
return $meta;
}
private function sanitizeMarkdowns($text){
$rules = array (
'/(#+)(.*)/' => '\2', // headers
'/(<|<)!--\n((.*|\n)*)\n--(>|\>)/' => '', // comments
'/(\*|-|_){3}/' => '', // hr
'/!\[([^\[]+)\]\(([^\)]+)\)/' => '', // images
'/\[([^\[]+)\]\(([^\)]+)\)/' => '\1', // links
'/(\*\*|__)(.*?)\1/' => '\2', // bold
'/(\*|_)(.*?)\1/' => '\2', // emphasis
'/\~\~(.*?)\~\~/' => '\1', // del
'/\:\"(.*?)\"\:/' => '\1', // quote
'/```(.*)\n((.*|\n)+)\n```/' => '\2', // fence code
'/`(.*?)`/' => '\1', // inline code
'/(\*|\+|-)(.*)/' => '\2', // ul lists
'/\n[0-9]+\.(.*)/' => '\2', // ol lists
'/(>|\>)+(.*)/' => '\2', // blockquotes
);
foreach ($rules as $regex => $replacement) {
if (is_callable ( $replacement)) {
$text = preg_replace_callback ($regex, $replacement, $text);
} else {
$text = preg_replace ($regex, $replacement, $text);
}
}
return substr(htmlspecialchars($text, ENT_QUOTES, 'UTF-8'),0,140);
}
}