Skip to content

Commit c298b64

Browse files
Merge pull request #593 from Smartling/wp-971-acf-optimizations
refactor ACF support (WP-971)
2 parents f06a15c + 31d5504 commit c298b64

4 files changed

Lines changed: 62 additions & 72 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": "5.0.1",
4+
"version": "5.0.2",
55
"description": "",
66
"type": "wordpress-plugin",
77
"repositories": [

inc/Smartling/Extensions/Acf/AcfDynamicSupport.php

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,20 @@ private function getDatabaseDefinitions(): array
126126
foreach ($groups as $groupKey => $group) {
127127
$defs[$groupKey] = [
128128
'global_type' => 'group',
129-
'active' => 1,
129+
'active' => 1,
130130
];
131-
$fields = $this->getFieldsByGroup($blog, [$groupKey => $group]);
132-
if (0 < count($fields)) {
133-
foreach ($fields as $fieldKey => $field) {
134-
$defs[$fieldKey] = [
135-
'global_type' => 'field',
136-
'type' => $field['type'],
137-
'name' => $field['name'],
138-
'parent' => $field['parent'],
139-
];
140-
141-
if ('clone' === $field['type']) {
142-
if (array_key_exists('clone', $field)) {
143-
$defs[$fieldKey]['clone'] = $field['clone'];
144-
} else {
145-
$this->getLogger()->debug('ACF field fieldType="clone" has no target. ' . json_encode($field));
146-
}
147-
}
131+
}
132+
foreach ($this->getFields($blog, array_map(static fn($item) => $item['post_id'], $groups)) as $fieldKey => $field) {
133+
$defs[$fieldKey] = [
134+
'global_type' => 'field',
135+
'type' => $field['type'],
136+
];
137+
138+
if ('clone' === $field['type']) {
139+
if (array_key_exists('clone', $field)) {
140+
$defs[$fieldKey]['clone'] = $field['clone'];
141+
} else {
142+
$this->getLogger()->debug('ACF field fieldType="clone" has no target. ' . json_encode($field));
148143
}
149144
}
150145
}
@@ -205,61 +200,16 @@ private function rawReadGroups(): array
205200
return $groups;
206201
}
207202

208-
private function rawReadFields($parentId, $parentKey): array
203+
private function rawReadFields($parentId): array
209204
{
210-
$posts = (new \WP_Query(
211-
[
212-
'post_type' => self::POST_TYPE_FIELD,
213-
'suppress_filters' => true,
214-
'posts_per_page' => -1,
215-
'post_status' => 'publish',
216-
'post_parent' => $parentId,
217-
]
218-
))->get_posts();
219-
220-
$fields = [];
221-
foreach ($posts as $post) {
222-
$configuration = unserialize($post->post_content);
223-
$fields[$post->post_name] = [
224-
'parent' => $parentKey,
225-
'name' => $post->post_excerpt,
226-
'type' => $configuration['type'],
227-
];
228-
$subFields = $this->rawReadFields($post->ID, $post->post_name);
229-
if (0 < count($subFields)) {
230-
$fields = array_merge($fields, $subFields);
231-
}
232-
}
233-
234-
return $fields;
205+
return $this->getFieldsFromPosts($this->getQueryByParentId($parentId)->get_posts());
235206
}
236207

237-
protected function getFieldsByGroup($blogId, $group): array
208+
private function getFields(int $blogId, array $parentIds): array
238209
{
239-
$dbFields = [];
240-
$needChange = $this->siteHelper->getCurrentBlogId() !== $blogId;
241-
try {
242-
if ($needChange) {
243-
$this->siteHelper->switchBlogId($blogId);
244-
}
245-
$keys = array_keys($group);
246-
$key = reset($keys);
247-
$_group = reset($group);
248-
$id = $_group['post_id'];
249-
250-
$dbFields = $this->rawReadFields($id, $key);
251-
252-
} catch (\Exception $e) {
253-
$this->getLogger()->warning(
254-
vsprintf('Error occurred while reading ACF data from blog %s. Message: %s', [$blogId, $e->getMessage()])
255-
);
256-
} finally {
257-
if ($needChange) {
258-
$this->siteHelper->restoreBlogId();
259-
}
260-
}
261-
262-
return $dbFields;
210+
return $this->siteHelper->withBlog($blogId, function () use ($parentIds): array {
211+
return $this->getFieldsFromPosts($this->getQueryByParentIds($parentIds)->get_posts());
212+
});
263213
}
264214

265215
protected function extractGroupsDefinitions(array $groups): array
@@ -714,4 +664,41 @@ public function getRuleId(string $key): string
714664

715665
return array_pop($matches[0]) ?? $key;
716666
}
667+
668+
private function getQueryByParentId($parentId): \WP_Query
669+
{
670+
return new \WP_Query(array_merge($this->getQueryArray(), ['post_parent' => $parentId]));
671+
}
672+
673+
private function getQueryByParentIds(array $parentIds): \WP_Query
674+
{
675+
return new \WP_Query(array_merge($this->getQueryArray(), ['post_parent__in' => $parentIds]));
676+
}
677+
678+
private function getQueryArray(): array
679+
{
680+
return [
681+
'post_type' => self::POST_TYPE_FIELD,
682+
'suppress_filters' => true,
683+
'posts_per_page' => -1,
684+
'post_status' => 'publish',
685+
];
686+
}
687+
688+
private function getFieldsFromPosts(array $posts): array
689+
{
690+
$fields = [];
691+
foreach ($posts as $post) {
692+
$configuration = unserialize($post->post_content);
693+
$fields[$post->post_name] = [
694+
'type' => $configuration['type'],
695+
];
696+
$subFields = $this->rawReadFields($post->ID);
697+
if (0 < count($subFields)) {
698+
$fields = array_merge($fields, $subFields);
699+
}
700+
}
701+
702+
return $fields;
703+
}
717704
}

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: 5.0.1
7+
Stable tag: 5.0.2
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.2 =
66+
* Improved performance when working on installations with a large number of ACF fields
67+
6568
= 5.0.1 =
6669
* Fixed issue where an exception during submission cloning would prevent the cloning queue from processing other items
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: 5.0.1
14+
* Version: 5.0.2
1515
* Author: Smartling
1616
* Author URI: https://www.smartling.com
1717
* License: GPL-2.0+

0 commit comments

Comments
 (0)