From 09b6c31ff830b72e0e2c97d6179b1c9cf93166fb Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:00:38 -0500 Subject: [PATCH 1/6] chore(docs): Fix typo on `@postclass` link --- docs/usage/wordpress.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/wordpress.md b/docs/usage/wordpress.md index 7771edd..2e2f57c 100644 --- a/docs/usage/wordpress.md +++ b/docs/usage/wordpress.md @@ -514,7 +514,7 @@ This is a directive version of [`wp_body_open`](https://developer.wordpress.org/ ## @postclass -`@postclass` functions the same as `[post_class](https://developer.wordpress.org/reference/functions/post_class/)` accepting an optional class and post ID. +`@postclass` functions the same as [`post_class`](https://developer.wordpress.org/reference/functions/post_class/) accepting an optional class and post ID. ```php @postclass From f5cdc12765c532c11fb72cad80b7dd3908656f90 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:06:00 -0500 Subject: [PATCH 2/6] chore(test): Add additional tests for `@posts` --- tests/Unit/WordPressTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Unit/WordPressTest.php b/tests/Unit/WordPressTest.php index 1fff330..6555d8b 100644 --- a/tests/Unit/WordPressTest.php +++ b/tests/Unit/WordPressTest.php @@ -27,6 +27,14 @@ expect($compiled)->toBe("put('p', is_a(1, 'WP_Post') ? (1)->ID : 1); ?>put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(1) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?>isNotEmpty() ? new WP_Query(\$posts->all()) : 1; ?>have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>"); }); + it('compiles correctly with post object', function () { + $directive = "@posts(get_post(1))"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe("put('p', is_a(get_post(1), 'WP_Post') ? (get_post(1))->ID : get_post(1)); ?>put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(get_post(1)) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?>isNotEmpty() ? new WP_Query(\$posts->all()) : get_post(1); ?>have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>"); + }); + it('compiles correctly with post ID array', function () { $directive = "@posts([1, 2, 3])"; @@ -34,6 +42,14 @@ expect($compiled)->toBe("put('p', is_a([1, 2, 3], 'WP_Post') ? ([1, 2, 3])->ID : [1, 2, 3]); ?>put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect([1, 2, 3]) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?>isNotEmpty() ? new WP_Query(\$posts->all()) : [1, 2, 3]; ?>have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>"); }); + + it('compiles correctly with post ID array containing object', function () { + $directive = "@posts([1, get_post(2), 3])"; + + $compiled = $this->compile($directive); + + expect($compiled)->toBe("put('p', is_a([1, get_post(2), 3], 'WP_Post') ? ([1, get_post(2), 3])->ID : [1, get_post(2), 3]); ?>put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect([1, get_post(2), 3]) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?>isNotEmpty() ? new WP_Query(\$posts->all()) : [1, get_post(2), 3]; ?>have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>"); + }); }); describe('@endposts', function () { From 5645c36cbdb1ee8aa888d9bc3b9f80806f1ac41a Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:07:14 -0500 Subject: [PATCH 3/6] chore(directives): Fix typo in ACF docblock --- src/Directives/Acf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Directives/Acf.php b/src/Directives/Acf.php index 868f3d9..5c1ee4c 100644 --- a/src/Directives/Acf.php +++ b/src/Directives/Acf.php @@ -12,7 +12,7 @@ | ACF Directives |-------------------------------------------------------------------------- | - | Directives specific to Advance Custom Fields. + | Directives specific to Advanced Custom Fields. | */ From 8612630ad25861401fcd6550883010000000bb11 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:08:12 -0500 Subject: [PATCH 4/6] chore(lint): Run Pint --- src/Directives/WordPress.php | 2 +- tests/Unit/HelpersTest.php | 2 +- tests/Unit/WordPressTest.php | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index d12ab47..4b91002 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -2,8 +2,8 @@ namespace Log1x\SageDirectives\Directives; -use Log1x\SageDirectives\Util; use Illuminate\Support\Str; +use Log1x\SageDirectives\Util; return [ diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index cbf6384..12c87b3 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -236,7 +236,7 @@ $compiled = $this->compile($directive); - expect($compiled)->toBe("addLoop(\$__currentLoopData); foreach(\$__currentLoopData as \$__i) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); ?>"); + expect($compiled)->toBe('addLoop($__currentLoopData); foreach($__currentLoopData as $__i) : $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>'); }); }); diff --git a/tests/Unit/WordPressTest.php b/tests/Unit/WordPressTest.php index 6555d8b..c22a6e8 100644 --- a/tests/Unit/WordPressTest.php +++ b/tests/Unit/WordPressTest.php @@ -16,11 +16,11 @@ $compiled = $this->compile($directive); - expect($compiled)->toBe(" have_posts()) : ?>post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>"); + expect($compiled)->toBe(' have_posts()) : ?>post_count); $__env->addLoop($__currentLoopData); while ($query->have_posts()) : $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $query->the_post(); ?>'); }); it('compiles correctly with post ID', function () { - $directive = "@posts(1)"; + $directive = '@posts(1)'; $compiled = $this->compile($directive); @@ -28,7 +28,7 @@ }); it('compiles correctly with post object', function () { - $directive = "@posts(get_post(1))"; + $directive = '@posts(get_post(1))'; $compiled = $this->compile($directive); @@ -36,7 +36,7 @@ }); it('compiles correctly with post ID array', function () { - $directive = "@posts([1, 2, 3])"; + $directive = '@posts([1, 2, 3])'; $compiled = $this->compile($directive); @@ -44,7 +44,7 @@ }); it('compiles correctly with post ID array containing object', function () { - $directive = "@posts([1, get_post(2), 3])"; + $directive = '@posts([1, get_post(2), 3])'; $compiled = $this->compile($directive); From d8c3536abcb256b232941984f5d3b0e0df4d2191 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:08:51 -0500 Subject: [PATCH 5/6] chore(util): Clean up docblocks --- src/Util.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Util.php b/src/Util.php index f047919..c197126 100644 --- a/src/Util.php +++ b/src/Util.php @@ -164,18 +164,7 @@ public static function toString($expression, $single = false) } /** - * A sad attempt to check if an expression passed is actually an array. - * Unfortunately, ANY expression passed to Blade is a string until it is - * returned and parsed through the compiler. Even attempting to manually - * convert the string to an array will then cause a string to array exception - * during compiled time– so regardless, it must then be converted back to a - * string. - * - * @see Utilities::toString() - * - * The only other way to approach this would be a clever `preg_match_all()` - * or `eval()` which isn't happening. I've poached every other Blade directives - * library and none have a viable solution. + * Determine if the expression looks like an array. * * @param mixed $expression * @return bool From 447e8301c81211fac78c57e71318970a4d4400c7 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 30 Jul 2023 01:09:51 -0500 Subject: [PATCH 6/6] chore(util): Do not use `else` --- src/Util.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Util.php b/src/Util.php index c197126..831500b 100644 --- a/src/Util.php +++ b/src/Util.php @@ -146,11 +146,9 @@ public static function toString($expression, $single = false) $keys = ''; foreach ($expression as $key => $value) { - if ($single) { - $keys .= self::wrap($value).','; - } else { - $keys .= self::wrap($key).' => '.self::wrap($value).', '; - } + $keys .= $single ? + self::wrap($value).',' : + self::wrap($key).' => '.self::wrap($value).', '; } $keys = trim(Str::replaceLast(',', '', $keys));