@@ -31,22 +31,22 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param string $arg It can be menu id, slug or full name. |
| 33 | 33 | */ |
| 34 | - public function __construct( $arg = null ) { |
|
| 34 | + public function __construct($arg = null) { |
|
| 35 | 35 | $locations = get_nav_menu_locations(); |
| 36 | 36 | |
| 37 | - if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) { |
|
| 38 | - $menu_id = $this->check_menu_id( $arg ); |
|
| 39 | - } elseif ( is_array( $locations ) && count( $locations ) ) { |
|
| 40 | - $menu_id = $this->get_locations_menu_id( $locations, $arg ); |
|
| 41 | - } elseif ( is_string( $arg ) ) { |
|
| 42 | - $menu_id = $this->get_menu_id_by_name( $arg ); |
|
| 37 | + if (is_numeric($arg) && 0 !== absint($arg)) { |
|
| 38 | + $menu_id = $this->check_menu_id($arg); |
|
| 39 | + } elseif (is_array($locations) && count($locations)) { |
|
| 40 | + $menu_id = $this->get_locations_menu_id($locations, $arg); |
|
| 41 | + } elseif (is_string($arg)) { |
|
| 42 | + $menu_id = $this->get_menu_id_by_name($arg); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - if ( ! isset( $menu_id ) ) { |
|
| 45 | + if (!isset($menu_id)) { |
|
| 46 | 46 | $menu_id = $this->get_first_menu_id(); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - if ( $menu_id ) { |
|
| 49 | + if ($menu_id) { |
|
| 50 | 50 | $this->ID = $menu_id; |
| 51 | 51 | $this->init(); |
| 52 | 52 | } |
@@ -57,21 +57,21 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | protected function init() { |
| 59 | 59 | $_return = array(); |
| 60 | - $items = wp_get_nav_menu_items( $this->ID ); |
|
| 60 | + $items = wp_get_nav_menu_items($this->ID); |
|
| 61 | 61 | |
| 62 | - foreach ( $items as $item ) { |
|
| 63 | - $_return[ $item->ID ] = new Menu_Item( $item ); |
|
| 62 | + foreach ($items as $item) { |
|
| 63 | + $_return[$item->ID] = new Menu_Item($item); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Apply nesting. |
| 67 | - foreach ( $_return as $item_id => $item ) { |
|
| 67 | + foreach ($_return as $item_id => $item) { |
|
| 68 | 68 | if ( |
| 69 | - isset( $item->menu_item_parent ) && |
|
| 69 | + isset($item->menu_item_parent) && |
|
| 70 | 70 | $item->menu_item_parent && |
| 71 | - isset( $_return[ $item->menu_item_parent ] ) |
|
| 71 | + isset($_return[$item->menu_item_parent]) |
|
| 72 | 72 | ) { |
| 73 | - $_return[ $item->menu_item_parent ]->add_child( $item ); |
|
| 74 | - unset( $_return[ $item_id ] ); |
|
| 73 | + $_return[$item->menu_item_parent]->add_child($item); |
|
| 74 | + unset($_return[$item_id]); |
|
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
@@ -84,10 +84,10 @@ discard block |
||
| 84 | 84 | * @return int|bool |
| 85 | 85 | */ |
| 86 | 86 | protected function get_first_menu_id() { |
| 87 | - $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
|
| 87 | + $menus = get_terms('nav_menu', array('hide_empty' => true)); |
|
| 88 | 88 | |
| 89 | - if ( is_array( $menus ) && count( $menus ) ) { |
|
| 90 | - if ( isset( $menus[0]->term_id ) ) { |
|
| 89 | + if (is_array($menus) && count($menus)) { |
|
| 90 | + if (isset($menus[0]->term_id)) { |
|
| 91 | 91 | return $menus[0]->term_id; |
| 92 | 92 | } |
| 93 | 93 | } |
@@ -102,13 +102,13 @@ discard block |
||
| 102 | 102 | * @param int|string $arg navigation id. |
| 103 | 103 | * @return int |
| 104 | 104 | */ |
| 105 | - protected function get_locations_menu_id( $locations, $arg ) { |
|
| 106 | - if ( is_numeric( $arg ) ) { |
|
| 107 | - $arg = array_search( $arg, $locations ); |
|
| 105 | + protected function get_locations_menu_id($locations, $arg) { |
|
| 106 | + if (is_numeric($arg)) { |
|
| 107 | + $arg = array_search($arg, $locations); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if ( isset( $locations[ $arg ] ) ) { |
|
| 111 | - $menu_id = $locations[ $arg ]; |
|
| 110 | + if (isset($locations[$arg])) { |
|
| 111 | + $menu_id = $locations[$arg]; |
|
| 112 | 112 | |
| 113 | 113 | return $menu_id; |
| 114 | 114 | } |
@@ -121,12 +121,12 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return int|boolean |
| 123 | 123 | */ |
| 124 | - protected function check_menu_id( $menu_id ) { |
|
| 125 | - $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) ); |
|
| 124 | + protected function check_menu_id($menu_id) { |
|
| 125 | + $menus = get_terms('nav_menu', array('hide_empty' => true)); |
|
| 126 | 126 | |
| 127 | - if ( is_array( $menus ) && count( $menus ) ) { |
|
| 128 | - foreach ( $menus as $menu ) { |
|
| 129 | - if ( absint( $menu->term_id ) === absint( $menu_id ) ) { |
|
| 127 | + if (is_array($menus) && count($menus)) { |
|
| 128 | + foreach ($menus as $menu) { |
|
| 129 | + if (absint($menu->term_id) === absint($menu_id)) { |
|
| 130 | 130 | return $menu_id; |
| 131 | 131 | } |
| 132 | 132 | } |
@@ -142,13 +142,13 @@ discard block |
||
| 142 | 142 | * |
| 143 | 143 | * @return int|bool |
| 144 | 144 | */ |
| 145 | - protected function get_menu_id_by_name( $slug = null ) { |
|
| 146 | - if ( $slug && is_string( $slug ) ) { |
|
| 147 | - if ( $menu_id = get_term_by( 'slug', $slug, 'nav_menu' ) ) { |
|
| 145 | + protected function get_menu_id_by_name($slug = null) { |
|
| 146 | + if ($slug && is_string($slug)) { |
|
| 147 | + if ($menu_id = get_term_by('slug', $slug, 'nav_menu')) { |
|
| 148 | 148 | return $menu_id; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - if ( $menu_id = get_term_by( 'name', $slug, 'nav_menu' ) ) { |
|
| 151 | + if ($menu_id = get_term_by('name', $slug, 'nav_menu')) { |
|
| 152 | 152 | return $menu_id; |
| 153 | 153 | } |
| 154 | 154 | } |
@@ -36,9 +36,11 @@ |
||
| 36 | 36 | |
| 37 | 37 | if ( is_numeric( $arg ) && 0 !== absint( $arg ) ) { |
| 38 | 38 | $menu_id = $this->check_menu_id( $arg ); |
| 39 | - } elseif ( is_array( $locations ) && count( $locations ) ) { |
|
| 39 | + } |
|
| 40 | + elseif ( is_array( $locations ) && count( $locations ) ) { |
|
| 40 | 41 | $menu_id = $this->get_locations_menu_id( $locations, $arg ); |
| 41 | - } elseif ( is_string( $arg ) ) { |
|
| 42 | + } |
|
| 43 | + elseif ( is_string( $arg ) ) { |
|
| 42 | 44 | $menu_id = $this->get_menu_id_by_name( $arg ); |
| 43 | 45 | } |
| 44 | 46 | |