1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Page; |
9
|
|
|
use |
10
|
|
|
cs\Config, |
11
|
|
|
cs\Language, |
12
|
|
|
cs\Page, |
13
|
|
|
cs\Request, |
14
|
|
|
cs\Singleton, |
15
|
|
|
h; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Meta class for generation of various meta tags |
19
|
|
|
* |
20
|
|
|
* @method $this og(string $property, string|string[] $content) |
21
|
|
|
*/ |
22
|
|
|
class Meta { |
23
|
|
|
use |
24
|
|
|
Singleton; |
25
|
|
|
/** |
26
|
|
|
* Is used as <head prefix="$head_prefix"> |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $head_prefix = ''; |
30
|
|
|
/** |
31
|
|
|
* If false - <head> will not be added automatically, and should be in template if needed |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
public $no_head = false; |
35
|
|
|
protected $links = ''; |
36
|
|
|
protected $og_data = []; |
37
|
|
|
protected $og_type = ''; |
38
|
|
|
protected $image_src = false; |
39
|
|
|
/** |
40
|
|
|
* Common wrapper to add all necessary meta tags with images |
41
|
|
|
* |
42
|
|
|
* @param string|string[] $images |
43
|
|
|
* |
44
|
|
|
* @return Meta |
45
|
|
|
*/ |
46
|
2 |
|
function image ($images) { |
47
|
2 |
|
if (!$images) { |
48
|
2 |
|
return $this; |
49
|
|
|
} |
50
|
2 |
|
$images = (array)$images; |
51
|
2 |
|
if (!$this->image_src && $images[0]) { |
52
|
2 |
|
$this->image_src = true; |
53
|
2 |
|
$this->links .= h::link( |
54
|
|
|
[ |
55
|
2 |
|
'href' => $images[0], |
56
|
2 |
|
'rel' => 'image_src' |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
} |
60
|
2 |
|
$this->__call('og', ['image', $images]); |
61
|
2 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
/** |
64
|
|
|
* Common wrapper for generation of various Open Graph protocol meta tags |
65
|
|
|
* |
66
|
|
|
* @param string $type |
67
|
|
|
* @param mixed[] $params |
68
|
|
|
* |
69
|
|
|
* @return Meta |
70
|
|
|
*/ |
71
|
14 |
|
function __call ($type, $params) { |
72
|
14 |
|
if (!$params) { |
73
|
4 |
|
$this->og_type = $type; |
74
|
4 |
|
$this->og_data['type'] = h::meta( |
75
|
|
|
[ |
76
|
4 |
|
'property' => "og:type", |
|
|
|
|
77
|
4 |
|
'content' => $type |
78
|
|
|
] |
79
|
|
|
); |
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
14 |
|
if (!$params[0]) { |
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
14 |
|
if (is_array($params[1])) { |
86
|
6 |
|
foreach ($params[1] as $p) { |
87
|
6 |
|
$this->__call($type, [$params[0], $p]); |
88
|
|
|
} |
89
|
14 |
|
} elseif ($params[1] || $params[1] === 0) { |
90
|
14 |
|
if (!isset($this->og_data[$params[0]])) { |
91
|
14 |
|
$this->og_data[$params[0]] = ''; |
92
|
|
|
} |
93
|
14 |
|
$this->og_data[$params[0]] .= h::meta( |
94
|
|
|
[ |
95
|
14 |
|
'property' => "$type:$params[0]", |
96
|
14 |
|
'content' => $params[1] |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
} |
100
|
14 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* Generates Open Graph protocol information, and puts it into HTML |
104
|
|
|
* |
105
|
|
|
* Usually called by system itself, there is no need to call it manually |
106
|
|
|
*/ |
107
|
14 |
|
function render () { |
108
|
14 |
|
$og = &$this->og_data; |
109
|
14 |
|
$this->fill_required_properties($og); |
110
|
14 |
|
$prefix = 'og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#'; |
111
|
14 |
|
$type = explode('.', $this->og_type, 2)[0]; |
112
|
|
|
switch ($type) { |
113
|
14 |
|
case 'article': |
114
|
10 |
|
case 'blog': |
115
|
10 |
|
case 'book': |
116
|
10 |
|
case 'profile': |
117
|
10 |
|
case 'video': |
118
|
10 |
|
case 'website': |
119
|
4 |
|
$prefix .= " $type: http://ogp.me/ns/$type#"; |
120
|
4 |
|
break; |
121
|
|
|
} |
122
|
14 |
|
$Page = Page::instance(); |
123
|
14 |
|
$Page->Head = |
124
|
14 |
|
$Page->Head. |
125
|
14 |
|
implode('', $og). |
126
|
14 |
|
$this->links; |
127
|
14 |
|
if (!$this->no_head) { |
128
|
14 |
|
$Page->Head = h::head( |
129
|
14 |
|
$Page->Head, |
130
|
|
|
[ |
131
|
14 |
|
'prefix' => $prefix.$this->head_prefix |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
14 |
|
} |
136
|
|
|
/** |
137
|
|
|
* If type, title and other important properties were not specified - try to guess and fill them automatically |
138
|
|
|
* |
139
|
|
|
* @param array $og |
140
|
|
|
*/ |
141
|
14 |
|
protected function fill_required_properties (&$og) { |
142
|
14 |
|
$Page = Page::instance(); |
143
|
14 |
|
if (!@$og['title']) { |
144
|
12 |
|
$this->og('title', $Page->Title); |
145
|
|
|
} |
146
|
14 |
|
if (!@$og['description']) { |
147
|
12 |
|
$this->og('description', $Page->Description); |
148
|
|
|
} |
149
|
14 |
|
$Config = Config::instance(); |
150
|
14 |
|
if (!@$og['url']) { |
151
|
14 |
|
$Request = Request::instance(); |
152
|
|
|
/** @noinspection NestedTernaryOperatorInspection */ |
153
|
14 |
|
$this->og( |
154
|
14 |
|
'url', |
155
|
14 |
|
$Request->home_page |
156
|
12 |
|
? $Config->base_url() |
157
|
14 |
|
: ($Page->canonical_url ?: $Config->base_url().'/'.$Request->path_normalized) |
158
|
|
|
); |
159
|
|
|
} |
160
|
14 |
|
if (!@$og['site_name']) { |
161
|
14 |
|
$this->og('site_name', get_core_ml_text('name')); |
162
|
|
|
} |
163
|
14 |
|
if (!@$og['type']) { |
164
|
10 |
|
$this->og('type', 'website'); |
165
|
|
|
} |
166
|
14 |
|
$this->fill_required_properties_multilingual($og); |
167
|
14 |
|
} |
168
|
|
|
/* |
169
|
|
|
* @param array $og |
170
|
|
|
*/ |
171
|
14 |
|
protected function fill_required_properties_multilingual (&$og) { |
172
|
14 |
|
$Config = Config::instance(); |
173
|
14 |
|
if (!$Config->core['multilingual']) { |
174
|
12 |
|
return; |
175
|
|
|
} |
176
|
2 |
|
$L = Language::instance(); |
177
|
2 |
|
if (!@$og['locale']) { |
178
|
2 |
|
$this->og('locale', $L->clocale); |
179
|
|
|
} |
180
|
|
|
if ( |
181
|
2 |
|
!@$og['locale:alternate'] && |
182
|
2 |
|
count($Config->core['active_languages']) > 1 |
183
|
|
|
) { |
184
|
2 |
|
foreach ($Config->core['active_languages'] as $lang) { |
185
|
2 |
|
if ($lang != $L->clanguage) { |
186
|
2 |
|
$this->og('locale:alternate', $L->get('clocale', $lang)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
2 |
|
} |
191
|
|
|
} |
192
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.