-
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.
Merge pull request #511 from Yoast/feature/507-configure-records
[FEATURE] Possibility to automatically add Yoast SEO functionality to custom records
- Loading branch information
Showing
32 changed files
with
1,774 additions
and
335 deletions.
There are no files selected for viewing
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
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\EventListener; | ||
|
||
use YoastSeoForTypo3\YoastSeo\Record\Builder\AbstractBuilder; | ||
|
||
abstract class AbstractListener | ||
{ | ||
/** | ||
* @var \YoastSeoForTypo3\YoastSeo\Record\Builder\AbstractBuilder | ||
*/ | ||
protected AbstractBuilder $builder; | ||
|
||
public function __construct(AbstractBuilder $builder) | ||
{ | ||
$this->builder = $builder; | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\EventListener; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent; | ||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
use YoastSeoForTypo3\YoastSeo\Record\RecordService; | ||
|
||
class RecordCanonicalListener | ||
{ | ||
protected TypoScriptFrontendController $typoScriptFrontendController; | ||
|
||
protected RecordService $recordService; | ||
|
||
public function __construct(TypoScriptFrontendController $typoScriptFrontendController = null, RecordService $recordService = null) | ||
{ | ||
if ($typoScriptFrontendController === null) { | ||
$typoScriptFrontendController = GeneralUtility::makeInstance(TypoScriptFrontendController::class); | ||
} | ||
if ($recordService === null) { | ||
$recordService = GeneralUtility::makeInstance(RecordService::class); | ||
} | ||
|
||
$this->typoScriptFrontendController = $typoScriptFrontendController; | ||
$this->recordService = $recordService; | ||
} | ||
|
||
public function setCanonical(ModifyUrlForCanonicalTagEvent $event): void | ||
{ | ||
$activeRecord = $this->recordService->getActiveRecord(); | ||
if (!$activeRecord instanceof Record) { | ||
return; | ||
} | ||
|
||
$canonicalLink = $activeRecord->getRecordData()['canonical_link'] ?? ''; | ||
if (empty($canonicalLink)) { | ||
return; | ||
} | ||
|
||
$event->setUrl( | ||
$this->typoScriptFrontendController->cObj->typoLink_URL([ | ||
'parameter' => $canonicalLink, | ||
'forceAbsoluteUrl' => true, | ||
]) | ||
); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\EventListener; | ||
|
||
use TYPO3\CMS\Core\Database\Event\AlterTableDefinitionStatementsEvent; | ||
use YoastSeoForTypo3\YoastSeo\Record\RecordRegistry; | ||
|
||
class TableDefinitionsListener extends AbstractListener | ||
{ | ||
public function addDatabaseSchema(AlterTableDefinitionStatementsEvent $event): void | ||
{ | ||
foreach (RecordRegistry::getInstance()->getRecords() as $record) { | ||
$this->builder | ||
->setRecord($record) | ||
->build(); | ||
} | ||
$event->addSqlData(implode(LF . LF, $this->builder->getResult())); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\EventListener; | ||
|
||
use TYPO3\CMS\Core\Configuration\Event\AfterTcaCompilationEvent; | ||
use YoastSeoForTypo3\YoastSeo\Record\RecordRegistry; | ||
|
||
class TcaBuiltListener extends AbstractListener | ||
{ | ||
public function addRecordTca(AfterTcaCompilationEvent $event): void | ||
{ | ||
$GLOBALS['TCA'] = $event->getTca(); | ||
foreach (RecordRegistry::getInstance()->getRecords() as $record) { | ||
$this->builder | ||
->setRecord($record) | ||
->build(); | ||
} | ||
$event->setTca($GLOBALS['TCA']); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\Exception; | ||
|
||
class UnsupportedVersionException extends \Exception | ||
{ | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\MetaTag\Generator; | ||
|
||
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; | ||
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry; | ||
use TYPO3\CMS\Core\Resource\FileReference; | ||
use TYPO3\CMS\Core\Resource\ProcessedFile; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Service\ImageService; | ||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
|
||
abstract class AbstractGenerator | ||
{ | ||
protected MetaTagManagerRegistry $managerRegistry; | ||
|
||
public function __construct(MetaTagManagerRegistry $managerRegistry = null) | ||
{ | ||
if ($managerRegistry === null) { | ||
$managerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class); | ||
} | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
/** | ||
* @see \TYPO3\CMS\Seo\MetaTag\MetaTagGenerator | ||
* @param array $fileReferences | ||
* @return array | ||
*/ | ||
protected function generateSocialImages(array $fileReferences): array | ||
{ | ||
$imageService = GeneralUtility::makeInstance(ImageService::class); | ||
|
||
$socialImages = []; | ||
|
||
/** @var FileReference $file */ | ||
foreach ($fileReferences as $file) { | ||
$arguments = $file->getProperties(); | ||
$cropVariantCollection = CropVariantCollection::create((string)$arguments['crop']); | ||
$cropVariant = ($arguments['cropVariant'] ?? false) ?: 'social'; | ||
$cropArea = $cropVariantCollection->getCropArea($cropVariant); | ||
$crop = $cropArea->makeAbsoluteBasedOnFile($file); | ||
|
||
$processingConfiguration = [ | ||
'crop' => $crop, | ||
'maxWidth' => 2000, | ||
]; | ||
|
||
$processedImage = $file->getOriginalFile()->process( | ||
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, | ||
$processingConfiguration | ||
); | ||
|
||
$imageUri = $imageService->getImageUri($processedImage, true); | ||
|
||
$socialImages[] = [ | ||
'url' => $imageUri, | ||
'width' => floor($processedImage->getProperty('width')), | ||
'height' => floor($processedImage->getProperty('height')), | ||
'alternative' => $arguments['alternative'], | ||
]; | ||
} | ||
|
||
return $socialImages; | ||
} | ||
|
||
abstract public function generate(Record $record): void; | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\MetaTag\Generator; | ||
|
||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
|
||
class DescriptionGenerator extends AbstractGenerator | ||
{ | ||
public function generate(Record $record): void | ||
{ | ||
$description = $record->getRecordData()[$record->getDescriptionField()] ?? ''; | ||
|
||
if (!empty($description)) { | ||
$manager = $this->managerRegistry->getManagerForProperty('description'); | ||
$manager->removeProperty('description'); | ||
$manager->addProperty('description', $description); | ||
} | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\MetaTag\Generator; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Resource\FileCollector; | ||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
|
||
class OpenGraphGenerator extends AbstractGenerator | ||
{ | ||
public function generate(Record $record): void | ||
{ | ||
$ogTitle = $record->getRecordData()['og_title'] ?? ''; | ||
if (!empty($ogTitle)) { | ||
$manager = $this->managerRegistry->getManagerForProperty('og:title'); | ||
$manager->removeProperty('og:title'); | ||
$manager->addProperty('og:title', $ogTitle); | ||
} | ||
|
||
$ogDescription = $record->getRecordData()['og_description'] ?? ''; | ||
if ($ogDescription) { | ||
$manager = $this->managerRegistry->getManagerForProperty('og:description'); | ||
$manager->removeProperty('og:description'); | ||
$manager->addProperty('og:description', $ogDescription); | ||
} | ||
|
||
if ($record->getRecordData()['og_image']) { | ||
$fileCollector = GeneralUtility::makeInstance(FileCollector::class); | ||
$fileCollector->addFilesFromRelation($record->getTableName(), 'og_image', $record->getRecordData()); | ||
$manager = $this->managerRegistry->getManagerForProperty('og:image'); | ||
|
||
$ogImages = $this->generateSocialImages($fileCollector->getFiles()); | ||
if (count($ogImages) > 0) { | ||
$manager->removeProperty('og:image'); | ||
} | ||
foreach ($ogImages as $ogImage) { | ||
$subProperties = []; | ||
$subProperties['url'] = $ogImage['url']; | ||
$subProperties['width'] = $ogImage['width']; | ||
$subProperties['height'] = $ogImage['height']; | ||
|
||
if (!empty($ogImage['alternative'])) { | ||
$subProperties['alt'] = $ogImage['alternative']; | ||
} | ||
|
||
$manager->addProperty( | ||
'og:image', | ||
$ogImage['url'], | ||
$subProperties | ||
); | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\MetaTag\Generator; | ||
|
||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
|
||
class RobotsGenerator extends AbstractGenerator | ||
{ | ||
public function generate(Record $record): void | ||
{ | ||
if (!isset($record->getRecordData()['no_index'], $record->getRecordData()['no_follow'])) { | ||
return; | ||
} | ||
|
||
$noIndex = ((bool)$record->getRecordData()['no_index']) ? 'noindex' : 'index'; | ||
$noFollow = ((bool)$record->getRecordData()['no_follow']) ? 'nofollow' : 'follow'; | ||
|
||
if ($noIndex === 'noindex' || $noFollow === 'nofollow') { | ||
$manager = $this->managerRegistry->getManagerForProperty('robots'); | ||
$manager->removeProperty('robots'); | ||
$manager->addProperty('robots', implode(',', [$noIndex, $noFollow])); | ||
} | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace YoastSeoForTypo3\YoastSeo\MetaTag\Generator; | ||
|
||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Frontend\Resource\FileCollector; | ||
use YoastSeoForTypo3\YoastSeo\Record\Record; | ||
|
||
class TwitterGenerator extends AbstractGenerator | ||
{ | ||
public function generate(Record $record): void | ||
{ | ||
$twitterCard = $record->getRecordData()['twitter_card'] ?? ''; | ||
if (!empty($twitterCard)) { | ||
$manager = $this->managerRegistry->getManagerForProperty('twitter:card'); | ||
$manager->removeProperty('twitter:card'); | ||
$manager->addProperty('twitter:card', $twitterCard); | ||
} | ||
|
||
$twitterTitle = $record->getRecordData()['twitter_title'] ?? ''; | ||
if (!empty($twitterTitle)) { | ||
$manager = $this->managerRegistry->getManagerForProperty('twitter:title'); | ||
$manager->removeProperty('twitter:title'); | ||
$manager->addProperty('twitter:title', $twitterTitle); | ||
} | ||
|
||
$twitterDescription = $record->getRecordData()['twitter_description']; | ||
if (!empty($twitterDescription)) { | ||
$manager = $this->managerRegistry->getManagerForProperty('twitter:description'); | ||
$manager->removeProperty('twitter:description'); | ||
$manager->addProperty('twitter:description', $twitterDescription); | ||
} | ||
|
||
if ($record->getRecordData()['twitter_image']) { | ||
$fileCollector = GeneralUtility::makeInstance(FileCollector::class); | ||
$fileCollector->addFilesFromRelation($record->getTableName(), 'twitter_image', $record->getRecordData()); | ||
$manager = $this->managerRegistry->getManagerForProperty('twitter:image'); | ||
|
||
$twitterImages = $this->generateSocialImages($fileCollector->getFiles()); | ||
if (count($twitterImages) > 0) { | ||
$manager->removeProperty('twitter:image'); | ||
} | ||
foreach ($twitterImages as $twitterImage) { | ||
$subProperties = []; | ||
|
||
if (!empty($twitterImage['alternative'])) { | ||
$subProperties['alt'] = $twitterImage['alternative']; | ||
} | ||
|
||
$manager->addProperty( | ||
'twitter:image', | ||
$twitterImage['url'], | ||
$subProperties | ||
); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.