forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShowItemExcerptWalkerNavMenu.class.php
184 lines (163 loc) · 5.13 KB
/
ShowItemExcerptWalkerNavMenu.class.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* stores the ShowItemExcerptWalkerNavMenu class definition
*/
/**
* Shows the title, the post thumbnail
* and the excerpt for every menu entry.
* Additional markup will be appended
* to the default menu entry.
* @author etessore
* @version 1.0.0
* @package classes
*/
class ShowItemExcerptWalkerNavMenu extends Walker_Nav_Menu {
/**
* @var string the template
*/
public $tpl;
public $thumb_media_size = 'thumbnail';
public $classes = array();
/**
* @var string the excerpt buffer
*/
public $excerpt_output;
/**
* Initializes the object with default values.
* Hint: overload this if you want
* to change the printed markup
*/
public function __construct(){
$this->tpl = new SubstitutionTemplate();
$this->tpl->set_tpl(<<< EOF
<div id="%id%" class="preview-entry">
<div class="grid_2 alpha">
<div class="image">%image%</div>
<div class="permalink">%permalink%</div>
</div>
<div class="grid_8 omega">
<div class="title">%title%</div>
<div class="excerpt">%excerpt%</div>
</div>
</div>
EOF
);
$this->excerpt_output = '';
}
/**
* Builds the %image% substitution
* @param int $post_id the post ID
* @param string $size the media size
* @param array $args optional arguments for the anchor
* @return string the image markup
*/
public function get_image($item, $size, $args){
return HtmlHelper::anchor(
get_permalink($item->object_id),
get_the_post_thumbnail($item->object_id, $size),
$args
);
}
/**
* Builds the %id% substitution
* @param int $post_id the post ID
* @return string the id
*/
public function get_excerpt_id($item){
return 'navmenu-excerpt-'.$item->object_id;
}
/**
* Builds the id of the html li element
* @param object $item the menu item
* @param array $args additional arguments
* @return string the id
*/
public function get_container_id($item, $args){
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
return $id;
}
/**
* Builds the %permalink% substitution
* @param int $post_id the post ID
* @param array $args optional arguments for the anchor tag
* @return string html anchor tag for the permalink
*/
public function get_permalink($item, $args=array()){
return HtmlHelper::anchor(
get_permalink($item->object_id),
$this->get_title($item),
$args
);
}
/**
* Builds the %title% substitution
* @param int $post_id the post ID
* @return Ambigous <string, mixed> the title
*/
public function get_title($item){
return apply_filters( 'the_title', $item->title, $item->ID );
}
/**
* Builds the %excerpt% substitution
* @param int $post_id the post ID
* @return Ambigous <string, mixed> the excerpt
*/
public function get_excerpt($item){
global $post;
$post = get_post($item->object_id);
$toret = get_the_excerpt();
wp_reset_postdata();
return $toret;
}
/**
* Builds the %href% substitution
* @param int $post_id the post ID
* @return string the href for the current menu element
*/
public function get_href($item){
if(isset($item->object_id)) return get_permalink($item->object_id);
}
/**
* Adds a class to the li
* @param string $class the class
* @return ShowItemExcerptWalkerNavMenu $this for chainability
*/
public function add_class($class){
$this->classes[] = $class;
return $this;
}
/**
* Sets the thumbnail media dimension
* @param string $size the media dimension
* @return ShowItemExcerptWalkerNavMenu $this for chainability
*/
public function set_thumb_media_size($size){
$this->thumb_media_size = $size;
return $this;
}
/**
* (non-PHPdoc)
* @see Walker_Nav_Menu::start_el()
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $args
*/
function start_el(&$output, $item, $depth, $args){
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$this->classes = array_merge((array) $item->classes, $this->classes);
$this->add_class('menu-item-' . $item->ID);
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $this->classes ), $item, $args ) );
$output .= $indent . '<li' . $this->get_container_id($item, $args) . ' class="' . esc_attr( $class_names ) . '"' .'>';
$this->tpl
->set_markup('id', $this->get_excerpt_id($item))
->set_markup('image', $this->get_image($item, $this->thumb_media_size, array('title'=>$this->get_title($item))))
->set_markup('permalink', $this->get_permalink($item, array('title'=>$this->get_title($item))))
->set_markup('title', $this->get_title($item))
->set_markup('excerpt', $this->get_excerpt($item))
->set_markup('href', $this->get_href($item));
$this->excerpt_output .= $this->tpl->replace_markup();
$output .= apply_filters( 'walker_nav_menu_start_el', $this->tpl->replace_markup(), $item, $depth, $args );
}
}