Skip to content

Commit

Permalink
enhance(directives): Add support for passing multiple roles to @role (
Browse files Browse the repository at this point in the history
Fixes #49) (#89)
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents dbbb0e7 + ed215c4 commit 0c9e9fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,20 @@ Accessing an ACF field, sub field, or even option field is just as easy:

## @role

`@role` is a simple conditional that allows you to display specific content only to users who are logged in and have a specific role. With [`wp_get_current_user()->roles`](https://developer.wordpress.org/reference/functions/wp_get_current_user/) returning an array of roles in all lowercase, the passed role is automatically lowercased using `strtolower`. It can be closed using `@endrole`.
`@role` allows you to display specific content only to users who are logged in and have a specific role. With [`wp_get_current_user()->roles`](https://developer.wordpress.org/reference/functions/wp_get_current_user/) returning an array of roles in all lowercase, the passed role is automatically lowercased using `strtolower`. It can be closed using `@endrole`.

```php
@role('author')
This content is only displayed to Authors.
@endrole
```

```php
@role('author', 'contributor')
This content is only displayed to Authors and Contributors.
@endrole
```

## @user

`@user` is a simple `is_user_logged_in()` conditional to display specific content only when a user is logged in. It can be closed using `@enduser`.
Expand Down
9 changes: 8 additions & 1 deletion src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,15 @@

'role' => function ($expression) {
$expression = Util::parse($expression);
$condition = [];

return "<?php if (is_user_logged_in() && in_array(strtolower({$expression->get(0)}), (array) wp_get_current_user()->roles)) : ?>"; // phpcs:ignore
foreach ($expression as $value) {
$condition[] = "&& in_array(strtolower({$value}), (array) wp_get_current_user()->roles)";
}

$conditions = implode(' ', $condition);

return "<?php if (is_user_logged_in() {$conditions}) : ?>"; // phpcs:ignore
},

'endrole' => function () {
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,14 @@

expect($compiled)->toBe("<?php if (is_user_logged_in() && in_array(strtolower('editor'), (array) wp_get_current_user()->roles)) : ?>");
});

it('compiles correctly with multiple roles', function () {
$directive = "@role('editor', 'author', 'contributor')";

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

expect($compiled)->toBe("<?php if (is_user_logged_in() && in_array(strtolower('editor'), (array) wp_get_current_user()->roles) && in_array(strtolower('author'), (array) wp_get_current_user()->roles) && in_array(strtolower('contributor'), (array) wp_get_current_user()->roles)) : ?>");
});
});

describe('@endrole', function () {
Expand Down

0 comments on commit 0c9e9fc

Please sign in to comment.