Skip to content

Commit e981d8a

Browse files
Merge pull request #588 from Smartling/WP-967-always-send-related
send related content for translation if requested, regardless of submission status (WP-967)
2 parents 42e762c + 375d3b6 commit e981d8a

File tree

12 files changed

+78
-118
lines changed

12 files changed

+78
-118
lines changed

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

inc/Smartling/Models/DetectedRelations.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,22 @@
55
class DetectedRelations
66
{
77
public const ORIGINAL_REFERENCES_KEY = 'originalReferences';
8-
public const MISSING_TRANSLATED_REFERENCES_KEY = 'missingTranslatedReferences';
98
private array $originalReferences;
10-
private array $missingReferences = [];
119

1210
public function __construct(array $originalReferences)
1311
{
1412
$this->originalReferences = $originalReferences;
1513
}
1614

17-
public function addMissingReference(int $targetBlogId, string $contentType, int $id): void
18-
{
19-
$this->missingReferences[$targetBlogId][$contentType][] = $id;
20-
}
21-
2215
public function getOriginalReferences(): array
2316
{
2417
return $this->originalReferences;
2518
}
2619

27-
public function getMissingReferences(): array
28-
{
29-
return $this->missingReferences;
30-
}
31-
3220
public function toArray(): array
3321
{
3422
return [
3523
self::ORIGINAL_REFERENCES_KEY => $this->originalReferences,
36-
self::MISSING_TRANSLATED_REFERENCES_KEY => $this->missingReferences,
3724
];
3825
}
3926
}

inc/Smartling/Models/UserTranslationRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function isBulk(): bool
4747
return count($this->ids) > 0;
4848
}
4949

