icon-functions.php
210 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SVG icons related functions and filters |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | if ( ! function_exists( 'jetpack_social_menu_include_svg_icons' ) ) : |
| 9 | /** |
| 10 | * Add SVG definitions to the footer. |
| 11 | */ |
| 12 | function jetpack_social_menu_include_svg_icons() { |
| 13 | // Return early if Social Menu doesn't exist. |
| 14 | if ( ! has_nav_menu( 'jetpack-social-menu' ) ) { |
| 15 | return; |
| 16 | } |
| 17 | // Define SVG sprite file. |
| 18 | $svg_icons = __DIR__ . '/social-menu.svg'; |
| 19 | // If it exists and we use the SVG menu type, include it. |
| 20 | if ( file_exists( $svg_icons ) && 'svg' === jetpack_social_menu_get_type() ) { |
| 21 | require_once $svg_icons; |
| 22 | } |
| 23 | } |
| 24 | add_action( 'wp_footer', 'jetpack_social_menu_include_svg_icons', 9999 ); |
| 25 | endif; |
| 26 | |
| 27 | if ( ! function_exists( 'jetpack_social_menu_get_svg' ) ) : |
| 28 | /** |
| 29 | * Return SVG markup. |
| 30 | * |
| 31 | * @param array $args { |
| 32 | * Parameters needed to display an SVG. |
| 33 | * |
| 34 | * @type string $icon Required SVG icon filename. |
| 35 | * } |
| 36 | * @return string SVG markup. |
| 37 | */ |
| 38 | function jetpack_social_menu_get_svg( $args = array() ) { |
| 39 | // Make sure $args are an array. |
| 40 | if ( empty( $args ) ) { |
| 41 | return esc_html__( 'Please define default parameters in the form of an array.', 'jetpack' ); |
| 42 | } |
| 43 | |
| 44 | // Define an icon. |
| 45 | if ( false === array_key_exists( 'icon', $args ) ) { |
| 46 | return esc_html__( 'Please define an SVG icon filename.', 'jetpack' ); |
| 47 | } |
| 48 | |
| 49 | // Set defaults. |
| 50 | $defaults = array( |
| 51 | 'icon' => '', |
| 52 | 'fallback' => false, |
| 53 | ); |
| 54 | |
| 55 | // Parse args. |
| 56 | $args = wp_parse_args( $args, $defaults ); |
| 57 | |
| 58 | // Set aria hidden. |
| 59 | $aria_hidden = ' aria-hidden="true"'; |
| 60 | |
| 61 | // Begin SVG markup. |
| 62 | $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . ' role="img">'; |
| 63 | |
| 64 | /* |
| 65 | * Display the icon. |
| 66 | * |
| 67 | * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10. |
| 68 | * |
| 69 | * See https://core.trac.wordpress.org/ticket/38387. |
| 70 | */ |
| 71 | $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> '; |
| 72 | |
| 73 | // Add some markup to use as a fallback for browsers that do not support SVGs. |
| 74 | if ( $args['fallback'] ) { |
| 75 | $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>'; |
| 76 | } |
| 77 | |
| 78 | $svg .= '</svg>'; |
| 79 | |
| 80 | return $svg; |
| 81 | } |
| 82 | endif; |
| 83 | |
| 84 | if ( ! function_exists( 'jetpack_social_menu_nav_menu_social_icons' ) ) : |
| 85 | /** |
| 86 | * Display SVG icons in social links menu. |
| 87 | * |
| 88 | * @param string $item_output The menu item output. |
| 89 | * @param WP_Post $item Menu item object. |
| 90 | * @param int $depth Depth of the menu. |
| 91 | * @param array $args wp_nav_menu() arguments. |
| 92 | * @return string $item_output The menu item output with social icon. |
| 93 | */ |
| 94 | function jetpack_social_menu_nav_menu_social_icons( $item_output, $item, $depth, $args ) { |
| 95 | // Get supported social icons. |
| 96 | $social_icons = jetpack_social_menu_social_links_icons(); |
| 97 | |
| 98 | // Change SVG icon inside social links menu if there is supported URL. |
| 99 | if ( 'jetpack-social-menu' === $args->theme_location ) { |
| 100 | foreach ( $social_icons as $attr => $value ) { |
| 101 | /* |
| 102 | * attr can be a URL host, or a regex, starting with #. |
| 103 | * Let's check for both scenarios. |
| 104 | */ |
| 105 | if ( |
| 106 | // First Regex. |
| 107 | ( |
| 108 | '#' === substr( $attr, 0, 1 ) && '#' === substr( $attr, -1 ) |
| 109 | && preg_match( $attr, $item_output ) |
| 110 | ) |
| 111 | // Then, regular host name. |
| 112 | || false !== strpos( $item_output, $attr ) |
| 113 | ) { |
| 114 | $item_output = str_replace( |
| 115 | $args->link_after, |
| 116 | '</span>' . jetpack_social_menu_get_svg( array( 'icon' => esc_attr( $value ) ) ), |
| 117 | $item_output |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return $item_output; |
| 124 | } |
| 125 | add_filter( 'walker_nav_menu_start_el', 'jetpack_social_menu_nav_menu_social_icons', 10, 4 ); |
| 126 | endif; |
| 127 | |
| 128 | if ( ! function_exists( 'jetpack_social_menu_social_links_icons' ) ) : |
| 129 | /** |
| 130 | * Returns an array of supported social links (URL / regex and icon name). |
| 131 | * For regex, use the # delimiter. |
| 132 | * |
| 133 | * @return array $social_links_icons |
| 134 | */ |
| 135 | function jetpack_social_menu_social_links_icons() { |
| 136 | // Supported social links icons. |
| 137 | $social_links_icons = array( |
| 138 | '#https?:\/\/(www\.)?amazon\.(com|cn|in|fr|de|it|nl|es|co|ca)\/#' => 'amazon', |
| 139 | '500px.com' => '500px', |
| 140 | 'apple.com' => 'apple', |
| 141 | 'itunes.com' => 'apple', |
| 142 | 'bandcamp.com' => 'bandcamp', |
| 143 | 'behance.net' => 'behance', |
| 144 | 'blogger.com' => 'blogger', |
| 145 | 'blogspot.com' => 'blogger', |
| 146 | 'codepen.io' => 'codepen', |
| 147 | 'deviantart.com' => 'deviantart', |
| 148 | 'discord.gg' => 'discord', |
| 149 | 'discordapp.com' => 'discord', |
| 150 | 'digg.com' => 'digg', |
| 151 | 'dribbble.com' => 'dribbble', |
| 152 | 'dropbox.com' => 'dropbox', |
| 153 | 'etsy.com' => 'etsy', |
| 154 | 'eventbrite.com' => 'eventbrite', |
| 155 | 'facebook.com' => 'facebook', |
| 156 | '/feed/' => 'feed', |
| 157 | 'flickr.com' => 'flickr', |
| 158 | 'foursquare.com' => 'foursquare', |
| 159 | 'ghost.org' => 'ghost', |
| 160 | 'goodreads.com' => 'goodreads', |
| 161 | 'google.com' => 'google', |
| 162 | 'github.com' => 'github', |
| 163 | 'instagram.com' => 'instagram', |
| 164 | 'linkedin.com' => 'linkedin', |
| 165 | 'mailto:' => 'mail', |
| 166 | 'meetup.com' => 'meetup', |
| 167 | 'medium.com' => 'medium', |
| 168 | 'nextdoor.com' => 'nextdoor', |
| 169 | 'patreon.com' => 'patreon', |
| 170 | 'pinterest.' => 'pinterest', |
| 171 | 'getpocket.com' => 'pocket', |
| 172 | 'ravelry.com' => 'ravelry', |
| 173 | 'reddit.com' => 'reddit', |
| 174 | 'skype.com' => 'skype', |
| 175 | 'skype:' => 'skype', |
| 176 | 'slideshare.net' => 'slideshare', |
| 177 | 'snapchat.com' => 'snapchat', |
| 178 | 'soundcloud.com' => 'soundcloud', |
| 179 | 'spotify.com' => 'spotify', |
| 180 | 'stackoverflow.com' => 'stackoverflow', |
| 181 | 'strava.com' => 'strava', |
| 182 | 'stumbleupon.com' => 'stumbleupon', |
| 183 | 'telegram.me' => 'telegram', |
| 184 | 'tiktok.com' => 'tiktok', |
| 185 | 'tumblr.com' => 'tumblr', |
| 186 | 'twitch.tv' => 'twitch', |
| 187 | 'twitter.com' => 'twitter', |
| 188 | 'vimeo.com' => 'vimeo', |
| 189 | 'vk.com' => 'vk', |
| 190 | 'whatsapp.com' => 'whatsapp', |
| 191 | 'woocommerce.com' => 'woocommerce', |
| 192 | 'wordpress.org' => 'wordpress', |
| 193 | 'wordpress.com' => 'wordpress', |
| 194 | 'yelp.com' => 'yelp', |
| 195 | 'xanga.com' => 'xanga', |
| 196 | 'youtube.com' => 'youtube', |
| 197 | ); |
| 198 | |
| 199 | /* |
| 200 | * Add Mastodon instances to this array. |
| 201 | */ |
| 202 | $mastodon_instance_list = jetpack_mastodon_get_instance_list(); |
| 203 | foreach ( $mastodon_instance_list as $instance ) { |
| 204 | $social_links_icons[ $instance ] = 'mastodon'; |
| 205 | } |
| 206 | |
| 207 | return $social_links_icons; |
| 208 | } |
| 209 | endif; |
| 210 |