This repository has been archived by the owner on Jan 10, 2018. It is now read-only.
/
icon-functions.php
199 lines (175 loc) · 5.91 KB
/
icon-functions.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* SVG icons related functions and filters
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
*/
/**
* Add SVG definitions to the footer.
*/
function twentyseventeen_include_svg_icons() {
// Define SVG sprite file.
$svg_icons = get_parent_theme_file_path( '/assets/images/svg-icons.svg' );
// If it exists, include it.
if ( file_exists( $svg_icons ) ) {
require_once( $svg_icons );
}
}
add_action( 'wp_footer', 'twentyseventeen_include_svg_icons', 9999 );
/**
* Return SVG markup.
*
* @param array $args {
* Parameters needed to display an SVG.
*
* @type string $icon Required SVG icon filename.
* @type string $title Optional SVG title.
* @type string $desc Optional SVG description.
* }
* @return string SVG markup.
*/
function twentyseventeen_get_svg( $args = array() ) {
// Make sure $args are an array.
if ( empty( $args ) ) {
return __( 'Please define default parameters in the form of an array.', 'twentyseventeen' );
}
// Define an icon.
if ( false === array_key_exists( 'icon', $args ) ) {
return __( 'Please define an SVG icon filename.', 'twentyseventeen' );
}
// Set defaults.
$defaults = array(
'icon' => '',
'title' => '',
'desc' => '',
'aria_hidden' => true, // Hide from screen readers.
'fallback' => false,
);
// Parse args.
$args = wp_parse_args( $args, $defaults );
// Set aria hidden.
$aria_hidden = '';
if ( true === $args['aria_hidden'] ) {
$aria_hidden = ' aria-hidden="true"';
}
// Set ARIA.
$aria_labelledby = '';
if ( $args['title'] && $args['desc'] ) {
$aria_labelledby = ' aria-labelledby="title desc"';
}
// Begin SVG markup.
$svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
// If there is a title, display it.
if ( $args['title'] ) {
$svg .= '<title>' . esc_html( $args['title'] ) . '</title>';
}
// If there is a description, display it.
if ( $args['desc'] ) {
$svg .= '<desc>' . esc_html( $args['desc'] ) . '</desc>';
}
// Use absolute path in the Customizer so that icons show up in there.
if ( is_customize_preview() ) {
$svg .= '<use xlink:href="' . get_parent_theme_file_uri( '/assets/images/svg-icons.svg#icon-' . esc_html( $args['icon'] ) ) . '"></use>';
} else {
$svg .= '<use xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use>';
}
// Add some markup to use as a fallback for browsers that do not support SVGs.
if ( $args['fallback'] ) {
$svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
}
$svg .= '</svg>';
return $svg;
}
/**
* Display SVG icons in social links menu.
*
* @param string $item_output The menu item output.
* @param WP_Post $item Menu item object.
* @param int $depth Depth of the menu.
* @param array $args wp_nav_menu() arguments.
* @return string $item_output The menu item output with social icon.
*/
function twentyseventeen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
// Get supported social icons.
$social_icons = twentyseventeen_social_links_icons();
// Change SVG icon inside social links menu if there is supported URL.
if ( 'social' === $args->theme_location ) {
foreach ( $social_icons as $attr => $value ) {
if ( false !== strpos( $item_output, $attr ) ) {
$item_output = str_replace( $args->link_after, '</span>' . twentyseventeen_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
}
}
}
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'twentyseventeen_nav_menu_social_icons', 10, 4 );
/**
* Add dropdown icon if menu item has children.
*
* @param string $title The menu item's title.
* @param object $item The current menu item.
* @param array $args An array of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
* @return string $title The menu item's title with dropdown icon.
*/
function twentyseventeen_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
if ( 'top' === $args->theme_location ) {
foreach ( $item->classes as $value ) {
if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
$title = $title . twentyseventeen_get_svg( array( 'icon' => 'expand' ) );
}
}
}
return $title;
}
add_filter( 'nav_menu_item_title', 'twentyseventeen_dropdown_icon_to_menu_link', 10, 4 );
/**
* Returns an array of supported social links (URL and icon name).
*
* @return array $social_links_icons
*/
function twentyseventeen_social_links_icons() {
// Supported social links icons.
$social_links_icons = array(
'behance.net' => 'behance',
'codepen.io' => 'codepen',
'deviantart.com' => 'deviantart',
'digg.com' => 'digg',
'dribbble.com' => 'dribbble',
'dropbox.com' => 'dropbox',
'facebook.com' => 'facebook',
'flickr.com' => 'flickr',
'foursquare.com' => 'foursquare',
'plus.google.com' => 'google-plus',
'github.com' => 'github',
'instagram.com' => 'instagram',
'linkedin.com' => 'linkedin',
'mailto:' => 'envelope-o',
'medium.com' => 'medium',
'path.com' => 'path',
'pinterest.com' => 'pinterest-p',
'getpocket.com' => 'get-pocket',
'polldaddy.com' => 'polldaddy',
'reddit.com' => 'reddit-alien',
'skype.com' => 'skype',
'skype:' => 'skype',
'slideshare.net' => 'slideshare',
'snapchat.com' => 'snapchat-ghost',
'soundcloud.com' => 'soundcloud',
'spotify.com' => 'spotify',
'stumbleupon.com' => 'stumbleupon',
'tumblr.com' => 'tumblr',
'twitch.tv' => 'twitch',
'twitter.com' => 'twitter',
'vimeo.com' => 'vimeo',
'vine.co' => 'vine',
'vk.com' => 'vk',
'wordpress.org' => 'wordpress',
'wordpress.com' => 'wordpress',
'yelp.com' => 'yelp',
'youtube.com' => 'youtube',
);
return apply_filters( 'twentyseventeen_social_links_icons', $social_links_icons );
}