Skip to content

Commit 5ecc337

Browse files
authored
fix(directive): Fix condition when passing multiple roles to @role (#98)
2 parents adf2dde + 0f4120b commit 5ecc337

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/Directives/WordPress.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Log1x\SageDirectives\Directives;
44

55
use Log1x\SageDirectives\Util;
6+
use Illuminate\Support\Str;
67

78
return [
89

@@ -482,12 +483,14 @@
482483
$condition = [];
483484

484485
foreach ($expression as $value) {
485-
$condition[] = "&& in_array(strtolower({$value}), (array) wp_get_current_user()->roles)";
486+
$condition[] = "in_array(strtolower({$value}), (array) wp_get_current_user()->roles) ||";
486487
}
487488

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

490-
return "<?php if (is_user_logged_in() {$conditions}) : ?>"; // phpcs:ignore
491+
$conditions = Str::beforeLast($conditions, ' ||');
492+
493+
return "<?php if (is_user_logged_in() && ({$conditions})) : ?>";
491494
},
492495

493496
'endrole' => function () {

tests/Unit/WordPressTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,15 +594,15 @@
594594

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

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

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

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

605-
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)) : ?>");
605+
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))) : ?>");
606606
});
607607
});
608608

0 commit comments

Comments
 (0)