Skip to content

Commit

Permalink
Merge pull request #35 from aberkow/develop
Browse files Browse the repository at this point in the history
adding a class to list items  that contain outbound links
  • Loading branch information
aberkow committed Nov 16, 2018
2 parents 1c66105 + a7a2dfc commit 0713988
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/A11y/Menu_Walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ class Menu_Walker extends \Walker_Nav_Menu {
public function __construct() {
$this->parentItemCount = 0;
}

/**
* Check if the item has a link which is going outbound.
*
* @param [string] $url the URL of the item
* @return boolean
*/
private function is_outbound_link($url) {
// parse the url to get the host
$parsed = parse_url($url);
$host = $parsed['host'];

// is the item going to an outbound link?
return $host === $_SERVER['HTTP_HOST'] ? false : true;
}
public function start_lvl(&$output, $depth = 0, $args = array()) {
// the levels are only the interior <ul> tags. they don't include the exterior wrapping tag.

Expand All @@ -28,7 +43,6 @@ public function end_lvl(&$output, $depth = 0, $args = array()) {
$output .= "</ul>";
}
public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {

// prevent errors from being thrown if a menu hasn't been set yet.
if (!$item->db_id) {
return;
Expand All @@ -45,9 +59,15 @@ public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
if (!empty($item->classes)) {
$classes = (array)$item->classes;
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));

// a list of class names plus the html class attribute
$class_names = $class_names ? " class='no-js am-list-item " . esc_attr($class_names) . "'" : "";
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));

if (!$this->is_outbound_link($item->url)) {
$class_names = $class_names ? " class='no-js am-list-item " . esc_attr($class_names) . "'" : "";
} else {
$class_names = $class_names ? " class='no-js am-list-item am-outbound " . esc_attr($class_names) . "'" : "";
}

// check that items with children have links
// make sure that links with [http(s)://]# are rendered as buttons.
Expand Down

0 comments on commit 0713988

Please sign in to comment.