a0b4caad1415ea0246c8817b2292f0ab8deffd81
[Packages/TYPO3.CMS.git] / typo3 / sysext / seo / Classes / MetaTag / MetaTagGenerator.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Seo\MetaTag;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
20 use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
21 use TYPO3\CMS\Core\Resource\FileReference;
22 use TYPO3\CMS\Core\Resource\ProcessedFile;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Extbase\Service\ImageService;
25 use TYPO3\CMS\Frontend\Resource\FileCollector;
26
27 /**
28 * Class to add the metatags for the SEO fields in core
29 *
30 * @internal
31 */
32 class MetaTagGenerator
33 {
34 /**
35 * Generate the meta tags that can be set in backend and add them to frontend by using the MetaTag API
36 *
37 * @param array $params
38 */
39 public function generate(array $params)
40 {
41 $metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
42
43 if (!empty($params['page']['description'])) {
44 $manager = $metaTagManagerRegistry->getManagerForProperty('description');
45 $manager->addProperty('description', $params['page']['description']);
46 }
47
48 if (!empty($params['page']['og_title'])) {
49 $manager = $metaTagManagerRegistry->getManagerForProperty('og:title');
50 $manager->addProperty('og:title', $params['page']['og_title']);
51 }
52
53 if (!empty($params['page']['og_description'])) {
54 $manager = $metaTagManagerRegistry->getManagerForProperty('og:description');
55 $manager->addProperty('og:description', $params['page']['og_description']);
56 }
57
58 if (!empty($params['page']['og_image'])) {
59 $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
60 $fileCollector->addFilesFromRelation('pages', 'og_image', $params['page']);
61 $manager = $metaTagManagerRegistry->getManagerForProperty('og:image');
62
63 $ogImages = $this->generateSocialImages($fileCollector->getFiles());
64 foreach ($ogImages as $ogImage) {
65 $subProperties = [];
66 $subProperties['url'] = $ogImage['url'];
67 $subProperties['width'] = $ogImage['width'];
68 $subProperties['height'] = $ogImage['height'];
69
70 if (!empty($ogImage['alternative'])) {
71 $subProperties['alt'] = $ogImage['alternative'];
72 }
73
74 $manager->addProperty(
75 'og:image',
76 $ogImage['url'],
77 $subProperties
78 );
79 }
80 }
81
82 if (!empty($params['page']['twitter_title'])) {
83 $manager = $metaTagManagerRegistry->getManagerForProperty('twitter:title');
84 $manager->addProperty('twitter:title', $params['page']['twitter_title']);
85 }
86
87 if (!empty($params['page']['twitter_description'])) {
88 $manager = $metaTagManagerRegistry->getManagerForProperty('twitter:description');
89 $manager->addProperty('twitter:description', $params['page']['twitter_description']);
90 }
91
92 if (!empty($params['page']['twitter_image'])) {
93 $fileCollector = GeneralUtility::makeInstance(FileCollector::class);
94 $fileCollector->addFilesFromRelation('pages', 'twitter_image', $params['page']);
95 $manager = $metaTagManagerRegistry->getManagerForProperty('twitter:image');
96
97 $twitterImages = $this->generateSocialImages($fileCollector->getFiles());
98 foreach ($twitterImages as $twitterImage) {
99 $subProperties = [];
100
101 if (!empty($twitterImage['alternative'])) {
102 $subProperties['alt'] = $twitterImage['alternative'];
103 }
104
105 $manager->addProperty(
106 'twitter:image',
107 $twitterImage['url'],
108 $subProperties
109 );
110 }
111 }
112
113 $noIndex = ((bool)$params['page']['no_index']) ? 'noindex' : 'index';
114 $noFollow = ((bool)$params['page']['no_follow']) ? 'nofollow' : 'follow';
115
116 if ($noIndex === 'noindex' || $noFollow === 'nofollow') {
117 $manager = $metaTagManagerRegistry->getManagerForProperty('robots');
118 $manager->addProperty('robots', implode(',', [$noIndex, $noFollow]));
119 }
120 }
121
122 /**
123 * @param array $fileReferences
124 * @return array
125 */
126 protected function generateSocialImages(array $fileReferences): array
127 {
128 $imageService = GeneralUtility::makeInstance(ImageService::class);
129
130 $socialImages = [];
131
132 /** @var FileReference $file */
133 foreach ($fileReferences as $file) {
134 $arguments = $file->getProperties();
135 $cropVariantCollection = CropVariantCollection::create((string)$arguments['crop']);
136 $cropVariant = $arguments['cropVariant'] ?: 'default';
137 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
138 $crop = $cropArea->makeAbsoluteBasedOnFile($file);
139
140 $cropInformation = $crop->asArray();
141
142 $processingConfiguration = [
143 'crop' => $crop
144 ];
145
146 $processedImage = $file->getOriginalFile()->process(
147 ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
148 $processingConfiguration
149 );
150
151 $imageUri = $imageService->getImageUri($processedImage, true);
152
153 $socialImages[] = [
154 'url' => $imageUri,
155 'width' => floor($cropInformation['width']),
156 'height' => floor($cropInformation['height']),
157 'alternative' => $arguments['alternative'],
158 ];
159 }
160
161 return $socialImages;
162 }
163 }