PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev3
Elementor Website Builder – more than just a page builder v3.32.0-dev3
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.32.0-dev3, at modules/atomic-widgets/elements/atomic-svg/atomic-svg.php

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