Skip to content

Commit

Permalink
feature(image): allow passing raw as the second parameter to return…
Browse files Browse the repository at this point in the history
… an attachment URL (Fixes #24)

bugfix(set): allow the second parameter to accept a value containing commas (e.g. an array) (Fixes #28)
enhancement(utilities): add `limit` param to the `parse` method to allow passing a limit to `explode()` (#28)
docs(update): add `@image` example for `raw`
  • Loading branch information
Log1x committed Dec 2, 2019
1 parent 1410caa commit 98e37d6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
14 changes: 12 additions & 2 deletions docs/usage/wordpress.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ It accepts the same parameters as `@term`:

## @image

`@image` echo's an image using [`wp_get_attachment_image()`](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/).
`@image` echo's an image using [`wp_get_attachment_image()`](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/).

Since I find this mostly useful with ACF fields (being that it automatically handles responsive image sizes), if ACF is present and a field name in the form of a `string` is passed as the first parameter, `@image` will attempt to use the built in [`Util::field()`](https://github.com/Log1x/sage-directives/blob/master/src/Utilities.php#L48-L74) utility to deep-dive `get_field()` and `get_sub_field()` to retrieve your image field, and if it returns as an array instead of `id`, automatically check for the existance of `$image['id']` and pass that value to `wp_get_attachment_image()`.

Expand All @@ -375,7 +375,15 @@ Optionally, pass it an image size and an alt tag:
@image(1, 'full', 'My alt tag')
```

If you need access to the `<img>` tag attributes, use an array as the third parameter instead:
If you require an image without a set `width`, `height`, or `srcset`, our friends at WordPress core [don't agree](https://core.trac.wordpress.org/ticket/14110) with you and their word is law.

But since we do what we want, you can pass `raw` as an image size to return the attachment URL and build the image markup yourself.

```php
<img src="@image(1, 'raw')" alt="Take that, WordPress." />
```

Outside of a `raw` image, if you need access to the `<img>` tag attributes directly, use an array as the third parameter instead:

```php
@image(1, 'thumbnail', ['alt' => 'My alt tag', 'class' => 'block w-32 h-32'])
Expand All @@ -387,6 +395,8 @@ Accessing an ACF field, sub field, or even option field is just as easy:
@image('my_image_field')
@image('my_image_field', 'full', 'My alt tag')
@image('my_image_field', 'thumbnail', ['alt' => 'My alt tag', 'class' => 'block w-32 h-32'])

<img src="@image('my_image_field', 'raw')" alt="My alt tag" />
```

## @shortcode
Expand Down
2 changes: 1 addition & 1 deletion src/Directives/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

'set' => function ($expression) {
if (Str::contains($expression, ',')) {
$expression = Util::parse($expression);
$expression = Util::parse($expression, 2);

return "<?php {$expression->get(0)} = {$expression->get(1)}; ?>";
}
Expand Down
4 changes: 4 additions & 0 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@
$expression = $expression->put(0, is_array($image) && ! empty($image['id']) ? $image['id'] : $image);
}

if (Util::strip($expression->get(1)) == 'raw') {
return "<?php echo wp_get_attachment_url({$expression->get(0)}); ?>";
}

if (! empty($expression->get(3))) {
$expression = $expression->put(2, Util::clean($expression->slice(2)->all()));
}
Expand Down
7 changes: 4 additions & 3 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class Util
* Parse expression passed to directive.
*
* @param string $expression
* @param int $limit
* @return \Illuminate\Support\Collection
*/
public static function parse($expression)
public static function parse($expression, $limit = PHP_INT_MAX)
{
return collect(explode(',', $expression))
return collect(explode(',', $expression, $limit))
->map(function ($item) {
return trim($item);
});
Expand Down Expand Up @@ -82,7 +83,7 @@ public static function clean($expression)
* Dives for an ACF field or sub field and returns the value if it exists.
*
* @param string $field
* @param integer $id
* @param int $id
* @return mixed
*/
public static function field($field, $id = null)
Expand Down

0 comments on commit 98e37d6

Please sign in to comment.