50-
private static function validate(array $array)
50+
private static function validate(array $array): void
5151
{
5252
if (!array_key_exists('source', $array)) {
5353
throw new \InvalidArgumentException('Source array required');

inc/Smartling/Services/ContentRelationsDiscoveryService.php

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,12 @@ public function createSubmissions(UserTranslationRequest $request): void
217217

218218
$relatedIds = [];
219219
try {
220-
foreach ($request->getRelationsOrdered() as $relations) {
221-
foreach ($relations as $content) {
222-
foreach ($content as $contentType => $contentIds) {
223-
if (!array_key_exists($contentType, $relatedIds)) {
224-
$relatedIds[$contentType] = [];
225-
}
226-
$relatedIds[$contentType] = array_merge($relatedIds[$contentType], $contentIds);
220+
foreach ($request->getRelationsOrdered() as $content) {
221+
foreach ($content as $contentType => $contentIds) {
222+
if (!array_key_exists($contentType, $relatedIds)) {
223+
$relatedIds[$contentType] = [];
227224
}
225+
$relatedIds[$contentType] = array_merge($relatedIds[$contentType], $contentIds);
228226
}
229227
}
230228
foreach ($relatedIds as $contentType => $contentIds) {
@@ -568,25 +566,7 @@ public function getRelations(string $contentType, int $id, array $targetBlogIds)
568566
$detectedReferences = $this->normalizeReferences($detectedReferences);
569567
$this->getLogger()->debug('References after normalizing: ' . json_encode($detectedReferences));
570568

571-
$responseData = new DetectedRelations($detectedReferences);
572-
573-
foreach ($targetBlogIds as $targetBlogId) {
574-
foreach ($detectedReferences as $detectedContentType => $ids) {
575-
if (in_array($detectedContentType, array_merge($this->contentTypeManager->getRegisteredContentTypes(), $this->externalContentManager->getExternalContentTypes()), true)) {
576-
foreach ($ids as $detectedId) {
577-
if (!$this->submissionManager->submissionExistsNoLastError($detectedContentType, $curBlogId, $detectedId, $targetBlogId)) {
578-
$responseData->addMissingReference($targetBlogId, $detectedContentType, $detectedId);
579-
} else {
580-
$this->getLogger()->debug("Skipped adding relatedId=$detectedId for sourceContentId=$id, blogId=$targetBlogId: submission exists");
581-
}
582-
}
583-
} else {
584-
$this->getLogger()->debug("Excluded $detectedContentType from related submissions, type not in registered or external types");
585-
}
586-
}
587-
}
588-
589-
return $responseData;
569+
return new DetectedRelations($detectedReferences);
590570
}
591571

592572
public function normalizeReferences(array $references): array
@@ -682,17 +662,16 @@ private function getSources(UserCloneRequest $request, int $targetBlogId): array
682662
{
683663
$sources = [];
684664

685-
foreach ($request->getRelationsOrdered() as $relationSet) {
686-
foreach (($relationSet[$targetBlogId] ?? []) as $type => $ids) {
687-
foreach ($ids as $id) {
688-
if ($id === $request->getContentId() && $type === $request->getContentType()) {
689-
$this->getLogger()->info("Related list contains reference to root content, skip adding sourceId=$id, contentType=$type to sources list");
690-
} else {
691-
$sources[] = [
692-
'id' => $id,
693-
'type' => $type,
694-
];
695-
}
665+
$relationSet = $request->getRelationsOrdered();
666+
foreach (($relationSet[$targetBlogId] ?? []) as $type => $ids) {
667+
foreach ($ids as $id) {
668+
if ($id === $request->getContentId() && $type === $request->getContentType()) {
669+
$this->getLogger()->info("Related list contains reference to root content, skip adding sourceId=$id, contentType=$type to sources list");
670+
} else {
671+
$sources[] = [
672+
'id' => $id,
673+
'type' => $type,
674+
];
696675
}
697676
}
698677
}

inc/Smartling/Services/ContentRelationsHandler.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
* "page":[2],
2626
* "post_tag":[13,14],
2727
* "category":[1]
28-
* },
29-
* "missingTranslatedReferences":{
30-
* "2":{"attachment":[244,232,231]},
31-
* "3":{"attachment":[244,232,26,231]},
32-
* "4":{"attachment":[244,232,26,231],"post":[1],"post_tag":[13,14]},
33-
* "5":{"attachment":[244,232,26,231],"post_tag":[13,14]}
3428
* }
3529
* }
3630
* }

inc/Smartling/WP/Controller/TestRunController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ public function testRun($data): void
190190
$this->contentRelationDiscoveryService->createSubmissions(new UserTranslationRequest(
191191
$post->ID,
192192
$post->post_type,
193-
$this->contentRelationDiscoveryService->getRelations($post->post_type, $post->ID, [$targetBlogId])->getMissingReferences(),
193+
[$targetBlogId => $this->contentRelationDiscoveryService
194+
->getRelations($post->post_type, $post->ID, [$targetBlogId])->getOriginalReferences()],
194195
[$targetBlogId],
195196
new JobInformation($job->getJobUid(), true, $job->getJobName(), 'Test run job', '', 'UTC'),
196197
[],

inc/Smartling/WP/View/ContentEditJob.php

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
<script>
3030
const isBulkSubmitPage = <?= $isBulkSubmitPage ? 'true' : 'false'?>;
31-
let l1Relations = {missingTranslatedReferences: {}, originalReferences: {}};
32-
let l2Relations = {missingTranslatedReferences: {}, originalReferences: {}};
31+
let l1Relations = {originalReferences: {}};
32+
let l2Relations = {originalReferences: {}};
3333
let globalButton;
3434
</script>
3535

@@ -141,7 +141,7 @@
141141
</td>
142142
</tr>
143143
<tr id="relationsInfo">
144-
<th>New content to be uploaded:</th>
144+
<th>Related content to be uploaded:</th>
145145
<td id="relatedContent">
146146
</td>
147147
</tr>
@@ -387,38 +387,27 @@ function (i, e) {
387387
minDate: 0
388388
});
389389

390-
const mergeRelations = function mergeRelations(a, b) {
391-
a = a || {};
392-
b = b || {};
393-
const result = JSON.parse(JSON.stringify(a));
394-
for (const blogId in b) {
395-
if (!result.hasOwnProperty(blogId)) {
396-
result[blogId] = {};
397-
}
398-
for (const type in b[blogId]) {
399-
if (!result[blogId].hasOwnProperty(type)) {
400-
result[blogId][type] = [];
401-
}
402-
result[blogId][type] = result[blogId][type].concat(b[blogId][type]).filter((value, index, self) => self.indexOf(value) === index);
403-
}
404-
}
405-
406-
return result;
407-
}
408-
409390
const recalculateRelations = function recalculateRelations() {
410-
$("#relatedContent").html("");
391+
const relatedContent = $("#relatedContent");
392+
relatedContent.html("");
411393
const relations = {};
412-
let missingRelations = {};
394+
let allRelations = {};
413395
const cloneDepth = $('#cloneDepth').val();
414396
switch (cloneDepth) {
415397
case "0":
416398
return;
417399
case "1":
418-
missingRelations = l1Relations.missingTranslatedReferences;
400+
allRelations = l1Relations.originalReferences;
419401
break;
420402
case "2":
421-
missingRelations = mergeRelations(l1Relations.missingTranslatedReferences, l2Relations.missingTranslatedReferences);
403+
allRelations = {...l1Relations.originalReferences};
404+
for (const type in l2Relations.originalReferences) {
405+
if (allRelations[type]) {
406+
allRelations[type] = [...new Set([...allRelations[type], ...l2Relations.originalReferences[type]])];
407+
} else {
408+
allRelations[type] = l2Relations.originalReferences[type];
409+
}
410+
}
422411
break;
423412
default:
424413
console.error(`Unsupported clone depth value: ${cloneDepth}`);
@@ -430,20 +419,17 @@ function (i, e) {
430419
}
431420
return html;
432421
};
433-
$(".job-wizard input.mcheck[type=checkbox]:checked").each(function () {
434-
const blogId = this.dataset.blogId;
435-
if (missingRelations && missingRelations.hasOwnProperty(blogId)) {
436-
for (const sysType in missingRelations[blogId]) {
437-
let sysCount = missingRelations[blogId][sysType].length;
438-
if (relations.hasOwnProperty(sysType)) {
439-
relations[sysType] += sysCount;
440-
} else {
441-
relations[sysType] = sysCount;
442-
}
443-
$("#relatedContent").html(buildRelationsHint(relations));
422+
if (allRelations && Object.keys(allRelations).length > 0) {
423+
for (const sysType in allRelations) {
424+
let sysCount = allRelations[sysType].length;
425+
if (relations.hasOwnProperty(sysType)) {
426+
relations[sysType] += sysCount;
427+
} else {
428+
relations[sysType] = sysCount;
444429
}
445430
}
446-
});
431+
relatedContent.html(buildRelationsHint(relations));
432+
}
447433
};
448434

449435
const loadRelations = function loadRelations(contentType, contentId, level = 1) {
@@ -464,16 +450,10 @@ function (i, e) {
464450
case 2:
465451
const originalReferences = data.response.data.originalReferences;
466452
for (const relatedType in originalReferences) {
467-
for (const relatedId of originalReferences[relatedType]) {
468-
if (!l2Relations.originalReferences.hasOwnProperty(relatedType)) {
469-
l2Relations.originalReferences[relatedType] = [];
470-
}
471-
l2Relations.originalReferences[relatedType] = l2Relations.originalReferences[relatedType].concat(originalReferences[relatedType]);
453+
if (!l2Relations.originalReferences.hasOwnProperty(relatedType)) {
454+
l2Relations.originalReferences[relatedType] = [];
472455
}
473-
l2Relations.missingTranslatedReferences = mergeRelations(
474-
l2Relations.missingTranslatedReferences,
475-
data.response.data.missingTranslatedReferences,
476-
);
456+
l2Relations.originalReferences[relatedType] = l2Relations.originalReferences[relatedType].concat(originalReferences[relatedType]);
477457
}
478458
break;
479459
}
@@ -547,12 +527,29 @@ function (i, e) {
547527
};
548528

549529
if (!isBulkSubmitPage) {
530+
const prepareRequest = (originalRefs) => {
531+
const result = {};
532+
$(".job-wizard input.mcheck[type=checkbox]:checked").each(function () {
533+
const blogId = this.dataset.blogId;
534+
result[blogId] = originalRefs;
535+
});
536+
return result;
537+
};
538+
550539
switch ($('#cloneDepth').val()) {
551540
case "1":
552-
data.relations = {1: l1Relations.missingTranslatedReferences};
541+
data.relations = prepareRequest(l1Relations.originalReferences);
553542
break;
554543
case "2":
555-
data.relations = {1: l1Relations.missingTranslatedReferences, 2: l2Relations.missingTranslatedReferences}
544+
const mergedOriginal = {...l1Relations.originalReferences};
545+
for (const type in l2Relations.originalReferences) {
546+
if (mergedOriginal[type]) {
547+
mergedOriginal[type] = [...new Set([...mergedOriginal[type], ...l2Relations.originalReferences[type]])];
548+
} else {
549+
mergedOriginal[type] = l2Relations.originalReferences[type];
550+
}
551+
}
552+
data.relations = prepareRequest(mergedOriginal);
556553
break;
557554
}
558555
}

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: translation, localization, multilingual, internationalization, smartling
44
Requires at least: 5.5
55
Tested up to: 6.6.2
66
Requires PHP: 8.0
7-
Stable tag: 4.4.1
7+
Stable tag: 5.0.0
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+
= 5.0.0 =
66+
* Related assets will now be processed for translation when the content is submitted from the widget, even if they were previously sent for translation
67+
6568
= 4.4.1 =
6669
* Added possibility to clear the test run flag regardless of the test run status
6770
* Fixed test run erroneously flagging taxonomy submissions as failed

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

tests/IntegrationTests/tests/CloneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function testNoMediaDuplication(): void
4646
],
4747
], function () use ($childPostId, $imageId, $relationsDiscoveryService, $rootPostId, $targetBlogId) {
4848
$references = $relationsDiscoveryService->getRelations('post', $rootPostId, [$targetBlogId]);
49-
$this->assertCount(1, $references->getMissingReferences()[$targetBlogId]['post']);
50-
$this->assertEquals($childPostId, $references->getMissingReferences()[$targetBlogId]['post'][0]);
49+
$this->assertCount(1, $references->getOriginalReferences()['post']);
50+
$this->assertEquals($childPostId, $references->getOriginalReferences()['post'][0]);
5151
$relationsDiscoveryService->clone(new UserCloneRequest($rootPostId, 'post', [
5252
1 => [$targetBlogId => ['post' => [$childPostId]]],
5353
2 => [$targetBlogId => ['attachment' => [$imageId]]],

0 commit comments

Comments
 (0)