Skip to content

Commit c0b3b7a

Browse files
committed
Add support for customizing Menus and Menu Items
1 parent 94aa19f commit c0b3b7a

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
vendor
22
composer.lock
33
.DS_Store
4-
.idea
4+
.idea
5+
tests/_output
6+
tests/wp
7+
wp-content

lib/Config/MenuItem.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Understory;
4+
5+
use Timber;
6+
7+
class MenuItem extends Timber\MenuItem implements MetaDataBinding
8+
{
9+
public function getBindingName()
10+
{
11+
return $this->post_type;
12+
}
13+
14+
/**
15+
* Implentation of MetaDataBinding::getMetaValue
16+
*
17+
* @param string $key Key for the meta field
18+
* @return string Value of the meta field
19+
*/
20+
public function getMetaValue($key)
21+
{
22+
return \get_post_meta($this->ID, $key, true);
23+
}
24+
25+
/**
26+
* Implentation of MetaDataBinding::setMetaValue
27+
*
28+
* @param string $key Key for the meta field
29+
* @param string $value Value for the meta field
30+
*/
31+
public function setMetaValue($key, $value)
32+
{
33+
\update_post_meta($this->ID, $key, $value);
34+
}
35+
36+
/**
37+
* Order of method calls:
38+
*
39+
* 1. getMethodName($args)
40+
* 2. methodName($args)
41+
* 3. propertyName
42+
* 4. fall back to Timber's core implementation
43+
*
44+
* @param string $propertyName
45+
* @param array $args
46+
* @return mixed
47+
*/
48+
public function __call($propertyName, $args = [])
49+
{
50+
if (method_exists($this, 'get'.$propertyName)) {
51+
return call_user_func_array([$this, 'get'.$propertyName], $args);
52+
} else if (method_exists($this, $propertyName)) {
53+
return call_user_func_array([$this, $propertyName], $args);
54+
} else if (property_exists($this, $propertyName)) {
55+
return $this->$propertyName;
56+
}
57+
58+
return parent::__call($propertyName, $args);
59+
}
60+
}

lib/CustomNavMenuItem.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Understory;
4+
5+
abstract class CustomNavMenuItem extends CustomPostType
6+
{
7+
static public $name = 'nav_menu_item';
8+
9+
public function setPost($post = null)
10+
{
11+
$this->setMetaDataBinding(new MenuItem($post));
12+
$this->getConfig();
13+
}
14+
}

lib/Menu.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Understory;
4+
5+
use Timber;
6+
7+
class Menu extends Timber\Menu
8+
{
9+
public $MenuItemClass = MenuItem::class;
10+
}

0 commit comments

Comments
 (0)