1 | <?php |
||
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 () { |
|
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) { |
|
168 | /* |
||
169 | * @param array $og |
||
170 | */ |
||
171 | 14 | protected function fill_required_properties_multilingual (&$og) { |
|
191 | } |
||
192 |