icon-functions.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. $unique_id = null;
  31. // Make sure $args are an array.
  32. if ( empty( $args ) ) {
  33. return __( 'Please define default parameters in the form of an array.', 'photos' );
  34. }
  35. // Define an icon.
  36. if ( false === array_key_exists( 'icon', $args ) ) {
  37. return __( 'Please define an SVG icon filename.', 'photos' );
  38. }
  39. // Set defaults.
  40. $defaults = array(
  41. 'icon' => '',
  42. 'title' => '',
  43. 'desc' => '',
  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. // Set ARIA.
  51. $aria_labelledby = '';
  52. /*
  53. * Photos 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 photos_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ) ) ); ?>
  58. *
  59. * 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' ) ) ); ?>
  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. // Add some markup to use as a fallback for browsers that do not support SVGs.
  90. if ( $args['fallback'] ) {
  91. $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
  92. }
  93. $svg .= '</svg>';
  94. return $svg;
  95. }
  96. /**
  97. * Add dropdown icon if menu item has children.
  98. *
  99. * @param string $title The menu item's title.
  100. * @param object $item The current menu item.
  101. * @param array $args An array of wp_nav_menu() arguments.
  102. * @param int $depth Depth of menu item. Used for padding.
  103. * @return string $title The menu item's title with dropdown icon.
  104. */
  105. function photos_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
  106. if ( 'menu-1' === $args->theme_location ) {
  107. foreach ( $item->classes as $value ) {
  108. if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
  109. $title = $title . photos_get_svg( array( 'icon' => 'expand' ) );
  110. }
  111. }
  112. }
  113. return $title;
  114. }
  115. add_filter( 'nav_menu_item_title', 'photos_dropdown_icon_to_menu_link', 10, 4 );
  116. /**
  117. * Add an icon above the post thumbnail image (when appropriate)
  118. */
  119. function photos_post_icon() {
  120. $icon = '';
  121. if ( is_sticky() ) {
  122. $icon = 'pinned';
  123. $title = esc_html__( 'Featured', 'photos' );
  124. } elseif ( post_password_required() ) {
  125. $icon = 'lock';
  126. $title = esc_html__( 'Password Protected', 'photos' );
  127. }
  128. if ( empty( $icon ) ) return;
  129. $args = array();
  130. $args['icon'] = $icon;
  131. $args['title'] = $title;
  132. return photos_get_svg( $args );
  133. }