Skip to content

Commit

Permalink
enhance(directives): Add support for customizing the @field format va…
Browse files Browse the repository at this point in the history
…lue (Fixes #43) (#97)
  • Loading branch information
Log1x authored Jul 29, 2023
2 parents 3e15669 + 6efe9b7 commit adf2dde
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/Directives/Acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,26 @@
$expression = Util::parse($expression);

if (Util::isIdentifier($expression->get(2))) {
return "<?php echo get_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]; ?>";
if (empty($expression->get(3))) {
$expression->put(3, 'true');
}

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

if (Util::isIdentifier($expression->get(1))) {
return "<?php echo get_field({$expression->get(0)}, {$expression->get(1)}); ?>";
if (empty($expression->get(2))) {
$expression->put(2, 'true');
}

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

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

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

return "<?php echo get_field({$expression}); ?>";
Expand Down
42 changes: 37 additions & 5 deletions tests/Unit/AcfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,71 @@

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

expect($compiled)->toEqual("<?php echo get_field('item')['key']; ?>");
expect($compiled)->toEqual("<?php echo get_field('item', null, true)['key']; ?>");
});

it('compiles correctly with post ID', function () {
$directive = "@field('item', 1)";

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

expect($compiled)->toEqual("<?php echo get_field('item', 1); ?>");
expect($compiled)->toEqual("<?php echo get_field('item', 1, true); ?>");
});

it('compiles correctly with key and post ID', function () {
$directive = "@field('item', 'key', 1)";

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

expect($compiled)->toEqual("<?php echo get_field('item', 1)['key']; ?>");
expect($compiled)->toEqual("<?php echo get_field('item', 1, true)['key']; ?>");
});

it('compiles correctly with post ID and formatting', function () {
$directive = "@field('item', 1, false)";

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

expect($compiled)->toEqual("<?php echo get_field('item', 1, false); ?>");
});

it('compiles correctly with key, post ID and formatting', function () {
$directive = "@field('item', 'key', 1, false)";

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

expect($compiled)->toEqual("<?php echo get_field('item', 1, false)['key']; ?>");
});

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

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

expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID); ?>");
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, true); ?>");
});

it('compiles correctly with key and post object', function () {
$directive = "@field('item', 'key', \$post->ID)";

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

expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID)['key']; ?>");
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, true)['key']; ?>");
});

it('compiles correctly with post object and formatting', function () {
$directive = "@field('item', \$post->ID, false)";

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

expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, false); ?>");
});

it('compiles correctly with key, post object and formatting', function () {
$directive = "@field('item', 'key', \$post->ID, false)";

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

expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, false)['key']; ?>");
});
});

Expand Down

0 comments on commit adf2dde

Please sign in to comment.