PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.1
Elementor Website Builder – more than just a page builder v3.31.1
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 / elements / atomic-svg / atomic-svg.php

atomic-svg.php in Elementor Website Builder – more than just a page builder 3.31.1, at modules/atomic-widgets/elements/atomic-svg/atomic-svg.php

183 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Svg;
3
4 use Elementor\Modules\AtomicWidgets\Controls\Section;
5 use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
7 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
8 use Elementor\Core\Utils\Svg\Svg_Sanitizer;
9 use Elementor\Modules\AtomicWidgets\Controls\Types\Svg_Control;
10 use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type;
11 use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Array_Prop_Type;
12 use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
13 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
14 use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
15 use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
16 use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
17 use Elementor\Utils;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // Exit if accessed directly.
21 }
22
23 class Atomic_Svg extends Atomic_Widget_Base {
24 const BASE_STYLE_KEY = 'base';
25 const DEFAULT_SVG = 'images/default-svg.svg';
26 const DEFAULT_SVG_PATH = ELEMENTOR_ASSETS_PATH . self::DEFAULT_SVG;
27 const DEFAULT_SVG_URL = ELEMENTOR_ASSETS_URL . self::DEFAULT_SVG;
28
29 public static function get_element_type(): string {
30 return 'e-svg';
31 }
32
33 public function get_title() {
34 return esc_html__( 'SVG', 'elementor' );
35 }
36
37 public function get_keywords() {
38 return [ 'ato', 'atom', 'atoms', 'atomic' ];
39 }
40
41 public function get_icon() {
42 return 'eicon-svg';
43 }
44
45 protected static function define_props_schema(): array {
46 return [
47 'classes' => Classes_Prop_Type::make()->default( [] ),
48 'svg' => Image_Src_Prop_Type::make()->default_url( static::DEFAULT_SVG_URL ),
49 'link' => Link_Prop_Type::make(),
50 'attributes' => Key_Value_Array_Prop_Type::make(),
51 ];
52 }
53
54 protected function define_atomic_controls(): array {
55 return [
56 Section::make()
57 ->set_label( esc_html__( 'Content', 'elementor' ) )
58 ->set_items( [
59 Svg_Control::bind_to( 'svg' )
60 ->set_label( __( 'SVG', 'elementor' ) ),
61 ] ),
62 ];
63 }
64
65 protected function get_settings_controls(): array {
66 return [
67 Link_Control::bind_to( 'link' )
68 ->set_label( __( 'Link', 'elementor' ) ),
69 ];
70 }
71
72 protected function define_base_styles(): array {
73 $display_value = String_Prop_Type::generate( 'inline-block' );
74
75 $size = Size_Prop_Type::generate( [
76 'size' => 65,
77 'unit' => 'px',
78 ] );
79
80 return [
81 self::BASE_STYLE_KEY => Style_Definition::make()
82 ->add_variant(
83 Style_Variant::make()
84 ->add_prop( 'display', $display_value )
85 ->add_prop( 'width', $size )
86 ->add_prop( 'height', $size )
87 ),
88 ];
89 }
90
91 protected function render() {
92 $settings = $this->get_atomic_settings();
93
94 $svg = $this->get_svg_content( $settings );
95
96 if ( ! $svg ) {
97 return;
98 }
99
100 $svg = new \WP_HTML_Tag_Processor( $svg );
101
102 if ( ! $svg->next_tag( 'svg' ) ) {
103 return;
104 }
105
106 $svg->set_attribute( 'fill', 'currentColor' );
107 $this->add_svg_style( $svg, 'width: 100%; height: 100%; overflow: unset;' );
108
109 $svg_html = ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() );
110
111 $classes = array_filter( array_merge(
112 [ self::BASE_STYLE_KEY => $this->get_base_styles_dictionary()[ self::BASE_STYLE_KEY ] ],
113 $settings['classes']
114 ) );
115
116 $classes_string = implode( ' ', $classes );
117
118 $cssid_attribute = ! empty( $settings['_cssid'] ) ? 'id="' . esc_attr( $settings['_cssid'] ) . '"' : '';
119
120 $all_attributes = trim( $cssid_attribute . ' ' . $settings['attributes'] );
121
122 if ( isset( $settings['link'] ) && ! empty( $settings['link']['href'] ) ) {
123 $svg_html = sprintf(
124 '<a href="%s" target="%s" class="%s" %s>%s</a>',
125 $settings['link']['href'],
126 esc_attr( $settings['link']['target'] ),
127 esc_attr( $classes_string ),
128 $all_attributes,
129 $svg_html
130 );
131 } else {
132 $svg_html = sprintf( '<div class="%s" %s>%s</div>', esc_attr( $classes_string ), $all_attributes, $svg_html );
133 }
134
135 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
136 echo $svg_html;
137 }
138
139 private function get_svg_content( $settings ) {
140 if ( isset( $settings['svg']['id'] ) ) {
141 $content = Utils::file_get_contents(
142 get_attached_file( $settings['svg']['id'] )
143 );
144
145 if ( $content ) {
146 return $content;
147 }
148 }
149
150 if (
151 isset( $settings['svg']['url'] ) &&
152 static::DEFAULT_SVG_URL !== $settings['svg']['url']
153 ) {
154 $content = wp_safe_remote_get(
155 $settings['svg']['url']
156 );
157
158 if ( ! is_wp_error( $content ) ) {
159 return $content['body'];
160 }
161 }
162
163 $content = Utils::file_get_contents(
164 static::DEFAULT_SVG_PATH
165 );
166
167 return $content ? $content : null;
168 }
169
170 private function add_svg_style( &$svg, $new_style ) {
171 $svg_style = $svg->get_attribute( 'style' );
172 $svg_style = trim( (string) $svg_style );
173
174 if ( empty( $svg_style ) ) {
175 $svg_style = $new_style;
176 } else {
177 $svg_style = rtrim( $svg_style, ';' ) . '; ' . $new_style;
178 }
179
180 $svg->set_attribute( 'style', $svg_style );
181 }
182 }
183