-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuItem.php
More file actions
99 lines (82 loc) · 2.39 KB
/
MenuItem.php
File metadata and controls
99 lines (82 loc) · 2.39 KB
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
96
97
98
99
<?php
/**
* ThemePlate menu iterator
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate;
use WP_Post;
/**
* @phpstan-type DecoratedMenuItem WP_Post&object{
* menu_item_parent: string,
* title: string,
* url: string,
* target: string,
* attr_title: string,
* description: string,
* classes: array<int, string>,
* xfn: string,
* current: bool,
* current_item_ancestor: bool,
* current_item_parent: bool,
* }
*/
class MenuItem {
public int $id;
public string $slug;
public int $parent;
public string $label;
public string $url;
public string $target;
public string $title;
public string $info;
/** @var string[] */
public array $classes;
public string $xfn;
/** @var MenuItem[] */
public array $children;
public bool $is_active;
public bool $is_active_parent;
public bool $is_active_ancestor;
/** @param DecoratedMenuItem $post */
public function __construct( WP_Post $post ) {
$this->id = $post->ID;
$this->slug = $post->post_name;
$this->parent = (int) $post->menu_item_parent;
$this->label = $post->title;
$this->url = $post->url;
$this->target = $post->target;
$this->title = $post->attr_title;
$this->info = $post->description;
$this->classes = array_filter( $post->classes, array( $this, 'filter' ) );
$this->xfn = $post->xfn;
$this->children = array();
$this->is_active = $post->current;
$this->is_active_parent = $post->current_item_parent;
$this->is_active_ancestor = $post->current_item_ancestor;
}
protected function filter( string $classname ): bool {
if ( '' === $classname ) {
return false;
}
$blacklist = array(
'menu-item',
'menu-item-home',
'menu-item-privacy-policy',
'current-menu-item',
'current-menu-ancestor',
'current-menu-parent',
'page_item',
'current_page_item',
'current_page_parent',
'current_page_ancestor',
);
if ( in_array( $classname, $blacklist, true ) ) {
return false;
}
// menu-item-type-test menu-item-object-test page-item-2 current-test-ancestor current-test-parent
$pattern = '^(menu-item-(type|object)-\w+|page-item-\d+|current-\w+-(parent|ancestor))$';
return in_array( preg_match( '/' . $pattern . '/', $classname ), array( 0, false ), true );
}
}