-
Notifications
You must be signed in to change notification settings - Fork 6
add visual configurator (WP-1003) #620
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
Merged
vsolovei-smartling
merged 7 commits into
master
from
WP-1003-visual-property-mappings-config
Jun 2, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f7eefc
add visual configurator (WP-1003)
vsolovei-smartling 575d2a1
use previous external content output instead of overwriting, add js e…
vsolovei-smartling 5e5024e
remove content type, use modal to work with rules (WP-1003)
vsolovei-smartling 4e06ba7
expand by default (WP-1003)
vsolovei-smartling bb72e22
reformat comments (WP-1003)
vsolovei-smartling 353445e
escape output (WP-1003)
vsolovei-smartling d1465a7
review changes (WP-1003)
vsolovei-smartling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 274 additions & 0 deletions
274
inc/Smartling/ContentTypes/ExternalContentJsonRules.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| <?php | ||
|
|
||
| namespace Smartling\ContentTypes; | ||
|
|
||
| use Smartling\Extensions\Pluggable; | ||
| use Smartling\Helpers\LoggerSafeTrait; | ||
| use Smartling\Helpers\WordpressFunctionProxyHelper; | ||
| use Smartling\Replacers\ReplacerFactory; | ||
| use Smartling\Submissions\SubmissionEntity; | ||
| use Smartling\Tuner\JsonFieldRule; | ||
| use Smartling\Tuner\JsonFieldRulesManager; | ||
| use Smartling\Vendor\JsonPath\JsonObject; | ||
|
|
||
| class ExternalContentJsonRules implements ContentTypeModifyingInterface | ||
| { | ||
| use LoggerSafeTrait; | ||
|
|
||
| public const PLUGIN_ID = 'json-rules'; | ||
|
|
||
| public function __construct( | ||
| private JsonFieldRulesManager $rulesManager, | ||
| private ReplacerFactory $replacerFactory, | ||
| private WordpressFunctionProxyHelper $wpProxy, | ||
| ) { | ||
| } | ||
|
|
||
| public function getMaxVersion(): string | ||
| { | ||
| return '99'; | ||
| } | ||
|
|
||
| public function getMinVersion(): string | ||
| { | ||
| return '0'; | ||
| } | ||
|
|
||
| public function getPluginId(): string | ||
| { | ||
| return self::PLUGIN_ID; | ||
| } | ||
|
|
||
| public function getPluginPaths(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function getPluginSupportLevel(): string | ||
| { | ||
| return Pluggable::SUPPORTED; | ||
| } | ||
|
|
||
| public function getSupportLevel(string $contentType, ?int $contentId = null): string | ||
| { | ||
| $this->rulesManager->loadData(); | ||
| return $this->rulesManager->listItems() === [] | ||
| ? Pluggable::NOT_SUPPORTED | ||
| : Pluggable::SUPPORTED; | ||
| } | ||
|
|
||
| public function getExternalContentTypes(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function getContentFields(SubmissionEntity $submission, bool $raw): array | ||
| { | ||
| $result = []; | ||
| $this->rulesManager->loadData(); | ||
| foreach ($this->getRulesByMetaKey() as $metaKey => $rules) { | ||
| $json = $this->readMetaJson($submission->getSourceId(), $metaKey); | ||
| if ($json === null) { | ||
| continue; | ||
| } | ||
| foreach ($rules as $rule) { | ||
| if ($this->parseReplacer($rule->getReplacerId())[0] !== ReplacerFactory::REPLACER_TRANSLATE) { | ||
| continue; | ||
| } | ||
| $matches = $this->safeGet($json, $rule->getPropertyPath()); | ||
| foreach ($matches as $index => $value) { | ||
| if (is_string($value) && $value !== '') { | ||
| $result[$this->buildKey($metaKey, $rule->getPropertyPath(), $index)] = $value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| public function getRelatedContent(string $contentType, int $contentId): array | ||
| { | ||
| $result = []; | ||
| $this->rulesManager->loadData(); | ||
| foreach ($this->getRulesByMetaKey() as $metaKey => $rules) { | ||
| $json = $this->readMetaJson($contentId, $metaKey); | ||
| if ($json === null) { | ||
| continue; | ||
| } | ||
| foreach ($rules as $rule) { | ||
| [$replacer, $hint] = $this->parseReplacer($rule->getReplacerId()); | ||
| if ($replacer !== ReplacerFactory::REPLACER_RELATED) { | ||
| continue; | ||
| } | ||
| $referencedType = $hint !== '' ? $hint : ContentTypeHelper::CONTENT_TYPE_UNKNOWN; | ||
| foreach ($this->safeGet($json, $rule->getPropertyPath()) as $value) { | ||
| if (is_numeric($value) && (int)$value > 0) { | ||
| $result[$referencedType][] = (int)$value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| foreach ($result as $type => $ids) { | ||
| $result[$type] = array_values(array_unique($ids)); | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| public function setContentFields(array $original, array $translation, SubmissionEntity $submission): ?array | ||
| { | ||
| $translations = $translation[$this->getPluginId()] ?? []; | ||
| unset($translation[$this->getPluginId()]); | ||
|
|
||
| $this->rulesManager->loadData(); | ||
| $changed = false; | ||
| foreach ($this->getRulesByMetaKey() as $metaKey => $rules) { | ||
| // Prefer a translation already produced by a prior handler over the source. | ||
| // JsonRules' edits act as a delta on top of bundled handlers rather than replacing their work. | ||
| $sourceJson = $translation['meta'][$metaKey] ?? $original['meta'][$metaKey] ?? null; | ||
| if (!is_string($sourceJson) || $sourceJson === '') { | ||
| continue; | ||
| } | ||
| try { | ||
| $jsonObject = new JsonObject($sourceJson); | ||
| } catch (\Throwable $e) { | ||
| $this->getLogger()->debug("Failed to parse meta $metaKey as JSON: " . $e->getMessage()); | ||
| continue; | ||
| } | ||
| $modified = false; | ||
| foreach ($rules as $rule) { | ||
| [$replacer] = $this->parseReplacer($rule->getReplacerId()); | ||
| if ($replacer === ReplacerFactory::REPLACER_TRANSLATE) { | ||
| $modified = $this->applyTranslateRule($jsonObject, $rule, $metaKey, $translations) || $modified; | ||
| } elseif ($replacer === ReplacerFactory::REPLACER_RELATED) { | ||
| $modified = $this->applyRelatedRule($jsonObject, $rule, $submission) || $modified; | ||
| } | ||
| } | ||
| if ($modified) { | ||
| $translation['meta'][$metaKey] = $jsonObject->getJson(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); | ||
| $changed = true; | ||
| } | ||
| } | ||
|
|
||
| return $changed ? $translation : null; | ||
| } | ||
|
|
||
| public function removeUntranslatableFieldsForUpload(array $source, SubmissionEntity $submission): array | ||
| { | ||
| $this->rulesManager->loadData(); | ||
| foreach (array_keys($this->getRulesByMetaKey()) as $metaKey) { | ||
| if (isset($source['meta'][$metaKey])) { | ||
| unset($source['meta'][$metaKey]); | ||
| } | ||
| } | ||
| return $source; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, JsonFieldRule[]> | ||
| */ | ||
| private function getRulesByMetaKey(): array | ||
| { | ||
| $result = []; | ||
| foreach ($this->rulesManager->listItems() as $rule) { | ||
| $result[$rule->getMetaKey()][] = $rule; | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| private function readMetaJson(int $contentId, string $metaKey): ?array | ||
| { | ||
| $value = $this->wpProxy->getPostMeta($contentId, $metaKey, true); | ||
| if (!is_string($value) || $value === '') { | ||
| return null; | ||
| } | ||
| try { | ||
| $decoded = json_decode($value, true, 512, JSON_THROW_ON_ERROR); | ||
| } catch (\JsonException) { | ||
| return null; | ||
| } | ||
| return is_array($decoded) ? $decoded : null; | ||
| } | ||
|
|
||
| private function safeGet(array $json, string $path): array | ||
| { | ||
| try { | ||
| $result = (new JsonObject($json))->get($path); | ||
| } catch (\Throwable $e) { | ||
| $this->getLogger()->debug("JsonPath get failed for path=$path: " . $e->getMessage()); | ||
| return []; | ||
| } | ||
| if (!is_array($result)) { | ||
| return []; | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| private function applyTranslateRule(JsonObject $jsonObject, JsonFieldRule $rule, string $metaKey, array $translations): bool | ||
| { | ||
| $objects = $jsonObject->getJsonObjects($rule->getPropertyPath()); | ||
| if ($objects === false || $objects === null) { | ||
| return false; | ||
| } | ||
| if (!is_array($objects)) { | ||
| $objects = [$objects]; | ||
| } | ||
| $changed = false; | ||
| foreach ($objects as $index => $node) { | ||
| $key = $this->buildKey($metaKey, $rule->getPropertyPath(), $index); | ||
| if (array_key_exists($key, $translations)) { | ||
| $ref = &$node->getValue(); | ||
| $ref = $translations[$key]; | ||
| unset($ref); | ||
| $changed = true; | ||
| } | ||
| } | ||
| return $changed; | ||
| } | ||
|
|
||
| private function applyRelatedRule(JsonObject $jsonObject, JsonFieldRule $rule, SubmissionEntity $submission): bool | ||
| { | ||
| try { | ||
| $replacer = $this->replacerFactory->getReplacer($rule->getReplacerId()); | ||
| } catch (\Throwable $e) { | ||
| $this->getLogger()->notice("Unable to resolve replacer {$rule->getReplacerId()}: " . $e->getMessage()); | ||
| return false; | ||
| } | ||
| $objects = $jsonObject->getJsonObjects($rule->getPropertyPath()); | ||
| if ($objects === false || $objects === null) { | ||
| return false; | ||
| } | ||
| if (!is_array($objects)) { | ||
| $objects = [$objects]; | ||
| } | ||
| $changed = false; | ||
| foreach ($objects as $node) { | ||
| $ref = &$node->getValue(); | ||
| $original = $ref; | ||
| if (!is_numeric($original) || (int)$original <= 0) { | ||
| unset($ref); | ||
| continue; | ||
| } | ||
| $replaced = $replacer->processAttributeOnDownload($original, $original, $submission); | ||
| if ($replaced !== $original) { | ||
| $ref = $replaced; | ||
| $changed = true; | ||
| } | ||
| unset($ref); | ||
| } | ||
| return $changed; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{0:string,1:string} [replacerId, contentTypeHint] | ||
| */ | ||
| private function parseReplacer(string $replacerId): array | ||
| { | ||
| $parts = explode('|', $replacerId, 2); | ||
| return [$parts[0], $parts[1] ?? '']; | ||
| } | ||
|
|
||
| private function buildKey(string $metaKey, string $path, int $index): string | ||
| { | ||
| return $metaKey . '|' . $path . '|' . $index; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Smartling\Replacers; | ||
|
|
||
| class TranslateReplacer extends DoNothingContentReplacer | ||
| { | ||
| public function getLabel(): string | ||
| { | ||
| return 'Translate'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we reflect this in some "meta" file in connector? does it affect minimum WP version we support from now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, https://github.com/Smartling/wordpress-localization-plugin/blob/master/smartling-connector.php#L22