Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
23 | class WP_JSON_Menus { |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Register menu routes for WP API. |
||
28 | * |
||
29 | * @since 1.0.0 |
||
30 | * @param array $routes Existing routes |
||
31 | * @return array Modified routes |
||
32 | */ |
||
33 | public function register_routes( $routes ) { |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Get menus. |
||
62 | * |
||
63 | * @since 1.0.0 |
||
64 | * @return array All registered menus |
||
65 | */ |
||
66 | public static function get_menus() { |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Get a menu. |
||
98 | * |
||
99 | * @since 1.0.0 |
||
100 | * @param int $id ID of the menu |
||
101 | * @return array Menu data |
||
102 | */ |
||
103 | public function get_menu( $id ) { |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Get a menu rendered in html. |
||
137 | * |
||
138 | * @since 1.0.0 |
||
139 | * @param int $id ID of the menu |
||
140 | * @return array Menu data |
||
141 | */ |
||
142 | public function get_menu_html( $menu_id ) { |
||
143 | |||
144 | $json_url_base = get_json_url(); |
||
145 | $wp_menu_object = $menu_id ? wp_get_nav_menu_object( $menu_id ) : array(); |
||
146 | $wp_menu_items = $menu_id ? wp_get_nav_menu_items( $menu_id ) : array(); |
||
147 | |||
148 | $json_menu = array(); |
||
149 | |||
150 | View Code Duplication | if ( $wp_menu_object ) : |
|
151 | |||
152 | $menu = (array) $wp_menu_object; |
||
153 | $json_menu['ID'] = abs( $menu['term_id'] ); |
||
154 | $json_menu['name'] = $menu['name']; |
||
155 | $json_menu['slug'] = $menu['slug']; |
||
156 | $json_menu['description'] = $menu['description']; |
||
157 | $json_menu['count'] = abs( $menu['count'] ); |
||
158 | |||
159 | ob_start(); |
||
160 | wp_nav_menu( array( 'menu' => $menu_id ) ); |
||
161 | $json_menu['render_html']=ob_get_clean(); |
||
162 | |||
163 | $json_menu['meta']['links']['collection'] = $json_url_base . '/menus/'; |
||
164 | $json_menu['meta']['links']['self'] = $json_url_base . '/menu-html/' . $menu_id; |
||
165 | |||
166 | endif; |
||
167 | |||
168 | return $json_menu; |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Get menu locations. |
||
174 | * |
||
175 | * @since 1.0.0 |
||
176 | * @return array All registered menus locations |
||
177 | */ |
||
178 | public static function get_menu_locations() { |
||
179 | |||
180 | $locations = get_nav_menu_locations(); |
||
181 | $registered_menus = get_registered_nav_menus(); |
||
182 | $json_url = get_json_url() . '/menu-locations/'; |
||
183 | $json_menus = array(); |
||
184 | |||
185 | if ( $locations && $registered_menus ) : |
||
186 | |||
187 | foreach ( $registered_menus as $slug => $label ) : |
||
188 | |||
189 | // Sanity check |
||
190 | if ( ! isset( $locations[ $slug ] ) ) { |
||
191 | continue; |
||
192 | } |
||
193 | |||
194 | $json_menus[ $slug ]['ID'] = $locations[ $slug ]; |
||
195 | $json_menus[ $slug ]['label'] = $label; |
||
196 | $json_menus[ $slug ]['meta']['links']['collection'] = $json_url; |
||
197 | $json_menus[ $slug ]['meta']['links']['self'] = $json_url . $slug; |
||
198 | |||
199 | endforeach; |
||
200 | |||
201 | endif; |
||
202 | |||
203 | return $json_menus; |
||
204 | } |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Get menu for location. |
||
209 | * |
||
210 | * @since 1.0.0 |
||
211 | * @param string $location The theme location menu name |
||
212 | * @return array The menu for the corresponding location |
||
213 | */ |
||
214 | public function get_menu_location( $location ) { |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Returns all child nav_menu_items under a specific parent. |
||
263 | * |
||
264 | * @since 1.1.0 |
||
265 | * @param int $parent_id the parent nav_menu_item ID |
||
266 | * @param array $nav_menu_items navigation menu items |
||
267 | * @param bool $depth gives all children or direct children only |
||
268 | * @return array returns filtered array of nav_menu_items |
||
269 | */ |
||
270 | View Code Duplication | public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
292 | |||
293 | |||
294 | /** |
||
295 | * Format a menu item for JSON API consumption. |
||
296 | * |
||
297 | * @since 1.1.0 |
||
298 | * @param object|array $menu_item the menu item |
||
299 | * @param bool $children get menu item children (default false) |
||
300 | * @param array $menu the menu the item belongs to (used when $children is set to true) |
||
301 | * @return array a formatted menu item for JSON |
||
302 | */ |
||
303 | View Code Duplication | public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
330 | |||
331 | |||
332 | } |
||
333 | |||
336 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.