|
| 1 | +<?php |
| 2 | +namespace BEA\Theme\Framework\Helpers\Formatting\Image; |
| 3 | + |
| 4 | +/** |
| 5 | + * @usage BEA\Theme\Framework\Helpers\Formatting\Image\get_the_image( 1, [ 'data-location' => 'image-size' ] ); |
| 6 | + * |
| 7 | + * @param int $image_id Attachment post ID |
| 8 | + * |
| 9 | + * @param array $attributes { |
| 10 | + * Attributes for the image markup. |
| 11 | + * |
| 12 | + * @type string $src Image attachment URL. |
| 13 | + * @type string $data-location Location of image. |
| 14 | + * @type string $class CSS class name or space-separated list of classes. |
| 15 | + * Default `attachment-$size_class size-$size_class`, |
| 16 | + * where `$size_class` is the image size being requested. |
| 17 | + * @type string $alt Image description for the alt attribute. |
| 18 | + * @type string $srcset The 'srcset' attribute value. |
| 19 | + * @type string $sizes The 'sizes' attribute value. |
| 20 | + * @type string|false $loading The 'loading' attribute value. Passing a value of false |
| 21 | + * will result in the attribute being omitted for the image. |
| 22 | + * Defaults to 'lazy', depending on wp_lazy_loading_enabled(). |
| 23 | + * } |
| 24 | + * |
| 25 | + * @param array $settings { |
| 26 | + * Optional. Settings for the image markup. |
| 27 | + * |
| 28 | + * @type string $size Optional. The 'sizes' attribute value. |
| 29 | + * @type string $before Optional. Markup to prepend to the image. Default empty. |
| 30 | + * @type string $after Optional. Markup to append to the image. Default empty. |
| 31 | + * |
| 32 | + * } |
| 33 | + * |
| 34 | + * @return string Return the markup of the image |
| 35 | + */ |
| 36 | +function get_the_image( int $image_id, array $attributes, array $settings = [] ): string { |
| 37 | + if ( $image_id <= 0 ) { |
| 38 | + return ''; |
| 39 | + } |
| 40 | + |
| 41 | + $attributes = wp_parse_args( |
| 42 | + $attributes, |
| 43 | + [ |
| 44 | + 'data-location' => '', |
| 45 | + 'class' => '', |
| 46 | + ] |
| 47 | + ); |
| 48 | + |
| 49 | + $attributes = apply_filters( 'bea_theme_framework_the_image_attributes', $attributes, $image_id, $settings ); |
| 50 | + |
| 51 | + $settings = wp_parse_args( |
| 52 | + $settings, |
| 53 | + [ |
| 54 | + 'size' => 'thumbnail', |
| 55 | + 'before' => '', |
| 56 | + 'after' => '', |
| 57 | + ] |
| 58 | + ); |
| 59 | + |
| 60 | + $settings = apply_filters( 'bea_theme_framework_the_image_settings', $settings, $image_id, $attributes ); |
| 61 | + $image_markup = \wp_get_attachment_image( |
| 62 | + $image_id, |
| 63 | + $settings['size'], |
| 64 | + false, |
| 65 | + $attributes |
| 66 | + ); |
| 67 | + |
| 68 | + if ( empty( $image_markup ) ) { |
| 69 | + return ''; |
| 70 | + } |
| 71 | + |
| 72 | + $image_markup = apply_filters( 'bea_theme_framework_the_image_markup', $image_markup, $image_id, $attributes, $settings ); |
| 73 | + |
| 74 | + return $settings['before'] . $image_markup . $settings['after']; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * @usage BEA\Theme\Framework\Helpers\Formatting\Image\the_image( 1, [ 'data-location' => 'image-size' ], ['before' => ''); |
| 79 | + * |
| 80 | + * @param int $image_id Attachment post ID |
| 81 | + * |
| 82 | + * @param array $attributes { |
| 83 | + * Attributes for the image markup. |
| 84 | + * |
| 85 | + * @type string $src Image attachment URL. |
| 86 | + * @type string $data-location Location of image. |
| 87 | + * @type string $class CSS class name or space-separated list of classes. |
| 88 | + * Default `attachment-$size_class size-$size_class`, |
| 89 | + * where `$size_class` is the image size being requested. |
| 90 | + * @type string $alt Image description for the alt attribute. |
| 91 | + * @type string $srcset The 'srcset' attribute value. |
| 92 | + * @type string $sizes The 'sizes' attribute value. |
| 93 | + * @type string|false $loading The 'loading' attribute value. Passing a value of false |
| 94 | + * will result in the attribute being omitted for the image. |
| 95 | + * Defaults to 'lazy', depending on wp_lazy_loading_enabled(). |
| 96 | + * } |
| 97 | + * |
| 98 | + * @param array $settings { |
| 99 | + * Optional. Settings for the image markup. |
| 100 | + * |
| 101 | + * @type string $size Optional. The 'sizes' attribute value. |
| 102 | + * @type string $before Optional. Markup to prepend to the image. Default empty. |
| 103 | + * @type string $after Optional. Markup to append to the image. Default empty. |
| 104 | + * |
| 105 | + * } |
| 106 | + * |
| 107 | + * @return void Echo of the image markup |
| 108 | + */ |
| 109 | +function the_image( int $image_id, array $attributes, array $settings = [] ): void { |
| 110 | + echo get_the_image( $image_id, $attributes, $settings ); |
| 111 | +} |
0 commit comments