-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f51a6e0
Showing
7 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.php] | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Brandon Nifong | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# Navi | ||
|
||
Hate the WordPress NavWalker? Me too. | ||
|
||
Navi is a simple package that allows you to return a WordPress menu as an iterable object containing the necessities to build out your menu how you want. | ||
|
||
## Installation | ||
|
||
Install via Composer: | ||
|
||
```bash | ||
$ composer require log1x/navi | ||
``` | ||
|
||
## Usage | ||
|
||
### Basic Usage | ||
|
||
```php | ||
use Log1x\Navi\Navi; | ||
|
||
$navigation = (new Navi())->build('primary_navigation'); | ||
``` | ||
|
||
### Sage 10 | ||
|
||
When using Sage 10, you can take advantage of Navi's Service Provider and Facade to avoid needing to reinitialize the Navi class. | ||
|
||
```php | ||
# Composers/Navigation.php | ||
|
||
<?php | ||
|
||
namespace App\Composers; | ||
|
||
use Roots\Acorn\View\Composer; | ||
use Log1x\Navi\NaviFacade as Navi; | ||
|
||
class Navigation extends Composer | ||
{ | ||
/** | ||
* List of views served by this composer. | ||
* | ||
* @var array | ||
*/ | ||
protected static $views = [ | ||
'partials.navigation' | ||
]; | ||
|
||
/** | ||
* Data to be passed to view before rendering. | ||
* | ||
* @param array $data | ||
* @param \Illuminate\View\View $view | ||
* @return array | ||
*/ | ||
public function with($data, $view) | ||
{ | ||
return [ | ||
'navigation' => $this->navigation(), | ||
]; | ||
} | ||
|
||
/** | ||
* Returns the primary navigation. | ||
* | ||
* @return string | ||
*/ | ||
public function navigation() | ||
{ | ||
return Navi::build('primary_navigation'); | ||
} | ||
} | ||
``` | ||
|
||
```php | ||
# views/partials/navigation.blade.php | ||
|
||
@if ($navigation) | ||
<ul class="my-menu"> | ||
@foreach ($navigation as $item) | ||
<li class="my-menu-item {{ $item->active ? 'active' : '' }}"> | ||
<a href="{{ $item->url }}"> | ||
{{ $item->label }} | ||
</a> | ||
|
||
@if ($item->children) | ||
<ul class="my-child-menu"> | ||
@foreach ($item->children as $child) | ||
<li class="my-child-item {{ $child->active ? 'active' : '' }}"> | ||
<a href="{{ $child->url }}"> | ||
{{ $child->label }} | ||
</a> | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif | ||
``` | ||
|
||
## Example Output | ||
|
||
When calling `build()`, it will parse the passed navigation menu and return a simple, nested object of your menu items. By default, `build()` calls `primary_navigation` which is the default menu theme location on Sage. | ||
|
||
```php | ||
array [ | ||
24677 => { | ||
+"parent": false | ||
+"id": 24677 | ||
+"label": "Home" | ||
+"slug": "home" | ||
+"url": "/" | ||
+"active": true | ||
} | ||
24678 => { | ||
+"parent": false | ||
+"id": 24678 | ||
+"label": "Blog" | ||
+"slug": "blog" | ||
+"url": "#" | ||
+"active": false | ||
+"children": array [ | ||
24721 => { | ||
+"parent": 24678 | ||
+"id": 24721 | ||
+"label": "Example" | ||
+"slug": "example" | ||
+"url": "#" | ||
+"active": false | ||
+"children": array [ | ||
... | ||
] | ||
} | ||
] | ||
} | ||
] | ||
``` | ||
|
||
That being said, depending on how deep your menu is– you can ultimately just keep looping over `->children` indefinitely. | ||
|
||
## Bug Reports | ||
|
||
If you discover a bug in Navi, please [open an issue](https://github.com/log1x/navi/issues). | ||
|
||
## Contributing | ||
|
||
Contributing whether it be through PRs, reporting an issue, or suggesting an idea is encouraged and appreciated. | ||
|
||
## License | ||
|
||
Navi is provided under the [MIT License](https://github.com/log1x/navi/blob/master/LICENSE.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "log1x/navi", | ||
"type": "package", | ||
"license": "MIT", | ||
"description": "A simple alternative to WordPress' NavWalker", | ||
"authors": [ | ||
{ | ||
"name": "Brandon Nifong", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"keywords": [ | ||
"wordpress", | ||
"navwalker" | ||
], | ||
"support": { | ||
"issues": "https://github.com/log1x/navi/issues" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Log1x\\Navi\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=7.1" | ||
}, | ||
"extra": { | ||
"acorn": { | ||
"providers": [ | ||
"Log1x\\Navi\\NaviServiceProvider" | ||
], | ||
"aliases": { | ||
"Navi": "Log1x\\Navi\\NaviFacade" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Log1x\Navi; | ||
|
||
class Navi | ||
{ | ||
/** | ||
* Parse the array of WP_Post objects returned by wp_get_nav_menu_items(). | ||
* | ||
* @param array $items | ||
* @return object | ||
*/ | ||
protected function parse($items) | ||
{ | ||
if (! is_array($items)) { | ||
return; | ||
} | ||
|
||
_wp_menu_item_classes_by_context($items); | ||
|
||
return $this->tree( | ||
collect($items) | ||
->map(function ($item) { | ||
return (object) [ | ||
'parent' => $this->hasParent($item), | ||
'id' => $item->ID, | ||
'label' => $item->title, | ||
'slug' => $item->post_name, | ||
'url' => $item->url, | ||
'active' => $item->current | ||
]; | ||
}) | ||
); | ||
} | ||
|
||
/** | ||
* Returns the menu item's parent if it exists. | ||
* | ||
* @param WP_Post $item | ||
* @return integer|boolean | ||
*/ | ||
protected function hasParent($item) | ||
{ | ||
return $item->menu_item_parent != 0 ? $item->menu_item_parent : false; | ||
} | ||
|
||
/** | ||
* Build a multi-dimensional array containing children nav items. | ||
* | ||
* @param object $items | ||
* @param integer $parent | ||
* @param array $branch | ||
* @return array | ||
*/ | ||
protected function tree($items, $parent = 0, $branch = []) | ||
{ | ||
foreach ($items as $item) { | ||
if ($item->parent == $parent) { | ||
$children = $this->tree($items, $item->id); | ||
|
||
if (! empty($children)) { | ||
$item->children = $children; | ||
} | ||
|
||
$branch[$item->id] = $item; | ||
unset($item); | ||
} | ||
}; | ||
|
||
return $branch; | ||
} | ||
|
||
/** | ||
* Build an object containing our navigation. | ||
* | ||
* @param int|string|WP_Term $menu | ||
* @return object | ||
*/ | ||
public function build($menu = 'primary_navigation') | ||
{ | ||
if (is_string($menu)) { | ||
$menu = get_nav_menu_locations()[$menu] ?? []; | ||
} | ||
|
||
if (empty($menu)) { | ||
return; | ||
} | ||
|
||
return $this->parse( | ||
wp_get_nav_menu_items($menu) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Log1x\Navi; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class NaviFacade extends Facade | ||
{ | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'navi'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Log1x\Navi; | ||
|
||
use Roots\Acorn\ServiceProvider; | ||
|
||
class NaviServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->singleton('navi', function () { | ||
return new Navi; | ||
}); | ||
} | ||
} |