Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(directives): Add support for customizing the @field format value (Fixes #43) #97

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading