Skip to content

Commit cb34494

Browse files
authored
✨ Add @menu, @hasmenu and @endhasmenu WordPress directives (#147)
1 parent c6b5ed9 commit cb34494

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Directives/WordPress.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,24 @@ public function directives(): array
631631

632632
return "<?php echo get_theme_mod({$mod}); ?>";
633633
},
634+
635+
/*
636+
|---------------------------------------------------------------------
637+
| @menu / @hasmenu / @endhasmenu
638+
|---------------------------------------------------------------------
639+
*/
640+
641+
'menu' => function ($expression) {
642+
return "<?php wp_nav_menu($expression); ?>";
643+
},
644+
645+
'hasmenu' => function ($expression) {
646+
return "<?php if (has_nav_menu($expression)) : ?>";
647+
},
648+
649+
'endhasmenu' => function () {
650+
return '<?php endif; ?>';
651+
},
634652
];
635653
}
636654
}

tests/Unit/WordPressTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,33 @@
845845
expect($compiled)->toBe("<?php echo get_theme_mod('mod', 'default'); ?>");
846846
});
847847
});
848+
849+
describe('@menu', function () {
850+
it('compiles correctly', function () {
851+
$directive = "@menu(['theme_location' => 'primary_navigation'])";
852+
853+
$compiled = $this->compile($directive);
854+
855+
expect($compiled)->toBe("<?php wp_nav_menu(['theme_location' => 'primary_navigation']); ?>");
856+
});
857+
});
858+
859+
describe('@hasmenu', function () {
860+
it('compiles correctly', function () {
861+
$directive = "@hasmenu('primary_navigation')";
862+
863+
$compiled = $this->compile($directive);
864+
865+
expect($compiled)->toBe("<?php if (has_nav_menu('primary_navigation')) : ?>");
866+
});
867+
});
868+
869+
describe('@endhasmenu', function () {
870+
it('compiles correctly', function () {
871+
$directive = '@endhasmenu';
872+
873+
$compiled = $this->compile($directive);
874+
875+
expect($compiled)->toBe('<?php endif; ?>');
876+
});
877+
});

0 commit comments

Comments
 (0)