[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sun Mar 29 01:00:04 2020 Cross-referenced by PHPXref 0.7.1