Skip to content

Commit

Permalink
fix(directive): Fix @published and @modified directives with cust…
Browse files Browse the repository at this point in the history
…om date format (Fixes #57) (#88)
  • Loading branch information
Log1x committed Jul 29, 2023
2 parents 6cfbb31 + c063bbf commit dbbb0e7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/Directives/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,8 @@

'implode' => function ($expression) {
if (Str::contains($expression, ',')) {
$expression = str_replace(['\', \'', '\',\''], ['\'*\'', '\'* \''], $expression);
$expression = Util::parse($expression);

$expression->put(0, str_replace(['\'*\'', '\'* \''], ['\', \'', '\',\''], $expression->get(0)));

return "<?php echo implode({$expression->get(0)}, {$expression->get(1)}); ?>";
}
},
Expand Down
32 changes: 22 additions & 10 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,35 @@

'published' => function ($expression) {
if (! empty($expression)) {
return "<?php if (is_a({$expression}, 'WP_Post') || is_int({$expression})) : ?>".
"<?php echo get_the_date('', {$expression}); ?>".
'<?php else : ?>'.
"<?php echo get_the_date({$expression}); ?>".
'<?php endif; ?>';
$expression = Util::parse($expression);

if (Util::isIdentifier($expression->get(0))) {
return "<?php echo get_the_date('', {$expression->get(0)}); ?>";
}

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

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

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

'modified' => function ($expression) {
if (! empty($expression)) {
return "<?php if (is_a({$expression}, 'WP_Post') || is_numeric({$expression})) : ?>".
"<?php echo get_the_modified_date('', {$expression}); ?>".
'<?php else : ?>'.
"<?php echo get_the_modified_date({$expression}); ?>".
'<?php endif; ?>';
$expression = Util::parse($expression);

if (Util::isIdentifier($expression->get(0))) {
return "<?php echo get_the_modified_date('', {$expression->get(0)}); ?>";
}

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

return "<?php echo get_the_modified_date({$expression->get(0)}); ?>";
}

return '<?php echo get_the_modified_date(); ?>';
Expand Down
10 changes: 8 additions & 2 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ class Util
*
* @param string $expression
* @param int $limit
* @param string $delimiter
* @return \Illuminate\Support\Collection
*/
public static function parse($expression, $limit = PHP_INT_MAX)
public static function parse($expression, $limit = PHP_INT_MAX, $delimiter = '__comma__')
{
$expression = preg_replace_callback('/\'(.*?)\'|"(.*?)"/', function ($matches) use ($delimiter) {
return str_replace(',', $delimiter, $matches[0]);
}, $expression);

return collect(explode(',', $expression, $limit))
->map(function ($item) {
->map(function ($item) use ($delimiter) {
$item = str_replace($delimiter, ',', $item);
$item = trim($item);

if (is_numeric($item)) {
Expand Down
36 changes: 34 additions & 2 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,23 @@

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

expect($compiled)->toBe("<?php if (is_a(1, 'WP_Post') || is_int(1)) : ?><?php echo get_the_date('', 1); ?><?php else : ?><?php echo get_the_date(1); ?><?php endif; ?>");
expect($compiled)->toBe("<?php echo get_the_date('', 1); ?>");
});

it('compiles correctly with format', function () {
$directive = "@published('F j, Y')";

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

expect($compiled)->toBe("<?php echo get_the_date('F j, Y'); ?>");
});

it('compiles correctly with post and format', function () {
$directive = "@published('F j, Y', 1)";

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

expect($compiled)->toBe("<?php echo get_the_date('F j, Y', 1); ?>");
});
});

Expand All @@ -300,7 +316,23 @@

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

expect($compiled)->toBe("<?php if (is_a(1, 'WP_Post') || is_numeric(1)) : ?><?php echo get_the_modified_date('', 1); ?><?php else : ?><?php echo get_the_modified_date(1); ?><?php endif; ?>");
expect($compiled)->toBe("<?php echo get_the_modified_date('', 1); ?>");
});

it('compiles correctly with format', function () {
$directive = "@modified('F j, Y')";

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

expect($compiled)->toBe("<?php echo get_the_modified_date('F j, Y'); ?>");
});

it('compiles correctly with post and format', function () {
$directive = "@modified('F j, Y', 1)";

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

expect($compiled)->toBe("<?php echo get_the_modified_date('F j, Y', 1); ?>");
});
});

Expand Down

0 comments on commit dbbb0e7

Please sign in to comment.