Completed
Push — master ( 929199...154e92 )
by Claudio
17s queued 11s
created
wp-rest-api-v2-menus.php 1 patch
Spacing   +68 added lines, -68 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
 		}
@@ -120,17 +120,17 @@  discard block
 block discarded – undo
120 120
  *
121 121
  * @return array List of menu items
122 122
  */
123
-function wp_api_v2_menus_get_menu_items( $id ) {
124
-	$menu_items = wp_get_nav_menu_items( $id );
123
+function wp_api_v2_menus_get_menu_items($id) {
124
+	$menu_items = wp_get_nav_menu_items($id);
125 125
 
126 126
 	// check if there is acf installed
127
-	if ( class_exists( 'acf' ) ) {
128
-		foreach ( $menu_items as $menu_key => $menu_item ) {
129
-			$fields = get_fields( $menu_item->ID );
130
-			if ( ! empty( $fields ) ) {
131
-				foreach ( $fields as $field_key => $item ) {
127
+	if (class_exists('acf')) {
128
+		foreach ($menu_items as $menu_key => $menu_item) {
129
+			$fields = get_fields($menu_item->ID);
130
+			if ( ! empty($fields)) {
131
+				foreach ($fields as $field_key => $item) {
132 132
 					// add all acf custom fields
133
-					$menu_items[ $menu_key ]->$field_key = $item;
133
+					$menu_items[$menu_key]->$field_key = $item;
134 134
 				}
135 135
 			}
136 136
 		}
@@ -139,16 +139,16 @@  discard block
 block discarded – undo
139 139
 	// wordpress does not group child menu items with parent menu items
140 140
 	$child_items = [];
141 141
 	// pull all child menu items into separate object
142
-	foreach ( $menu_items as $key => $item ) {
142
+	foreach ($menu_items as $key => $item) {
143 143
 
144
-		if($item->type == 'post_type') {
144
+		if ($item->type == 'post_type') {
145 145
 			// add slug to menu items
146
-			$slug = basename( get_permalink($item->object_id) );
146
+			$slug = basename(get_permalink($item->object_id));
147 147
 			$item->slug = $slug;
148
-		} else if($item->type == 'taxonomy') {
148
+		} else if ($item->type == 'taxonomy') {
149 149
 			$cat = get_term($item->object_id);
150 150
 			$item->slug = $cat->slug;
151
-		} else if($item->type == 'post_type_archive') {
151
+		} else if ($item->type == 'post_type_archive') {
152 152
 			$post_type_data = get_post_type_object($item->object);
153 153
 
154 154
 			if ($post_type_data->has_archive) {
@@ -156,20 +156,20 @@  discard block
 block discarded – undo
156 156
 			}
157 157
 		}
158 158
 
159
-		if ( $item->menu_item_parent ) {
160
-			array_push( $child_items, $item );
161
-			unset( $menu_items[ $key ] );
159
+		if ($item->menu_item_parent) {
160
+			array_push($child_items, $item);
161
+			unset($menu_items[$key]);
162 162
 		}
163 163
 	}
164 164
 
165 165
 	// push child items into their parent item in the original object
166 166
 	do {
167
-		foreach($child_items as $key => $child_item) {
168
-			if(wp_api_v2_menus_dna_test($menu_items, $child_item)) {
167
+		foreach ($child_items as $key => $child_item) {
168
+			if (wp_api_v2_menus_dna_test($menu_items, $child_item)) {
169 169
 				unset($child_items[$key]);
170 170
 			}
171 171
 		}
172
-	} while(count($child_items));
172
+	} while (count($child_items));
173 173
 
174 174
 	return array_values($menu_items);
175 175
 }
@@ -183,28 +183,28 @@  discard block
 block discarded – undo
183 183
  *
184 184
  * @return object Menu's data with his items
185 185
  */
186
-function wp_api_v2_menus_get_menu_data( $data ) {
186
+function wp_api_v2_menus_get_menu_data($data) {
187 187
 	// This ensure retro compatibility with versions `<= 0.5` when this endpoint
188 188
 	//   was allowing locations id in place of menus id
189
-	if ( has_nav_menu( $data['id'] ) ) {
190
-		$menu = wp_api_v2_locations_get_menu_data( $data );
191
-	} else if ( is_nav_menu( $data['id'] ) ) {
192
-		if ( is_int( $data['id'] ) ) {
189
+	if (has_nav_menu($data['id'])) {
190
+		$menu = wp_api_v2_locations_get_menu_data($data);
191
+	} else if (is_nav_menu($data['id'])) {
192
+		if (is_int($data['id'])) {
193 193
 			$id = $data['id'];
194 194
 		} else {
195
-			$id = wp_get_nav_menu_object( $data['id'] );
195
+			$id = wp_get_nav_menu_object($data['id']);
196 196
 		}
197
-		$menu        = get_term( $id );
198
-		$menu->items = wp_api_v2_menus_get_menu_items( $id );
197
+		$menu        = get_term($id);
198
+		$menu->items = wp_api_v2_menus_get_menu_items($id);
199 199
 	} else {
200
-		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 ) );
200
+		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));
201 201
 	}
202 202
 
203 203
 	// check if there is acf installed
204
-	if ( class_exists( 'acf' ) ) {
205
-		$fields = get_fields( $menu );
206
-		if ( ! empty( $fields ) ) {
207
-			foreach ( $fields as $field_key => $item ) {
204
+	if (class_exists('acf')) {
205
+		$fields = get_fields($menu);
206
+		if ( ! empty($fields)) {
207
+			foreach ($fields as $field_key => $item) {
208 208
 				// add all acf custom fields
209 209
 				$menu->$field_key = $item;
210 210
 			}
@@ -214,24 +214,24 @@  discard block
 block discarded – undo
214 214
 	return $menu;
215 215
 }
216 216
 
217
-add_action( 'rest_api_init', function () {
218
-	register_rest_route( 'menus/v1', '/menus', array(
217
+add_action('rest_api_init', function() {
218
+	register_rest_route('menus/v1', '/menus', array(
219 219
 		'methods'  => 'GET',
220 220
 		'callback' => 'wp_api_v2_menus_get_all_menus',
221
-	) );
221
+	));
222 222
 
223
-	register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
223
+	register_rest_route('menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array(
224 224
 		'methods'  => 'GET',
225 225
 		'callback' => 'wp_api_v2_menus_get_menu_data',
226
-	) );
226
+	));
227 227
 
228
-	register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
228
+	register_rest_route('menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array(
229 229
 		'methods'  => 'GET',
230 230
 		'callback' => 'wp_api_v2_locations_get_menu_data',
231
-	) );
231
+	));
232 232
 
233
-	register_rest_route( 'menus/v1', '/locations', array(
233
+	register_rest_route('menus/v1', '/locations', array(
234 234
 		'methods'  => 'GET',
235 235
 		'callback' => 'wp_api_v2_menu_get_all_locations',
236
-	) );
236
+	));
237 237
 } );
Please login to merge, or discard this patch.