From d8ea25510936359d2c2dd25564561190c1bf0fc2 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 29 Jul 2023 09:23:08 -0500 Subject: [PATCH 1/4] fix(directive): Fix `@published` and `@modified` directives with custom date format --- src/Directives/WordPress.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index dbea831..7d0a1de 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -229,11 +229,17 @@ 'published' => function ($expression) { if (! empty($expression)) { - return "". - "". - ''. - "". - ''; + $expression = Util::parse($expression); + + if (Util::isIdentifier($expression->get(0))) { + return "get(0)}); ?>"; + } + + if (! Util::isIdentifier($expression->get(0)) && empty($expression->get(1))) { + return "get(0)}); ?>"; + } + + return "get(0)}, {$expression->get(1)}); ?>"; } return ''; @@ -241,11 +247,17 @@ 'modified' => function ($expression) { if (! empty($expression)) { - return "". - "". - ''. - "". - ''; + $expression = Util::parse($expression); + + if (Util::isIdentifier($expression->get(0))) { + return "get(0)}); ?>"; + } + + if (Util::isIdentifier($expression->get(1))) { + return "get(0)}, {$expression->get(1)}); ?>"; + } + + return "get(0)}); ?>"; } return ''; From becf10176710a9080ed8b56bd9f82d4a01d3c2ac Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 29 Jul 2023 09:23:35 -0500 Subject: [PATCH 2/4] enhance(util): Improve the `parse` utility when dealing with delimiters --- src/Util.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Util.php b/src/Util.php index cff1f2a..f047919 100644 --- a/src/Util.php +++ b/src/Util.php @@ -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)) { From 78a95318b5734523ffa86e6910cbf772854b3d2d Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 29 Jul 2023 09:23:55 -0500 Subject: [PATCH 3/4] chore(test): Add tests for `@published` and `@modified` using custom date format --- tests/Unit/WordPressTest.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/Unit/WordPressTest.php b/tests/Unit/WordPressTest.php index f23cf0b..ab67eb4 100644 --- a/tests/Unit/WordPressTest.php +++ b/tests/Unit/WordPressTest.php @@ -282,7 +282,23 @@ $compiled = $this->compile($directive); - expect($compiled)->toBe(""); + expect($compiled)->toBe(""); + }); + + it('compiles correctly with format', function () { + $directive = "@published('F j, Y')"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe(""); + }); + + it('compiles correctly with post and format', function () { + $directive = "@published('F j, Y', 1)"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe(""); }); }); @@ -300,7 +316,23 @@ $compiled = $this->compile($directive); - expect($compiled)->toBe(""); + expect($compiled)->toBe(""); + }); + + it('compiles correctly with format', function () { + $directive = "@modified('F j, Y')"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe(""); + }); + + it('compiles correctly with post and format', function () { + $directive = "@modified('F j, Y', 1)"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe(""); }); }); From c063bbf7c008dad7733ca157192cc4c20b4f1e33 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 29 Jul 2023 09:24:18 -0500 Subject: [PATCH 4/4] chore(directive): Remove unnecessary `str_replace` on implode --- src/Directives/Helpers.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Directives/Helpers.php b/src/Directives/Helpers.php index df243ba..cc0e6b4 100644 --- a/src/Directives/Helpers.php +++ b/src/Directives/Helpers.php @@ -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 "get(0)}, {$expression->get(1)}); ?>"; } },