Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
opengraph.php
1<?
2
4
7
8class OpenGraph extends Parser
9{
16 public function handle(HtmlDocument $document)
17 {
18 if($document->getTitle() == '')
19 {
20 $ogTitle = $document->getMetaContent('og:title');
21 if($ogTitle <> '')
22 {
23 $document->setTitle($ogTitle);
24 }
25 }
26
27 if($document->getDescription() == '')
28 {
29 $ogDescription = $document->getMetaContent('og:description');
30 if($ogDescription <> '')
31 {
32 $document->setDescription($ogDescription);
33 }
34 }
35
36 if($document->getImage() == '')
37 {
38 $ogImage = $document->getMetaContent('og:image:secure_url') ?: $document->getMetaContent('og:image');
39 if($ogImage <> '')
40 {
41 $document->setImage($ogImage);
42 }
43 }
44
45 $this->parseVideoData($document);
46
47 if(!$document->getExtraField('SITE_NAME'))
48 {
49 $ogSiteName = $document->getMetaContent('og:site_name');
50 if($ogSiteName <> '')
51 {
52 $document->setExtraField('SITE_NAME', $ogSiteName);
53 }
54 }
55
56 /* Not really opengraph property :), but it's placed in opengraph parser to prevent executing full parser chain
57 just to get favicon */
58 if(!$document->getExtraField('FAVICON'))
59 {
60 if($favicon = $document->getLinkHref('icon'))
61 {
62 $document->setExtraField('FAVICON', $favicon);
63 }
64 }
65 }
66
67 protected function parseVideoData(HtmlDocument $document)
68 {
69 if(!$document->getExtraField('VIDEO'))
70 {
71 $ogVideo = $document->getMetaContent('og:video')
72 ?? $document->getMetaContent('og:video:secure_url')
73 ?? $document->getMetaContent('og:video:url')
74 ?? '';
75 if($ogVideo <> '')
76 {
77 $document->setExtraField('VIDEO', $ogVideo);
78 }
79 }
80
81 if(!$document->getExtraField('VIDEO_TYPE'))
82 {
83 $ogVideoType = $document->getMetaContent('og:video:type') ?? '';
84 if($ogVideoType <> '')
85 {
86 $document->setExtraField('VIDEO_TYPE', $ogVideoType);
87 }
88 }
89
90 if(!$document->getExtraField('VIDEO_WIDTH'))
91 {
92 $ogVideoWidth = $document->getMetaContent('og:video:width') ?? '';
93 if($ogVideoWidth <> '')
94 {
95 $document->setExtraField('VIDEO_WIDTH', $ogVideoWidth);
96 }
97 }
98
99 if(!$document->getExtraField('VIDEO_HEIGHT'))
100 {
101 $ogVideoHeight = $document->getMetaContent('og:video:height') ?? '';
102 if($ogVideoHeight <> '')
103 {
104 $document->setExtraField('VIDEO_HEIGHT', $ogVideoHeight);
105 }
106 }
107 }
108}
setExtraField($fieldName, $fieldValue)
parseVideoData(HtmlDocument $document)
Definition opengraph.php:67