icon-functions.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * SVG icons related functions
  5. *
  6. * @package Seedlet
  7. * @since 1.0.0
  8. */
  9. /**
  10. * Gets the SVG code for a given icon.
  11. */
  12. function seedlet_get_icon_svg( $icon, $size = 24 ) {
  13. return Seedlet_SVG_Icons::get_svg( 'ui', $icon, $size );
  14. }
  15. /**
  16. * Gets the SVG code for a given social icon.
  17. */
  18. function seedlet_get_social_icon_svg( $icon, $size = 24 ) {
  19. return Seedlet_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 seedlet_get_social_link_svg( $uri, $size = 24 ) {
  25. return Seedlet_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 output.
  31. * @param WP_Post $item Menu item object.
  32. * @param int $depth Depth of the menu.
  33. * @param array $args wp_nav_menu() arguments.
  34. * @return string $item_output The menu item output with social icon.
  35. */
  36. function seedlet_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 = seedlet_get_social_link_svg( $item->url, 26 );
  40. if ( empty( $svg ) ) {
  41. $svg = seedlet_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', 'seedlet_nav_menu_social_icons', 10, 4 );