Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(directives): Add support for passing multiple roles to @role (Fixes #49) #89

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading