| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Image_Transformer extends Transformer_Base { |
| 13 |
public function transform( $value, Props_Resolver_Context $context ) { |
| 14 |
if ( ! empty( $value['src']['id'] ) ) { |
| 15 |
$image_src = wp_get_attachment_image_src( |
| 16 |
(int) $value['src']['id'], |
| 17 |
$value['size'] ?? 'full' |
| 18 |
); |
| 19 |
|
| 20 |
if ( ! $image_src ) { |
| 21 |
throw new \Exception( 'Cannot get image src.' ); |
| 22 |
} |
| 23 |
|
| 24 |
[ $src, $width, $height ] = $image_src; |
| 25 |
|
| 26 |
return [ |
| 27 |
'src' => $src, |
| 28 |
'width' => (int) $width, |
| 29 |
'height' => (int) $height, |
| 30 |
'srcset' => wp_get_attachment_image_srcset( $value['src']['id'], $value['size'] ), |
| 31 |
'alt' => get_post_meta( $value['src']['id'], '_wp_attachment_image_alt', true ), |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
if ( empty( $value['src']['url'] ) ) { |
| 36 |
throw new \Exception( 'Invalid image URL.' ); |
| 37 |
} |
| 38 |
|
| 39 |
return [ |
| 40 |
'src' => $value['src']['url'], |
| 41 |
]; |
| 42 |
} |
| 43 |
} |
| 44 |
|