Completed
Push — master ( 41d350...46fa66 )
by Claudio
15s queued 13s
created
wp-rest-api-v2-menus.php 1 patch
Spacing   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -12,18 +12,18 @@  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
-				$menus[ $key ]->acf = new stdClass();
19
+		if (class_exists('acf')) {
20
+			$fields = get_fields($menu);
21
+			if ( ! empty($fields)) {
22
+				$menus[$key]->acf = new stdClass();
23 23
 
24
-				foreach ( $fields as $field_key => $item ) {
24
+				foreach ($fields as $field_key => $item) {
25 25
 					// add all acf custom fields
26
-					$menus[ $key ]->acf->$field_key = $item;
26
+					$menus[$key]->acf->$field_key = $item;
27 27
 				}
28 28
 			}
29 29
 		}
@@ -40,14 +40,14 @@  discard block
 block discarded – undo
40 40
 function wp_api_v2_menu_get_all_locations() {
41 41
 	$nav_menu_locations = get_nav_menu_locations();
42 42
 	$locations          = new stdClass;
43
-	foreach ( $nav_menu_locations as $location_slug => $menu_id ) {
44
-		if ( get_term( $location_slug ) !== null ) {
45
-			$locations->{$location_slug} = get_term( $location_slug );
43
+	foreach ($nav_menu_locations as $location_slug => $menu_id) {
44
+		if (get_term($location_slug) !== null) {
45
+			$locations->{$location_slug} = get_term($location_slug);
46 46
 		} else {
47 47
 			$locations->{$location_slug} = new stdClass;
48 48
 		}
49 49
 		$locations->{$location_slug}->slug = $location_slug;
50
-		$locations->{$location_slug}->menu = get_term( $menu_id );
50
+		$locations->{$location_slug}->menu = get_term($menu_id);
51 51
 	}
52 52
 
53 53
 	return apply_filters('wp_api_v2_menus__locations', $locations);
@@ -60,26 +60,26 @@  discard block
 block discarded – undo
60 60
  *
61 61
  * @return object Menu's data with his items
62 62
  */
63
-function wp_api_v2_locations_get_menu_data( $data ) {
63
+function wp_api_v2_locations_get_menu_data($data) {
64 64
 	// Create default empty object
65 65
 	$menu = new stdClass;
66 66
 
67 67
 	// this could be replaced with `if (has_nav_menu($data['id']))`
68
-	if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) {
68
+	if (($locations = get_nav_menu_locations()) && isset($locations[$data['id']])) {
69 69
 		// Replace default empty object with the location object
70
-		$menu        = get_term( $locations[ $data['id'] ] );
71
-		$menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] );
70
+		$menu        = get_term($locations[$data['id']]);
71
+		$menu->items = wp_api_v2_menus_get_menu_items($locations[$data['id']]);
72 72
 	} else {
73
-		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 ) );
73
+		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));
74 74
 	}
75 75
 
76 76
 	// check if there is acf installed
