PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.2
Elementor Website Builder – more than just a page builder v4.3.2
4.3.2 4.3.1 4.3.0 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 All 455 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.3.2, at modules/atomic-widgets/props-resolver/transformers/svg-src-transformer.php

121 lines 2.8 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_content = $this->fetch_local_svg_content( $url );
53
54 return $local_content ? $local_content : $this->fetch_remote_svg_content( $url );
55 }
56
57 private function fetch_local_svg_content( string $url ): ?string {
58 $local_path = $this->resolve_local_path( $url );
59
60 if ( ! $local_path ) {
61 return null;
62 }
63
64 $content = Utils::file_get_contents( $local_path );
65
66 return $content ? $content : null;
67 }
68
69 private function fetch_remote_svg_content( string $url ): ?string {
70 $response = wp_safe_remote_get( $url );
71
72 if ( is_wp_error( $response ) ) {
73 return null;
74 }
75
76 if ( \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) {
77 return null;
78 }
79
80 $body = wp_remote_retrieve_body( $response );
81
82 return $body ? $body : null;
83 }
84
85 private function resolve_local_path( string $url ): ?string {
86 $site_url = site_url();
87
88 if ( 0 !== strpos( $url, $site_url ) ) {
89 return null;
90 }
91
92 $relative = substr( $url, strlen( $site_url ) );
93 $path = ABSPATH . ltrim( $relative, '/' );
94
95 return file_exists( $path ) ? $path : null;
96 }
97
98 private function process_svg( string $content ): string {
99 $svg = new \WP_HTML_Tag_Processor( $content );
100
101 if ( ! $svg->next_tag( 'svg' ) ) {
102 return '';
103 }
104
105 $svg->set_attribute( 'fill', 'currentColor' );
106 $this->merge_inline_styles( $svg );
107
108 return ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() );
109 }
110
111 private function merge_inline_styles( \WP_HTML_Tag_Processor $svg ): void {
112 $existing = trim( (string) $svg->get_attribute( 'style' ) );
113
114 $merged = empty( $existing )
115 ? self::SVG_INLINE_STYLES
116 : rtrim( $existing, ';' ) . '; ' . self::SVG_INLINE_STYLES;
117
118 $svg->set_attribute( 'style', $merged );
119 }
120 }
121