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 / icon-transformer.php

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

82 lines 2.1 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\Page_Assets\Data_Managers\Font_Icon_Svg\Manager as Font_Icon_Svg_Data_Manager;
6 use Elementor\Core\Utils\Svg\Svg_Sanitizer;
7 use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
8 use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
9 use Elementor\Utils;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 class Icon_Transformer extends Transformer_Base {
16 const SVG_INLINE_STYLES = 'width: 100%; height: 100%; overflow: unset;';
17
18 public function transform( $value, Props_Resolver_Context $context ) {
19 $icon = [
20 'value' => $value['value'] ?? '',
21 'library' => $value['library'] ?? '',
22 ];
23
24 $icon['font_family'] = Font_Icon_Svg_Data_Manager::get_font_family( $icon['library'] );
25
26 if ( ! $icon['font_family'] ) {
27 return [
28 'html' => '',
29 'url' => null,
30 ];
31 }
32
33 $icon_data = Font_Icon_Svg_Data_Manager::get_font_icon_svg_data( $icon );
34 $html = $this->build_svg( $icon_data );
35
36 return [
37 'html' => $html,
38 'url' => null,
39 ];
40 }
41
42 private function build_svg( $icon_data ): string {
43 if ( empty( $icon_data['path'] ) ) {
44 return '';
45 }
46
47 $attributes = [
48 'viewBox' => '0 0 ' . $icon_data['width'] . ' ' . $icon_data['height'],
49 'xmlns' => 'http://www.w3.org/2000/svg',
50 ];
51
52 $svg = '<svg ' . Utils::render_html_attributes( $attributes ) . '>' .
53 '<path d="' . esc_attr( $icon_data['path'] ) . '"></path>' .
54 '</svg>';
55
56 return $this->process_svg( $svg );
57 }
58
59 private function process_svg( string $content ): string {
60 $svg = new \WP_HTML_Tag_Processor( $content );
61
62 if ( ! $svg->next_tag( 'svg' ) ) {
63 return '';
64 }
65
66 $svg->set_attribute( 'fill', 'currentColor' );
67 $this->merge_inline_styles( $svg );
68
69 return ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() );
70 }
71
72 private function merge_inline_styles( \WP_HTML_Tag_Processor $svg ): void {
73 $existing = trim( (string) $svg->get_attribute( 'style' ) );
74
75 $merged = empty( $existing )
76 ? self::SVG_INLINE_STYLES
77 : rtrim( $existing, ';' ) . '; ' . self::SVG_INLINE_STYLES;
78
79 $svg->set_attribute( 'style', $merged );
80 }
81 }
82