77
-	if ( class_exists( 'acf' ) ) {
78
-		$fields = get_fields( $menu );
79
-		if ( ! empty( $fields ) ) {
77
+	if (class_exists('acf')) {
78
+		$fields = get_fields($menu);
79
+		if ( ! empty($fields)) {
80 80
 			$menu->acf = new stdClass();
81 81
 
82
-			foreach ( $fields as $field_key => $item ) {
82
+			foreach ($fields as $field_key => $item) {
83 83
 				// add all acf custom fields
84 84
 				$menu->acf->$field_key = $item;
85 85
 			}
@@ -97,18 +97,18 @@  discard block
 block discarded – undo
97 97
  *
98 98
  * @return bool True if the parent is found, false otherwise
99 99
  */
100
-function wp_api_v2_menus_dna_test( &$parents, $child ) {
101
-	foreach ( $parents as $key => $item ) {
102
-		if ( $child->menu_item_parent == $item->ID ) {
103
-			if ( ! $item->child_items ) {
100
+function wp_api_v2_menus_dna_test(&$parents, $child) {
101
+	foreach ($parents as $key => $item) {
102
+		if ($child->menu_item_parent == $item->ID) {
103
+			if ( ! $item->child_items) {
104 104
 				$item->child_items = [];
105 105
 			}
106
-			array_push( $item->child_items, $child );
106
+			array_push($item->child_items, $child);
107 107
 			return true;
108 108
 		}
109 109
 
110
-		if($item->child_items) {
111
-			if(wp_api_v2_menus_dna_test($item->child_items, $child)) {
110
+		if ($item->child_items) {
111
+			if (wp_api_v2_menus_dna_test($item->child_items, $child)) {
112 112
 				return true;
113 113
 			}
114 114
 		}
@@ -120,9 +120,9 @@  discard block
 block discarded – undo
120 120
 /**
121 121
  * Search object in an array by ID
122 122
  */
123
-function wp_api_v2_find_object_by_id( $array, $id ) {
124
-	foreach ( $array as $element ) {
125
-		if ( $id == $element->ID ) {
123
+function wp_api_v2_find_object_by_id($array, $id) {
124
+	foreach ($array as $element) {
125
+		if ($id == $element->ID) {
126 126
 				return $element;
127 127
 		}
128 128
 	}
@@ -137,20 +137,20 @@  discard block
 block discarded – undo
137 137
  *
138 138
  * @return array List of menu items
139 139
  */
140
-function wp_api_v2_menus_get_menu_items( $id ) {
141
-	$menu_items = wp_get_nav_menu_items( $id );
140
+function wp_api_v2_menus_get_menu_items($id) {
141
+	$menu_items = wp_get_nav_menu_items($id);
142 142
 	$all_menu_items = $menu_items;
143 143
 
144 144
 	// check if there is acf installed
145
-	if ( class_exists( 'acf' ) ) {
146
-		foreach ( $menu_items as $menu_key => $menu_item ) {
147
-			$fields = get_fields( $menu_item->ID );
148
-			if ( ! empty( $fields ) ) {
145
+	if (class_exists('acf')) {
146
+		foreach ($menu_items as $menu_key => $menu_item) {
147
+			$fields = get_fields($menu_item->ID);
148
+			if ( ! empty($fields)) {
149 149
 				$menu_items[$menu_key]->acf = new stdClass();
150 150
 
151
-				foreach ( $fields as $field_key => $item ) {
151
+				foreach ($fields as $field_key => $item) {
152 152
 					// add all acf custom fields
153
-					$menu_items[ $menu_key ]->acf->$field_key = $item;
153
+					$menu_items[$menu_key]->acf->$field_key = $item;
154 154
 				}
155 155
 			}
156 156
 		}
@@ -159,16 +159,16 @@  discard block
 block discarded – undo
159 159
 	// wordpress does not group child menu items with parent menu items
160 160
 	$child_items = [];
161 161
 	// pull all child menu items into separate object
162
-	foreach ( $menu_items as $key => $item ) {
162
+	foreach ($menu_items as $key => $item) {
163 163
 
164
-		if($item->type == 'post_type') {
164
+		if ($item->type == 'post_type') {
165 165
 			// add slug to menu items
166
-			$slug = get_post_field( 'post_name', $item->object_id );
166
+			$slug = get_post_field('post_name', $item->object_id);
167 167
 			$item->slug = $slug;
168
-		} else if($item->type == 'taxonomy') {
168
+		} else if ($item->type == 'taxonomy') {
169 169
 			$cat = get_term($item->object_id);
170 170
 			$item->slug = $cat->slug;
171
-		} else if($item->type == 'post_type_archive') {
171
+		} else if ($item->type == 'post_type_archive') {
172 172
 			$post_type_data = get_post_type_object($item->object);
173 173
 
174 174
 			if ($post_type_data->has_archive) {
@@ -183,19 +183,19 @@  discard block
 block discarded – undo
183 183
 			$item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail');
184 184
 		}
185 185
 
186
-		if ( $item->menu_item_parent ) {
187
-			array_push( $child_items, $item );
188
-			unset( $menu_items[ $key ] );
186
+		if ($item->menu_item_parent) {
187
+			array_push($child_items, $item);
188
+			unset($menu_items[$key]);
189 189
 		}
190 190
 
191 191
 	}
192 192
 
193 193
 	// push child items into their parent item in the original object
194 194
 	do {
195
-		foreach($child_items as $key => $child_item) {
196
-			$parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent );
195
+		foreach ($child_items as $key => $child_item) {
196
+			$parent = wp_api_v2_find_object_by_id($all_menu_items, $child_item->menu_item_parent);
197 197
 
198
-			if ( empty( $parent ) ) {
198
+			if (empty($parent)) {
199 199
 				unset($child_items[$key]);
200 200
 			}
201 201
 
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 				unset($child_items[$key]);
204 204
 			}
205 205
 		}
206
-	} while(count($child_items));
206
+	} while (count($child_items));
207 207
 
208 208
 	return apply_filters('wp_api_v2_menus__menu_items', array_values($menu_items));
209 209
 }
@@ -217,30 +217,30 @@  discard block
 block discarded – undo
217 217
  *
218 218
  * @return object Menu's data with his items
219 219
  */
220
-function wp_api_v2_menus_get_menu_data( $data ) {
220
+function wp_api_v2_menus_get_menu_data($data) {
221 221
 	// This ensure retro compatibility with versions `<= 0.5` when this endpoint
222 222
 	//   was allowing locations id in place of menus id
223
-	if ( has_nav_menu( $data['id'] ) ) {
224
-		$menu = wp_api_v2_locations_get_menu_data( $data );
225
-	} else if ( is_nav_menu( $data['id'] ) ) {
226
-		if ( is_int( $data['id'] ) ) {
223
+	if (has_nav_menu($data['id'])) {
224
+		$menu = wp_api_v2_locations_get_menu_data($data);
225
+	} else if (is_nav_menu($data['id'])) {
226
+		if (is_int($data['id'])) {
227 227
 			$id = $data['id'];
228 228
 		} else {
229
-			$id = wp_get_nav_menu_object( $data['id'] );
229
+			$id = wp_get_nav_menu_object($data['id']);
230 230
 		}
231
-		$menu        = get_term( $id );
232
-		$menu->items = wp_api_v2_menus_get_menu_items( $id );
231
+		$menu        = get_term($id);
232
+		$menu->items = wp_api_v2_menus_get_menu_items($id);
233 233
 	} else {
234
-		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 ) );
234
+		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));
235 235
 	}
236 236
 
237 237
 	// check if there is acf installed
238
-	if ( class_exists( 'acf' ) ) {
239
-		$fields = get_fields( $menu );
240
-		if ( ! empty( $fields ) ) {
238
+	if (class_exists('acf')) {
239
+		$fields = get_fields($menu);
240
+		if ( ! empty($fields)) {
241 241
 			$menu->acf = new stdClass();
242 242
 
243
-			foreach ( $fields as $field_key => $item ) {
243
+			foreach ($fields as $field_key => $item) {
244 244
 				// add all acf custom fields
245 245
 				$menu->acf->$field_key = $item;
246 246
 			}
@@ -250,28 +250,28 @@  discard block
 block discarded – undo
250 250
 	return apply_filters('wp_api_v2_menus__menu', $menu);
251 251
 }
252 252
 
253
-add_action( 'rest_api_init', function () {
254
-	register_rest_route( 'menus/v1', '/menus', array(
253
+add_action('rest_api_init', function() {
254
+	register_rest_route('menus/v1', '/menus', array(
255 255
 		'methods'  => 'GET',
256 256
 		'callback' => 'wp_api_v2_menus_get_all_menus',
257 257
 		'permission_callback' => '__return_true'
258
-	) );
258
+	));
259 259
 
260
-	register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
260
+	register_rest_route('menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
261 261
 		'methods'  => 'GET',
262 262
 		'callback' => 'wp_api_v2_menus_get_menu_data',
263 263
 		'permission_callback' => '__return_true'
264
-	) );
264
+	));
265 265
 
266
-	register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
266
+	register_rest_route('menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
267 267
 		'methods'  => 'GET',
268 268
 		'callback' => 'wp_api_v2_locations_get_menu_data',
269 269
 		'permission_callback' => '__return_true'
270
-	) );
270
+	));
271 271
 
272
-	register_rest_route( 'menus/v1', '/locations', array(
272
+	register_rest_route('menus/v1', '/locations', array(
273 273
 		'methods'  => 'GET',
274 274
 		'callback' => 'wp_api_v2_menu_get_all_locations',
275 275
 		'permission_callback' => '__return_true'
276
-	) );
276
+	));
277 277
 } );
Please login to merge, or discard this patch.