From 190b435dc7bf23db9f1907fd2d49dda6769b1a2c Mon Sep 17 00:00:00 2001 From: Andrew Boyd Date: Fri, 9 Nov 2018 14:49:26 -0500 Subject: [PATCH] Adds limit to post recursion when iterating through nested WordPress posts. --- library/class-acf-to-rest-api-recursive.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) mode change 100644 => 100755 library/class-acf-to-rest-api-recursive.php diff --git a/library/class-acf-to-rest-api-recursive.php b/library/class-acf-to-rest-api-recursive.php old mode 100644 new mode 100755 index 74e12e8..f5258bb --- a/library/class-acf-to-rest-api-recursive.php +++ b/library/class-acf-to-rest-api-recursive.php @@ -6,6 +6,10 @@ if ( ! class_exists( 'ACF_To_REST_API_Recursive' ) ) { class ACF_To_REST_API_Recursive { + + protected static $recursedPosts = []; + public static $postRecursionDepth = 1; + public static function init() { self::hooks(); } @@ -18,9 +22,9 @@ private static function hooks() { protected static function get_types() { $types = array( - 'options' => 'options', - 'comments' => 'comments', - 'users' => 'users', + 'options' => 'options', + 'comments' => 'comments', + 'users' => 'users' ); $types += (array) get_post_types( array( 'show_in_rest' => true ) ); @@ -47,11 +51,15 @@ public static function get_fields( $data ) { public static function get_fields_recursive( $item ) { if ( is_object( $item ) ) { + if (get_class($item) === 'WP_Post') { + static::$recursedPosts[$item->ID] = static::$recursedPosts[$item->ID] ?? 0; + static::$recursedPosts[$item->ID]++; + } $item->acf = array(); - + $depth = $item->depth ?? 0; $fields = get_fields( $item ); - if ( $fields ) { + if ( $fields && static::$recursedPosts[$item->ID] < static::$postRecursionDepth ) { $item->acf = $fields; array_walk_recursive( $item->acf, array( __CLASS__, 'get_fields_recursive' ) ); }