Skip to content

Commit 4882be8

Browse files
authored
chore: small clean up (#99)
2 parents 5ecc337 + 447e830 commit 4882be8

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

docs/usage/wordpress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ This is a directive version of [`wp_body_open`](https://developer.wordpress.org/
514514

515515
## @postclass
516516

517-
`@postclass` functions the same as `[post_class](https://developer.wordpress.org/reference/functions/post_class/)` accepting an optional class and post ID.
517+
`@postclass` functions the same as [`post_class`](https://developer.wordpress.org/reference/functions/post_class/) accepting an optional class and post ID.
518518

519519
```php
520520
@postclass

src/Directives/Acf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| ACF Directives
1313
|--------------------------------------------------------------------------
1414
|
15-
| Directives specific to Advance Custom Fields.
15+
| Directives specific to Advanced Custom Fields.
1616
|
1717
*/
1818

src/Directives/WordPress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Log1x\SageDirectives\Directives;
44

5-
use Log1x\SageDirectives\Util;
65
use Illuminate\Support\Str;
6+
use Log1x\SageDirectives\Util;
77

88
return [
99

src/Util.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ public static function toString($expression, $single = false)
146146
$keys = '';
147147

148148
foreach ($expression as $key => $value) {
149-
if ($single) {
150-
$keys .= self::wrap($value).',';
151-
} else {
152-
$keys .= self::wrap($key).' => '.self::wrap($value).', ';
153-
}
149+
$keys .= $single ?
150+
self::wrap($value).',' :
151+
self::wrap($key).' => '.self::wrap($value).', ';
154152
}
155153

156154
$keys = trim(Str::replaceLast(',', '', $keys));
@@ -164,18 +162,7 @@ public static function toString($expression, $single = false)
164162
}
165163

166164
/**
167-
* A sad attempt to check if an expression passed is actually an array.
168-
* Unfortunately, ANY expression passed to Blade is a string until it is
169-
* returned and parsed through the compiler. Even attempting to manually
170-
* convert the string to an array will then cause a string to array exception
171-
* during compiled time– so regardless, it must then be converted back to a
172-
* string.
173-
*
174-
* @see Utilities::toString()
175-
*
176-
* The only other way to approach this would be a clever `preg_match_all()`
177-
* or `eval()` which isn't happening. I've poached every other Blade directives
178-
* library and none have a viable solution.
165+
* Determine if the expression looks like an array.
179166
*
180167
* @param mixed $expression
181168
* @return bool

tests/Unit/HelpersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236

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

239-
expect($compiled)->toBe("<?php \$__currentLoopData = range(1, 5); \$__env->addLoop(\$__currentLoopData); foreach(\$__currentLoopData as \$__i) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); ?>");
239+
expect($compiled)->toBe('<?php $__currentLoopData = range(1, 5); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $__i) : $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>');
240240
});
241241
});
242242

tests/Unit/WordPressTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,40 @@
1616

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

19-
expect($compiled)->toBe("<?php if (empty(\$query)) : ?><?php global \$wp_query; ?><?php \$query = \$wp_query; ?><?php endif; ?> <?php if (\$query->have_posts()) : ?><?php \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
19+
expect($compiled)->toBe('<?php if (empty($query)) : ?><?php global $wp_query; ?><?php $query = $wp_query; ?><?php endif; ?> <?php if ($query->have_posts()) : ?><?php $__currentLoopData = range(1, $query->post_count); $__env->addLoop($__currentLoopData); while ($query->have_posts()) : $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $query->the_post(); ?>');
2020
});
2121

2222
it('compiles correctly with post ID', function () {
23-
$directive = "@posts(1)";
23+
$directive = '@posts(1)';
2424

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

2727
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(1, 'WP_Post') || is_numeric(1)) : ?><?php \$posts->put('p', is_a(1, 'WP_Post') ? (1)->ID : 1); ?><?php endif; ?><?php if (is_array(1)) : ?><?php \$posts ->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'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : 1; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
2828
});
2929

30+
it('compiles correctly with post object', function () {
31+
$directive = '@posts(get_post(1))';
32+
33+
$compiled = $this->compile($directive);
34+
35+
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(get_post(1), 'WP_Post') || is_numeric(get_post(1))) : ?><?php \$posts->put('p', is_a(get_post(1), 'WP_Post') ? (get_post(1))->ID : get_post(1)); ?><?php endif; ?><?php if (is_array(get_post(1))) : ?><?php \$posts ->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'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : get_post(1); ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
36+
});
37+
3038
it('compiles correctly with post ID array', function () {
31-
$directive = "@posts([1, 2, 3])";
39+
$directive = '@posts([1, 2, 3])';
3240

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

3543
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, 2, 3], 'WP_Post') || is_numeric([1, 2, 3])) : ?><?php \$posts->put('p', is_a([1, 2, 3], 'WP_Post') ? ([1, 2, 3])->ID : [1, 2, 3]); ?><?php endif; ?><?php if (is_array([1, 2, 3])) : ?><?php \$posts ->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'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, 2, 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
3644
});
45+
46+
it('compiles correctly with post ID array containing object', function () {
47+
$directive = '@posts([1, get_post(2), 3])';
48+
49+
$compiled = $this->compile($directive);
50+
51+
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, get_post(2), 3], 'WP_Post') || is_numeric([1, get_post(2), 3])) : ?><?php \$posts->put('p', is_a([1, get_post(2), 3], 'WP_Post') ? ([1, get_post(2), 3])->ID : [1, get_post(2), 3]); ?><?php endif; ?><?php if (is_array([1, get_post(2), 3])) : ?><?php \$posts ->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'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, get_post(2), 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
52+
});
3753
});
3854

3955
describe('@endposts', function () {

0 commit comments

Comments
 (0)