diff --git a/docs/usage/wordpress.md b/docs/usage/wordpress.md index fe7066f..69c63ab 100644 --- a/docs/usage/wordpress.md +++ b/docs/usage/wordpress.md @@ -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()`. @@ -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 `` 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 +Take that, WordPress. +``` + +Outside of a `raw` image, if you need access to the `` 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']) @@ -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']) + +My alt tag ``` ## @shortcode diff --git a/src/Directives/Helpers.php b/src/Directives/Helpers.php index a250f14..6e46358 100644 --- a/src/Directives/Helpers.php +++ b/src/Directives/Helpers.php @@ -169,7 +169,7 @@ 'set' => function ($expression) { if (Str::contains($expression, ',')) { - $expression = Util::parse($expression); + $expression = Util::parse($expression, 2); return "get(0)} = {$expression->get(1)}; ?>"; } diff --git a/src/Directives/WordPress.php b/src/Directives/WordPress.php index 78c528d..a860a62 100644 --- a/src/Directives/WordPress.php +++ b/src/Directives/WordPress.php @@ -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 "get(0)}); ?>"; + } + if (! empty($expression->get(3))) { $expression = $expression->put(2, Util::clean($expression->slice(2)->all())); } diff --git a/src/Utilities.php b/src/Utilities.php index d6a28cd..342c2d9 100644 --- a/src/Utilities.php +++ b/src/Utilities.php @@ -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); }); @@ -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)