PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wp-content/themes/twentyseventeen/inc/icon-functions.php

https://gitlab.com/morganestes/wordpress-develop
PHP | 220 lines | 111 code | 24 blank | 85 comment | 13 complexity | add91992a7be3ffe5a3e699546ff45c9 MD5 | raw file
  1. <?php
  2. /**
  3. * SVG icons related functions and filters
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Seventeen
  7. * @since 1.0
  8. */
  9. /**
  10. * Add SVG definitions to the footer.
  11. */
  12. function twentyseventeen_include_svg_icons() {
  13. // Define SVG sprite file.
  14. $svg_icons = get_parent_theme_file_path( '/assets/images/svg-icons.svg' );
  15. // If it exists, include it.
  16. if ( file_exists( $svg_icons ) ) {
  17. require_once( $svg_icons );
  18. }
  19. }
  20. add_action( 'wp_footer', 'twentyseventeen_include_svg_icons', 9999 );
  21. /**
  22. * Return SVG markup.
  23. *
  24. * @param array $args {
  25. * Parameters needed to display an SVG.
  26. *
  27. * @type string $icon Required SVG icon filename.
  28. * @type string $title Optional SVG title.
  29. * @type string $desc Optional SVG description.
  30. * }
  31. * @return string SVG markup.
  32. */
  33. function twentyseventeen_get_svg( $args = array() ) {
  34. // Make sure $args are an array.
  35. if ( empty( $args ) ) {
  36. return __( 'Please define default parameters in the form of an array.', 'twentyseventeen' );
  37. }
  38. // Define an icon.
  39. if ( false === array_key_exists( 'icon', $args ) ) {
  40. return __( 'Please define an SVG icon filename.', 'twentyseventeen' );
  41. }
  42. // Set defaults.
  43. $defaults = array(
  44. 'icon' => '',
  45. 'title' => '',
  46. 'desc' => '',
  47. 'fallback' => false,
  48. );
  49. // Parse args.
  50. $args = wp_parse_args( $args, $defaults );
  51. // Set aria hidden.
  52. $aria_hidden = ' aria-hidden="true"';
  53. // Set ARIA.
  54. $aria_labelledby = '';
  55. /*
  56. * Twenty Seventeen doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
  57. *
  58. * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
  59. *
  60. * Example 1 with title: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ) ) ); ?>
  61. *
  62. * 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' ) ) ); ?>
  63. *
  64. * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
  65. */
  66. if ( $args['title'] ) {
  67. $aria_hidden = '';
  68. $unique_id = uniqid();
  69. $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
  70. if ( $args['desc'] ) {
  71. $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
  72. }
  73. }
  74. // Begin SVG markup.
  75. $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
  76. // Display the title.
  77. if ( $args['title'] ) {
  78. $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
  79. // Display the desc only if the title is already set.
  80. if ( $args['desc'] ) {
  81. $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
  82. }
  83. }
  84. /*
  85. * Display the icon.
  86. *
  87. * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
  88. *
  89. * See https://core.trac.wordpress.org/ticket/38387.
  90. */
  91. $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
  92. // Add some markup to use as a fallback for browsers that do not support SVGs.
  93. if ( $args['fallback'] ) {
  94. $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
  95. }
  96. $svg .= '</svg>';
  97. return $svg;
  98. }
  99. /**
  100. * Display SVG icons in social links menu.
  101. *
  102. * @param string $item_output The menu item output.
  103. * @param WP_Post $item Menu item object.
  104. * @param int $depth Depth of the menu.
  105. * @param array $args wp_nav_menu() arguments.
  106. * @return string $item_output The menu item output with social icon.
  107. */
  108. function twentyseventeen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
  109. // Get supported social icons.
  110. $social_icons = twentyseventeen_social_links_icons();
  111. // Change SVG icon inside social links menu if there is supported URL.
  112. if ( 'social' === $args->theme_location ) {
  113. foreach ( $social_icons as $attr => $value ) {
  114. if ( false !== strpos( $item_output, $attr ) ) {
  115. $item_output = str_replace( $args->link_after, '</span>' . twentyseventeen_get_svg( array( 'icon' => esc_attr( $value ) ) ), $item_output );
  116. }
  117. }
  118. }
  119. return $item_output;
  120. }
  121. add_filter( 'walker_nav_menu_start_el', 'twentyseventeen_nav_menu_social_icons', 10, 4 );
  122. /**
  123. * Add dropdown icon if menu item has children.
  124. *
  125. * @param string $title The menu item's title.
  126. * @param WP_Post $item The current menu item.
  127. * @param array $args An array of wp_nav_menu() arguments.
  128. * @param int $depth Depth of menu item. Used for padding.
  129. * @return string $title The menu item's title with dropdown icon.
  130. */
  131. function twentyseventeen_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
  132. if ( 'top' === $args->theme_location ) {
  133. foreach ( $item->classes as $value ) {
  134. if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
  135. $title = $title . twentyseventeen_get_svg( array( 'icon' => 'angle-down' ) );
  136. }
  137. }
  138. }
  139. return $title;
  140. }
  141. add_filter( 'nav_menu_item_title', 'twentyseventeen_dropdown_icon_to_menu_link', 10, 4 );
  142. /**
  143. * Returns an array of supported social links (URL and icon name).
  144. *
  145. * @return array $social_links_icons
  146. */
  147. function twentyseventeen_social_links_icons() {
  148. // Supported social links icons.
  149. $social_links_icons = array(
  150. 'behance.net' => 'behance',
  151. 'codepen.io' => 'codepen',
  152. 'deviantart.com' => 'deviantart',
  153. 'digg.com' => 'digg',
  154. 'docker.com' => 'dockerhub',
  155. 'dribbble.com' => 'dribbble',
  156. 'dropbox.com' => 'dropbox',
  157. 'facebook.com' => 'facebook',
  158. 'flickr.com' => 'flickr',
  159. 'foursquare.com' => 'foursquare',
  160. 'plus.google.com' => 'google-plus',
  161. 'github.com' => 'github',
  162. 'instagram.com' => 'instagram',
  163. 'linkedin.com' => 'linkedin',
  164. 'mailto:' => 'envelope-o',
  165. 'medium.com' => 'medium',
  166. 'pinterest.com' => 'pinterest-p',
  167. 'pscp.tv' => 'periscope',
  168. 'getpocket.com' => 'get-pocket',
  169. 'reddit.com' => 'reddit-alien',
  170. 'skype.com' => 'skype',
  171. 'skype:' => 'skype',
  172. 'slideshare.net' => 'slideshare',
  173. 'snapchat.com' => 'snapchat-ghost',
  174. 'soundcloud.com' => 'soundcloud',
  175. 'spotify.com' => 'spotify',
  176. 'stumbleupon.com' => 'stumbleupon',
  177. 'tumblr.com' => 'tumblr',
  178. 'twitch.tv' => 'twitch',
  179. 'twitter.com' => 'twitter',
  180. 'vimeo.com' => 'vimeo',
  181. 'vine.co' => 'vine',
  182. 'vk.com' => 'vk',
  183. 'wordpress.org' => 'wordpress',
  184. 'wordpress.com' => 'wordpress',
  185. 'yelp.com' => 'yelp',
  186. 'youtube.com' => 'youtube',
  187. );
  188. /**
  189. * Filter Twenty Seventeen social links icons.
  190. *
  191. * @since Twenty Seventeen 1.0
  192. *
  193. * @param array $social_links_icons Array of social links icons.
  194. */
  195. return apply_filters( 'twentyseventeen_social_links_icons', $social_links_icons );
  196. }