icon-functions.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * SVG icons related functions and filters
  4. *
  5. * @package Radcliffe 2
  6. */
  7. /**
  8. * Add SVG definitions to the footer.
  9. */
  10. function radcliffe_2_include_svg_icons() {
  11. // Define SVG sprite file.
  12. $svg_icons = get_parent_theme_file_path( '/assets/images/icons.svg' );
  13. // If it exists, include it.
  14. if ( file_exists( $svg_icons ) ) {
  15. require_once( $svg_icons );
  16. }
  17. }
  18. add_action( 'wp_footer', 'radcliffe_2_include_svg_icons', 9999 );
  19. /**
  20. * Return SVG markup.
  21. *
  22. * @param array $args {
  23. * Parameters needed to display an SVG.
  24. *
  25. * @type string $icon Required SVG icon filename.
  26. * @type string $title Optional SVG title.
  27. * @type string $desc Optional SVG description.
  28. * }
  29. * @return string SVG markup.
  30. */
  31. function radcliffe_2_get_svg( $args = array() ) {
  32. // Make sure $args are an array.
  33. if ( empty( $args ) ) {
  34. return esc_html__( 'Please define default parameters in the form of an array.', 'radcliffe-2' );
  35. }
  36. // Define an icon.
  37. if ( false === array_key_exists( 'icon', $args ) ) {
  38. return esc_html__( 'Please define an SVG icon filename.', 'radcliffe-2' );
  39. }
  40. // Set defaults.
  41. $defaults = array(
  42. 'icon' => '',
  43. 'title' => '',
  44. 'desc' => '',
  45. );
  46. // Parse args.
  47. $args = wp_parse_args( $args, $defaults );
  48. // Set aria hidden.
  49. $aria_hidden = ' aria-hidden="true"';
  50. // Set ARIA.
  51. $aria_labelledby = '';
  52. /*
  53. * Radcliffe 2 doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
  54. *
  55. * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
  56. *
  57. * Example 1 with title: <?php echo radcliffe_2_get_svg( array( 'icon' => 'arrow-right', 'title' => esc_html__( 'This is the title', 'textdomain' ) ) ); ?>
  58. *
  59. * Example 2 with title and description: <?php echo radcliffe_2_get_svg( array( 'icon' => 'arrow-right', 'title' => esc_html__( 'This is the title', 'textdomain' ), 'desc' => esc_html__( 'This is the description', 'textdomain' ) ) ); ?>
  60. *
  61. * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
  62. */
  63. if ( $args['title'] ) {
  64. $aria_hidden = '';
  65. $unique_id = uniqid();
  66. $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
  67. if ( $args['desc'] ) {
  68. $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
  69. }
  70. }
  71. // Begin SVG markup.
  72. $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
  73. // Display the title.
  74. if ( $args['title'] ) {
  75. $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
  76. // Display the desc only if the title is already set.
  77. if ( $args['desc'] ) {
  78. $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
  79. }
  80. }
  81. /*
  82. * Display the icon.
  83. *
  84. * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
  85. *
  86. * See https://core.trac.wordpress.org/ticket/38387.
  87. */
  88. $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
  89. $svg .= '</svg>';
  90. return $svg;
  91. }
  92. /**
  93. * Add dropdown icon if menu item has children.
  94. *
  95. * @param string $title The menu item's title.
  96. * @param object $item The current menu item.
  97. * @param array $args An array of wp_nav_menu() arguments.
  98. * @param int $depth Depth of menu item. Used for padding.
  99. * @return string $title The menu item's title with dropdown icon.
  100. */
  101. function radcliffe_2_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
  102. if ( 'menu-1' === $args->theme_location ) {
  103. foreach ( $item->classes as $value ) {
  104. if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
  105. $title = $title . radcliffe_2_get_svg( array( 'icon' => 'expand' ) );
  106. }
  107. }
  108. }
  109. return $title;
  110. }
  111. add_filter( 'nav_menu_item_title', 'radcliffe_2_dropdown_icon_to_menu_link', 10, 4 );