From 0254500ba39568939c6c58e1626c95d76ff0bc6a Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 7 Sep 2019 06:24:15 -0500 Subject: [PATCH] v1.0.2 * 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` --- CHANGELOG.md | 17 ++++++++++++++++ README.md | 56 +++++++++++++++++++++++++++++++++++---------------- composer.lock | 4 ++-- src/Navi.php | 48 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..58be459 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ed92cbc..0239268 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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(), @@ -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(); } } ``` @@ -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 [ ... ] diff --git a/composer.lock b/composer.lock index a5e38c9..8b3a8fb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9783165478148d3b9ef4b6128cf28ef5", + "content-hash": "d61dc261310a09c7af6439b7ce514644", "packages": [], "packages-dev": [ { @@ -65,7 +65,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.1" + "php": ">=7.1.3" }, "platform-dev": [] } diff --git a/src/Navi.php b/src/Navi.php index 006ee98..398f039 100644 --- a/src/Navi.php +++ b/src/Navi.php @@ -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(). * @@ -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, @@ -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 = []) @@ -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') { @@ -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) + ) ); } }