Completed
Push — master ( 0e9f90...205170 )
by Andrew
03:10 queued 22s
created

Menu::get_locations_menu_id()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 11
rs 9.4285
1
<?php
2
/**
3
 * Class for handling menu functionality.
4
 *
5
 * @package Classy
6
 */
7
8
namespace Classy;
9
10
/**
11
 * Class Menu.
12
 */
13
class Menu {
14
15
	/**
16
	 * Menu ID.
17
	 *
18
	 * @var int
19
	 */
20
	public $ID;
21
22
	/**
23
	 * Items.
24
	 *
25
	 * @var array
26
	 */
27
	protected $items;
28
29
	/**
30
	 * Main constructor. Tries to find menu id based on provided arg (or not) and inits menu.
31
	 *
32
	 * @param string $arg It can be menu id, slug or full name.
33
	 */
34
	public function __construct( $arg = null ) {
35
		$locations = get_nav_menu_locations();
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 );
43
		}
44
45
		if ( ! isset( $menu_id ) ) {
46
			$menu_id = $this->get_first_menu_id();
47
		}
48
49
		if ( $menu_id ) {
50
			$this->ID = $menu_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $menu_id can also be of type boolean. However, the property $ID is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
			$this->init();
52
		}
53
	}
54
55
	/**
56
	 * Init menu.
57
	 */
58
	protected function init() {
59
		$_return = array();
60
		$items = wp_get_nav_menu_items( $this->ID );
61
62
		foreach ( $items as $item ) {
63
			$_return[ $item->ID ] = new Menu_Item( $item );
64
		}
65
66
		// Apply nesting.
67
		foreach ( $_return as $item_id => $item ) {
68
			if (
69
				isset( $item->menu_item_parent ) &&
70
				$item->menu_item_parent &&
71
				isset( $_return[ $item->menu_item_parent ] )
72
			) {
73
				$_return[ $item->menu_item_parent ]->add_child( $item );
74
				unset( $_return[ $item_id ] );
75
			}
76
		}
77
78
		$this->items = $_return;
79
	}
80
81
	/**
82
	 * Returns first menu id or false if there are no menus.
83
	 *
84
	 * @return int|bool
85
	 */
86
	protected function get_first_menu_id() {
87
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
88
89
		if ( is_array( $menus ) && count( $menus ) ) {
90
			if ( isset( $menus[0]->term_id ) ) {
91
				return $menus[0]->term_id;
92
			}
93
		}
94
95
		return false;
96
	}
97
98
	/**
99
	 * Performs menu id lookup inside navigation Menus.
100
	 *
101
	 * @param  array      $locations locations that are returned by get_nav_menu_locations().
102
	 * @param  int|string $arg navigation id.
103
	 * @return int
104
	 */
105
	protected function get_locations_menu_id( $locations, $arg ) {
106
		if ( is_numeric( $arg ) ) {
107
			$arg = array_search( $arg, $locations );
108
		}
109
110
		if ( isset( $locations[ $arg ] ) ) {
111
			$menu_id = $locations[ $arg ];
112
113
			return $menu_id;
114
		}
115
	}
116
117
	/**
118
	 * Checks if the provided menu id exists.
119
	 *
120
	 * @param int $menu_id Menu ID.
121
	 *
122
	 * @return int|boolean
123
	 */
124
	protected function check_menu_id( $menu_id ) {
125
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
126
127
		if ( is_array( $menus ) && count( $menus ) ) {
128
			foreach ( $menus as $menu ) {
129
				if ( absint( $menu->term_id ) === absint( $menu_id ) ) {
130
					return $menu_id;
131
				}
132
			}
133
		}
134
135
		return false;
136
	}
137
138
	/**
139
	 * Returns menu id by menu name/slug.
140
	 *
141
	 * @param string $slug Menu's name.
142
	 *
143
	 * @return int|bool
144
	 */
145
	protected function get_menu_id_by_name( $slug = null ) {
146
		if ( $slug && is_string( $slug ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
147
			if ( $menu_id = get_term_by( 'slug', $slug, 'nav_menu' ) ) {
148
				return $menu_id;
149
			}
150
151
			if ( $menu_id = get_term_by( 'name', $slug, 'nav_menu' ) ) {
152
				return $menu_id;
153
			}
154
		}
155
156
		return false;
157
	}
158
159
	/**
160
	 * Returns menu items.
161
	 *
162
	 * @return array
163
	 */
164
	public function get_items() {
165
		return $this->items;
166
	}
167
}
168