Merge pull request #1427 from roots/move-walker-and-remove-bootstrap-header
Remove nav walker and Bootstrap navbar
This commit is contained in:
@@ -17,7 +17,6 @@ $sage_includes = [
|
||||
'lib/config.php', // Configuration
|
||||
'lib/assets.php', // Scripts and stylesheets
|
||||
'lib/titles.php', // Page titles
|
||||
'lib/nav.php', // Custom nav modifications
|
||||
'lib/extras.php', // Custom functions
|
||||
];
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use Roots\Sage\ConditionalTagCheck;
|
||||
* Enable theme features
|
||||
*/
|
||||
add_theme_support('soil-clean-up'); // Enable clean up from Soil
|
||||
add_theme_support('soil-nav-walker'); // Enable cleaner nav walker from Soil
|
||||
add_theme_support('soil-relative-urls'); // Enable relative URLs from Soil
|
||||
add_theme_support('soil-nice-search'); // Enable nice search from Soil
|
||||
add_theme_support('soil-jquery-cdn'); // Enable to load jQuery from the Google CDN
|
||||
|
||||
122
lib/nav.php
122
lib/nav.php
@@ -1,122 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Roots\Sage\Nav;
|
||||
|
||||
use Roots\Sage\Utils;
|
||||
|
||||
/**
|
||||
* Cleaner walker for wp_nav_menu()
|
||||
*
|
||||
* Walker_Nav_Menu (WordPress default) example output:
|
||||
* <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="/">Home</a></li>
|
||||
* <li id="menu-item-9" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9"><a href="/sample-page/">Sample Page</a></l
|
||||
*
|
||||
* NavWalker example output:
|
||||
* <li class="menu-home"><a href="/">Home</a></li>
|
||||
* <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li>
|
||||
*/
|
||||
class NavWalker extends \Walker_Nav_Menu {
|
||||
private $cpt; // Boolean, is current post a custom post type
|
||||
private $archive; // Stores the archive page for current URL
|
||||
|
||||
public function __construct() {
|
||||
add_filter('nav_menu_css_class', array($this, 'cssClasses'), 10, 2);
|
||||
add_filter('nav_menu_item_id', '__return_null');
|
||||
$cpt = get_post_type();
|
||||
$this->cpt = in_array($cpt, get_post_types(array('_builtin' => false)));
|
||||
$this->archive = get_post_type_archive_link($cpt);
|
||||
}
|
||||
|
||||
public function checkCurrent($classes) {
|
||||
return preg_match('/(current[-_])|active|dropdown/', $classes);
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
function start_lvl(&$output, $depth = 0, $args = []) {
|
||||
$output .= "\n<ul class=\"dropdown-menu\">\n";
|
||||
}
|
||||
|
||||
function start_el(&$output, $item, $depth = 0, $args = [], $id = 0) {
|
||||
$item_html = '';
|
||||
parent::start_el($item_html, $item, $depth, $args);
|
||||
|
||||
if ($item->is_dropdown && ($depth === 0)) {
|
||||
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
|
||||
$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
|
||||
} elseif (stristr($item_html, 'li class="divider')) {
|
||||
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
|
||||
} elseif (stristr($item_html, 'li class="dropdown-header')) {
|
||||
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
|
||||
}
|
||||
|
||||
$item_html = apply_filters('sage/wp_nav_menu_item', $item_html);
|
||||
$output .= $item_html;
|
||||
}
|
||||
|
||||
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
|
||||
$element->is_dropdown = ((!empty($children_elements[$element->ID]) && (($depth + 1) < $max_depth || ($max_depth === 0))));
|
||||
|
||||
if ($element->is_dropdown) {
|
||||
$element->classes[] = 'dropdown';
|
||||
|
||||
foreach ($children_elements[$element->ID] as $child) {
|
||||
if ($child->current_item_parent || Utils\url_compare($this->archive, $child->url)) {
|
||||
$element->classes[] = 'active';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$element->is_active = strpos($this->archive, $element->url);
|
||||
|
||||
if ($element->is_active) {
|
||||
$element->classes[] = 'active';
|
||||
}
|
||||
|
||||
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
|
||||
}
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
public function cssClasses($classes, $item) {
|
||||
$slug = sanitize_title($item->title);
|
||||
|
||||
if ($this->cpt) {
|
||||
$classes = str_replace('current_page_parent', '', $classes);
|
||||
|
||||
if (Utils\url_compare($this->archive, $item->url)) {
|
||||
$classes[] = 'active';
|
||||
}
|
||||
}
|
||||
|
||||
$classes = preg_replace('/(current(-menu-|[-_]page[-_])(item|parent|ancestor))/', 'active', $classes);
|
||||
$classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
|
||||
|
||||
$classes[] = 'menu-' . $slug;
|
||||
|
||||
$classes = array_unique($classes);
|
||||
|
||||
return array_filter($classes, 'Roots\\Sage\\Utils\\is_element_empty');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up wp_nav_menu_args
|
||||
*
|
||||
* Remove the container
|
||||
* Remove the id="" on nav menu items
|
||||
*/
|
||||
function nav_menu_args($args = '') {
|
||||
$nav_menu_args = [];
|
||||
$nav_menu_args['container'] = false;
|
||||
|
||||
if (!$args['items_wrap']) {
|
||||
$nav_menu_args['items_wrap'] = '<ul class="%2$s">%3$s</ul>';
|
||||
}
|
||||
|
||||
if (!$args['depth']) {
|
||||
$nav_menu_args['depth'] = 2;
|
||||
}
|
||||
|
||||
return array_merge($args, $nav_menu_args);
|
||||
}
|
||||
add_filter('wp_nav_menu_args', __NAMESPACE__ . '\\nav_menu_args');
|
||||
add_filter('nav_menu_item_id', '__return_null');
|
||||
@@ -11,38 +11,3 @@ function get_search_form() {
|
||||
return $form;
|
||||
}
|
||||
add_filter('get_search_form', __NAMESPACE__ . '\\get_search_form');
|
||||
|
||||
/**
|
||||
* Make a URL relative
|
||||
*/
|
||||
function root_relative_url($input) {
|
||||
preg_match('|https?://([^/]+)(/.*)|i', $input, $matches);
|
||||
if (!isset($matches[1]) || !isset($matches[2])) {
|
||||
return $input;
|
||||
} elseif (($matches[1] === $_SERVER['SERVER_NAME']) || $matches[1] === $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) {
|
||||
return wp_make_link_relative($input);
|
||||
} else {
|
||||
return $input;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare URL against relative URL
|
||||
*/
|
||||
function url_compare($url, $rel) {
|
||||
$url = trailingslashit($url);
|
||||
$rel = trailingslashit($rel);
|
||||
if ((strcasecmp($url, $rel) === 0) || root_relative_url($url) == $rel) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if element is empty
|
||||
*/
|
||||
function is_element_empty($element) {
|
||||
$element = trim($element);
|
||||
return !empty($element);
|
||||
}
|
||||
|
||||
@@ -1,21 +1,10 @@
|
||||
<?php use Roots\Sage\Nav\NavWalker; ?>
|
||||
|
||||
<header class="banner navbar navbar-default navbar-static-top" role="banner">
|
||||
<header class="banner" role="banner">
|
||||
<div class="container">
|
||||
<div class="navbar-header">
|
||||
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
|
||||
<span class="sr-only"><?= __('Toggle navigation', 'sage'); ?></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="navbar-brand" href="<?= esc_url(home_url('/')); ?>"><?php bloginfo('name'); ?></a>
|
||||
</div>
|
||||
|
||||
<nav class="collapse navbar-collapse" role="navigation">
|
||||
<a class="brand" href="<?= esc_url(home_url('/')); ?>"><?php bloginfo('name'); ?></a>
|
||||
<nav role="navigation">
|
||||
<?php
|
||||
if (has_nav_menu('primary_navigation')) :
|
||||
wp_nav_menu(['theme_location' => 'primary_navigation', 'walker' => new NavWalker(), 'menu_class' => 'nav navbar-nav']);
|
||||
wp_nav_menu(['theme_location' => 'primary_navigation', 'menu_class' => 'nav']);
|
||||
endif;
|
||||
?>
|
||||
</nav>
|
||||
|
||||
Reference in New Issue
Block a user