| 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 |
|
| 12 |
use Elementor\Modules\AtomicWidgets\PropTypes\Link_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 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
class Atomic_Svg extends Atomic_Widget_Base { |
| 22 |
const BASE_STYLE_KEY = 'base'; |
| 23 |
const DEFAULT_SIZE = 'full'; |
| 24 |
const DEFAULT_SVG_PATH = ELEMENTOR_ASSETS_URL . 'images/a-default-svg.svg'; |
| 25 |
|
| 26 |
public static function get_element_type(): string { |
| 27 |
return 'a-svg'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_title() { |
| 31 |
return esc_html__( 'Atomic SVG', 'elementor' ); |
| 32 |
} |
| 33 |
|
| 34 |
public function get_icon() { |
| 35 |
return 'eicon-svg'; |
| 36 |
} |
| 37 |
|
| 38 |
protected static function define_props_schema(): array { |
| 39 |
return [ |
| 40 |
'classes' => Classes_Prop_Type::make()->default( [] ), |
| 41 |
'svg' => Image_Src_Prop_Type::make()->default_url( self::DEFAULT_SVG_PATH ), |
| 42 |
'link' => Link_Prop_Type::make(), |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
protected function define_atomic_controls(): array { |
| 47 |
return [ |
| 48 |
Section::make() |
| 49 |
->set_label( esc_html__( 'Content', 'elementor' ) ) |
| 50 |
->set_items( [ |
| 51 |
Svg_Control::bind_to( 'svg' ), |
| 52 |
Link_Control::bind_to( 'link' ) |
| 53 |
->set_placeholder( __( 'Paste URL or type', 'elementor' ) ), |
| 54 |
] ), |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
protected function define_base_styles(): array { |
| 59 |
$width = Size_Prop_Type::generate( [ |
| 60 |
'size' => 65, |
| 61 |
'unit' => 'px', |
| 62 |
] ); |
| 63 |
$height = Size_Prop_Type::generate( [ |
| 64 |
'size' => 65, |
| 65 |
'unit' => 'px', |
| 66 |
] ); |
| 67 |
|
| 68 |
return [ |
| 69 |
self::BASE_STYLE_KEY => Style_Definition::make() |
| 70 |
->add_variant( |
| 71 |
Style_Variant::make() |
| 72 |
->add_prop( 'width', $width ) |
| 73 |
->add_prop( 'height', $height ) |
| 74 |
), |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
protected function render() { |
| 79 |
$settings = $this->get_atomic_settings(); |
| 80 |
$svg_url = isset( $settings['svg']['url'] ) ? $settings['svg']['url'] : null; |
| 81 |
|
| 82 |
if ( ! $svg_url && isset( $settings['svg']['id'] ) ) { |
| 83 |
$attachment = wp_get_attachment_image_src( $settings['svg']['id'], self::DEFAULT_SIZE ); |
| 84 |
$svg_url = isset( $attachment[0] ) ? $attachment[0] : null; |
| 85 |
} |
| 86 |
|
| 87 |
$svg = file_get_contents( $svg_url ); |
| 88 |
$svg = $svg ? new \WP_HTML_Tag_Processor( $svg ) : null; |
| 89 |
|
| 90 |
if ( $svg && $svg->next_tag( 'svg' ) ) { |
| 91 |
$this->set_svg_attributes( $svg, $settings ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $svg ) { |
| 95 |
$svg_html = ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() ); |
| 96 |
} |
| 97 |
|
| 98 |
$svg_html = $svg_html ?? file_get_contents( self::DEFAULT_SVG_PATH ); |
| 99 |
|
| 100 |
if ( isset( $settings['link'] ) && ! empty( $settings['link']['href'] ) ) { |
| 101 |
$svg_html = sprintf( '<a href="%s" target="%s"> %s </a>', esc_url( $settings['link']['href'] ), esc_attr( $settings['link']['target'] ), $svg_html ); |
| 102 |
} |
| 103 |
|
| 104 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 105 |
echo $svg_html; |
| 106 |
} |
| 107 |
|
| 108 |
private function set_svg_attributes( \WP_HTML_Tag_Processor $svg, $settings ) { |
| 109 |
$svg->set_attribute( 'fill', 'currentColor' ); |
| 110 |
$string_classes = implode( ' ', $settings['classes'] ); |
| 111 |
$svg->add_class( $string_classes ); |
| 112 |
$base_styles = $this->get_base_styles_dictionary()[ self::BASE_STYLE_KEY ]; |
| 113 |
$svg->add_class( $base_styles ); |
| 114 |
} |
| 115 |
} |
| 116 |
|