1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace sokolnikov911\YandexTurboPages\helpers; |
4
|
|
|
|
5
|
|
|
use sokolnikov911\YandexTurboPages\Channel; |
6
|
|
|
|
7
|
|
|
class Content |
8
|
|
|
{ |
9
|
|
|
const SHARE_TYPE_FACEBOOK = 'facebook'; |
10
|
|
|
const SHARE_TYPE_GOOGLE = 'google'; |
11
|
|
|
const SHARE_TYPE_ODNOKLASSNIKI = 'odnoklassniki'; |
12
|
|
|
const SHARE_TYPE_TELEGRAM = 'telegram'; |
13
|
|
|
const SHARE_TYPE_TWITTER = 'twitter'; |
14
|
|
|
const SHARE_TYPE_VKONTAKTE = 'vkontakte'; |
15
|
|
|
|
16
|
|
|
const SLIDER_DATA_VIEW_SQUARE = 'square'; |
17
|
|
|
const SLIDER_DATA_VIEW_PORTRAIT = 'portrait'; |
18
|
|
|
const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape'; |
19
|
|
|
|
20
|
|
|
const SLIDER_DATA_ITEM_VIEW_COVER = 'cover'; |
21
|
|
|
const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain'; |
22
|
|
|
|
23
|
|
|
const OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generate header element |
27
|
|
|
* @param string $h1 |
28
|
|
|
* @param string|null $h2 |
29
|
|
|
* @param string|null $imgUrl |
30
|
|
|
* @param string|null $imgCaption |
31
|
|
|
* @param array|null $menuArray array of arrays with pairs of url and content |
32
|
|
|
* [ |
33
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
34
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
35
|
|
|
* ] |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
9 |
|
public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
39
|
|
|
string $imgCaption = null, array $menuArray = null): string |
40
|
|
|
{ |
41
|
9 |
|
$header = '<h1>' . $h1 . '</h1>'; |
42
|
9 |
|
$header .= $h2 ? '<h2>' . $h2 . '</h2>' : ''; |
43
|
9 |
|
$header .= $menuArray ? self::generateMenu($menuArray) : ''; |
44
|
9 |
|
$header .= $imgUrl ? self::img($imgUrl, $imgCaption) : ''; |
45
|
|
|
|
46
|
9 |
|
return '<header>' . $header . '</header>'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generate image element |
51
|
|
|
* @param string $imgUrl |
52
|
|
|
* @param string|null $imgCaption |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
9 |
|
public static function img(string $imgUrl, string $imgCaption = null): string |
56
|
|
|
{ |
57
|
9 |
|
$imageString = '<img src="' . $imgUrl . '" />'; |
58
|
|
|
|
59
|
9 |
|
$imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : ''; |
60
|
|
|
|
61
|
9 |
|
return '<figure>' . $imageString . '</figure>'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate video element |
66
|
|
|
* @param string $videoUrl |
67
|
|
|
* @param string|null $videoCaption |
68
|
|
|
* @param string $imgUrl |
69
|
|
|
* @param string $type |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
6 |
|
public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
73
|
|
|
{ |
74
|
6 |
|
$videoString = '<video><source src="' . $videoUrl . '" type="' . $type . '" /></video>'; |
75
|
6 |
|
$videoString .= $imgUrl ? '<img src="' . $imgUrl . '" />' : ''; |
76
|
6 |
|
$videoString .= $videoCaption ? '<figcaption>' . $videoCaption . '</figcaption>' : ''; |
77
|
|
|
|
78
|
6 |
|
return '<figure>' . $videoString . '</figure>'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate video element for external video |
83
|
|
|
* @param string $videoUrl |
84
|
|
|
* @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen, |
85
|
|
|
* referrerpolicy, sandbox, hd. Options example: |
86
|
|
|
* [ |
87
|
|
|
* 'width' => 640, |
88
|
|
|
* 'height' => 480, |
89
|
|
|
* 'frameborder' => 1, |
90
|
|
|
* 'allowfullscreen' => 'true', |
91
|
|
|
* 'referrerpolicy' => 'unsafe-url', |
92
|
|
|
* 'sandbox' => 'allow-forms allow-modals', |
93
|
|
|
* 'hd' => 3 |
94
|
|
|
* ] |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
6 |
|
public static function externalVideo(string $videoUrl, array $options = []): string |
98
|
|
|
{ |
99
|
6 |
|
$videoString = '<iframe src="' . $videoUrl . '"'; |
100
|
|
|
|
101
|
6 |
|
foreach ($options as $key => $value) { |
102
|
3 |
|
$videoString .= ' ' . $key . '="' . $value . '"'; |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
$videoString .= '></iframe>'; |
106
|
|
|
|
107
|
6 |
|
return $videoString; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Generate images gallery |
112
|
|
|
* @param array $imagesArray Array of images urls |
113
|
|
|
* ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
114
|
|
|
* @param string|null $header |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
6 |
|
public static function gallery(array $imagesArray, string $header = null): string |
118
|
|
|
{ |
119
|
6 |
|
$galleryString = $header ? '<header>' . $header . '</header>' : ''; |
120
|
|
|
|
121
|
6 |
|
foreach ($imagesArray as $image) { |
122
|
6 |
|
$galleryString .= '<img src="' . $image . '" />'; |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
return '<div data-block="gallery">' . $galleryString . '</div>'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generate media slider |
130
|
|
|
* @param array $itemsArray Array of items with data |
131
|
|
|
* [ |
132
|
|
|
* ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
133
|
|
|
* ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
134
|
|
|
* ['url' => 'http://example.com/image3.jpg'], |
135
|
|
|
* ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
136
|
|
|
* ] |
137
|
|
|
* @param string|null $header |
138
|
|
|
* @param string $dataView |
139
|
|
|
* @param string $dataItemView |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
6 |
|
public static function slider(array $itemsArray, string $header = null, |
143
|
|
|
string $dataView = self::SLIDER_DATA_VIEW_SQUARE, |
144
|
|
|
string $dataItemView = self::SLIDER_DATA_ITEM_VIEW_COVER): string |
145
|
|
|
{ |
146
|
6 |
|
$sliderString = $header ? '<header>' . $header . '</header>' : ''; |
147
|
|
|
|
148
|
6 |
|
$sliderString .= self::generateSliderItemsBlock($itemsArray); |
149
|
|
|
|
150
|
6 |
|
return '<div data-block="slider" data-view="' . $dataView . '" data-item-view="' |
151
|
6 |
|
. $dataItemView . '">' . $sliderString . '</div>'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Generate share block |
156
|
|
|
* @param array|null $networks Array of network names |
157
|
|
|
* [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
158
|
|
|
* Can be empty, in this way all possible network types will be showed. |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
6 |
|
public static function share(array $networks = null): string |
162
|
|
|
{ |
163
|
6 |
|
$networksString = $networks |
164
|
3 |
|
? 'data-network="' . implode(',', $networks) . '"' |
165
|
6 |
|
: ''; |
166
|
|
|
|
167
|
6 |
|
return '<div data-block="share" ' . $networksString . '></div>'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Generate rating block |
172
|
|
|
* @param float $currentRating |
173
|
|
|
* @param float $maxRating |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
12 |
|
public static function rating(float $currentRating, float $maxRating): string |
177
|
|
|
{ |
178
|
12 |
|
if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) { |
179
|
9 |
|
throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0."); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return '<div itemscope="" itemtype="http://schema.org/Rating"> |
183
|
3 |
|
<meta itemprop="ratingValue" content="' . $currentRating . '" /> |
184
|
3 |
|
<meta itemprop="bestRating" content="' . $maxRating . '" /> |
185
|
|
|
</div>'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Generate button |
190
|
|
|
* @param string $text |
191
|
|
|
* @param string $url |
192
|
|
|
* @param string $phone Phone number in RFC-3966 format |
193
|
|
|
* @param string|null $buttonColor Can be Text or HEX |
194
|
|
|
* @param string|null $textColor Can be Text or HEX |
195
|
|
|
* @param bool $isBoldText |
196
|
|
|
* @param bool $isDisabled |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
12 |
|
public static function button(string $text, string $url = '', string $phone = '', |
200
|
|
|
string $buttonColor = null, string $textColor = null, |
201
|
|
|
bool $isBoldText = false, bool $isDisabled = false): string |
202
|
|
|
{ |
203
|
12 |
|
if (!$url && !$phone) { |
204
|
3 |
|
throw new \UnexpectedValueException('Please set url or phone number for button'); |
205
|
|
|
} |
206
|
|
|
|
207
|
9 |
|
$formAction = $url ? $url : 'tel:' . $phone; |
208
|
9 |
|
$buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : ''; |
209
|
9 |
|
$textColorString = $textColor ? 'data-color="' . $textColor . '"' : ''; |
210
|
9 |
|
$isBoldTextString = $isBoldText ? 'data-primary="true"' : ''; |
211
|
9 |
|
$isDisabledString = $isDisabled ? 'disabled="true"' : ''; |
212
|
|
|
|
213
|
|
|
return "<button |
214
|
9 |
|
formaction=\"" . $formAction . "\" |
215
|
9 |
|
" . $buttonColorString . " |
216
|
9 |
|
" . $textColorString . " |
217
|
9 |
|
" . $isBoldTextString . " |
218
|
9 |
|
" . $isDisabledString . ">" . $text . "</button>"; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Generate comment block |
223
|
|
|
* @param string $url URL to comments page |
224
|
|
|
* @param array $commentsArray multidimensional or one-dimensional array of comments, |
225
|
|
|
* can has unlimited includes, example: |
226
|
|
|
* [ |
227
|
|
|
* [ |
228
|
|
|
* 'author' => 'First Author Name', |
229
|
|
|
* 'avatar' => 'http://example.com/user1.jpg', |
230
|
|
|
* 'title' => 'Comment Title', |
231
|
|
|
* 'subtitle' => '2017-12-10', |
232
|
|
|
* 'content' => 'Somme comment text', |
233
|
|
|
* 'comments' => [ |
234
|
|
|
* [ |
235
|
|
|
* 'author' => 'Third Author Name', |
236
|
|
|
* 'avatar' => 'http://example.com/user3.jpg', |
237
|
|
|
* 'title' => 'Comment Title', |
238
|
|
|
* 'subtitle' => '2017-12-12', |
239
|
|
|
* 'content' => 'Some answer text' |
240
|
|
|
* ], |
241
|
|
|
* [ |
242
|
|
|
* 'author' => 'Another Author Name', |
243
|
|
|
* 'avatar' => 'http://example.com/user4.jpg', |
244
|
|
|
* 'title' => 'Comment Title', |
245
|
|
|
* 'subtitle' => '2017-12-13', |
246
|
|
|
* 'content' => 'Another answer text' |
247
|
|
|
* ], |
248
|
|
|
* ] |
249
|
|
|
* ], |
250
|
|
|
* [ |
251
|
|
|
* 'author' => 'Second Author Name', |
252
|
|
|
* 'avatar' => 'http://example.com/user2.jpg', |
253
|
|
|
* 'title' => 'Comment Title', |
254
|
|
|
* 'subtitle' => '2017-12-11', |
255
|
|
|
* 'content' => 'Some comment text' |
256
|
|
|
* ], |
257
|
|
|
* ] |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
3 |
|
public static function comment(string $url, array $commentsArray): string |
261
|
|
|
{ |
262
|
3 |
|
$commentBlock = self::generateCommentBlock($commentsArray); |
263
|
|
|
|
264
|
3 |
|
return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>'; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Generate accordion |
269
|
|
|
* @param array $accordionArray array accordion elements |
270
|
|
|
* [ |
271
|
|
|
* ['title' => 'Page title 1', 'text' => 'Text 1'], |
272
|
|
|
* ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
273
|
|
|
* ] |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
3 |
|
public static function accordion(array $accordionArray): string |
277
|
|
|
{ |
278
|
3 |
|
$accordionString = '<div data-block="accordion">'; |
279
|
|
|
|
280
|
3 |
|
foreach ($accordionArray as $item) { |
281
|
3 |
|
$expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : ''; |
282
|
3 |
|
$accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>'; |
283
|
|
|
} |
284
|
|
|
|
285
|
3 |
|
$accordionString .= '</div>'; |
286
|
|
|
|
287
|
3 |
|
return $accordionString; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Generate Ad block position element |
292
|
|
|
* @param string $turboAdId value of $turboAdId used in Channel() class |
293
|
|
|
* @return string |
294
|
|
|
* |
295
|
|
|
* @see Channel::$adTurboAdId |
296
|
|
|
*/ |
297
|
3 |
|
public static function adBlockPosition(string $turboAdId): string |
298
|
|
|
{ |
299
|
3 |
|
return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>'; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Generate content block for media slider |
304
|
|
|
* @param array $itemsArray Array of items with data |
305
|
|
|
* [ |
306
|
|
|
* ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
307
|
|
|
* ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
308
|
|
|
* ['url' => 'http://example.com/image3.jpg'], |
309
|
|
|
* ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
310
|
|
|
* ] |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
6 |
|
private static function generateSliderItemsBlock(array $itemsArray): string |
314
|
|
|
{ |
315
|
6 |
|
$sliderString = ''; |
316
|
|
|
|
317
|
6 |
|
foreach ($itemsArray as $item) { |
318
|
6 |
|
$sliderString .= '<figure>'; |
319
|
|
|
|
320
|
6 |
|
if (isset($item['title'])) { |
321
|
6 |
|
$sliderString .= '<figcaption>' . $item['title'] . '</figcaption>'; |
322
|
|
|
} |
323
|
|
|
|
324
|
6 |
|
if (isset($item['url'])) { |
325
|
6 |
|
$sliderString .= '<img src="' . $item['url'] . '" />'; |
326
|
6 |
|
} elseif (isset($item['href'])) { |
327
|
6 |
|
$sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>'; |
328
|
|
|
} |
329
|
|
|
|
330
|
6 |
|
$sliderString .= '</figure>'; |
331
|
|
|
} |
332
|
|
|
|
333
|
6 |
|
return $sliderString; |
334
|
|
|
} |
335
|
|
|
|
336
|
3 |
|
private static function generateCommentBlock(array $commentsArray): string |
337
|
|
|
{ |
338
|
3 |
|
$commentBlock = ''; |
339
|
|
|
|
340
|
3 |
|
foreach ($commentsArray as $commentArray) { |
341
|
3 |
|
$author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : ''; |
342
|
3 |
|
$avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : ''; |
343
|
3 |
|
$subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : ''; |
344
|
|
|
|
345
|
|
|
$commentBlock .= '<div |
346
|
|
|
data-block="comment" |
347
|
3 |
|
' . $author . ' |
348
|
3 |
|
' . $avatar . ' |
349
|
3 |
|
' . $subtitle . ' |
350
|
|
|
><div data-block="content">'; |
351
|
|
|
|
352
|
3 |
|
$commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : ''; |
353
|
3 |
|
$commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : ''; |
354
|
|
|
|
355
|
3 |
|
if (isset($commentArray['comments'])) { |
356
|
3 |
|
$commentBlock .= '<div data-block="comments">'; |
357
|
3 |
|
$commentBlock .= self::generateCommentBlock($commentArray['comments']); |
358
|
3 |
|
$commentBlock .= '</div>'; |
359
|
|
|
} |
360
|
|
|
|
361
|
3 |
|
$commentBlock .= '</div>'; |
362
|
|
|
} |
363
|
|
|
|
364
|
3 |
|
return $commentBlock; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Generate header menu |
369
|
|
|
* @param array $menuArray array of arrays with pairs of url and title |
370
|
|
|
* [ |
371
|
|
|
* ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
372
|
|
|
* ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
373
|
|
|
* ] |
374
|
|
|
* @return string |
375
|
|
|
*/ |
376
|
3 |
|
private static function generateMenu(array $menuArray) |
377
|
|
|
{ |
378
|
3 |
|
$menuString = ''; |
379
|
|
|
|
380
|
3 |
|
foreach ($menuArray as $menuItem) { |
381
|
3 |
|
$menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>'; |
382
|
|
|
} |
383
|
|
|
|
384
|
3 |
|
return '<menu>' . $menuString . '</menu>'; |
385
|
|
|
} |
386
|
|
|
} |