Skip to content

Commit

Permalink
enhance(directives): Add a @postmeta directive (Fixes #79) (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents 0c9e9fc + 3682429 commit 4e97354
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ If `@query` is not used and an argument is not passed to `@posts`, it will use t
@endnoposts
```

## @postmeta

`@postmeta` allows you to get the post meta for a post using [`get_post_meta`](https://developer.wordpress.org/reference/functions/get_post_meta/).

`@postmeta` will always expect a `key` as the first argument with an optional `post_id` for the second.

The third argument is optional and allows you to control the `$single` bool to only return a single value.

```php
@postmeta('key')
@postmeta('key', 1)
@postmeta(1)
@postmeta('key', 1, true)
```

## @title

`@title` echo's the current posts title using [`get_the_title()`](https://developer.wordpress.org/reference/functions/get_the_title/).
Expand Down
44 changes: 44 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,50 @@
return '<?php wp_reset_postdata(); endif; ?>';
},

/*
|---------------------------------------------------------------------
| @postmeta
|---------------------------------------------------------------------
*/

'postmeta' => function ($expression) {
if (! empty($expression)) {
$expression = Util::parse($expression);

if (Util::isIdentifier($expression->get(0))) {
if (empty($expression->get(1))) {
return "<?php echo get_post_meta({$expression->get(0)}); ?>";
}

if (empty($expression->get(2))) {
$expression->put(2, 'false');
}

return "<?php echo get_post_meta({$expression->get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>";
}

if (Util::isIdentifier($expression->get(1))) {
if (empty($expression->get(2))) {
$expression->put(2, 'false');
}

return "<?php echo get_post_meta({$expression->get(1)}, {$expression->get(0)}, {$expression->get(2)}); ?>";
}

if (empty($expression->get(1))) {
$expression->put(1, 'false');
}

if (! Util::isIdentifier($expression->get(0))) {
return "<?php echo get_post_meta(get_the_ID(), {$expression->get(0)}, {$expression->get(1)}); ?>";
}

return "<?php echo get_post_meta(get_the_ID(), {$expression->get(0)}, {$expression->get(1)}); ?>";
}

return '<?php echo get_post_meta(get_the_ID()); ?>';
},

/*
|---------------------------------------------------------------------
| @title / @content / @excerpt / @permalink / @thumbnail
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,40 @@
});
});

describe('@postmeta', function () {
it('compiles correctly', function () {
$directive = "@postmeta('foo')";

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

expect($compiled)->toBe("<?php echo get_post_meta(get_the_ID(), 'foo', false); ?>");
});

it('compiles correctly with object', function () {
$directive = "@postmeta('foo', \$post->ID)";

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

expect($compiled)->toBe("<?php echo get_post_meta(\$post->ID, 'foo', false); ?>");
});

it('compiles correctly with post', function () {
$directive = "@postmeta('foo', 1)";

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

expect($compiled)->toBe("<?php echo get_post_meta(1, 'foo', false); ?>");
});

it('compiles correctly with post and single', function () {
$directive = "@postmeta('foo', 1, true)";

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

expect($compiled)->toBe("<?php echo get_post_meta(1, 'foo', true); ?>");
});
});

describe('@title', function () {
it('compiles correctly', function () {
$directive = '@title';
Expand Down

0 comments on commit 4e97354

Please sign in to comment.