Skip to content

Commit

Permalink
enhance(directives): Add a @postclass directive (Fixes #31) (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents 4e97354 + 0e311d9 commit 56019a4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,13 @@ This is just a directive version of [`wp_footer`](https://developer.wordpress.or
## @wpbodyopen

This is a directive version of [`wp_body_open`](https://developer.wordpress.org/reference/functions/wp_body_open/) except that it will work on WordPress versions below 5.2.

## @postclass

`@postclass` functions the same as `[post_class](https://developer.wordpress.org/reference/functions/post_class/)` accepting an optional class and post ID.

```php
@postclass
@postclass('bg-white')
@postclass('bg-white', $post->ID)
```
10 changes: 10 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,16 @@
return "<?php if (function_exists('wp_body_open')) { wp_body_open(); } else { do_action('wp_body_open'); } ?>";
},

/*
|---------------------------------------------------------------------
| @postclass
|---------------------------------------------------------------------
*/

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

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

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

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

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

it('compiles correctly with a custom class', function () {
$directive = "@postclass('custom-class')";

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

expect($compiled)->toBe("<?php post_class('custom-class'); ?>");
});

it('compiles correctly with a custom class and post', function () {
$directive = "@postclass('custom-class', \$post->ID)";

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

expect($compiled)->toBe("<?php post_class('custom-class', \$post->ID); ?>");
});
});

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

0 comments on commit 56019a4

Please sign in to comment.