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_REST_Menus { |
||
|
|||
24 | |||
25 | |||
26 | /** |
||
27 | * Get WP API namespace. |
||
28 | * |
||
29 | * @since 1.2.0 |
||
30 | * @return string |
||
31 | */ |
||
32 | public static function get_api_namespace() { |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Get WP API Menus namespace. |
||
39 | * |
||
40 | * @since 1.2.1 |
||
41 | * @return string |
||
42 | */ |
||
43 | public static function get_plugin_namespace() { |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Register menu routes for WP API v2. |
||
50 | * |
||
51 | * @since 1.2.0 |
||
52 | * @return array |
||
53 | */ |
||
54 | public function register_routes() { |
||
55 | |||
56 | register_rest_route( self::get_plugin_namespace(), '/menus', array( |
||
57 | array( |
||
58 | 'methods' => WP_REST_Server::READABLE, |
||
59 | 'callback' => array( $this, 'get_menus' ), |
||
60 | ) |
||
61 | ) ); |
||
62 | |||
63 | register_rest_route( self::get_plugin_namespace(), '/menu-html/(?P<menu_id>[a-zA-Z0-9_-]+)', array( |
||
64 | array( |
||
65 | 'methods' => WP_REST_Server::READABLE, |
||
66 | 'callback' => array( $this, 'get_menu_html' ), |
||
67 | 'args' => array( |
||
68 | 'context' => array( |
||
69 | 'default' => 'view', |
||
70 | ), |
||
71 | ), |
||
72 | ) |
||
73 | ) ); |
||
74 | |||
75 | register_rest_route( self::get_plugin_namespace(), '/menu-html/(?P<id>\d+)', array( |
||
76 | array( |
||
77 | 'methods' => WP_REST_Server::READABLE, |
||
78 | 'callback' => array( $this, 'get_menu_html' ), |
||
79 | 'args' => array( |
||
80 | 'context' => array( |
||
81 | 'default' => 'view', |
||
82 | ), |
||
83 | ), |
||
84 | ) |
||
85 | ) ); |
||
86 | |||
87 | register_rest_route( self::get_plugin_namespace(), '/menu-locations', array( |
||
88 | array( |
||
89 | 'methods' => WP_REST_Server::READABLE, |
||
90 | 'callback' => array( $this, 'get_menu_locations' ), |
||
91 | ) |
||
92 | ) ); |
||
93 | |||
94 | register_rest_route( self::get_plugin_namespace(), '/menu-locations/(?P<location>[a-zA-Z0-9_-]+)', array( |
||
95 | array( |
||
96 | 'methods' => WP_REST_Server::READABLE, |
||
97 | 'callback' => array( $this, 'get_menu_location' ), |
||
98 | ) |
||
99 | ) ); |
||
100 | |||
101 | } |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Get menus. |
||
106 | * |
||
107 | * @since 1.2.0 |
||
108 | * @return array All registered menus |
||
109 | */ |
||
110 | public static function get_menus() { |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Get a menu. |
||
142 | * |
||
143 | * @since 1.2.0 |
||
144 | * @param $request |
||
145 | * @return array Menu data |
||
146 | */ |
||
147 | public function get_menu( $request ) { |
||
180 | |||
181 | |||
182 | |||
183 | /** |
||
184 | * Get a menu rendered in html. |
||
185 | * |
||
186 | * @since 1.2.0 |
||
187 | * @param $request |
||
188 | * @return array Menu data |
||
189 | */ |
||
190 | public function get_menu_html( $request ) { |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Handle nested menu items. |
||
223 | * |
||
224 | * Given a flat array of menu items, split them into parent/child items |
||
225 | * and recurse over them to return children nested in their parent. |
||
226 | * |
||
227 | * @since 1.2.0 |
||
228 | * @param $menu_items |
||
229 | * @param $parent |
||
230 | * @return array |
||
231 | */ |
||
232 | private function nested_menu_items( &$menu_items, $parent = null ) { |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Check if a collection of menu items contains an item that is the parent id of 'id'. |
||
259 | * |
||
260 | * @since 1.2.0 |
||
261 | * @param array $items |
||
262 | * @param int $id |
||
263 | * @return array |
||
264 | */ |
||
265 | private function has_children( $items, $id ) { |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Get menu locations. |
||
274 | * |
||
275 | * @since 1.2.0 |
||
276 | * @param $request |
||
277 | * @return array All registered menus locations |
||
278 | */ |
||
279 | View Code Duplication | public static function get_menu_locations( $request ) { |
|
306 | |||
307 | |||
308 | /** |
||
309 | * Get menu for location. |
||
310 | * |
||
311 | * @since 1.2.0 |
||
312 | * @param $request |
||
313 | * @return array The menu for the corresponding location |
||
314 | */ |
||
315 | public function get_menu_location( $request ) { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Returns all child nav_menu_items under a specific parent. |
||
378 | * |
||
379 | * @since 1.2.0 |
||
380 | * @param int $parent_id The parent nav_menu_item ID |
||
381 | * @param array $nav_menu_items Navigation menu items |
||
382 | * @param bool $depth Gives all children or direct children only |
||
383 | * @return array returns filtered array of nav_menu_items |
||
384 | */ |
||
385 | View Code Duplication | public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
407 | |||
408 | |||
409 | /** |
||
410 | * Format a menu item for REST API consumption. |
||
411 | * |
||
412 | * @since 1.2.0 |
||
413 | * @param object|array $menu_item The menu item |
||
414 | * @param bool $children Get menu item children (default false) |
||
415 | * @param array $menu The menu the item belongs to (used when $children is set to true) |
||
416 | * @return array a formatted menu item for REST |
||
417 | */ |
||
418 | View Code Duplication | public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
445 | |||
446 | |||
447 | } |
||
448 | |||
451 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.