Skip to content

Commit 70c604e

Browse files
author
Miika Arponen
committed
Allow dev to determine the maximum level of recursion in get_acf_post function
1 parent 02e71a5 commit 70c604e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

classes/query.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ public static function get_post( $id, $args = array() ) {
8484
public static function get_acf_post( $id, $args = array() ) {
8585

8686
$defaults = [
87-
'meta_keys' => null,
88-
'single' => false,
89-
'meta_type' => 'post',
90-
'whole_fields' => false,
91-
'recursive' => false,
92-
'output' => 'OBJECT'
87+
'meta_keys' => null,
88+
'single' => false,
89+
'meta_type' => 'post',
90+
'whole_fields' => false,
91+
'max_recursion_level' => 0,
92+
'current_recursion_level' => 0,
93+
'output' => 'OBJECT'
9394
];
9495

9596
$options = array_merge( $defaults, $args );
@@ -102,10 +103,10 @@ public static function get_acf_post( $id, $args = array() ) {
102103
$acfpost['fields'] = get_fields( $id );
103104

104105
// Get fields with relational post data as a whole acf object
105-
if ( isset( $recursive ) ) {
106+
if ( $current_recursion_level < $max_recursion_level ) {
106107

107-
// Let's avoid infinite loops by stopping recursion after one level. You may dig deeper in your view model.
108-
$options['recursive'] = apply_filters( 'dustpress/dustpress_helper/infinite_recursion', false );
108+
// Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model.
109+
$options['current_recursion_level'] = apply_filters( 'dustpress/query/current_recursion_level', ++$current_recursion_level );
109110

110111
if ( is_array( $acfpost['fields'] ) && count( $acfpost['fields'] ) > 0 ) {
111112
foreach ( $acfpost['fields'] as &$field ) {
@@ -117,7 +118,8 @@ public static function get_acf_post( $id, $args = array() ) {
117118
'acf-field-group',
118119
'acf-field',
119120
];
120-
$ignored_types = apply_filters( 'dustpress/dustpress_helper/ignore_on_recursion', $ignored_types );
121+
122+
$ignored_types = apply_filters( 'dustpress/query/ignore_on_recursion', $ignored_types );
121123

122124
// A direct relation field
123125
if ( is_object( $field ) && isset( $field->post_type ) && ! in_array( $field->post_type, $ignored_types ) ) {
@@ -133,7 +135,7 @@ public static function get_acf_post( $id, $args = array() ) {
133135
foreach ( $repeater as &$row ) {
134136

135137
// Post in a repeater
136-
if ( is_object( $row ) && isset( $row->post_type ) && ! in_array( $field->post_type, $ignored_types ) ) {
138+
if ( is_object( $row ) && isset( $row->post_type ) && is_object( $field ) && isset( $field->post_type ) && is_array( $field->post_type ) && ! in_array( $field->post_type, $ignored_types ) ) {
137139
$row = self::get_acf_post( $row->ID, $options );
138140
}
139141

@@ -144,7 +146,7 @@ public static function get_acf_post( $id, $args = array() ) {
144146
}
145147
}
146148
}
147-
elseif ( isset( $wholeFields ) ) {
149+
elseif ( true == $whole_fields ) {
148150
if ( is_array( $acfpost['fields'] ) && count( $acfpost['fields'] ) > 0 ) {
149151
foreach( $acfpost['fields'] as $name => &$field ) {
150152
$field = get_field_object($name, $id, true);

dustpress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Author: Miika Arponen & Ville Siltala / Geniem Oy
77
Author URI: http://www.geniem.com
88
License: GPLv3
9-
Version: 1.0.5
9+
Version: 1.0.6
1010
*/
1111

1212
final class DustPress {

0 commit comments

Comments
 (0)