| 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 |
|