@@ -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}
0 commit comments