-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-social-walker.php
More file actions
174 lines (155 loc) · 5.24 KB
/
class-social-walker.php
File metadata and controls
174 lines (155 loc) · 5.24 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
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
<?php
/**
* description of package
*
* @package YourPackage
* @subpackage Subpackage name
* @author firstname lastname <user@host.com>
*/
class Social_Walker extends Walker_Nav_Menu {
/**
* Tell Walker where to inherit it's parent and id values.
*
* @var array $db_fields fields for query.
*/
public $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id',
);
/**
* Start the element output.
*
* @see Walker::start_el()
*
* @since 3.0.0
*
* @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 array $args An array of arguments. @see wp_nav_menu()
* @param int $id Current item ID.
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$li_classes = array();
$a_classes = array();
$img_classes = array( 'screen-reader-text' );
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'social-menu-item-' . $item->ID;
foreach ( $classes as $class_name ) {
$li_classes[] = $class_name;
}
/**
* Filter the CSS class(es) applied to a menu item's <li>.
*
* @since 3.0.0
*
* @see wp_nav_menu()
*
* @param array $classes The CSS classes that are applied to the menu item's <li>.
* @param object $item The current menu item.
* @param array $args An array of wp_nav_menu() arguments.
*/
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $li_classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Filter the ID applied to a menu item's <li>.
*
* @since 3.0.1
*
* @see wp_nav_menu()
*
* @param string $menu_id The ID that is applied to the menu item's <li>.
* @param object $item The current menu item.
* @param array $args An array of wp_nav_menu() arguments.
*/
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names . '>';
$atts = array();
$atts['class'] = implode( ' ', $a_classes );
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
/**
* Filter the HTML attributes applied to a menu item's <a>.
*
* @since 3.6.0
*
* @see wp_nav_menu()
*
* @param array $atts {
* The HTML attributes applied to the menu item's <a>, empty strings are ignored.
*
* @type string $title Title attribute.
* @type string $target Target attribute.
* @type string $rel The rel attribute.
* @type string $href The href attribute.
* }
* @param object $item The current menu item.
* @param array $args An array of wp_nav_menu() arguments.
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$args = (object) $args;
list( $name, $size ) = explode( '-', $classes[0] );
$size = isset( $size ) ? $size : 44;
$img_url = $this->class_to_icon_url( $name, $size );
$item_title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
/**
* This filter is documented in wp-includes/post-template.php
*/
$item_output .= $args->link_before;
$item_output .= sprintf(
'<img src="%s" height="%d" width="%d" alt="%s">',
$img_url,
$size,
$size,
$item_title
);
$item_output .= $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
/**
* Filter a menu item's starting output.
*
* The menu item's starting output only includes $args->before, the opening <a>,
* the menu item's title, the closing </a>, and $args->after. Currently, there is
* no filter for modifying the opening and closing <li> for a menu item.
*
* @since 3.0.0
*
* @see wp_nav_menu()
*
* @param string $item_output The menu item's starting HTML output.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of wp_nav_menu() arguments.
*/
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* Create an image url based on a class name.
*
* @param string $name the name of the class.
* @param int|bool $size the dimensions of the image.
*
* @return string
*/
protected function class_to_icon_url( $name, $size = false ) {
$filename = "icons8-{$name}.png";
$img_url = get_template_directory_uri();
$img_url .= '/img/social-icons';
$img_url .= '/' . $filename;
return $img_url;
}
}