-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Restructured AdvancedRobots functionality
- Loading branch information
Showing
5 changed files
with
133 additions
and
66 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
Classes/MetaTag/AdvancedRobots/AdvancedRobotsGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters