Nessuna descrizione

icon-functions.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * SVG icons related functions
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Nineteen
  7. * @since Twenty Nineteen 1.0
  8. */
  9. /**
  10. * Gets the SVG code for a given icon.
  11. */
  12. function twentynineteen_get_icon_svg( $icon, $size = 24 ) {
  13. return TwentyNineteen_SVG_Icons::get_svg( 'ui', $icon, $size );
  14. }
  15. /**
  16. * Gets the SVG code for a given social icon.
  17. */
  18. function twentynineteen_get_social_icon_svg( $icon, $size = 24 ) {
  19. return TwentyNineteen_SVG_Icons::get_svg( 'social', $icon, $size );
  20. }
  21. /**
  22. * Detects the social network from a URL and returns the SVG code for its icon.
  23. */
  24. function twentynineteen_get_social_link_svg( $uri, $size = 24 ) {
  25. return TwentyNineteen_SVG_Icons::get_social_link_svg( $uri, $size );
  26. }
  27. /**
  28. * Display SVG icons in social links menu.
  29. *
  30. * @param string $item_output The menu item's starting HTML output.
  31. * @param WP_Post $item Menu item data object.
  32. * @param int $depth Depth of the menu. Used for padding.
  33. * @param stdClass $args An object of wp_nav_menu() arguments.
  34. * @return string The menu item output with social icon.
  35. */
  36. function twentynineteen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
  37. // Change SVG icon inside social links menu if there is supported URL.
  38. if ( 'social' === $args->theme_location ) {
  39. $svg = twentynineteen_get_social_link_svg( $item->url, 26 );
  40. if ( empty( $svg ) ) {
  41. $svg = twentynineteen_get_icon_svg( 'link' );
  42. }
  43. $item_output = str_replace( $args->link_after, '</span>' . $svg, $item_output );
  44. }
  45. return $item_output;
  46. }
  47. add_filter( 'walker_nav_menu_start_el', 'twentynineteen_nav_menu_social_icons', 10, 4 );
  48. /**
  49. * Add a dropdown icon to top-level menu items.
  50. *
  51. * @param string $item_output The menu item's starting HTML output.
  52. * @param WP_Post $item Menu item data object.
  53. * @param int $depth Depth of the menu. Used for padding.
  54. * @param stdClass $args An object of wp_nav_menu() arguments.
  55. * @return string Nav menu item start element.
  56. */
  57. function twentynineteen_add_dropdown_icons( $item_output, $item, $depth, $args ) {
  58. // Only add class to 'top level' items on the 'primary' menu.
  59. if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  60. return $item_output;
  61. }
  62. if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
  63. // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
  64. // @todo Only do this for nested submenus? If on a first-level submenu, then really the link could be "#" since the desire is to remove the target entirely.
  65. $link = sprintf(
  66. '<button class="menu-item-link-return" tabindex="-1">%s',
  67. twentynineteen_get_icon_svg( 'chevron_left', 24 )
  68. );
  69. // Replace opening <a> with <button>.
  70. $item_output = preg_replace(
  71. '/<a\s.*?>/',
  72. $link,
  73. $item_output,
  74. 1 // Limit.
  75. );
  76. // Replace closing </a> with </button>.
  77. $item_output = preg_replace(
  78. '#</a>#i',
  79. '</button>',
  80. $item_output,
  81. 1 // Limit.
  82. );
  83. } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
  84. // Add SVG icon to parent items.
  85. $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
  86. $item_output .= sprintf(
  87. '<button class="submenu-expand" tabindex="-1">%s</button>',
  88. $icon
  89. );
  90. }
  91. return $item_output;
  92. }
  93. add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );