Skip to content

Commit

Permalink
enhance(directives): Add a @sidebar, @hassidebar, and `@endhassid…
Browse files Browse the repository at this point in the history
…ebar` directive (Fixes #71) (#92)
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents 56019a4 + 5df3ba5 commit bfc059c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,17 @@ This is a directive version of [`wp_body_open`](https://developer.wordpress.org/
@postclass('bg-white')
@postclass('bg-white', $post->ID)
```

## @sidebar

`@sidebar` is a simply directive that calls [`dynamic_sidebar`](https://developer.wordpress.org/reference/functions/dynamic_sidebar/).

It comes with two assisting directives `@hassidebar` and `@endhassidebar` that checks for the existence of the sidebar using [`is_active_sidebar`](https://developer.wordpress.org/reference/functions/is_active_sidebar/).

```php
@sidebar('sidebar-primary')

@hassidebar('sidebar-primary')
@sidebar('sidebar-primary')
@endhassidebar
```
18 changes: 18 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,24 @@
return "<?php post_class({$expression}); ?>";
},

/*
|---------------------------------------------------------------------
| @sidebar / @hassidebar / @endhassidebar
|---------------------------------------------------------------------
*/

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

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

'endhassidebar' => function () {
return '<?php endif; ?>';
},

/*
|---------------------------------------------------------------------
| @__
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,36 @@
});
});

describe('@sidebar', function () {
it('compiles correctly', function () {
$directive = "@sidebar('sidebar-1')";

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

expect($compiled)->toBe("<?php dynamic_sidebar('sidebar-1'); ?>");
});
});

describe('@hassidebar', function () {
it('compiles correctly', function () {
$directive = "@hassidebar('sidebar-1')";

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

expect($compiled)->toBe("<?php if (is_active_sidebar('sidebar-1')) : ?>");
});
});

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

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

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

describe('@__', function () {
it('compiles correctly', function () {
$directive = "@__('Hello World', 'sage')";
Expand Down

0 comments on commit bfc059c

Please sign in to comment.