-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavMenuTrait.php
95 lines (84 loc) · 1.99 KB
/
NavMenuTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* NavMenu Trait
*
* @author Whizark <devaloka@whizark.com>
* @see http://whizark.com
* @copyright Copyright (C) 2015 Whizark.
* @license MIT
* @license GPL-2.0
* @license GPL-3.0
*/
namespace Devaloka\Component\NavMenu;
use RuntimeException;
/**
* Trait NavMenuTrait
*
* @package Devaloka\Component\NavMenu
*/
trait NavMenuTrait
{
/**
* Gets the menu location identifier.
*
* @return string The location identifier.
*/
abstract public function getLocation();
/**
* Gets the menu location descriptive text.
*
* @return string The location descriptive text.
*/
abstract public function getDescription();
/**
* Gets the default options for the menu.
*
* @return mixed[] The default options.
*/
public function getDefaultOptions()
{
return [];
}
/**
* Renders the menu with the given options.
*
* @param mixed[] $options The options.
*
* @return string The rendered HTML.
*/
public function render(array $options = [])
{
$options = array_merge($this->getDefaultOptions(), $options);
$options['echo'] = false;
return wp_nav_menu($options);
}
/**
* Displays the menu with the given options.
*
* @param mixed[] $options The options.
*/
public function display(array $options = [])
{
$options = array_merge($this->getDefaultOptions(), $options);
$options['echo'] = true;
wp_nav_menu($options);
}
/**
* Registers the menu.
*/
public function register()
{
register_nav_menu($this->getLocation(), $this->getDescription());
}
/**
* Unregisters the menu.
*
* @throws RuntimeException If the menu cannot be unregistered.
*/
public function unregister()
{
if (!unregister_nav_menu($this->getLocation())) {
throw new RuntimeException('Cannot unregister the NavMenu.');
}
}
}