[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentynineteen/inc/ -> icon-functions.php (source)

   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  /**
  11   * Gets the SVG code for a given icon.
  12   *
  13   * @param string $icon The specific icon to retrieve.
  14   * @param int    $size The desired width and height for the SVG icon.
  15   */
  16  function twentynineteen_get_icon_svg( $icon, $size = 24 ) {
  17      return TwentyNineteen_SVG_Icons::get_svg( 'ui', $icon, $size );
  18  }
  19  
  20  /**
  21   * Gets the SVG code for a given social icon.
  22   *
  23   * @param string $icon The specific icon to retrieve.
  24   * @param int    $size The desired width and height for the SVG icon.
  25   */
  26  function twentynineteen_get_social_icon_svg( $icon, $size = 24 ) {
  27      return TwentyNineteen_SVG_Icons::get_svg( 'social', $icon, $size );
  28  }
  29  
  30  /**
  31   * Detects the social network from a URL and returns the SVG code for its icon.
  32   *
  33   * @param string $uri  The URL of the social network link.
  34   * @param int    $size The desired width and height for the SVG icon.
  35   */
  36  function twentynineteen_get_social_link_svg( $uri, $size = 24 ) {
  37      return TwentyNineteen_SVG_Icons::get_social_link_svg( $uri, $size );
  38  }
  39  
  40  /**
  41   * Display SVG icons in social links menu.
  42   *
  43   * @param string   $item_output The menu item's starting HTML output.
  44   * @param WP_Post  $item        Menu item data object.
  45   * @param int      $depth       Depth of the menu. Used for padding.
  46   * @param stdClass $args        An object of wp_nav_menu() arguments.
  47   * @return string The menu item output with social icon.
  48   */
  49  function twentynineteen_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
  50      // Change SVG icon inside social links menu if there is supported URL.
  51      if ( 'social' === $args->theme_location ) {
  52          $svg = twentynineteen_get_social_link_svg( $item->url, 32 );
  53          if ( empty( $svg ) ) {
  54              $svg = twentynineteen_get_icon_svg( 'link' );
  55          }
  56          $item_output = str_replace( $args->link_after, '</span>' . $svg, $item_output );
  57      }
  58  
  59      return $item_output;
  60  }
  61  add_filter( 'walker_nav_menu_start_el', 'twentynineteen_nav_menu_social_icons', 10, 4 );
  62  
  63  /**
  64   * Add a dropdown icon to top-level menu items.
  65   *
  66   * @param string   $item_output The menu item's starting HTML output.
  67   * @param WP_Post  $item        Menu item data object.
  68   * @param int      $depth       Depth of the menu. Used for padding.
  69   * @param stdClass $args        An object of wp_nav_menu() arguments.
  70   * @return string Nav menu item start element.
  71   */
  72  function twentynineteen_add_dropdown_icons( $item_output, $item, $depth, $args ) {
  73  
  74      // Only add class to 'top level' items on the 'primary' menu.
  75      if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
  76          return $item_output;
  77      }
  78  
  79      if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
  80          // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
  81          // @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.
  82          $link = sprintf(
  83              '<button class="menu-item-link-return" tabindex="-1">%s',
  84              twentynineteen_get_icon_svg( 'chevron_left', 24 )
  85          );
  86  
  87          // Replace opening <a> with <button>.
  88          $item_output = preg_replace(
  89              '/<a\s.*?>/',
  90              $link,
  91              $item_output,
  92              1 // Limit.
  93          );
  94  
  95          // Replace closing </a> with </button>.
  96          $item_output = preg_replace(
  97              '#</a>#i',
  98              '</button>',
  99              $item_output,
 100              1 // Limit.
 101          );
 102  
 103      } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
 104  
 105          // Add SVG icon to parent items.
 106          $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
 107  
 108          $item_output .= sprintf(
 109              '<button class="submenu-expand" tabindex="-1">%s</button>',
 110              $icon
 111          );
 112      }
 113  
 114      return $item_output;
 115  }
 116  add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );


Generated : Thu Dec 26 08:20:01 2024 Cross-referenced by PHPXref