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

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

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