Skip to content

Commit a04d91c

Browse files
Merge pull request #581 from Smartling/WP-956-acf-relations
add support for acf metafield relation arrays (WP-956)
2 parents 252d744 + 1218eff commit a04d91c

7 files changed

Lines changed: 72 additions & 18 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "smartling/wordpress-connector",
33
"license": "GPL-2.0-or-later",
4-
"version": "4.3.4",
4+
"version": "4.3.5",
55
"description": "",
66
"type": "wordpress-plugin",
77
"repositories": [

inc/Smartling/Extensions/Acf/AcfTypeDetector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ private function getFieldKeyFieldName($fieldName, SubmissionEntity $submission)
9696
private function getFieldKeyFieldNameByMetaFields($fieldName, array $metadata)
9797
{
9898
if (false === $fieldKey = $this->getCache()->get($this->getCacheKeyByFieldName($fieldName))) {
99-
$_realFieldName = preg_replace('#^meta\/#ius', '', $fieldName);
99+
$matches = [];
100+
$_realFieldName = preg_match('#^(?:meta/)?([^/]+)#i', $fieldName, $matches) ? $matches[1] : $fieldName;
100101
if (array_key_exists('_' . $_realFieldName, $metadata)) {
101102
$fieldKey = $metadata['_' . $_realFieldName];
102103
$this->getCache()->set($this->getCacheKeyByFieldName($fieldName), $fieldKey, static::$cacheExpireSec);
@@ -108,7 +109,7 @@ private function getFieldKeyFieldNameByMetaFields($fieldName, array $metadata)
108109
return $fieldKey;
109110
}
110111

111-
private function getProcessorByFieldKey($key, $fieldName)
112+
public function getProcessorByFieldKey($key, $fieldName)
112113
{
113114
if (!array_key_exists($key, AcfDynamicSupport::$acfReverseDefinitionAction)) {
114115
MonologWrapper::getLogger(__CLASS__)

inc/Smartling/Helpers/RelativeLinkedAttachmentCoreHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ public function processor(AfterDeserializeContentEventParameters $params): void
7474

7575
protected function processString(string|array &$stringValue): void
7676
{
77-
$replacer = new PairReplacerHelper();
78-
$matches = [];
79-
8077
if (is_array($stringValue)) {
8178
foreach ($stringValue as &$value) {
8279
$this->processString($value);
8380
}
8481
return;
8582
}
8683

84+
$replacer = new PairReplacerHelper();
85+
$matches = [];
86+
8787
if (0 < preg_match_all(self::ACF_GUTENBERG_BLOCK, $stringValue, $matches)) {
8888
// ACF has it's own processing because we know how it works with attachments
8989
// TODO move to GutenbergBlockHelper

inc/Smartling/Services/ContentRelationsDiscoveryService.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Smartling\Helpers\FileUriHelper;
2727
use Smartling\Helpers\GutenbergBlockHelper;
2828
use Smartling\Helpers\LoggerSafeTrait;
29+
use Smartling\Helpers\MetaFieldProcessor\BulkProcessors\PostBasedProcessor;
2930
use Smartling\Helpers\MetaFieldProcessor\DefaultMetaFieldProcessor;
3031
use Smartling\Helpers\MetaFieldProcessor\MetaFieldProcessorAbstract;
3132
use Smartling\Helpers\MetaFieldProcessor\MetaFieldProcessorManager;
@@ -528,10 +529,9 @@ public function getRelations(string $contentType, int $id, array $targetBlogIds)
528529
}
529530
}
530531

531-
/**
532-
* If processor is detected
533-
*/
534-
if ($processor instanceof MetaFieldProcessorAbstract && 0 !== (int)$fValue) {
532+
if ($processor instanceof PostBasedProcessor && is_array($this->wordpressProxy->maybe_unserialize($fValue))) {
533+
$detectedReferences[self::POST_BASED_PROCESSOR][] = $this->wordpressProxy->maybe_unserialize($fValue);
534+
} elseif ($processor instanceof MetaFieldProcessorAbstract && 0 !== (int)$fValue) {
535535
$shortProcessorName = ArrayHelper::last(explode('\\', get_class($processor)));
536536

537537
$detectedReferences[$shortProcessorName][] = (int)$fValue;
@@ -605,12 +605,21 @@ public function normalizeReferences(array $references): array
605605
}
606606

607607
if (isset($references[self::POST_BASED_PROCESSOR])) {
608-
foreach ($references[self::POST_BASED_PROCESSOR] as $postId) {
609-
$postType = $this->wordpressProxy->get_post_type($postId);
610-
if ($postType !== false) {
611-
$result[$postType][] = $postId;
612-
} else {
613-
$this->getLogger()->warning("WordPress returned no post exist for detected reference postId=$postId");
608+
foreach ($references[self::POST_BASED_PROCESSOR] as $reference) {
609+
if (!is_array($reference)) {
610+
$reference = [$reference];
611+
}
612+
foreach ($reference as $referenceId) {
613+
if (!is_numeric($referenceId)) {
614+
$this->getLogger()->warning("Got non-numeric reference postId=$referenceId, skipping");
615+
}
616+
$referenceId = (int)$referenceId;
617+
$postType = $this->wordpressProxy->get_post_type($referenceId);
618+
if ($postType !== false) {
619+
$result[$postType][] = $referenceId;
620+
} else {
621+
$this->getLogger()->warning("WordPress returned no post exists for detected reference postId=$referenceId, skipping");
622+
}
614623
}
615624
}
616625
}

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: translation, localization, localisation, translate, multilingual, smartlin
44
Requires at least: 5.5
55
Tested up to: 6.6.2
66
Requires PHP: 8.0
7-
Stable tag: 4.3.4
7+
Stable tag: 4.3.5
88
License: GPLv2 or later
99

1010
Translate content in WordPress quickly and seamlessly with Smartling, the industry-leading Translation Management System.
@@ -62,6 +62,9 @@ Additional information on the Smartling Connector for WordPress can be found [he
6262
3. Track translation status within WordPress from the Submissions Board. View overall progress of submitted translation requests as well as resend updated content.
6363

6464
== Changelog ==
65+
= 4.3.5 =
66+
* Added support for ACF metafields that have multiple relations
67+
6568
= 4.3.4 =
6669
* Fixed issue where related ACF content was not detected when using the upload widget
6770

smartling-connector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Plugin Name: Smartling Connector
1212
* Plugin URI: https://www.smartling.com/products/automate/integrations/wordpress/
1313
* Description: Integrate your WordPress site with Smartling to upload your content and download translations.
14-
* Version: 4.3.4
14+
* Version: 4.3.5
1515
* Author: Smartling
1616
* Author URI: https://www.smartling.com
1717
* License: GPL-2.0+

tests/Smartling/Extensions/Acf/AcfTypeDetectorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Smartling\Extensions\Acf\AcfDynamicSupport;
77
use Smartling\Extensions\Acf\AcfTypeDetector;
88
use Smartling\Helpers\ArrayHelper;
9+
use Smartling\Helpers\Cache;
910
use Smartling\Helpers\ContentHelper;
1011
use Smartling\Helpers\MetaFieldProcessor\BulkProcessors\MediaBasedProcessor;
1112
use Smartling\Helpers\SiteHelper;
@@ -32,6 +33,46 @@ protected function tearDown(): void
3233
$acf_stores = $this->acfStores;
3334
}
3435

36+
/**
37+
* @dataProvider providerGetProcessorByMetaFields
38+
*/
39+
public function testGetProcessorByMetaFields(string $fieldName, array $metaFields = [])
40+
{
41+
$cache = $this->createMock(Cache::class);
42+
$cache->method('get')->willReturn(false);
43+
$x = $this->getMockBuilder(AcfTypeDetector::class)
44+
->setConstructorArgs([
45+
new ContentHelper(
46+
$this->createMock(ContentEntitiesIOFactory::class),
47+
$this->createMock(SiteHelper::class),
48+
new WordpressFunctionProxyHelper()
49+
),
50+
$cache,
51+
])
52+
->onlyMethods(["getProcessorByFieldKey"])
53+
->getMock();
54+
$x->expects($this->once())->method("getProcessorByFieldKey")->with("field_6835dc2b65da8", $fieldName);
55+
$x->getProcessorByMetaFields($fieldName, $metaFields);
56+
}
57+
58+
private function providerGetProcessorByMetaFields()
59+
{
60+
return [
61+
[
62+
"meta/field/0",
63+
["field/0" => "irrelevant", "_field" => "field_6835dc2b65da8"],
64+
],
65+
[
66+
"meta/field",
67+
["field" => "irrelevant", "_field" => "field_6835dc2b65da8"],
68+
],
69+
[
70+
"field",
71+
["field" => "irrelevant", "_field" => "field_6835dc2b65da8"],
72+
],
73+
];
74+
}
75+
3576
public function testGetProcessorForGutenberg()
3677
{
3778
global $acf_stores;

0 commit comments

Comments
 (0)