Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  * Breaking Change: `build()` now returns a fluent instance instead of an array by default.
  * Add `activeAncestor`, `activeParent`, `classes`, `title`, `description`, `target`, and `xfn` values to the item objects. (#2)
  * Add `CHANGELOG.md`
  • Loading branch information
Log1x committed Sep 7, 2019
1 parent 64c1581 commit 0254500
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 28 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## v1.0.2 (09-07-2019)

### Enhancements

- **Breaking Change**: `build()` now returns a [fluent instance](https://laravel.com/api/master/Illuminate/Support/Fluent.html) instead of an array by default. To restore original functionality, simply append `->toArray()` at the end of your `build()`.
- Add `activeAncestor`, `activeParent`, `classes`, `title`, `description`, `target`, and `xfn` values to the item objects. (#2)
- Add `CHANGELOG.md`.

## v1.0.1 (07-04-2019)

### Enhancements

- Set `$item->children` to `[]` by default.

## v1.0.0 (06-20-2019)

Initial release
56 changes: 39 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ $ composer require log1x/navi

### Basic Usage

By default, Navi returns a [fluent container](https://laravel.com/api/master/Illuminate/Support/Fluent.html) containing your navigation items.

```php
use Log1x\Navi\Navi;

Expand Down Expand Up @@ -60,11 +62,9 @@ class Navigation extends Composer
/**
* Data to be passed to view before rendering.
*
* @param array $data
* @param \Illuminate\View\View $view
* @return array
*/
public function with($data, $view)
public function with()
{
return [
'navigation' => $this->navigation(),
Expand All @@ -74,11 +74,11 @@ class Navigation extends Composer
/**
* Returns the primary navigation.
*
* @return string
* @return array
*/
public function navigation()
{
return Navi::build('primary_navigation');
return Navi::build('primary_navigation')->toArray();
}
}
```
Expand Down Expand Up @@ -113,33 +113,55 @@ class Navigation extends Composer

## 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.
When calling `build()`, it will parse the passed navigation menu and return a fluent container containing your menu items. To return an array of objectd, you can call `->toArray()` on `build()`. By default, `build()` calls `primary_navigation` which is the default menu theme location on Sage.

```php
array [
24677 => {
5 => {
+"parent": false
+"id": 24677
+"id": 5
+"label": "Home"
+"slug": "home"
+"url": "/"
+"url": "https://sage.test/"
+"active": true
+"activeAncestor": false
+"activeParent": false
+"classes": "example"
+"title": ""
+"description": ""
+"target": "_blank"
+"xfn": ""
+"children": []
}
24678 => {
6 => {
+"parent": false
+"id": 24678
+"label": "Blog"
+"slug": "blog"
+"url": "#"
+"id": 6
+"label": "Sample Page"
+"slug": "sample-page"
+"url": "https://sage.test/sample-page/"
+"active": false
+"activeAncestor": false
+"activeParent": false
+"classes": ""
+"title": ""
+"description": ""
+"target": ""
+"xfn": ""
+"children": array [
24721 => {
+"parent": 24678
+"id": 24721
7 => {
+"parent": 6
+"id": 7
+"label": "Example"
+"slug": "example"
+"url": "#"
+"active": false
+"activeAncestor": false
+"activeParent": false
+"classes": ""
+"title": ""
+"description": ""
+"target": ""
+"xfn": ""
+"children": array [
...
]
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 39 additions & 9 deletions src/Navi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

namespace Log1x\Navi;

use Illuminate\Support\Str;
use Illuminate\Support\Fluent;

class Navi
{
/**
* Blacklisted Classes
*
* @var array
*/
protected $classes = [
'menu-item',
'page_item',
'page-item',
];

/**
* Parse the array of WP_Post objects returned by wp_get_nav_menu_items().
*
Expand All @@ -30,7 +44,8 @@ protected function parse($items)
'active' => $item->current,
'activeAncestor' => $item->current_item_ancestor,
'activeParent' => $item->current_item_parent,
'classes' => $item->classes,
'classes' => $this->filterClasses($item->classes),
'title' => $item->attr_title,
'description' => $item->description,
'target' => $item->target,
'xfn' => $item->xfn,
Expand All @@ -43,19 +58,32 @@ protected function parse($items)
* Returns the menu item's parent if it exists.
*
* @param WP_Post $item
* @return integer|boolean
* @return int|boolean
*/
protected function hasParent($item)
{
return $item->menu_item_parent != 0 ? $item->menu_item_parent : false;
}

/**
* Returns a filtered list of classes.
*
* @param array $classes
* @return string
*/
protected function filterClasses($classes)
{
return collect($classes)->filter(function ($class) {
return ! Str::contains($class, $this->classes);
})->implode(' ');
}

/**
* Build a multi-dimensional array containing children nav items.
*
* @param object $items
* @param integer $parent
* @param array $branch
* @param object $items
* @param int $parent
* @param array $branch
* @return array
*/
protected function tree($items, $parent = 0, $branch = [])
Expand All @@ -79,10 +107,10 @@ protected function tree($items, $parent = 0, $branch = [])
}

/**
* Build an object containing our navigation.
* Build a fluent instance containing our navigation.
*
* @param int|string|WP_Term $menu
* @return object
* @return Illuminate\Support\Fluent
*/
public function build($menu = 'primary_navigation')
{
Expand All @@ -94,8 +122,10 @@ public function build($menu = 'primary_navigation')
return;
}

return $this->parse(
wp_get_nav_menu_items($menu)
return new Fluent(
$this->parse(
wp_get_nav_menu_items($menu)
)
);
}
}

0 comments on commit 0254500

Please sign in to comment.