3 * SVG icons related functions and filters
6 * @subpackage Twenty_Seventeen
11 * Add SVG definitions to the footer.
13 function twentyseventeen_include_svg_icons() {
14 // Define SVG sprite file.
15 $svg_icons = get_parent_theme_file_path( '/assets/images/svg-icons.svg' );
17 // If it exists, include it.
18 if ( file_exists( $svg_icons ) ) {
19 require_once( $svg_icons );
22 add_action( 'wp_footer', 'twentyseventeen_include_svg_icons', 9999 );
27 * @param array $args {
28 * Parameters needed to display an SVG.
30 * @type string $icon Required SVG icon filename.
31 * @type string $title Optional SVG title.
32 * @type string $desc Optional SVG description.
34 * @return string SVG markup.
36 function twentyseventeen_get_svg( $args = array() ) {
37 // Make sure $args are an array.
38 if ( empty( $args ) ) {
39 return __( 'Please define default parameters in the form of an array.', 'twentyseventeen' );
43 if ( false === array_key_exists( 'icon', $args ) ) {
44 return __( 'Please define an SVG icon filename.', 'twentyseventeen' );
56 $args = wp_parse_args( $args, $defaults );
59 $aria_hidden = ' aria-hidden="true"';
62 $aria_labelledby = '';
65 * Twenty Seventeen doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
67 * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
69 * Example 1 with title: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ) ) ); ?>
71 * Example 2 with title and description: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ), 'desc' => __( 'This is the description', 'textdomain' ) ) ); ?>
73 * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
75 if ( $args['title'] ) {
77 $unique_id = uniqid();
78 $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
80 if ( $args['desc'] ) {
81 $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
86 $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
89 if ( $args['title'] ) {
90 $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
92 // Display the desc only if the title is already set.
93 if ( $args['desc'] ) {
94 $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
101 * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
103 * See https://core.trac.wordpress.org/ticket/38387.
105 $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
107 // Add some markup to use as a fallback for browsers that do not support SVGs.
108 if ( $args['fallback'] ) {
109 $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
118 * Display SVG icons in social links menu.
120 * @param string $item_output The menu item output.
121 * @param WP_Post $item Menu item object.
122 * @param int $depth Depth of the menu.
123 * @param array $args wp_nav_menu() arguments.
124 * @return string $item_output The menu item output with social icon.
126 function twentyseventeen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
127 // Get supported social icons.
128 $social_icons = twentyseventeen_social_links_icons();
130 // Change SVG icon inside social links menu if there is supported URL.
131 if ( 'social' === $args->theme_location ) {
132 foreach ( $social_icons as $attr => $value ) {
133 if ( false !== strpos( $item_output, $attr ) ) {
134 $item_output = str_replace( $args->link_after, '</span>' . twentyseventeen_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
141 add_filter( 'walker_nav_menu_start_el', 'twentyseventeen_nav_menu_social_icons', 10, 4 );
144 * Add dropdown icon if menu item has children.
146 * @param string $title The menu item's title.
147 * @param object $item The current menu item.
148 * @param array $args An array of wp_nav_menu() arguments.
149 * @param int $depth Depth of menu item. Used for padding.
150 * @return string $title The menu item's title with dropdown icon.
152 function twentyseventeen_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
153 if ( 'top' === $args->theme_location ) {
154 foreach ( $item->classes as $value ) {
155 if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
156 $title = $title . twentyseventeen_get_svg( array( 'icon' => 'angle-down' ) );
163 add_filter( 'nav_menu_item_title', 'twentyseventeen_dropdown_icon_to_menu_link', 10, 4 );
166 * Returns an array of supported social links (URL and icon name).
168 * @return array $social_links_icons
170 function twentyseventeen_social_links_icons() {
171 // Supported social links icons.
172 $social_links_icons = array(
173 'behance.net' => 'behance',
174 'codepen.io' => 'codepen',
175 'deviantart.com' => 'deviantart',
176 'digg.com' => 'digg',
177 'dribbble.com' => 'dribbble',
178 'dropbox.com' => 'dropbox',
179 'facebook.com' => 'facebook',
180 'flickr.com' => 'flickr',
181 'foursquare.com' => 'foursquare',
182 'plus.google.com' => 'google-plus',
183 'github.com' => 'github',
184 'instagram.com' => 'instagram',
185 'linkedin.com' => 'linkedin',
186 'mailto:' => 'envelope-o',
187 'medium.com' => 'medium',
188 'pinterest.com' => 'pinterest-p',
189 'getpocket.com' => 'get-pocket',
190 'reddit.com' => 'reddit-alien',
191 'skype.com' => 'skype',
193 'slideshare.net' => 'slideshare',
194 'snapchat.com' => 'snapchat-ghost',
195 'soundcloud.com' => 'soundcloud',
196 'spotify.com' => 'spotify',
197 'stumbleupon.com' => 'stumbleupon',
198 'tumblr.com' => 'tumblr',
199 'twitch.tv' => 'twitch',
200 'twitter.com' => 'twitter',
201 'vimeo.com' => 'vimeo',
204 'wordpress.org' => 'wordpress',
205 'wordpress.com' => 'wordpress',
206 'yelp.com' => 'yelp',
207 'youtube.com' => 'youtube',
211 * Filter Twenty Seventeen social links icons.
213 * @since Twenty Seventeen 1.0
215 * @param array $social_links_icons
217 return apply_filters( 'twentyseventeen_social_links_icons', $social_links_icons );