Aucune description

icon-functions.php 5.9KB

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