123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * SVG icons related functions and filters
- *
- * @package Radcliffe 2
- */
- /**
- * Add SVG definitions to the footer.
- */
- function radcliffe_2_include_svg_icons() {
- // Define SVG sprite file.
- $svg_icons = get_parent_theme_file_path( '/assets/images/icons.svg' );
- // If it exists, include it.
- if ( file_exists( $svg_icons ) ) {
- require_once( $svg_icons );
- }
- }
- add_action( 'wp_footer', 'radcliffe_2_include_svg_icons', 9999 );
- /**
- * Return SVG markup.
- *
- * @param array $args {
- * Parameters needed to display an SVG.
- *
- * @type string $icon Required SVG icon filename.
- * @type string $title Optional SVG title.
- * @type string $desc Optional SVG description.
- * }
- * @return string SVG markup.
- */
- function radcliffe_2_get_svg( $args = array() ) {
- // Make sure $args are an array.
- if ( empty( $args ) ) {
- return esc_html__( 'Please define default parameters in the form of an array.', 'radcliffe-2' );
- }
- // Define an icon.
- if ( false === array_key_exists( 'icon', $args ) ) {
- return esc_html__( 'Please define an SVG icon filename.', 'radcliffe-2' );
- }
- // Set defaults.
- $defaults = array(
- 'icon' => '',
- 'title' => '',
- 'desc' => '',
- );
- // Parse args.
- $args = wp_parse_args( $args, $defaults );
- // Set aria hidden.
- $aria_hidden = ' aria-hidden="true"';
- // Set ARIA.
- $aria_labelledby = '';
- /*
- * Radcliffe 2 doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
- *
- * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
- *
- * Example 1 with title: <?php echo radcliffe_2_get_svg( array( 'icon' => 'arrow-right', 'title' => esc_html__( 'This is the title', 'textdomain' ) ) ); ?>
- *
- * Example 2 with title and description: <?php echo radcliffe_2_get_svg( array( 'icon' => 'arrow-right', 'title' => esc_html__( 'This is the title', 'textdomain' ), 'desc' => esc_html__( 'This is the description', 'textdomain' ) ) ); ?>
- *
- * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
- */
- if ( $args['title'] ) {
- $aria_hidden = '';
- $unique_id = uniqid();
- $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';
- if ( $args['desc'] ) {
- $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
- }
- }
- // Begin SVG markup.
- $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';
- // Display the title.
- if ( $args['title'] ) {
- $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';
- // Display the desc only if the title is already set.
- if ( $args['desc'] ) {
- $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
- }
- }
- /*
- * Display the icon.
- *
- * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
- *
- * See https://core.trac.wordpress.org/ticket/38387.
- */
- $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
- $svg .= '</svg>';
- return $svg;
- }
- /**
- * Add dropdown icon if menu item has children.
- *
- * @param string $title The menu item's title.
- * @param object $item The current menu item.
- * @param array $args An array of wp_nav_menu() arguments.
- * @param int $depth Depth of menu item. Used for padding.
- * @return string $title The menu item's title with dropdown icon.
- */
- function radcliffe_2_dropdown_icon_to_menu_link( $title, $item, $args, $depth ) {
- if ( 'menu-1' === $args->theme_location ) {
- foreach ( $item->classes as $value ) {
- if ( 'menu-item-has-children' === $value || 'page_item_has_children' === $value ) {
- $title = $title . radcliffe_2_get_svg( array( 'icon' => 'expand' ) );
- }
- }
- }
- return $title;
- }
- add_filter( 'nav_menu_item_title', 'radcliffe_2_dropdown_icon_to_menu_link', 10, 4 );
|