← All changes
|
modules/atomic-widgets/elements/atomic-svg/atomic-svg.php
+98
-41
4.3.0
→
3.31.4
View file →
| @@ -2,25 +2,20 @@ | ||
| 2 | 2 | namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Svg; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 5 | 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; | |
| 6 | 9 | use Elementor\Modules\AtomicWidgets\Controls\Types\Svg_Control; |
| 7 | -use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control; | |
| 8 | -use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base; | |
| 9 | -use Elementor\Modules\AtomicWidgets\Elements\Base\Has_Template; | |
| 10 | -use Elementor\Modules\AtomicWidgets\Module as Atomic_Widgets_Module; | |
| 11 | -use Elementor\Modules\AtomicWidgets\Elements\Base\Html_Tag_Computer; | |
| 12 | -use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; | |
| 13 | -use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; | |
| 14 | -use Elementor\Modules\AtomicWidgets\PropTypes\Icon_Prop_Type; | |
| 10 | +use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type; | |
| 11 | +use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Array_Prop_Type; | |
| 15 | 12 | use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 16 | 13 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 17 | 14 | use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 18 | -use Elementor\Modules\AtomicWidgets\PropTypes\Svg_Src_Prop_Type; | |
| 19 | -use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type; | |
| 20 | 15 | use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 21 | 16 | use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| 22 | -use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; | |
| 17 | +use Elementor\Utils; | |
| 23 | 18 | |
| 24 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 25 | 20 | exit; // Exit if accessed directly. |
| 26 | 21 | } |
| @@ -25,17 +20,13 @@ | ||
| 25 | 20 | exit; // Exit if accessed directly. |
| 26 | 21 | } |
| 27 | 22 | |
| 28 | 23 | class Atomic_Svg extends Atomic_Widget_Base { |
| 29 | - use Has_Template; | |
| 30 | - | |
| 31 | 24 | const BASE_STYLE_KEY = 'base'; |
| 32 | 25 | const DEFAULT_SVG = 'images/default-svg.svg'; |
| 33 | 26 | const DEFAULT_SVG_PATH = ELEMENTOR_ASSETS_PATH . self::DEFAULT_SVG; |
| 34 | 27 | const DEFAULT_SVG_URL = ELEMENTOR_ASSETS_URL . self::DEFAULT_SVG; |
| 35 | 28 | |
| 36 | - public static $widget_description = 'Display an SVG image with customizable styles and link options.'; | |
| 37 | - | |
| 38 | 29 | public static function get_element_type(): string { |
| 39 | 30 | return 'e-svg'; |
| 40 | 31 | } |
| 41 | 32 | |
| @@ -43,9 +34,9 @@ | ||
| 43 | 34 | return esc_html__( 'SVG', 'elementor' ); |
| 44 | 35 | } |
| 45 | 36 | |
| 46 | 37 | public function get_keywords() { |
| 47 | - return [ 'ato', 'atom', 'atoms', 'atomic', 'svg', 'icon', 'vector' ]; | |
| 38 | + return [ 'ato', 'atom', 'atoms', 'atomic' ]; | |
| 48 | 39 | } |
| 49 | 40 | |
| 50 | 41 | public function get_icon() { |
| 51 | 42 | return 'eicon-svg'; |
| @@ -50,20 +41,14 @@ | ||
| 50 | 41 | public function get_icon() { |
| 51 | 42 | return 'eicon-svg'; |
| 52 | 43 | } |
| 53 | 44 | |
| 54 | - public static function get_computed_html_tag( array $settings ): string { | |
| 55 | - return Html_Tag_Computer::compute( $settings, 'div' ); | |
| 56 | - } | |
| 57 | - | |
| 58 | 45 | protected static function define_props_schema(): array { |
| 59 | 46 | return [ |
| 60 | 47 | 'classes' => Classes_Prop_Type::make()->default( [] ), |
| 61 | - 'svg' => Union_Prop_Type::create_from( | |
| 62 | - Svg_Src_Prop_Type::make()->default_url( static::DEFAULT_SVG_URL ) | |
| 63 | - )->add_prop_type( Icon_Prop_Type::make() ), | |
| 48 | + 'svg' => Image_Src_Prop_Type::make()->default_url( static::DEFAULT_SVG_URL ), | |
| 64 | 49 | 'link' => Link_Prop_Type::make(), |
| 65 | - 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), | |
| 50 | + 'attributes' => Key_Value_Array_Prop_Type::make(), | |
| 66 | 51 | ]; |
| 67 | 52 | } |
| 68 | 53 | |
| 69 | 54 | protected function define_atomic_controls(): array { |
| @@ -69,18 +54,12 @@ | ||
| 69 | 54 | protected function define_atomic_controls(): array { |
| 70 | 55 | return [ |
| 71 | 56 | Section::make() |
| 72 | 57 | ->set_label( esc_html__( 'Content', 'elementor' ) ) |
| 73 | - ->set_id( 'content' ) | |
| 74 | 58 | ->set_items( [ |
| 75 | 59 | Svg_Control::bind_to( 'svg' ) |
| 76 | - ->set_label( __( 'SVG', 'elementor' ) ) | |
| 77 | - ->set_show_icon_library( Atomic_Widgets_Module::is_svg_library_active() ), | |
| 60 | + ->set_label( __( 'SVG', 'elementor' ) ), | |
| 78 | 61 | ] ), |
| 79 | - Section::make() | |
| 80 | - ->set_label( __( 'Settings', 'elementor' ) ) | |
| 81 | - ->set_id( 'settings' ) | |
| 82 | - ->set_items( $this->get_settings_controls() ), | |
| 83 | 62 | ]; |
| 84 | 63 | } |
| 85 | 64 | |
| 86 | 65 | protected function get_settings_controls(): array { |
| @@ -85,13 +64,9 @@ | ||
| 85 | 64 | |
| 86 | 65 | protected function get_settings_controls(): array { |
| 87 | 66 | return [ |
| 88 | 67 | Link_Control::bind_to( 'link' ) |
| 89 | - ->set_placeholder( __( 'Type or paste your URL', 'elementor' ) ) | |
| 90 | 68 | ->set_label( __( 'Link', 'elementor' ) ), |
| 91 | - Text_Control::bind_to( '_cssid' ) | |
| 92 | - ->set_label( __( 'ID', 'elementor' ) ) | |
| 93 | - ->set_meta( $this->get_css_id_control_meta() ), | |
| 94 | 69 | ]; |
| 95 | 70 | } |
| 96 | 71 | |
| 97 | 72 | protected function define_base_styles(): array { |
| @@ -112,14 +87,96 @@ | ||
| 112 | 87 | ), |
| 113 | 88 | ]; |
| 114 | 89 | } |
| 115 | 90 | |
| 116 | - protected function get_templates(): array { | |
| 117 | - return [ | |
| 118 | - 'elementor/elements/atomic-svg' => __DIR__ . '/atomic-svg.html.twig', | |
| 119 | - ]; | |
| 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; | |
| 120 | 137 | } |
| 121 | 138 | |
| 122 | - public function render_markdown(): string { | |
| 123 | - return ''; | |
| 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 ); | |
| 124 | 181 | } |
| 125 | 182 | } |