Make WordPress Core


Ignore:
Timestamp:
02/08/2020 12:59:44 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Twenty Nineteen: Improve code organization in template-functions.php by moving helper functions into their own file.

These functions are moved to inc/helper-functions.php:

  • twentynineteen_can_show_post_thumbnail()
  • twentynineteen_image_filters_enabled()
  • twentynineteen_get_avatar_size()
  • twentynineteen_is_comment_by_post_author()
  • twentynineteen_get_discussion_data()
  • twentynineteen_hsl_hex()

Additionally, twentynineteen_add_dropdown_icons() is moved to inc/icon-functions.php to join twentynineteen_nav_menu_social_icons().

Props akshayar, allancole, grapplerulrich.
Fixes #45984.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentynineteen/inc/icon-functions.php

    r46827 r47214  
    5151}
    5252add_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 */
     64function 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}
     108add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );
Note: See TracChangeset for help on using the changeset viewer.