Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
metaog.php
1<?php
3
4use \Bitrix\Landing\Hook;
5use \Bitrix\Landing\Internals\HookDataTable;
6use \Bitrix\Landing\File;
7use \Bitrix\Landing\Manager;
8use \Bitrix\Landing\Field;
9use \Bitrix\Landing\PublicAction;
10use \Bitrix\Landing\Landing\Seo;
11use \Bitrix\Main\Localization\Loc;
12
13Loc::loadMessages(__FILE__);
14
16{
21 protected function getMap()
22 {
23 return array(
24 'TITLE' => new Field\Textarea('TITLE', array(
25 'title' => Loc::getMessage('LANDING_HOOK_METAOG_TITLE'),
26 'placeholder' => Loc::getMessage('LANDING_HOOK_METAOG_TITLE_PLACEHOLDER'),
27 'maxlength' => 140,
28 'searchable' => true
29 )),
30 'DESCRIPTION' => new Field\Textarea('DESCRIPTION', array(
31 'title' => Loc::getMessage('LANDING_HOOK_METAOG_DESCRIPTION'),
32 'placeholder' => Loc::getMessage('LANDING_HOOK_METAOG_DESCRIPTION_PLACEHOLDER'),
33 'maxlength' => 300,
34 'searchable' => true
35 )),
36 'IMAGE' => new Field\Hidden('IMAGE', array(
37 'title' => Loc::getMessage('LANDING_HOOK_METAOG_PICTURE'),
38 'fetch_data_modification' => function($value)
39 {
41 {
42 if ($value > 0)
43 {
44 $path = File::getFilePath($value);
45 if ($path)
46 {
47 $path = Manager::getUrlFromFile($path);
48 return $path;
49 }
50 }
51 }
52 return $value;
53 }
54 ))
55 );
56 }
57
63 public static function getAllImages($entityType = Hook::ENTITY_TYPE_LANDING)
64 {
65 $images = array();
66 $res = HookDataTable::getList(array(
67 'select' => array(
68 'VALUE', 'ENTITY_ID'
69 ),
70 'filter' => array(
71 '=HOOK' => 'METAOG',
72 '=CODE' => 'IMAGE',
73 '=ENTITY_TYPE' => $entityType,
74 '=PUBLIC' => 'N'
75 )
76 ));
77 while ($row = $res->fetch())
78 {
79 $images[$row['ENTITY_ID']] = $row['VALUE'];
80 }
81
82 return $images;
83 }
84
89 public function getTitle()
90 {
91 return Loc::getMessage('LANDING_HOOK_METAOG_NAME');
92 }
93
98 public function enabledInEditMode()
99 {
100 return false;
101 }
102
107 public function enabled()
108 {
109 if ($this->issetCustomExec())
110 {
111 return true;
112 }
113
114 return
115 trim($this->fields['TITLE']) != '' ||
116 trim($this->fields['DESCRIPTION']) != '' ||
117 trim($this->fields['IMAGE']) != '';
118 }
119
124 public function exec()
125 {
126 if ($this->execCustom())
127 {
128 return;
129 }
130
131 $output = '';
132 $files = [];
133 $tags = [
134 'title' => \htmlspecialcharsbx(Seo::processValue('title', $this->fields['TITLE'])),
135 'description' => \htmlspecialcharsbx(Seo::processValue('description', $this->fields['DESCRIPTION'])),
136 'image' => trim($this->fields['IMAGE']),
137 'type' => 'website'
138 ];
139 foreach (['og', 'twitter'] as $rootTag)
140 {
141 foreach ($tags as $key => $val)
142 {
143 if ($key == 'image' && intval($val) > 0)
144 {
145 $val = intval($val);
146 if (!array_key_exists($val, $files))
147 {
148 $files[$val] = File::getFileArray($val);
149 }
150 $val = $files[$val];
151 }
152 if ($val)
153 {
154 if ($key == 'image')
155 {
156 if (is_array($val))
157 {
158 $val['SRC'] = Manager::getUrlFromFile($val['SRC']);
159 $output .= '<meta property="' . $rootTag . ':image" content="' . str_replace(' ', '%20', \htmlspecialcharsbx($val['SRC'])) . '" />';
160 if ($rootTag != 'twitter')
161 {
162 $output .=
163 '<meta property="' . $rootTag . ':image:width" content="' . $val['WIDTH'] . '" />' .
164 '<meta property="' . $rootTag . ':image:height" content="' . $val['HEIGHT'] . '" />';
165 }
166 }
167 else
168 {
169 $output .= '<meta property="' . $rootTag . ':image" content="' . str_replace(' ', '%20', \htmlspecialcharsbx($val)) . '" />';
170 }
171 if ($rootTag == 'twitter')
172 {
173 $output .= '<meta name="twitter:card" content="summary_large_image" />';
174 }
175 }
176 else
177 {
178 $output .= '<meta property="' . $rootTag . ':' . $key . '" content="' . $val . '" />';
179 }
180 }
181 }
182 }
183 if ($output)
184 {
185 Manager::setPageView('MetaOG', $output);
186 }
187 }
188}
static getFileArray($fileId)
Definition file.php:580
static getFilePath($fileId)
Definition file.php:600
static getAllImages($entityType=Hook::ENTITY_TYPE_LANDING)
Definition metaog.php:63
const ENTITY_TYPE_LANDING
Definition hook.php:25
static getUrlFromFile($file)
Definition manager.php:1067
static setPageView(string $marker, string $content, bool $skipTrim=false)
Definition manager.php:465
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29