Skip to content

refactor (WP-911) #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 31 additions & 96 deletions inc/Smartling/Extensions/Acf/AcfTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,154 +5,89 @@
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])) {
return $this->getAcfProcessor($acfField, $fields[$acfField]);
}
}

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);
Expand All @@ -161,6 +96,6 @@ private function getAcfProcessor($field, $key)
return $this->getProcessorByFieldKey($fieldKey, $field);
}

return false;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CustomFieldFilterHandler
{
public static $filters = [];
public static array $filters = [];

private static function getLogger()
{
Expand All @@ -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);
Expand All @@ -32,15 +32,15 @@ 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)]));
if (true === $parser->isValidFiler()) {
return $parser->getFilter();
}

return false;
return null;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions inc/Smartling/Services/ContentRelationsDiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down