Completed
Push — fix/visibility-condition-issue... ( eee6fd...a71c15 )
by
unknown
11:07
created

icon-functions.php ➔ jetpack_social_menu_nav_menu_social_icons()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 4
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * SVG icons related functions and filters
4
 */
5
6
/**
7
 * Add SVG definitions to the footer.
8
 */
9
function jetpack_social_menu_include_svg_icons() {
10
	// Define SVG sprite file.
11
	$svg_icons = dirname( __FILE__ ) . '/social-menu.svg';
12
13
	// If it exists, include it.
14
	if ( file_exists( $svg_icons ) ) {
15
		require_once( $svg_icons );
16
	}
17
}
18
add_action( 'wp_footer', 'jetpack_social_menu_include_svg_icons', 9999 );
19
20
/**
21
 * Return SVG markup.
22
 *
23
 * @param array $args {
24
 *     Parameters needed to display an SVG.
25
 *
26
 *     @type string $icon  Required SVG icon filename.
27
 * }
28
 * @return string SVG markup.
29
 */
30
function jetpack_social_menu_get_svg( $args = array() ) {
31
	// Make sure $args are an array.
32
	if ( empty( $args ) ) {
33
		return esc_html__( 'Please define default parameters in the form of an array.', 'jetpack' );
34
	}
35
36
	// Define an icon.
37
	if ( false === array_key_exists( 'icon', $args ) ) {
38
		return esc_html__( 'Please define an SVG icon filename.', 'jetpack' );
39
	}
40
41
	// Set defaults.
42
	$defaults = array(
43
		'icon'        => '',
44
		'fallback'    => false,
45
	);
46
47
	// Parse args.
48
	$args = wp_parse_args( $args, $defaults );
49
50
	// Set aria hidden.
51
	$aria_hidden = ' aria-hidden="true"';
52
53
	// Begin SVG markup.
54
	$svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . ' role="img">';
55
56
	/*
57
	 * Display the icon.
58
	 *
59
	 * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
60
	 *
61
	 * See https://core.trac.wordpress.org/ticket/38387.
62
	 */
63
	$svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
64
65
	// Add some markup to use as a fallback for browsers that do not support SVGs.
66
	if ( $args['fallback'] ) {
67
		$svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
68
	}
69
70
	$svg .= '</svg>';
71
72
	return $svg;
73
}
74
75
/**
76
 * Display SVG icons in social links menu.
77
 *
78
 * @param  string  $item_output The menu item output.
79
 * @param  WP_Post $item        Menu item object.
80
 * @param  int     $depth       Depth of the menu.
81
 * @param  array   $args        wp_nav_menu() arguments.
82
 * @return string  $item_output The menu item output with social icon.
83
 */
84
function jetpack_social_menu_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $item is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $depth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
	// Get supported social icons.
86
	$social_icons = jetpack_social_menu_social_links_icons();
87
88
	// Change SVG icon inside social links menu if there is supported URL.
89
	if ( 'jetpack-social-menu' === $args->theme_location ) {
90
		foreach ( $social_icons as $attr => $value ) {
91
			if ( false !== strpos( $item_output, $attr ) ) {
92
				$item_output = str_replace( $args->link_after, '</span>' . jetpack_social_menu_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
93
			}
94
		}
95
	}
96
97
	return $item_output;
98
}
99
add_filter( 'walker_nav_menu_start_el', 'jetpack_social_menu_nav_menu_social_icons', 10, 4 );
100
101
/**
102
 * Returns an array of supported social links (URL and icon name).
103
 *
104
 * @return array $social_links_icons
105
 */
106
function jetpack_social_menu_social_links_icons() {
107
	// Supported social links icons.
108
	$social_links_icons = array(
109
		'amazon.cn'       => 'amazon',
110
		'amazon.in'       => 'amazon',
111
		'amazon.fr'       => 'amazon',
112
		'amazon.de'       => 'amazon',
113
		'amazon.it'       => 'amazon',
114
		'amazon.nl'       => 'amazon',
115
		'amazon.es'       => 'amazon',
116
		'amazon.co'       => 'amazon',
117
		'amazon.ca'       => 'amazon',
118
		'amazon.com'      => 'amazon',
119
		'apple.com'       => 'apple',
120
		'itunes.com'      => 'apple',
121
		'bandcamp.com'    => 'bandcamp',
122
		'behance.net'     => 'behance',
123
		'codepen.io'      => 'codepen',
124
		'deviantart.com'  => 'deviantart',
125
		'digg.com'        => 'digg',
126
		'dribbble.com'    => 'dribbble',
127
		'dropbox.com'     => 'dropbox',
128
		'facebook.com'    => 'facebook',
129
		'/feed/'          => 'feed',
130
		'flickr.com'      => 'flickr',
131
		'foursquare.com'  => 'foursquare',
132
		'plus.google.com' => 'google-plus',
133
		'google.com'      => 'google',
134
		'github.com'      => 'github',
135
		'instagram.com'   => 'instagram',
136
		'linkedin.com'    => 'linkedin',
137
		'mailto:'         => 'mail',
138
		'medium.com'      => 'medium',
139
		'pinterest.com'   => 'pinterest',
140
		'getpocket.com'   => 'pocket',
141
		'reddit.com'      => 'reddit',
142
		'skype.com'       => 'skype',
143
		'skype:'          => 'skype',
144
		'slideshare.net'  => 'slideshare',
145
		'snapchat.com'    => 'snapchat',
146
		'soundcloud.com'  => 'soundcloud',
147
		'spotify.com'     => 'spotify',
148
		'stumbleupon.com' => 'stumbleupon',
149
		'tumblr.com'      => 'tumblr',
150
		'twitch.tv'       => 'twitch',
151
		'twitter.com'     => 'twitter',
152
		'vimeo.com'       => 'vimeo',
153
		'vk.com'          => 'vk',
154
		'wordpress.org'   => 'wordpress',
155
		'wordpress.com'   => 'wordpress',
156
		'yelp.com'        => 'yelp',
157
		'youtube.com'     => 'youtube',
158
	);
159
160
	return $social_links_icons;
161
}
162