icon-functions.php 4.5 KB

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