| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Svg\Svg_Sanitizer; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Svg_Src_Transformer extends Transformer_Base { |
| 15 |
const SVG_INLINE_STYLES = 'width: 100%; height: 100%; overflow: unset;'; |
| 16 |
|
| 17 |
public function transform( $value, Props_Resolver_Context $context ) { |
| 18 |
$id = isset( $value['id'] ) ? (int) $value['id'] : null; |
| 19 |
$url = $value['url'] ?? null; |
| 20 |
|
| 21 |
if ( $id ) { |
| 22 |
$resolved_url = wp_get_attachment_url( $id ); |
| 23 |
|
| 24 |
if ( $resolved_url ) { |
| 25 |
$url = $resolved_url; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
$svg_content = $this->fetch_svg_content( $id, $url ); |
| 30 |
$html = $svg_content ? $this->process_svg( $svg_content ) : ''; |
| 31 |
|
| 32 |
return [ |
| 33 |
'html' => $html, |
| 34 |
'url' => $url, |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
private function fetch_svg_content( ?int $id, ?string $url ): ?string { |
| 39 |
if ( $id ) { |
| 40 |
$path = get_attached_file( $id ); |
| 41 |
$content = $path ? Utils::file_get_contents( $path ) : null; |
| 42 |
|
| 43 |
if ( $content ) { |
| 44 |
return $content; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! $url ) { |
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
$local_path = $this->resolve_local_path( $url ); |
| 53 |
|
| 54 |
if ( $local_path ) { |
| 55 |
$content = Utils::file_get_contents( $local_path ); |
| 56 |
|
| 57 |
if ( $content ) { |
| 58 |
return $content; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$response = wp_safe_remote_get( $url ); |
| 63 |
|
| 64 |
if ( ! is_wp_error( $response ) ) { |
| 65 |
return $response['body']; |
| 66 |
} |
| 67 |
|
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
private function resolve_local_path( string $url ): ?string { |
| 72 |
$site_url = site_url(); |
| 73 |
|
| 74 |
if ( 0 !== strpos( $url, $site_url ) ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
|
| 78 |
$relative = substr( $url, strlen( $site_url ) ); |
| 79 |
$path = ABSPATH . ltrim( $relative, '/' ); |
| 80 |
|
| 81 |
return file_exists( $path ) ? $path : null; |
| 82 |
} |
| 83 |
|
| 84 |
private function process_svg( string $content ): string { |
| 85 |
$svg = new \WP_HTML_Tag_Processor( $content ); |
| 86 |
|
| 87 |
if ( ! $svg->next_tag( 'svg' ) ) { |
| 88 |
return ''; |
| 89 |
} |
| 90 |
|
| 91 |
$svg->set_attribute( 'fill', 'currentColor' ); |
| 92 |
$this->merge_inline_styles( $svg ); |
| 93 |
|
| 94 |
return ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() ); |
| 95 |
} |
| 96 |
|
| 97 |
private function merge_inline_styles( \WP_HTML_Tag_Processor $svg ): void { |
| 98 |
$existing = trim( (string) $svg->get_attribute( 'style' ) ); |
| 99 |
|
| 100 |
$merged = empty( $existing ) |
| 101 |
? self::SVG_INLINE_STYLES |
| 102 |
: rtrim( $existing, ';' ) . '; ' . self::SVG_INLINE_STYLES; |
| 103 |
|
| 104 |
$svg->set_attribute( 'style', $merged ); |
| 105 |
} |
| 106 |
} |
| 107 |
|