angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

icon-functions.php (3465B)


      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's starting HTML output.
     35  * @param WP_Post  $item        Menu item data object.
     36  * @param int      $depth       Depth of the menu. Used for padding.
     37  * @param stdClass $args        An object of wp_nav_menu() arguments.
     38  * @return string 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   $item_output The menu item's starting HTML output.
     58  * @param WP_Post  $item        Menu item data object.
     59  * @param int      $depth       Depth of the menu. Used for padding.
     60  * @param stdClass $args        An object of wp_nav_menu() arguments.
     61  * @return string Nav menu item start element.
     62  */
     63 function twentynineteen_add_dropdown_icons( $item_output, $item, $depth, $args ) {
     64 
     65 	// Only add class to 'top level' items on the 'primary' menu.
     66 	if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
     67 		return $item_output;
     68 	}
     69 
     70 	if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
     71 		// Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
     72 		// @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.
     73 		$link = sprintf(
     74 			'<button class="menu-item-link-return" tabindex="-1">%s',
     75 			twentynineteen_get_icon_svg( 'chevron_left', 24 )
     76 		);
     77 
     78 		// Replace opening <a> with <button>.
     79 		$item_output = preg_replace(
     80 			'/<a\s.*?>/',
     81 			$link,
     82 			$item_output,
     83 			1 // Limit.
     84 		);
     85 
     86 		// Replace closing </a> with </button>.
     87 		$item_output = preg_replace(
     88 			'#</a>#i',
     89 			'</button>',
     90 			$item_output,
     91 			1 // Limit.
     92 		);
     93 
     94 	} elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
     95 
     96 		// Add SVG icon to parent items.
     97 		$icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
     98 
     99 		$item_output .= sprintf(
    100 			'<button class="submenu-expand" tabindex="-1">%s</button>',
    101 			$icon
    102 		);
    103 	}
    104 
    105 	return $item_output;
    106 }
    107 add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );