PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.4
Elementor Website Builder – more than just a page builder v4.0.4
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / atomic-widgets / props-resolver / transformers / svg-src-transformer.php

svg-src-transformer.php in Elementor Website Builder – more than just a page builder 4.0.4, at modules/atomic-widgets/props-resolver/transformers/svg-src-transformer.php

107 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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