Skip to content

Commit

Permalink
✨ Add @menu, @hasmenu and @endhasmenu WordPress directives (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwebca committed Jun 21, 2024
1 parent c6b5ed9 commit cb34494
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,24 @@ public function directives(): array

return "<?php echo get_theme_mod({$mod}); ?>";
},

/*
|---------------------------------------------------------------------
| @menu / @hasmenu / @endhasmenu
|---------------------------------------------------------------------
*/

'menu' => function ($expression) {
return "<?php wp_nav_menu($expression); ?>";
},

'hasmenu' => function ($expression) {
return "<?php if (has_nav_menu($expression)) : ?>";
},

'endhasmenu' => function () {
return '<?php endif; ?>';
},
];
}
}
30 changes: 30 additions & 0 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,33 @@
expect($compiled)->toBe("<?php echo get_theme_mod('mod', 'default'); ?>");
});
});

describe('@menu', function () {
it('compiles correctly', function () {
$directive = "@menu(['theme_location' => 'primary_navigation'])";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php wp_nav_menu(['theme_location' => 'primary_navigation']); ?>");
});
});

describe('@hasmenu', function () {
it('compiles correctly', function () {
$directive = "@hasmenu('primary_navigation')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php if (has_nav_menu('primary_navigation')) : ?>");
});
});

describe('@endhasmenu', function () {
it('compiles correctly', function () {
$directive = '@endhasmenu';

$compiled = $this->compile($directive);

expect($compiled)->toBe('<?php endif; ?>');
});
});

0 comments on commit cb34494

Please sign in to comment.