diff --git a/Classes/MetaTag/AdvancedRobots/AdvancedRobotsGenerator.php b/Classes/MetaTag/AdvancedRobots/AdvancedRobotsGenerator.php new file mode 100644 index 00000000..8027c69c --- /dev/null +++ b/Classes/MetaTag/AdvancedRobots/AdvancedRobotsGenerator.php @@ -0,0 +1,73 @@ + $params + */ + public function generate(array $params): void + { + $record = $this->getRecord($params); + if ($record === null) { + return; + } + + $flags = $this->extractFlags($record); + if (array_sum($flags) === 0) { // All flags are false + return; + } + + $this->metaTagManager->removeRobotsTag(); + $robots = $this->robotsRuleGenerator->generateRules($flags); + $this->metaTagManager->addRobotsTag($robots); + } + + /** + * @param array $params + * @return array|null + */ + protected function getRecord(array $params): ?array + { + $activeRecord = $this->recordService->getActiveRecord(); + if ($activeRecord instanceof Record && $activeRecord->shouldGenerateRobotsTag()) { + return $activeRecord->getRecordData(); + } + if (isset($params['request']) && $params['request'] instanceof ServerRequest) { + $pageInfo = $params['request']->getAttribute('frontend.page.information'); + if ($pageInfo instanceof PageInformation) { + return $pageInfo->getPageRecord(); + } + } + return $params['page'] ?? null; + } + + /** + * @param array $record + * @return array + */ + protected function extractFlags(array $record): array + { + return [ + 'noImageIndex' => (bool)($record['tx_yoastseo_robots_noimageindex'] ?? false), + 'noArchive' => (bool)($record['tx_yoastseo_robots_noarchive'] ?? false), + 'noSnippet' => (bool)($record['tx_yoastseo_robots_nosnippet'] ?? false), + 'noIndex' => (bool)($record['no_index'] ?? false), + 'noFollow' => (bool)($record['no_follow'] ?? false), + ]; + } +} diff --git a/Classes/MetaTag/AdvancedRobots/RobotsMetaTagManager.php b/Classes/MetaTag/AdvancedRobots/RobotsMetaTagManager.php new file mode 100644 index 00000000..ac5c56c2 --- /dev/null +++ b/Classes/MetaTag/AdvancedRobots/RobotsMetaTagManager.php @@ -0,0 +1,30 @@ +manager = $metaTagManagerRegistry->getManagerForProperty('robots'); + } + + public function removeRobotsTag(): void + { + $this->manager->removeProperty('robots'); + } + + public function addRobotsTag(string $robots): void + { + $this->manager->addProperty('robots', $robots); + } +} diff --git a/Classes/MetaTag/AdvancedRobots/RobotsRulesGenerator.php b/Classes/MetaTag/AdvancedRobots/RobotsRulesGenerator.php new file mode 100644 index 00000000..7d26afdc --- /dev/null +++ b/Classes/MetaTag/AdvancedRobots/RobotsRulesGenerator.php @@ -0,0 +1,29 @@ + $flags + */ + public function generateRules(array $flags): string + { + $robots = []; + if ($flags['noImageIndex']) { + $robots[] = 'noimageindex'; + } + if ($flags['noArchive']) { + $robots[] = 'noarchive'; + } + if ($flags['noSnippet']) { + $robots[] = 'nosnippet'; + } + $robots[] = $flags['noIndex'] ? 'noindex' : 'index'; + $robots[] = $flags['noFollow'] ? 'nofollow' : 'follow'; + + return implode(',', $robots); + } +} diff --git a/Classes/MetaTag/AdvancedRobotsGenerator.php b/Classes/MetaTag/AdvancedRobotsGenerator.php deleted file mode 100644 index 95918d89..00000000 --- a/Classes/MetaTag/AdvancedRobotsGenerator.php +++ /dev/null @@ -1,65 +0,0 @@ - $params - */ - public function generate(array $params): void - { - $activeRecord = $this->recordService->getActiveRecord(); - if ($activeRecord instanceof Record && $activeRecord->shouldGenerateRobotsTag()) { - $record = $activeRecord->getRecordData(); - } elseif (isset($params['request']) && $params['request'] instanceof ServerRequest && $params['request']->getAttribute('frontend.page.information') instanceof PageInformation) { - $record = $params['request']->getAttribute('frontend.page.information')->getPageRecord(); - } else { - $record = $params['page'] ?? null; - } - - if ($record === null) { - return; - } - - $noImageIndex = (bool)($record['tx_yoastseo_robots_noimageindex'] ?? false); - $noArchive = (bool)($record['tx_yoastseo_robots_noarchive'] ?? false); - $noSnippet = (bool)($record['tx_yoastseo_robots_nosnippet'] ?? false); - $noIndex = (bool)($record['no_index'] ?? false); - $noFollow = (bool)($record['no_follow'] ?? false); - - if ($noImageIndex || $noArchive || $noSnippet || $noIndex || $noFollow) { - $metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class); - $manager = $metaTagManagerRegistry->getManagerForProperty('robots'); - $manager->removeProperty('robots'); - - $robots = []; - if ($noImageIndex) { - $robots[] = 'noimageindex'; - } - if ($noArchive) { - $robots[] = 'noarchive'; - } - if ($noSnippet) { - $robots[] = 'nosnippet'; - } - $robots[] = $noIndex ? 'noindex' : 'index'; - $robots[] = $noFollow ? 'nofollow' : 'follow'; - - $manager->addProperty('robots', implode(',', $robots)); - } - } -} diff --git a/ext_localconf.php b/ext_localconf.php index 41f606ed..b4f7c3b9 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -5,7 +5,7 @@ use YoastSeoForTypo3\YoastSeo\Backend\PageLayoutHeader; use YoastSeoForTypo3\YoastSeo\Frontend\AdditionalPreviewData; use YoastSeoForTypo3\YoastSeo\Frontend\UsePageCache; -use YoastSeoForTypo3\YoastSeo\MetaTag\AdvancedRobotsGenerator; +use YoastSeoForTypo3\YoastSeo\MetaTag\AdvancedRobots\AdvancedRobotsGenerator; use YoastSeoForTypo3\YoastSeo\MetaTag\RecordMetaTagGenerator; use YoastSeoForTypo3\YoastSeo\StructuredData\StructuredDataProviderManager; use YoastSeoForTypo3\YoastSeo\Updates\MigrateDashboardWidget;