1 | <?php |
||||||
2 | /* |
||||||
3 | Plugin Name: WP-REST-API V2 Menus |
||||||
4 | Version: 0.10 |
||||||
5 | Description: Adding menus endpoints on WP REST API v2 |
||||||
6 | Author: Claudio La Barbera |
||||||
7 | Author URI: https://thebatclaud.io |
||||||
8 | */ |
||||||
9 | |||||||
10 | /** |
||||||
11 | * Get all registered menus |
||||||
12 | * @return array List of menus with slug and description |
||||||
13 | */ |
||||||
14 | function wp_api_v2_menus_get_all_menus() { |
||||||
15 | $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
16 | |||||||
17 | foreach ( $menus as $key => $menu ) { |
||||||
18 | // check if there is acf installed |
||||||
19 | if ( class_exists( 'acf' ) ) { |
||||||
20 | $fields = get_fields( $menu ); |
||||||
0 ignored issues
–
show
The function
get_fields was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
21 | if ( ! empty( $fields ) ) { |
||||||
22 | $menus[ $key ]->acf = new stdClass(); |
||||||
23 | |||||||
24 | foreach ( $fields as $field_key => $item ) { |
||||||
25 | // add all acf custom fields |
||||||
26 | $menus[ $key ]->acf->$field_key = $item; |
||||||
27 | } |
||||||
28 | } |
||||||
29 | } |
||||||
30 | } |
||||||
31 | |||||||
32 | return apply_filters('wp_api_v2_menus__menus', $menus); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
33 | } |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Get all locations |
||||||
37 | * @return array List of locations |
||||||
38 | **/ |
||||||
39 | |||||||
40 | function wp_api_v2_menu_get_all_locations() { |
||||||
41 | $nav_menu_locations = get_nav_menu_locations(); |
||||||
0 ignored issues
–
show
The function
get_nav_menu_locations was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
42 | $locations = new stdClass; |
||||||
43 | foreach ( $nav_menu_locations as $location_slug => $menu_id ) { |
||||||
44 | if ( get_term( $location_slug ) !== null ) { |
||||||
0 ignored issues
–
show
The function
get_term was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
45 | $locations->{$location_slug} = get_term( $location_slug ); |
||||||
46 | } else { |
||||||
47 | $locations->{$location_slug} = new stdClass; |
||||||
48 | } |
||||||
49 | $locations->{$location_slug}->slug = $location_slug; |
||||||
50 | $locations->{$location_slug}->menu = get_term( $menu_id ); |
||||||
51 | } |
||||||
52 | |||||||
53 | return apply_filters('wp_api_v2_menus__locations', $locations); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
54 | } |
||||||
55 | |||||||
56 | /** |
||||||
57 | * Get menu's data from his id |
||||||
58 | * |
||||||
59 | * @param array $data WP REST API data variable |
||||||
60 | * |
||||||
61 | * @return object Menu's data with his items |
||||||
62 | */ |
||||||
63 | function wp_api_v2_locations_get_menu_data( $data ) { |
||||||
64 | // Create default empty object |
||||||
65 | $menu = new stdClass; |
||||||
0 ignored issues
–
show
|
|||||||
66 | |||||||
67 | // this could be replaced with `if (has_nav_menu($data['id']))` |
||||||
68 | if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $data['id'] ] ) ) { |
||||||
0 ignored issues
–
show
The function
get_nav_menu_locations was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
69 | // Replace default empty object with the location object |
||||||
70 | $menu = get_term( $locations[ $data['id'] ] ); |
||||||
0 ignored issues
–
show
The function
get_term was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
71 | $menu->items = wp_api_v2_menus_get_menu_items( $locations[ $data['id'] ] ); |
||||||
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 ) ); |
||||||
0 ignored issues
–
show
The type
WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
74 | } |
||||||
75 | |||||||
76 | // check if there is acf installed |
||||||
77 | if ( class_exists( 'acf' ) ) { |
||||||
78 | $fields = get_fields( $menu ); |
||||||
0 ignored issues
–
show
The function
get_fields was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
79 | if ( ! empty( $fields ) ) { |
||||||
80 | $menu->acf = new stdClass(); |
||||||
81 | |||||||
82 | foreach ( $fields as $field_key => $item ) { |
||||||
83 | // add all acf custom fields |
||||||
84 | $menu->acf->$field_key = $item; |
||||||
85 | } |
||||||
86 | } |
||||||
87 | } |
||||||
88 | |||||||
89 | return apply_filters('wp_api_v2_menus__menu', $menu); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
90 | } |
||||||
91 | |||||||
92 | /** |
||||||
93 | * Check if a menu item is child of one of the menu's element passed as reference |
||||||
94 | * |
||||||
95 | * @param $parents Menu's items |
||||||
0 ignored issues
–
show
|
|||||||
96 | * @param $child Menu's item to check |
||||||
97 | * |
||||||
98 | * @return bool True if the parent is found, false otherwise |
||||||
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 ) { |
||||||
104 | $item->child_items = []; |
||||||
105 | } |
||||||
106 | array_push( $item->child_items, $child ); |
||||||
107 | return true; |
||||||
108 | } |
||||||
109 | |||||||
110 | if($item->child_items) { |
||||||
111 | if(wp_api_v2_menus_dna_test($item->child_items, $child)) { |
||||||
112 | return true; |
||||||
113 | } |
||||||
114 | } |
||||||
115 | } |
||||||
116 | |||||||
117 | return false; |
||||||
118 | } |
||||||
119 | |||||||
120 | /** |
||||||
121 | * Search object in an array by ID |
||||||
122 | */ |
||||||
123 | function wp_api_v2_find_object_by_id( $array, $id ) { |
||||||
124 | foreach ( $array as $element ) { |
||||||
125 | if ( $id == $element->ID ) { |
||||||
126 | return $element; |
||||||
127 | } |
||||||
128 | } |
||||||
129 | |||||||
130 | return false; |
||||||
131 | } |
||||||
132 | |||||||
133 | /** |
||||||
134 | * Retrieve items for a specific menu |
||||||
135 | * |
||||||
136 | * @param $id Menu id |
||||||
0 ignored issues
–
show
The type
Menu was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
137 | * |
||||||
138 | * @return array List of menu items |
||||||
139 | */ |
||||||
140 | function wp_api_v2_menus_get_menu_items( $id ) { |
||||||
141 | $menu_items = wp_get_nav_menu_items( $id ); |
||||||
0 ignored issues
–
show
The function
wp_get_nav_menu_items was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
142 | |||||||
143 | // fallback: if menu_items is null then return empty array |
||||||
144 | if($menu_items === false) { |
||||||
145 | return []; |
||||||
146 | } |
||||||
147 | |||||||
148 | $all_menu_items = $menu_items; |
||||||
149 | |||||||
150 | // check if there is acf installed |
||||||
151 | if ( class_exists( 'acf' ) ) { |
||||||
152 | foreach ( $menu_items as $menu_key => $menu_item ) { |
||||||
153 | $fields = get_fields( $menu_item->ID ); |
||||||
0 ignored issues
–
show
The function
get_fields was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
154 | if ( ! empty( $fields ) ) { |
||||||
155 | $menu_items[$menu_key]->acf = new stdClass(); |
||||||
156 | |||||||
157 | foreach ( $fields as $field_key => $item ) { |
||||||
158 | // add all acf custom fields |
||||||
159 | $menu_items[ $menu_key ]->acf->$field_key = $item; |
||||||
160 | } |
||||||
161 | } |
||||||
162 | } |
||||||
163 | } |
||||||
164 | |||||||
165 | // wordpress does not group child menu items with parent menu items |
||||||
166 | $child_items = []; |
||||||
167 | // pull all child menu items into separate object |
||||||
168 | foreach ( $menu_items as $key => $item ) { |
||||||
169 | |||||||
170 | if($item->type == 'post_type') { |
||||||
171 | // add slug to menu items |
||||||
172 | $slug = get_post_field( 'post_name', $item->object_id ); |
||||||
0 ignored issues
–
show
The function
get_post_field was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
173 | $item->slug = $slug; |
||||||
174 | } else if($item->type == 'taxonomy') { |
||||||
175 | $cat = get_term($item->object_id); |
||||||
0 ignored issues
–
show
The function
get_term was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
176 | $item->slug = $cat->slug; |
||||||
177 | } else if($item->type == 'post_type_archive') { |
||||||
178 | $post_type_data = get_post_type_object($item->object); |
||||||
0 ignored issues
–
show
The function
get_post_type_object was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
179 | |||||||
180 | if ($post_type_data->has_archive) { |
||||||
181 | $item->slug = $post_type_data->rewrite['slug']; |
||||||
182 | } |
||||||
183 | } |
||||||
184 | |||||||
185 | if (isset($item->thumbnail_id) && $item->thumbnail_id) { |
||||||
186 | $item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail'); |
||||||
0 ignored issues
–
show
The function
wp_get_attachment_image_url was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
187 | } |
||||||
188 | if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) { |
||||||
189 | $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); |
||||||
190 | } |
||||||
191 | |||||||
192 | if ( $item->menu_item_parent ) { |
||||||
193 | array_push( $child_items, $item ); |
||||||
194 | unset( $menu_items[ $key ] ); |
||||||
195 | } |
||||||
196 | |||||||
197 | } |
||||||
198 | |||||||
199 | // push child items into their parent item in the original object |
||||||
200 | do { |
||||||
201 | foreach($child_items as $key => $child_item) { |
||||||
202 | $parent = wp_api_v2_find_object_by_id( $all_menu_items, $child_item->menu_item_parent ); |
||||||
203 | |||||||
204 | if ( empty( $parent ) ) { |
||||||
205 | unset($child_items[$key]); |
||||||
206 | } |
||||||
207 | |||||||
208 | else if (wp_api_v2_menus_dna_test($menu_items, $child_item)) { |
||||||
209 | unset($child_items[$key]); |
||||||
210 | } |
||||||
211 | } |
||||||
212 | } while(count($child_items)); |
||||||
213 | |||||||
214 | return apply_filters('wp_api_v2_menus__menu_items', array_values($menu_items)); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
215 | } |
||||||
216 | |||||||
217 | /** |
||||||
218 | * Get menu's data from his id. |
||||||
219 | * It ensures compatibility for previous versions when this endpoint |
||||||
220 | * was allowing locations id in place of menus id) |
||||||
221 | * |
||||||
222 | * @param array $data WP REST API data variable |
||||||
223 | * |
||||||
224 | * @return object Menu's data with his items |
||||||
225 | */ |
||||||
226 | function wp_api_v2_menus_get_menu_data( $data ) { |
||||||
227 | // This ensure retro compatibility with versions `<= 0.5` when this endpoint |
||||||
228 | // was allowing locations id in place of menus id |
||||||
229 | if ( has_nav_menu( $data['id'] ) ) { |
||||||
0 ignored issues
–
show
The function
has_nav_menu was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
230 | $menu = wp_api_v2_locations_get_menu_data( $data ); |
||||||
231 | } else if ( is_nav_menu( $data['id'] ) ) { |
||||||
0 ignored issues
–
show
The function
is_nav_menu was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
232 | if ( is_int( $data['id'] ) ) { |
||||||
233 | $id = $data['id']; |
||||||
234 | } else { |
||||||
235 | $id = wp_get_nav_menu_object( $data['id'] ); |
||||||
0 ignored issues
–
show
The function
wp_get_nav_menu_object was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
236 | } |
||||||
237 | $menu = get_term( $id ); |
||||||
0 ignored issues
–
show
The function
get_term was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
238 | $menu->items = wp_api_v2_menus_get_menu_items( $id ); |
||||||
239 | } else { |
||||||
240 | 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 ) ); |
||||||
241 | } |
||||||
242 | |||||||
243 | // check if there is acf installed |
||||||
244 | if ( class_exists( 'acf' ) ) { |
||||||
245 | $fields = get_fields( $menu ); |
||||||
0 ignored issues
–
show
The function
get_fields was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
246 | if ( ! empty( $fields ) ) { |
||||||
247 | $menu->acf = new stdClass(); |
||||||
248 | |||||||
249 | foreach ( $fields as $field_key => $item ) { |
||||||
250 | // add all acf custom fields |
||||||
251 | $menu->acf->$field_key = $item; |
||||||
252 | } |
||||||
253 | } |
||||||
254 | } |
||||||
255 | |||||||
256 | return apply_filters('wp_api_v2_menus__menu', $menu); |
||||||
0 ignored issues
–
show
The function
apply_filters was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
257 | } |
||||||
258 | |||||||
259 | add_action( 'rest_api_init', function () { |
||||||
0 ignored issues
–
show
The function
add_action was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
260 | register_rest_route( 'menus/v1', '/menus', array( |
||||||
0 ignored issues
–
show
The function
register_rest_route was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
261 | 'methods' => 'GET', |
||||||
262 | 'callback' => 'wp_api_v2_menus_get_all_menus', |
||||||
263 | 'permission_callback' => '__return_true' |
||||||
264 | ) ); |
||||||
265 | |||||||
266 | register_rest_route( 'menus/v1', '/menus/(?P<id>[a-zA-Z0-9_-]+)', array( |
||||||
267 | 'methods' => 'GET', |
||||||
268 | 'callback' => 'wp_api_v2_menus_get_menu_data', |
||||||
269 | 'permission_callback' => '__return_true' |
||||||
270 | ) ); |
||||||
271 | |||||||
272 | register_rest_route( 'menus/v1', '/locations/(?P<id>[a-zA-Z0-9_-]+)', array( |
||||||
273 | 'methods' => 'GET', |
||||||
274 | 'callback' => 'wp_api_v2_locations_get_menu_data', |
||||||
275 | 'permission_callback' => '__return_true' |
||||||
276 | ) ); |
||||||
277 | |||||||
278 | register_rest_route( 'menus/v1', '/locations', array( |
||||||
279 | 'methods' => 'GET', |
||||||
280 | 'callback' => 'wp_api_v2_menu_get_all_locations', |
||||||
281 | 'permission_callback' => '__return_true' |
||||||
282 | ) ); |
||||||
283 | } ); |
||||||
284 |