diff --git a/inc/Smartling/Extensions/Acf/AcfTypeDetector.php b/inc/Smartling/Extensions/Acf/AcfTypeDetector.php index 9e93270c..4ff587ae 100644 --- a/inc/Smartling/Extensions/Acf/AcfTypeDetector.php +++ b/inc/Smartling/Extensions/Acf/AcfTypeDetector.php @@ -5,143 +5,78 @@ use Smartling\Bootstrap; use Smartling\Helpers\Cache; use Smartling\Helpers\ContentHelper; +use Smartling\Helpers\LoggerSafeTrait; use Smartling\Helpers\MetaFieldProcessor\CustomFieldFilterHandler; use Smartling\Helpers\MetaFieldProcessor\MetaFieldProcessorInterface; -use Smartling\MonologWrapper\MonologWrapper; use Smartling\Submissions\SubmissionEntity; class AcfTypeDetector { + use LoggerSafeTrait; public const ACF_FIELD_GROUP_REGEX = '#(field|group)_([0-9a-f]){13}#'; - /** - * Default cache time (1 day) - * @var int - */ - public static $cacheExpireSec = 84600; - - /** - * @var Cache - */ - private $cache; - - /** - * @var ContentHelper - */ - private $contentHelper; - - /** - * @return Cache - */ - public function getCache() - { - return $this->cache; - } - - /** - * @param Cache $cache - */ - public function setCache($cache) - { - $this->cache = $cache; - } - - /** - * @return ContentHelper - */ - public function getContentHelper() - { - return $this->contentHelper; - } + private const CACHE_EXPIRE_SEC = 84600; - /** - * @param ContentHelper $contentHelper - */ - public function setContentHelper($contentHelper) + private function getCacheKeyByFieldName(string $fieldName): string { - $this->contentHelper = $contentHelper; + return 'acf-field-type-cache-'. $fieldName; } - private function getCacheKeyByFieldName($fieldName) - { - return vsprintf('acf-field-type-cache-%s', [$fieldName]); - } + public function __construct(private ContentHelper $contentHelper, private Cache $cache) {} - /** - * AcfTypeDetector constructor. - * - * @param ContentHelper $contentHelper - * @param Cache $cache - */ - public function __construct(ContentHelper $contentHelper, Cache $cache) + private function getFieldKeyFieldName(string $fieldName, SubmissionEntity $submission): ?string { - $this->setCache($cache); - $this->setContentHelper($contentHelper); - } - - /** - * @param string $fieldName - * @param SubmissionEntity $submission - * - * @return false|string - */ - private function getFieldKeyFieldName($fieldName, SubmissionEntity $submission) - { - if (false === $fieldKey = $this->getCache()->get($this->getCacheKeyByFieldName($fieldName))) { - $sourceMeta = $this->getContentHelper()->readSourceMetadata($submission); + if (false === $fieldKey = $this->cache->get($this->getCacheKeyByFieldName($fieldName))) { + $sourceMeta = $this->contentHelper->readSourceMetadata($submission); return $this->getFieldKeyFieldNameByMetaFields($fieldName, $sourceMeta); } - return $fieldKey; + + return is_string($fieldKey) ? $fieldKey : null; } - private function getFieldKeyFieldNameByMetaFields($fieldName, array $metadata) + private function getFieldKeyFieldNameByMetaFields(string $fieldName, array $metadata): ?string { - if (false === $fieldKey = $this->getCache()->get($this->getCacheKeyByFieldName($fieldName))) { - $_realFieldName = preg_replace('#^meta\/#ius', '', $fieldName); + if (false === $fieldKey = $this->cache->get($this->getCacheKeyByFieldName($fieldName))) { + $_realFieldName = preg_replace('#^meta/#', '', $fieldName); if (array_key_exists('_' . $_realFieldName, $metadata)) { $fieldKey = $metadata['_' . $_realFieldName]; - $this->getCache()->set($this->getCacheKeyByFieldName($fieldName), $fieldKey, static::$cacheExpireSec); + $this->cache->set($this->getCacheKeyByFieldName($fieldName), $fieldKey, self::CACHE_EXPIRE_SEC); } else { - return false; + return null; } } - return $fieldKey; + return is_string($fieldKey) ? $fieldKey : null; } - private function getProcessorByFieldKey($key, $fieldName) + private function getProcessorByFieldKey(string $key, string $fieldName): ?MetaFieldProcessorInterface { if (!array_key_exists($key, AcfDynamicSupport::$acfReverseDefinitionAction)) { - MonologWrapper::getLogger(__CLASS__) - ->info(vsprintf('No definition found for field \'%s\', key \'%s\'', [$fieldName, $key])); + $this->getLogger()->info(vsprintf('No definition found for field \'%s\', key \'%s\'', [$fieldName, $key])); - return false; + return null; } - $conf = AcfDynamicSupport::$acfReverseDefinitionAction[$key]; - $config = array_merge($conf, ['pattern' => vsprintf('^%s$', [$fieldName])]); - return CustomFieldFilterHandler::getProcessor(Bootstrap::getContainer(), $config); + return CustomFieldFilterHandler::getProcessor(Bootstrap::getContainer(), array_merge( + AcfDynamicSupport::$acfReverseDefinitionAction[$key], + ['pattern' => vsprintf('^%s$', [$fieldName])], + )); } - public function getProcessor($field, SubmissionEntity $submission) + public function getProcessor(string $field, SubmissionEntity $submission): ?MetaFieldProcessorInterface { return $this->getAcfProcessor($field, $this->getFieldKeyFieldName($field, $submission)); } - public function getProcessorByMetaFields($field, array $metaFields) + public function getProcessorByMetaFields(string $field, array $metaFields): ?MetaFieldProcessorInterface { return $this->getAcfProcessor($field, $this->getFieldKeyFieldNameByMetaFields($field, $metaFields)); } - /** - * @param string $field - * @param array $fields - * @return bool|MetaFieldProcessorInterface - */ - public function getProcessorForGutenberg($field, array $fields) + public function getProcessorForGutenberg(string $field, array $fields): ?MetaFieldProcessorInterface { $parts = explode('/', $field); $lastPart = end($parts); - if ($lastPart !== false && strpos($lastPart, '_') !== 0) { + if ($lastPart !== false && !str_starts_with($lastPart, '_')) { $parts[count($parts) - 1] = "_$lastPart"; $acfField = implode('/', $parts); if (array_key_exists($acfField, $fields) && is_string($fields[$acfField])) { @@ -149,10 +84,10 @@ public function getProcessorForGutenberg($field, array $fields) } } - return false; + return null; } - private function getAcfProcessor($field, $key) + private function getAcfProcessor(string $field, ?string $key): ?MetaFieldProcessorInterface { $matches = []; preg_match_all(self::ACF_FIELD_GROUP_REGEX, $key, $matches); @@ -161,6 +96,6 @@ private function getAcfProcessor($field, $key) return $this->getProcessorByFieldKey($fieldKey, $field); } - return false; + return null; } } diff --git a/inc/Smartling/Helpers/MetaFieldProcessor/CustomFieldFilterHandler.php b/inc/Smartling/Helpers/MetaFieldProcessor/CustomFieldFilterHandler.php index 4aa14627..1a450682 100644 --- a/inc/Smartling/Helpers/MetaFieldProcessor/CustomFieldFilterHandler.php +++ b/inc/Smartling/Helpers/MetaFieldProcessor/CustomFieldFilterHandler.php @@ -8,7 +8,7 @@ class CustomFieldFilterHandler { - public static $filters = []; + public static array $filters = []; private static function getLogger() { @@ -19,7 +19,7 @@ private static function getLogger() * @param ContainerBuilder $di * @param array $config */ - public static function registerFilter(ContainerBuilder $di, array $config) + public static function registerFilter(ContainerBuilder $di, array $config): void { self::getLogger()->debug(vsprintf('Registering filter for config: %s', [var_export($config, true)])); $parser = new FieldFilterConfigParser($config, $di); @@ -32,7 +32,7 @@ public static function registerFilter(ContainerBuilder $di, array $config) } } - public static function getProcessor(ContainerBuilder $di, array $config) + public static function getProcessor(ContainerBuilder $di, array $config): ?MetaFieldProcessorInterface { $parser = new FieldFilterConfigParser($config, $di); self::getLogger()->debug(vsprintf('looking for processor for config: %s', [var_export($config, true)])); @@ -40,7 +40,7 @@ public static function getProcessor(ContainerBuilder $di, array $config) return $parser->getFilter(); } - return false; + return null; } -} \ No newline at end of file +} diff --git a/inc/Smartling/Helpers/MetaFieldProcessor/MetaFieldProcessorManager.php b/inc/Smartling/Helpers/MetaFieldProcessor/MetaFieldProcessorManager.php index fc0ee1de..b2c3b587 100644 --- a/inc/Smartling/Helpers/MetaFieldProcessor/MetaFieldProcessorManager.php +++ b/inc/Smartling/Helpers/MetaFieldProcessor/MetaFieldProcessorManager.php @@ -100,10 +100,7 @@ public function getHandler(string $contentType): MetaFieldProcessorInterface private function tryGetAcfProcessor($fieldName, SubmissionEntity $submission, MetaFieldProcessorInterface $processor): MetaFieldProcessorInterface { if (in_array(get_class($processor), [DefaultMetaFieldProcessor::class, PostContentProcessor::class], true)) { - $_processor = $this->getAcfTypeDetector()->getProcessor($fieldName, $submission); - if (false !== $_processor) { - return $_processor; - } + return $this->getAcfTypeDetector()->getProcessor($fieldName, $submission) ?? $processor; } return $processor; diff --git a/inc/Smartling/Services/ContentRelationsDiscoveryService.php b/inc/Smartling/Services/ContentRelationsDiscoveryService.php index fd4cbefa..2e75a08c 100644 --- a/inc/Smartling/Services/ContentRelationsDiscoveryService.php +++ b/inc/Smartling/Services/ContentRelationsDiscoveryService.php @@ -519,10 +519,8 @@ public function getRelations(string $contentType, int $id, array $targetBlogIds) if ($processor instanceof DefaultMetaFieldProcessor) { $this->getLogger()->debug(vsprintf('Trying to treat \'%s\' field as ACF', [$fName])); $acfTypeDetector = $this->metaFieldProcessorManager->getAcfTypeDetector(); - $processor = $acfTypeDetector->getProcessorByMetaFields($fName, $content['meta']); - if ($processor === false) { - $processor = $acfTypeDetector->getProcessorForGutenberg($fName, $fields); - } + $processor = $acfTypeDetector->getProcessorByMetaFields($fName, $content['meta']) ?? + $acfTypeDetector->getProcessorForGutenberg($fName, $fields); } /**