Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
instagram.php
1<?php
2
4
11
12class Instagram extends Oembed
13{
14 protected $versionApiPrefix = 'v11.0';
15
16 protected const METADATA_TTL = 43200; // 12 hours
17
22 protected function detectOembedLink(HtmlDocument $document): bool
23 {
24 $result = false;
25
26 if(Loader::includeModule('socialservices'))
27 {
28 $urlPreviewEnable = Option::get('socialservices', 'facebook_instagram_url_preview_enable', 'N');
29 if($urlPreviewEnable === 'Y')
30 {
31 $urlAppId = Option::get('socialservices', 'facebook_appid', '');
32 $urlAppSecret = Option::get('socialservices', 'facebook_appsecret', '');
33
34 if(
35 !empty($urlAppId)
36 && !empty($urlAppSecret)
37 )
38 {
39 $this->metadataType = 'json';
40 $this->metadataUrl =
41 'https://graph.facebook.com/' . $this->versionApiPrefix . '/instagram_oembed?omitscript=true&url='
42 . $document->getUri()->getLocator()
43 . '&access_token='
44 . $urlAppId
45 . '|'
46 . $urlAppSecret;
47 $result = true;
48 }
49 }
50 }
51
52 return $result;
53 }
54
61 public function handle(HtmlDocument $document, HttpClient $httpClient = null)
62 {
63 if(
64 $this->detectOembedLink($document)
65 && $this->metadataUrl !== ''
66 )
67 {
68 if(!$httpClient)
69 {
70 $httpClient = $this->initHttpClient();
71 }
72 $rawMetadata = $this->getRawMetaData($httpClient);
73
74 if($rawMetadata !== false)
75 {
76 $parsedMetadata = $this->parseMetadata($rawMetadata);
77
78 if($parsedMetadata !== false)
79 {
80 if(
81 $this->metadataEncoding !== ''
82 && $document->getEncoding() !== $this->metadataEncoding)
83 {
84 $parsedMetadata = Encoding::convertEncoding($parsedMetadata, $this->metadataEncoding, $document->getEncoding());
85 }
86
87 if(isset($parsedMetadata['author_name']))
88 {
89 $document->setDescription('@' . $parsedMetadata['author_name']);
90 }
91
92 if(isset($parsedMetadata['provider_name']))
93 {
94 $document->setExtraField('PROVIDER_NAME', $parsedMetadata['provider_name']);
95 $document->setTitle($parsedMetadata['provider_name']);
96 }
97
98 if(isset($parsedMetadata['thumbnail_url']))
99 {
100 $document->setImage($parsedMetadata['thumbnail_url']);
101 }
102
103 $document->setDateExpire(DateTime::createFromTimestamp(time() + static::METADATA_TTL));
104 }
105 }
106 }
107 }
108}
static includeModule($moduleName)
Definition loader.php:69
static createFromTimestamp($timestamp)
Definition datetime.php:246
setExtraField($fieldName, $fieldValue)
setDateExpire(DateTime $dateExpire)
detectOembedLink(HtmlDocument $document)
Definition instagram.php:22
handle(HtmlDocument $document, HttpClient $httpClient=null)
Definition instagram.php:61
getRawMetaData(HttpClient $httpClient)
Definition oembed.php:187