Skip to content

Commit e197f69

Browse files
FIX infinite loop when parent is missing
1 parent 50e81b7 commit e197f69

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

wp-rest-api-v2-menus.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ function wp_api_v2_menus_dna_test( &$parents, $child ) {
113113
return false;
114114
}
115115

116+
/**
117+
* Search object in an array by ID
118+
*/
119+
function find_object_by_id( $array, $id ) {
120+
foreach ( $array as $element ) {
121+
if ( $id == $element->ID ) {
122+
return $element;
123+
}
124+
}
125+
126+
return false;
127+
}
128+
116129
/**
117130
* Retrieve items for a specific menu
118131
*
@@ -122,6 +135,7 @@ function wp_api_v2_menus_dna_test( &$parents, $child ) {
122135
*/
123136
function wp_api_v2_menus_get_menu_items( $id ) {
124137
$menu_items = wp_get_nav_menu_items( $id );
138+
$all_menu_items = $menu_items;
125139

126140
// check if there is acf installed
127141
if ( class_exists( 'acf' ) ) {
@@ -173,7 +187,13 @@ function wp_api_v2_menus_get_menu_items( $id ) {
173187
// push child items into their parent item in the original object
174188
do {
175189
foreach($child_items as $key => $child_item) {
176-
if(wp_api_v2_menus_dna_test($menu_items, $child_item)) {
190+
$parent = find_object_by_id( $all_menu_items, $child_item->menu_item_parent );
191+
192+
if ( empty( $parent ) ) {
193+
unset($child_items[$key]);
194+
}
195+
196+
else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) {
177197
unset($child_items[$key]);
178198
}
179199
}

0 commit comments

Comments
 (0)