Passed
Push — master ( f0cb5e...c9d24a )
by Claudio
02:11 queued 01:04
created
wp-rest-api-v2-menus.php 2 patches
Braces   +1 added lines, -3 removed lines patch added patch discarded remove patch
@@ -191,9 +191,7 @@
 block discarded – undo
191 191
 
192 192
 			if ( empty( $parent ) ) {
193 193
 				unset($child_items[$key]);
194
-			}
195
-
196
-			else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) {
194
+			} else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) {
197 195
 				unset($child_items[$key]);
198 196
 			}
199 197
 		}
Please login to merge, or discard this patch.
Spacing   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -12,16 +12,16 @@  discard block
 block discarded – undo
12 12
  * @return array List of menus with slug and description
13 13
  */
14 14
 function wp_api_v2_menus_get_all_menus() {
15
-	$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
15
+	$menus = get_terms('nav_menu', array('hide_empty' => true));
16 16
 
17
-	foreach ( $menus as $key => $menu ) {
17
+	foreach ($menus as $key => $menu) {
18 18
 		// check if there is acf installed
19
-		if ( class_exists( 'acf' ) ) {
20
-			$fields = get_fields( $menu );
21
-			if ( ! empty( $fields ) ) {
22
-				foreach ( $fields as $field_key => $item ) {
19
+		if (class_exists('acf')) {
20
+			$fields = get_fields($menu);
21
+			if ( ! empty($fields)) {
22
+				foreach ($fields as $field_key => $item) {
23 23
 					// add all acf custom fields
24
-					$menus[ $key ]->$field_key = $item;
24
+					$menus[$key]->$field_key = $item;
25 25
 				}
26 26
 			}
27 27
 		}
@@ -38,14 +38,14 @@  discard block
 block discarded – undo
38 38
 function wp_api_v2_menu_get_all_locations() {
39 39
 	$nav_menu_locations = get_nav_menu_locations();
40 40
 	$locations          = new stdClass;
41
-	foreach ( $nav_menu_locations as $location_slug => $menu_id ) {
42
-		if ( get_term( $location_slug ) !== null ) {
43
-			$locations->{$location_slug} = get_term( $location_slug );
41
+	foreach ($nav_menu_locations as $location_slug => $menu_id) {
42
+		if (get_term($location_slug) !== null) {
43
+			$locations->{$location_slug} = get_term($location_slug);
44 44
 		} else {
45 45
 			$locations->{$location_slug} = new stdClass;
46 46
 		}
47 47
 		$locations->{$location_slug}->slug = $location_slug;
48
-		$locations->{$location_slug}->menu = get_term( $menu_id );
48
+		$locations->{$location_slug}->menu = get_term($menu_id);
49 49
 	}
50 50
 
51 51
 	return $locations;
@@ -58,24 +58,24 @@  discard block
 block discarded – undo
58 58
  *
59 59
  * @return object Menu's data with his items
60 60
  */
61
-function wp_api_v2_locations_get_menu_data( $data ) {
61
+function wp_api_v2_locations_get_menu_data($data) {
62 62
 	// Create default empty object
63 63
 	$menu = new stdClass;
64 64
 
65 65
 	// this could be replaced with `if (has_nav_menu($data['id']))`
66
-	if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) {
66
+	if (($locations = get_nav_menu_locations()) && isset($locations[$data['id']])) {
67 67
 		// Replace default empty object with the location object
68
-		$menu        = get_term( $locations[ $data['id'] ] );
69
-		$menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] );
68
+		$menu        = get_term($locations[$data['id']]);
69
+		$menu->items = wp_api_v2_menus_get_menu_items($locations[$data['id']]);
70 70
 	} else {
71
-		return new WP_Error( 'not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array( 'status' => 404 ) );
71
+		return new WP_Error('not_found', 'No location has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing location ID or location slug.', array('status' => 404));
72 72
 	}
73 73
 
74 74
 	// check if there is acf installed
75
-	if ( class_exists( 'acf' ) ) {
76
-		$fields = get_fields( $menu );
77
-		if ( ! empty( $fields ) ) {
78
-			foreach ( $fields as $field_key => $item ) {
75
+	if (class_exists('acf')) {
76
+		$fields = get_fields($menu);
77
+		if ( ! empty($fields)) {
78
+			foreach ($fields as $field_key => $item) {
79 79
 				// add all acf custom fields
80 80
 				$menu->$field_key = $item;
81 81
 			}
@@ -93,18 +93,18 @@  discard block
 block discarded – undo
93 93
  *
94 94
  * @return bool True if the parent is found, false otherwise
95 95
  */
96
-function wp_api_v2_menus_dna_test( &$parents, $child ) {
97
-	foreach ( $parents as $key => $item ) {
98
-		if ( $child->menu_item_parent == $item->ID ) {
99
-			if ( ! $item->child_items ) {
96
+function wp_api_v2_menus_dna_test(&$parents, $child) {
97
+	foreach ($parents as $key => $item) {
98
+		if ($child->menu_item_parent == $item->ID) {
99
+			if ( ! $item->child_items) {
100 100
 				$item->child_items = [];
101 101
 			}
102
-			array_push( $item->child_items, $child );
102
+			array_push($item->child_items, $child);
103 103
 			return true;
104 104
 		}
105 105
 
106
-		if($item->child_items) {
107
-			if(wp_api_v2_menus_dna_test($item->child_items, $child)) {
106
+		if ($item->child_items) {
107
+			if (wp_api_v2_menus_dna_test($item->child_items, $child)) {
108 108
 				return true;
109 109
 			}
110 110
 		}
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
 /**
117 117
  * Search object in an array by ID
118 118
  */
119
-function wp_api_v2_find_object_by_id( $array, $id ) {
120
-	foreach ( $array as $element ) {
121
-		if ( $id == $element->ID ) {
119
+function wp_api_v2_find_object_by_id($array, $id) {
120
+	foreach ($array as $element) {
121
+		if ($id == $element->ID) {
122 122
 				return $element;
123 123
 		}
124 124
 	}
@@ -133,18 +133,18 @@  discard block
 block discarded – undo
133 133
  *
134 134
  * @return array List of menu items
135 135
  */
136
-function wp_api_v2_menus_get_menu_items( $id ) {
137
-	$menu_items = wp_get_nav_menu_items( $id );
136
+function wp_api_v2_menus_get_menu_items($id) {
137
+	$menu_items = wp_get_nav_menu_items($id);
138 138
 	$all_menu_items = $menu_items;
139 139
 
140 140
 	// check if there is acf installed
141
-	if ( class_exists( 'acf' ) ) {
142
-		foreach ( $menu_items as $menu_key => $menu_item ) {
143
-			$fields = get_fields( $menu_item->ID );
144
-			if ( ! empty( $fields ) ) {
145
-				foreach ( $fields as $field_key => $item ) {
141
+	if (class_exists('acf')) {
142
+		foreach ($menu_items as $menu_key => $menu_item) {
143
+			$fields = get_fields($menu_item->ID);
144
+			if ( ! empty($fields)) {
145
+				foreach ($fields as $field_key => $item) {
146 146
 					// add all acf custom fields
147
-					$menu_items[ $menu_key ]->$field_key = $item;
147
+					$menu_items[$menu_key]->$field_key = $item;
148 148
 				}
149 149
 			}
150 150
 		}
@@ -153,16 +153,16 @@  discard block
 block discarded – undo
153 153
 	// wordpress does not group child menu items with parent menu items
154 154
 	$child_items = [];
155 155
 	// pull all child menu items into separate object
156
-	foreach ( $menu_items as $key => $item ) {
156
+	foreach ($menu_items as $key => $item) {
157 157
 
158
-		if($item->type == 'post_type') {
158
+		if ($item->type == 'post_type') {
159 159
 			// add slug to menu items
160
-			$slug = basename( get_permalink($item->object_id) );
160
+			$slug = basename(get_permalink($item->object_id));
161 161
 			$item->slug = $slug;
162
-		} else if($item->type == 'taxonomy') {
162
+		} else if ($item->type == 'taxonomy') {
163 163
 			$cat = get_term($item->object_id);
164 164
 			$item->slug = $cat->slug;
165
-		} else if($item->type == 'post_type_archive') {
165
+		} else if ($item->type == 'post_type_archive') {
166 166
 			$post_type_data = get_post_type_object($item->object);
167 167
 
168 168
 			if ($post_type_data->has_archive) {
@@ -177,19 +177,19 @@  discard block
 block discarded – undo
177 177
 			$item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail');
178 178
 		}
179 179
 
180
-		if ( $item->menu_item_parent ) {
181
-			array_push( $child_items, $item );
182
-			unset( $menu_items[ $key ] );
180
+		if ($item->menu_item_parent) {
181
+			array_push($child_items, $item);
182
+			unset($menu_items[$key]);
183 183
 		}
184 184
 
185 185
 	}
186 186
 
187 187
 	// push child items into their parent item in the original object
188 188
 	do {
189
-		foreach($child_items as $key => $child_item) {
190
-			$parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent );
189
+		foreach ($child_items as $key => $child_item) {
190
+			$parent = wp_api_v2_find_object_by_id($all_menu_items, $child_item->menu_item_parent);
191 191
 
192
-			if ( empty( $parent ) ) {
192
+			if (empty($parent)) {
193 193
 				unset($child_items[$key]);
194 194
 			}
195 195
 
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 				unset($child_items[$key]);
198 198
 			}
199 199
 		}
200
-	} while(count($child_items));
200
+	} while (count($child_items));
201 201
 
202 202
 	return array_values($menu_items);
203 203
 }
@@ -211,28 +211,28 @@  discard block
 block discarded – undo
211 211
  *
212 212
  * @return object Menu's data with his items
213 213
  */
214
-function wp_api_v2_menus_get_menu_data( $data ) {
214
+function wp_api_v2_menus_get_menu_data($data) {
215 215
 	// This ensure retro compatibility with versions `<= 0.5` when this endpoint
216 216
 	//   was allowing locations id in place of menus id
217
-	if ( has_nav_menu( $data['id'] ) ) {
218
-		$menu = wp_api_v2_locations_get_menu_data( $data );
219
-	} else if ( is_nav_menu( $data['id'] ) ) {
220
-		if ( is_int( $data['id'] ) ) {
217
+	if (has_nav_menu($data['id'])) {
218
+		$menu = wp_api_v2_locations_get_menu_data($data);
219
+	} else if (is_nav_menu($data['id'])) {
220
+		if (is_int($data['id'])) {
221 221
 			$id = $data['id'];
222 222
 		} else {
223
-			$id = wp_get_nav_menu_object( $data['id'] );
223
+			$id = wp_get_nav_menu_object($data['id']);
224 224
 		}
225
-		$menu        = get_term( $id );
226
-		$menu->items = wp_api_v2_menus_get_menu_items( $id );
225
+		$menu        = get_term($id);
226
+		$menu->items = wp_api_v2_menus_get_menu_items($id);
227 227
 	} else {
228
-		return new WP_Error( 'not_found', 'No menu has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing menu ID, menu slug, location ID or location slug.', array( 'status' => 404 ) );
228
+		return new WP_Error('not_found', 'No menu has been found with this id or slug: `' . $data['id'] . '`. Please ensure you passed an existing menu ID, menu slug, location ID or location slug.', array('status' => 404));
229 229
 	}
230 230
 
231 231
 	// check if there is acf installed
232
-	if ( class_exists( 'acf' ) ) {
233
-		$fields = get_fields( $menu );
234
-		if ( ! empty( $fields ) ) {
235
-			foreach ( $fields as $field_key => $item ) {
232
+	if (class_exists('acf')) {
233
+		$fields = get_fields($menu);
234
+		if ( ! empty($fields)) {
235
+			foreach ($fields as $field_key => $item) {
236 236
 				// add all acf custom fields
237 237
 				$menu->$field_key = $item;
238 238
 			}
@@ -242,28 +242,28 @@  discard block
 block discarded – undo
242 242
 	return $menu;
243 243
 }
244 244
 
245
-add_action( 'rest_api_init', function () {
246
-	register_rest_route( 'menus/v1', '/menus', array(
245
+add_action('rest_api_init', function() {
246
+	register_rest_route('menus/v1', '/menus', array(
247 247
 		'methods'  => 'GET',
248 248
 		'callback' => 'wp_api_v2_menus_get_all_menus',
249 249
 		'permission_callback' => '__return_true'
250
-	) );
250
+	));
251 251
 
252
-	register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
252
+	register_rest_route('menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
253 253
 		'methods'  => 'GET',
254 254
 		'callback' => 'wp_api_v2_menus_get_menu_data',
255 255
 		'permission_callback' => '__return_true'
256
-	) );
256
+	));
257 257
 
258
-	register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
258
+	register_rest_route('menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
259 259
 		'methods'  => 'GET',
260 260
 		'callback' => 'wp_api_v2_locations_get_menu_data',
261 261
 		'permission_callback' => '__return_true'
262
-	) );
262
+	));
263 263
 
264
-	register_rest_route( 'menus/v1', '/locations', array(
264
+	register_rest_route('menus/v1', '/locations', array(
265 265
 		'methods'  => 'GET',
266 266
 		'callback' => 'wp_api_v2_menu_get_all_locations',
267 267
 		'permission_callback' => '__return_true'
268
-	) );
268
+	));
269 269
 } );
Please login to merge, or discard this patch.