Skip to content

Commit

Permalink
chore: small clean up (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jul 30, 2023
2 parents 5ecc337 + 447e830 commit 4882be8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Directives/Acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
| ACF Directives
|--------------------------------------------------------------------------
|
| Directives specific to Advance Custom Fields.
| Directives specific to Advanced Custom Fields.
|
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Log1x\SageDirectives\Directives;

use Log1x\SageDirectives\Util;
use Illuminate\Support\Str;
use Log1x\SageDirectives\Util;

return [

Expand Down
21 changes: 4 additions & 17 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -164,18 +162,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
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@

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

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

Expand Down
22 changes: 19 additions & 3 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,40 @@

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

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(); ?>");
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(); ?>');
});

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

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

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(); ?>");
});

it('compiles correctly with post object', function () {
$directive = '@posts(get_post(1))';

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

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(); ?>");
});

it('compiles correctly with post ID array', function () {
$directive = "@posts([1, 2, 3])";
$directive = '@posts([1, 2, 3])';

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

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(); ?>");
});

it('compiles correctly with post ID array containing object', function () {
$directive = '@posts([1, get_post(2), 3])';

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

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(); ?>");
});
});

describe('@endposts', function () {
Expand Down

0 comments on commit 4882be8

Please sign in to comment.