Conditions | 22 |
Paths | 246 |
Total Lines | 66 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 4 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
123 | function wp_api_v2_menus_get_menu_items( $id ) { |
||
124 | $menu_items = wp_get_nav_menu_items( $id ); |
||
125 | |||
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 ) { |
||
132 | // add all acf custom fields |
||
133 | $menu_items[ $menu_key ]->$field_key = $item; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | // wordpress does not group child menu items with parent menu items |
||
140 | $child_items = []; |
||
141 | // pull all child menu items into separate object |
||
142 | foreach ( $menu_items as $key => $item ) { |
||
143 | |||
144 | if($item->type == 'post_type') { |
||
145 | // add slug to menu items |
||
146 | $slug = basename( get_permalink($item->object_id) ); |
||
147 | $item->slug = $slug; |
||
148 | if (isset($item->thumbnail_id) && $item->thumbnail_id) { |
||
149 | $item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail'); |
||
150 | } |
||
151 | if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) { |
||
152 | $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); |
||
153 | } |
||
154 | |||
155 | } else if($item->type == 'taxonomy') { |
||
156 | $cat = get_term($item->object_id); |
||
157 | $item->slug = $cat->slug; |
||
158 | } else if($item->type == 'post_type_archive') { |
||
159 | $post_type_data = get_post_type_object($item->object); |
||
160 | |||
161 | if ($post_type_data->has_archive) { |
||
162 | $item->slug = $post_type_data->rewrite['slug']; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ( $item->menu_item_parent ) { |
||
167 | if (isset($item->thumbnail_id) && $item->thumbnail_id) { |
||
168 | $item->thumbnail_src = wp_get_attachment_image_url(intval($item->thumbnail_id), 'post-thumbnail'); |
||
169 | } |
||
170 | if (isset($item->thumbnail_hover_id) && $item->thumbnail_hover_id) { |
||
171 | $item->thumbnail_hover_src = wp_get_attachment_image_url(intval($item->thumbnail_hover_id), 'post-thumbnail'); |
||
172 | } |
||
173 | |||
174 | array_push( $child_items, $item ); |
||
175 | unset( $menu_items[ $key ] ); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | // push child items into their parent item in the original object |
||
180 | do { |
||
181 | foreach($child_items as $key => $child_item) { |
||
182 | if(wp_api_v2_menus_dna_test($menu_items, $child_item)) { |
||
183 | unset($child_items[$key]); |
||
184 | } |
||
185 | } |
||
186 | } while(count($child_items)); |
||
187 | |||
188 | return array_values($menu_items); |
||
189 | } |
||
251 | } ); |