-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathMetaTags.php
289 lines (256 loc) · 5.09 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
namespace Melbahja\Seo;
use Melbahja\Seo\Interfaces\SeoInterface;
/**
* @package Melbahja\Seo
* @since v2.0
* @see https://git.io/phpseo
* @license MIT
* @copyright 2019-present Mohamed Elabhja
*/
class MetaTags implements SeoInterface
{
/**
* Generated tags
* @var array
*/
protected $tags = [];
/**
* Twitter tags.
* @var array
*/
protected $twitterTags = [];
/**
* Open Graph tags.
* @var array
*/
protected $openGraphTags = [];
/**
* Page title
* @var string
*/
public $title = null;
/**
* Initiablize new meta tags builder
*
* @param array $tags
*/
public function __construct(array $tags = [])
{
foreach ($tags as $k => $v)
{
if (method_exists(static::class, $k)) {
$this->$k($v);
continue;
}
$this->meta($k, $v);
}
}
/**
* Set page and meta title
*
* @param string $title
* @return MetaTags
*/
public function title(string $title): MetaTags
{
$this->title = Helper::escape($title);
return $this->meta('title', $title)->og('title', $title)->twitter('title', $title);
}
/**
* Set page description.
* @param string $desc
* @return MetaTags
*/
public function description(string $desc): MetaTags
{
return $this->meta('description', $desc)->og('description', $desc)->twitter('description', $desc);
}
/**
* Set a mobile link (Http header "Vary: User-Agent" is required)
*
* @param string $url
* @return MetaTags
*/
public function mobile(string $url): MetaTags
{
return $this->push('link', [
'href' => $url,
'rel' => 'alternate',
'media' => 'only screen and (max-width: 640px)',
]);
}
/**
* Set robots meta tags.
*
* @param string $options For example: follow, index, max-snippet:-1, max-video-preview:-1, max-image-preview:large
* @param string $botName bot name or robots for all.
* @return MetaTags
*/
public function robots(string $options, string $botName = 'robots'): MetaTags
{
return $this->meta($botName, $options);
}
/**
* Set AMP link
*
* @param string $url
* @return MetaTags
*/
public function amp(string $url): MetaTags
{
return $this->push('link', [
'rel' => 'amphtml',
'href' => $url
]);
}
/**
* Set canonical url
*
* @param string $url
* @return MetaTags
*/
public function canonical(string $url): MetaTags
{
return $this->push('link', [
'rel' => 'canonical',
'href' => $url
]);
}
/**
* Set social media url.
*
* @param string $url
* @return MetaTags
*/
public function url(string $url): MetaTags
{
return $this->og('url', $url)->twitter('url', $url);
}
/**
* Set alternate language url.
*
* @param string $lang for eg: en
* @param string $url alternate language page url.
* @return MetaTags
*/
public function hreflang(string $lang, string $url): MetaTags
{
return $this->push('link', [
'rel' => 'alternate',
'href' => $url,
'hreflang' => $lang,
]);
}
/**
* Set a meta tag
*
* @param string $name
* @param string $value
* @return MetaTags
*/
public function meta(string $name, string $value): MetaTags
{
return $this->push('meta', [
'name' => $name,
'content' => $value,
]);
}
/**
* Append new tag
*
* @param string $name
* @param array $attrs
* @return MetaTags
*/
public function push(string $name, array $attrs): MetaTags
{
foreach ($attrs as $k => $v)
{
$attrs[$k] = $v;
}
$this->tags[] = [$name, $attrs];
return $this;
}
/**
* Set a open graph tag
*
* @param string $name
* @param string $value
* @return MetaTags
*/
public function og(string $name, string $value): MetaTags
{
$this->openGraphTags[] = ['meta', ['property' => "og:{$name}", 'content' => $value]];
return $this;
}
/**
* Set a twitter tag
*
* @param string $name
* @param string $value
* @return MetaTags
*/
public function twitter(string $name, string $value): MetaTags
{
$this->twitterTags[] = ['meta', ['property' => "twitter:{$name}", 'content' => $value]];
return $this;
}
/**
* Set short link tag
*
* @param string $url
* @return MetaTags
*/
public function shortlink(string $url): MetaTags
{
return $this->push('link', [
'rel' => 'shortlink',
'href' => $url
]);
}
/**
* Set image meta
*
* @param string $url
* @param string $card Twitter card
* @return MetaTags
*/
public function image(string $url, string $card = 'summary_large_image'): MetaTags
{
return $this->og('image', $url)->twitter('card', $card)->twitter('image', $url);
}
/**
* Build meta tags
*
* @param array $tags
* @return string
*/
public function build(array $tags): string
{
$out = '';
foreach ($tags as $tag)
{
$out .= "\n<{$tag[0]} ";
foreach ($tag[1] as $a => $v)
{
$out .= $a .'="'. Helper::escape($v) .'" ';
}
$out .= "/>";
}
return $out;
}
/**
* Object to string
*
* @return string
*/
public function __toString(): string
{
$title = '';
if ($this->title !== null) {
$title = "<title>{$this->title}</title>";
}
return $title . $this->build($this->tags) . $this->build($this->twitterTags) . $this->build($this->openGraphTags) ;
}
}