| 1 |
<?php |
| 2 |
namespace Elementor\Modules\AtomicWidgets\Elements\Div_Block; |
| 3 |
|
| 4 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control; |
| 5 |
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base; |
| 6 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 9 |
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 10 |
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 11 |
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 12 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 13 |
use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Array_Prop_Type; |
| 14 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| 15 |
use Elementor\Plugin; |
| 16 |
use Elementor\Utils; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
class Div_Block extends Atomic_Element_Base { |
| 23 |
const BASE_STYLE_KEY = 'base'; |
| 24 |
|
| 25 |
public static function get_type() { |
| 26 |
return 'e-div-block'; |
| 27 |
} |
| 28 |
|
| 29 |
public static function get_element_type(): string { |
| 30 |
return 'e-div-block'; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_title() { |
| 34 |
return esc_html__( 'Div Block', 'elementor' ); |
| 35 |
} |
| 36 |
|
| 37 |
public function get_keywords() { |
| 38 |
return [ 'ato', 'atom', 'atoms', 'atomic' ]; |
| 39 |
} |
| 40 |
|
| 41 |
public function get_icon() { |
| 42 |
return 'eicon-div-block'; |
| 43 |
} |
| 44 |
|
| 45 |
protected static function define_props_schema(): array { |
| 46 |
$tag_dependencies = Dependency_Manager::make() |
| 47 |
->where( [ |
| 48 |
'operator' => 'not_exist', |
| 49 |
'path' => [ 'link', 'destination' ], |
| 50 |
] ) |
| 51 |
->get(); |
| 52 |
|
| 53 |
$props = [ |
| 54 |
'classes' => Classes_Prop_Type::make() |
| 55 |
->default( [] ), |
| 56 |
'tag' => String_Prop_Type::make() |
| 57 |
->enum( [ 'div', 'header', 'section', 'article', 'aside', 'footer' ] ) |
| 58 |
->default( 'div' ) |
| 59 |
->set_dependencies( $tag_dependencies ), |
| 60 |
'link' => Link_Prop_Type::make(), |
| 61 |
|
| 62 |
'attributes' => Key_Value_Array_Prop_Type::make(), |
| 63 |
]; |
| 64 |
|
| 65 |
return $props; |
| 66 |
} |
| 67 |
|
| 68 |
protected function define_atomic_controls(): array { |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
protected function get_settings_controls(): array { |
| 73 |
return [ |
| 74 |
Select_Control::bind_to( 'tag' ) |
| 75 |
->set_label( esc_html__( 'HTML Tag', 'elementor' ) ) |
| 76 |
->set_options( [ |
| 77 |
[ |
| 78 |
'value' => 'div', |
| 79 |
'label' => 'Div', |
| 80 |
], |
| 81 |
[ |
| 82 |
'value' => 'header', |
| 83 |
'label' => 'Header', |
| 84 |
], |
| 85 |
[ |
| 86 |
'value' => 'section', |
| 87 |
'label' => 'Section', |
| 88 |
], |
| 89 |
[ |
| 90 |
'value' => 'article', |
| 91 |
'label' => 'Article', |
| 92 |
], |
| 93 |
[ |
| 94 |
'value' => 'aside', |
| 95 |
'label' => 'Aside', |
| 96 |
], |
| 97 |
[ |
| 98 |
'value' => 'footer', |
| 99 |
'label' => 'Footer', |
| 100 |
], |
| 101 |
]), |
| 102 |
Link_Control::bind_to( 'link' ) |
| 103 |
->set_label( __( 'Link', 'elementor' ) ) |
| 104 |
->set_meta( [ |
| 105 |
'topDivider' => true, |
| 106 |
] ), |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
protected function _get_default_child_type( array $element_data ) { |
| 111 |
$el_types = array_keys( Plugin::$instance->elements_manager->get_element_types() ); |
| 112 |
|
| 113 |
if ( in_array( $element_data['elType'], $el_types, true ) ) { |
| 114 |
return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] ); |
| 115 |
} |
| 116 |
|
| 117 |
return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] ); |
| 118 |
} |
| 119 |
|
| 120 |
protected function content_template() { |
| 121 |
?> |
| 122 |
<?php |
| 123 |
} |
| 124 |
|
| 125 |
public function before_render() { |
| 126 |
?> |
| 127 |
<<?php $this->print_html_tag(); ?> <?php $this->print_render_attribute_string( '_wrapper' ); |
| 128 |
$this->print_custom_attributes(); ?>> |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
public function after_render() { |
| 133 |
?> |
| 134 |
</<?php $this->print_html_tag(); ?>> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
private function print_custom_attributes() { |
| 139 |
$settings = $this->get_atomic_settings(); |
| 140 |
$attributes = $settings['attributes']; |
| 141 |
if ( ! empty( $attributes ) && is_string( $attributes ) ) { |
| 142 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 143 |
echo ' ' . $attributes; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Print safe HTML tag for the element based on the element settings. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
protected function print_html_tag() { |
| 153 |
$html_tag = $this->get_html_tag(); |
| 154 |
Utils::print_validated_html_tag( $html_tag ); |
| 155 |
} |
| 156 |
|
| 157 |
protected function get_html_tag(): string { |
| 158 |
$settings = $this->get_atomic_settings(); |
| 159 |
|
| 160 |
return ! empty( $settings['link']['href'] ) ? 'a' : ( $settings['tag'] ?? 'div' ); |
| 161 |
} |
| 162 |
|
| 163 |
protected function define_base_styles(): array { |
| 164 |
$display = String_Prop_Type::generate( 'block' ); |
| 165 |
|
| 166 |
return [ |
| 167 |
static::BASE_STYLE_KEY => Style_Definition::make() |
| 168 |
->add_variant( |
| 169 |
Style_Variant::make() |
| 170 |
->add_prop( 'display', $display ) |
| 171 |
->add_prop( 'padding', $this->get_base_padding() ) |
| 172 |
->add_prop( 'min-width', $this->get_base_min_width() ) |
| 173 |
), |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
protected function get_base_padding(): array { |
| 178 |
return Size_Prop_Type::generate( [ |
| 179 |
'size' => 10, |
| 180 |
'unit' => 'px', |
| 181 |
] ); |
| 182 |
} |
| 183 |
|
| 184 |
protected function get_base_min_width(): array { |
| 185 |
return Size_Prop_Type::generate( [ |
| 186 |
'size' => 30, |
| 187 |
'unit' => 'px', |
| 188 |
] ); |
| 189 |
} |
| 190 |
|
| 191 |
protected function add_render_attributes() { |
| 192 |
parent::add_render_attributes(); |
| 193 |
$settings = $this->get_atomic_settings(); |
| 194 |
$base_style_class = $this->get_base_styles_dictionary()[ static::BASE_STYLE_KEY ]; |
| 195 |
|
| 196 |
$attributes = [ |
| 197 |
'class' => [ |
| 198 |
'e-con', |
| 199 |
$base_style_class, |
| 200 |
...( $settings['classes'] ?? [] ), |
| 201 |
], |
| 202 |
]; |
| 203 |
|
| 204 |
if ( ! empty( $settings['_cssid'] ) ) { |
| 205 |
$attributes['id'] = esc_attr( $settings['_cssid'] ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( ! empty( $settings['link']['href'] ) ) { |
| 209 |
$attributes = array_merge( $attributes, $settings['link'] ); |
| 210 |
} |
| 211 |
|
| 212 |
$this->add_render_attribute( '_wrapper', $attributes ); |
| 213 |
} |
| 214 |
} |
| 215 |
|