Skip to content

Commit cb544b1

Browse files
committed
refactor(menu): change MenuHooks class
Changes MenuHooks into MenuIcons. Extracts abstract MenuItemFilter class.
1 parent 0ccd7d6 commit cb544b1

File tree

4 files changed

+105
-57
lines changed

4 files changed

+105
-57
lines changed

config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
BlockSpacing\BlockSpacingHooks::class,
5454

5555
// Menu
56-
Menu\MenuHooks::class,
56+
Menu\MenuIcons::class,
5757
Menu\MenuRegistrar::class,
5858

5959
// Post Type

src/classes/Common/Menu/MenuHooks.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoWStarterTheme\Common\Menu;
6+
7+
use DoWStarterTheme\Common\Helpers\SVG;
8+
9+
/**
10+
* Adds icons support to menu items.
11+
*
12+
* @phpstan-import-type MenuItem from MenuItemFilter
13+
*/
14+
class MenuIcons extends MenuItemFilter
15+
{
16+
/**
17+
* Class constructor.
18+
*
19+
* @param SVG $svg SVG helper instance.
20+
*/
21+
public function __construct(
22+
private SVG $svg,
23+
) {
24+
}
25+
26+
/**
27+
* Adds icon to menu item.
28+
*
29+
* @param MenuItem $item Menu item.
30+
* @return MenuItem customized menu item with icon.
31+
*/
32+
public function filterItem($item) {
33+
$icon = get_field('icon', $item);
34+
35+
if (! is_numeric($icon)) {
36+
return $item;
37+
}
38+
39+
// @phpstan-ignore assign.propertyReadOnly
40+
$item->title = sprintf(
41+
'%s%s',
42+
$this->svg->getAttachment((int)$icon),
43+
$item->title
44+
);
45+
46+
return $item;
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoWStarterTheme\Common\Menu;
6+
7+
use DoWStarterTheme\Common\Contracts\Hookable;
8+
use DoWStarterTheme\Deps\Illuminate\Support\Collection;
9+
10+
/**
11+
* Handles url placeholders for auth links, like login, register, and logout.
12+
*
13+
* @phpstan-type MenuItem \WP_Post & object{
14+
* attr_title: string,
15+
* classes: array<string>,
16+
* db_id: int,
17+
* description: string,
18+
* menu_item_parent: int,
19+
* object: string,
20+
* object_id: int,
21+
* post_parent: int,
22+
* target: string,
23+
* title: string,
24+
* type: string,
25+
* type_label: string,
26+
* url: string,
27+
* xfn: string,
28+
* _invalid: string,
29+
* }
30+
*/
31+
abstract class MenuItemFilter implements Hookable
32+
{
33+
/**
34+
* Replaces URLs in menu items.
35+
*
36+
* @filter wp_nav_menu_objects
37+
*
38+
* @param array<MenuItem> $items Menu items.
39+
* @return array<MenuItem> customized menu items with icons.
40+
*/
41+
public function filter(array $items) {
42+
return Collection::make($items)
43+
->map(
44+
fn ($item) => $this->filterItem($item)
45+
)
46+
->all();
47+
}
48+
49+
/**
50+
* Filter a single menu item.
51+
*
52+
* @param MenuItem $item The menu item to filter.
53+
* @return MenuItem
54+
*/
55+
abstract public function filterItem($item);
56+
}

0 commit comments

Comments
 (0)