Skip to content

Commit

Permalink
[TASK] Restructured AdvancedRobots functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
RinyVT committed Dec 23, 2024
1 parent bb628c6 commit 6d25008
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 66 deletions.
73 changes: 73 additions & 0 deletions Classes/MetaTag/AdvancedRobots/AdvancedRobotsGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\MetaTag\AdvancedRobots;

use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Frontend\Page\PageInformation;
use YoastSeoForTypo3\YoastSeo\Record\Record;
use YoastSeoForTypo3\YoastSeo\Record\RecordService;

class AdvancedRobotsGenerator
{
public function __construct(
protected RecordService $recordService,
protected RobotsMetaTagManager $metaTagManager,
protected RobotsRulesGenerator $robotsRuleGenerator
) {}

/**
* @param array<string, mixed> $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<string, mixed> $params
* @return array<string, mixed>|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<string, mixed> $record
* @return array<string, bool>
*/
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),
];
}
}
30 changes: 30 additions & 0 deletions Classes/MetaTag/AdvancedRobots/RobotsMetaTagManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\MetaTag\AdvancedRobots;

use TYPO3\CMS\Core\MetaTag\MetaTagManagerInterface;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class RobotsMetaTagManager
{
protected MetaTagManagerInterface $manager;

public function __construct()
{
$metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
$this->manager = $metaTagManagerRegistry->getManagerForProperty('robots');
}

public function removeRobotsTag(): void
{
$this->manager->removeProperty('robots');
}

public function addRobotsTag(string $robots): void
{
$this->manager->addProperty('robots', $robots);
}
}
29 changes: 29 additions & 0 deletions Classes/MetaTag/AdvancedRobots/RobotsRulesGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\MetaTag\AdvancedRobots;

class RobotsRulesGenerator
{
/**
* @param array<string, bool> $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);
}
}
65 changes: 0 additions & 65 deletions Classes/MetaTag/AdvancedRobotsGenerator.php

This file was deleted.

2 changes: 1 addition & 1 deletion ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6d25008

Please sign in to comment.