PluginProbe
Elementor Website Builder – more than just a page builder / 3.29.0-beta4
Elementor Website Builder – more than just a page builder v3.29.0-beta4
4.3.3 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 All 456 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.29.0-beta4, at modules/atomic-widgets/elements/atomic-svg/atomic-svg.php

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