Skip to content

Commit 8d235ca

Browse files
Merge pull request #522 from Smartling/WP-880-nested-attributes-locking
add nested attributes locking, add before clone content written filter (WP-880)
2 parents 42a374d + a686cff commit 8d235ca

14 files changed

Lines changed: 103 additions & 11 deletions

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": "3.8.12",
4+
"version": "3.8.13",
55
"description": "",
66
"type": "wordpress-plugin",
77
"repositories": [

inc/Smartling/Base/ExportedAPI.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ interface ExportedAPI
205205
*/
206206
public const ACTION_AFTER_TRANSLATION_APPLIED = 'smartling_action_after_translation_applied';
207207

208+
/**
209+
* @param array content
210+
* @param SubmissionEntity
211+
* @return array content
212+
*/
213+
public const FILTER_BEFORE_CLONE_CONTENT_WRITTEN = 'smartling_filter_before_clone_content_written';
208214
/**
209215
* @param array translation
210216
* @param array lockedData

inc/Smartling/Base/SmartlingCore.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function cloneContent(SubmissionEntity $submission): void
8888
$content[$key] = $this->gutenbergBlockHelper->replacePostTranslateBlockContent($value, $value, $submission);
8989
}
9090
}
91+
$content = apply_filters(ExportedAPI::FILTER_BEFORE_CLONE_CONTENT_WRITTEN, $content, $submission);
9192
$this->getContentHelper()->writeTargetContent($submission, $entity->fromArray($content));
9293

9394
$metadata = [];

inc/Smartling/Helpers/ArrayHelper.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class ArrayHelper
88
{
9+
private const NESTED_KEY_SEPARATOR = '.';
10+
911
/**
1012
* Retrieves the value of an array element or object property with the given key or property name.
1113
* If the key does not exist in the array or object, the default value will be returned instead.
@@ -49,7 +51,7 @@ public static function getValue(array|object $array, string|\Closure $key, mixed
4951
return $array[$key];
5052
}
5153

52-
if (($pos = strrpos($key, '.')) !== false) {
54+
if (($pos = strrpos($key, self::NESTED_KEY_SEPARATOR)) !== false) {
5355
$array = static::getValue($array, substr($key, 0, $pos), $default);
5456
$key = substr($key, $pos + 1);
5557
}
@@ -65,6 +67,23 @@ public static function getValue(array|object $array, string|\Closure $key, mixed
6567
return $default;
6668
}
6769

70+
public function setValue(array $array, string $key, mixed $value): array
71+
{
72+
$result = $array;
73+
$parts = explode(self::NESTED_KEY_SEPARATOR, $key);
74+
if (count($parts) > 1) {
75+
$index = array_shift($parts);
76+
if (!array_key_exists($index, $result)) {
77+
$result[$index] = [];
78+
}
79+
$result[$index] = $this->setValue($result[$index], implode(self::NESTED_KEY_SEPARATOR, $parts), $value);
80+
} else {
81+
$result[$key] = $value;
82+
}
83+
84+
return $result;
85+
}
86+
6887
/**
6988
* Removes an item from an array and returns the value. If the key does not exist in the array, the default value
7089
* will be returned instead.

inc/Smartling/Helpers/PostContentHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PostContentHelper
1313
public const SMARTLING_LOCKED = 'smartlingLocked';
1414
public const SMARTLING_LOCKED_ATTRIBUTES = 'smartlingLockedAttributes';
1515

16-
public function __construct(private GutenbergBlockHelper $blockHelper)
16+
public function __construct(private ArrayHelper $arrayHelper, private GutenbergBlockHelper $blockHelper)
1717
{
1818
}
1919

@@ -38,8 +38,9 @@ public function applyContentWithBlockLocks(string $target, string $content): str
3838
$targetBlock = $this->getBlockByPath($targetBlocks, $path);
3939
assert($targetBlock !== null);
4040
$attributes = $resultBlock->getAttributes();
41-
if (array_key_exists($attribute, $targetBlock->toArray()['attrs'])) {
42-
$attributes[$attribute] = $targetBlock->getAttributes()[$attribute];
41+
$targetValue = ArrayHelper::getValue($targetBlock->getAttributes(), $attribute);
42+
if ($targetValue !== null) {
43+
$attributes = $this->arrayHelper->setValue($attributes, $attribute, $targetValue);
4344
}
4445
$resultBlocks = $this->setBlockByPath($resultBlocks, $path, $resultBlock->withAttributes($attributes));
4546
}

inc/config/services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ services:
4040
- "@wp.proxy"
4141
- "@wp.link.helper"
4242

43+
helper.array:
44+
class: Smartling\Helpers\ArrayHelper
45+
4346
helper.customMenu:
4447
class: Smartling\Helpers\CustomMenuContentTypeHelper
4548
arguments:
@@ -57,6 +60,7 @@ services:
5760
helper.post.content:
5861
class: Smartling\Helpers\PostContentHelper
5962
arguments:
63+
- "@helper.array"
6064
- "@helper.gutenberg"
6165

6266
helper.user:

readme.txt

Lines changed: 1 addition & 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.4.1
66
Requires PHP: 8.0
7-
Stable tag: 3.8.12
7+
Stable tag: 3.8.13
88
License: GPLv2 or later
99

1010
Translate content in WordPress quickly and seamlessly with Smartling, the industry-leading Translation Management System.

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: 3.8.12
14+
* Version: 3.8.13
1515
* Author: Smartling
1616
* Author URI: https://www.smartling.com
1717
* License: GPL-2.0+

tests/Smartling/Base/SmartlingCoreTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Smartling\Exception\SmartlingDirectRunRuntimeException;
1010
use Smartling\Exception\SmartlingTargetPlaceholderCreationFailedException;
1111
use Smartling\Extensions\Acf\AcfDynamicSupport;
12+
use Smartling\Helpers\ArrayHelper;
1213
use Smartling\Helpers\ContentSerializationHelper;
1314
use Smartling\Helpers\FieldsFilterHelper;
1415
use Smartling\Helpers\FileUriHelper;
@@ -73,7 +74,7 @@ protected function setUp(): void
7374
),
7475
$this->createMock(FileUriHelper::class),
7576
$gutenbergBlockHelper,
76-
new PostContentHelper($gutenbergBlockHelper),
77+
new PostContentHelper(new ArrayHelper(), $gutenbergBlockHelper),
7778
new XmlHelper($this->createMock(ContentSerializationHelper::class), new SerializerJsonWithFallback(), $this->createMock(SettingsManager::class)),
7879
$this->createMock(TestRunHelper::class),
7980
$wpProxy,

tests/Smartling/Base/SmartlingCoreUploadTraitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Smartling\ContentTypes\ExternalContentManager;
99
use Smartling\DbAl\WordpressContentEntities\PostEntityStd;
1010
use Smartling\Extensions\Acf\AcfDynamicSupport;
11+
use Smartling\Helpers\ArrayHelper;
1112
use Smartling\Helpers\ContentHelper;
1213
use Smartling\Helpers\ContentSerializationHelper;
1314
use Smartling\Helpers\DecodedXml;
@@ -328,7 +329,7 @@ public function testApplyXmlLockedBlocksById()
328329

329330
private function assertSuccessApplyXml(SmartlingCoreUpload $smartlingCoreUpload, SubmissionEntity $submission, XmlHelper $xmlHelper)
330331
{
331-
$postContentHelper = new PostContentHelper(new GutenbergBlockHelper(
332+
$postContentHelper = new PostContentHelper(new ArrayHelper(), new GutenbergBlockHelper(
332333
$this->createMock(AcfDynamicSupport::class),
333334
$this->createMock(ContentSerializationHelper::class),
334335
$this->createMock(MediaAttachmentRulesManager::class),

0 commit comments

Comments
 (0)