| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements\Base; |
| 4 |
|
| 5 |
use Elementor\Element_Base; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns\Has_Meta; |
| 9 |
use Elementor\Plugin; |
| 10 |
use Elementor\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
abstract class Atomic_Element_Base extends Element_Base { |
| 17 |
use Has_Atomic_Base; |
| 18 |
use Has_Meta; |
| 19 |
|
| 20 |
protected $version = '0.0'; |
| 21 |
protected $styles = []; |
| 22 |
protected $interactions = []; |
| 23 |
protected $editor_settings = []; |
| 24 |
protected $origin_id = null; |
| 25 |
|
| 26 |
public static $widget_description = null; |
| 27 |
|
| 28 |
|
| 29 |
public function __construct( $data = [], $args = null ) { |
| 30 |
parent::__construct( $data, $args ); |
| 31 |
|
| 32 |
$this->version = $data['version'] ?? '0.0'; |
| 33 |
$this->styles = $data['styles'] ?? []; |
| 34 |
$this->interactions = $this->parse_atomic_interactions( $data['interactions'] ?? [] ); |
| 35 |
$this->editor_settings = $data['editor_settings'] ?? []; |
| 36 |
|
| 37 |
if ( static::$widget_description ) { |
| 38 |
$this->description( static::$widget_description ); |
| 39 |
} |
| 40 |
|
| 41 |
$this->origin_id = $data['origin_id'] ?? null; |
| 42 |
} |
| 43 |
|
| 44 |
private function parse_atomic_interactions( $interactions ) { |
| 45 |
if ( empty( $interactions ) ) { |
| 46 |
return []; |
| 47 |
} |
| 48 |
|
| 49 |
if ( is_string( $interactions ) ) { |
| 50 |
$decoded = json_decode( $interactions, true ); |
| 51 |
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { |
| 52 |
$interactions = $decoded; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! is_array( $interactions ) ) { |
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
return $interactions; |
| 61 |
} |
| 62 |
|
| 63 |
abstract protected function define_atomic_controls(): array; |
| 64 |
|
| 65 |
protected function define_atomic_style_states(): array { |
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
protected function define_atomic_pseudo_states(): array { |
| 70 |
return []; |
| 71 |
} |
| 72 |
|
| 73 |
public function get_global_scripts() { |
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
protected function get_initial_config() { |
| 78 |
$config = parent::get_initial_config(); |
| 79 |
$props_schema = static::get_props_schema(); |
| 80 |
|
| 81 |
$config['atomic'] = true; |
| 82 |
$config['atomic_controls'] = $this->get_atomic_controls(); |
| 83 |
$config['atomic_props_schema'] = $props_schema; |
| 84 |
$config['atomic_style_states'] = $this->define_atomic_style_states(); |
| 85 |
$config['atomic_pseudo_states'] = $this->define_atomic_pseudo_states(); |
| 86 |
$config['dependencies_per_target_mapping'] = Dependency_Manager::get_source_to_dependents( $props_schema ); |
| 87 |
$config['base_styles'] = $this->get_base_styles(); |
| 88 |
$config['version'] = $this->version; |
| 89 |
$config['show_in_panel'] = $this->should_show_in_panel(); |
| 90 |
$config['categories'] = $this->define_panel_categories(); |
| 91 |
$config['hide_on_search'] = false; |
| 92 |
$config['controls'] = []; |
| 93 |
$config['keywords'] = $this->get_keywords(); |
| 94 |
$config['default_children'] = $this->define_default_children(); |
| 95 |
$config['initial_attributes'] = $this->define_initial_attributes(); |
| 96 |
$config['include_in_widgets_config'] = true; |
| 97 |
$config['default_html_tag'] = $this->define_default_html_tag(); |
| 98 |
$config['meta'] = $this->get_meta(); |
| 99 |
$config['allowed_child_types'] = $this->define_allowed_child_types(); |
| 100 |
|
| 101 |
return $config; |
| 102 |
} |
| 103 |
|
| 104 |
protected function should_show_in_panel() { |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
protected function define_panel_categories(): array { |
| 109 |
return [ 'v4-elements' ]; |
| 110 |
} |
| 111 |
|
| 112 |
protected function define_default_children() { |
| 113 |
return []; |
| 114 |
} |
| 115 |
|
| 116 |
protected function define_default_html_tag() { |
| 117 |
return 'div'; |
| 118 |
} |
| 119 |
|
| 120 |
protected function define_initial_attributes() { |
| 121 |
return []; |
| 122 |
} |
| 123 |
|
| 124 |
protected function define_allowed_child_types() { |
| 125 |
return []; |
| 126 |
} |
| 127 |
|
| 128 |
protected function get_interaction_id() { |
| 129 |
return $this->origin_id ?? $this->get_id(); |
| 130 |
} |
| 131 |
|
| 132 |
protected function add_render_attributes() { |
| 133 |
parent::add_render_attributes(); |
| 134 |
|
| 135 |
$this->add_render_attribute( '_wrapper', 'data-interaction-id', $this->get_interaction_id() ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get Element keywords. |
| 140 |
* |
| 141 |
* Retrieve the element keywords. |
| 142 |
* |
| 143 |
* @since 3.29 |
| 144 |
* @access public |
| 145 |
* |
| 146 |
* @return array Element keywords. |
| 147 |
*/ |
| 148 |
public function get_keywords() { |
| 149 |
return []; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @return array<string, Prop_Type> |
| 154 |
*/ |
| 155 |
abstract protected static function define_props_schema(): array; |
| 156 |
|
| 157 |
/** |
| 158 |
* Get the HTML tag for rendering. |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
protected function get_html_tag(): string { |
| 163 |
$settings = $this->get_atomic_settings(); |
| 164 |
$default_html_tag = $this->define_default_html_tag(); |
| 165 |
|
| 166 |
if ( ! empty( $settings['link']['tag'] ) ) { |
| 167 |
return $settings['link']['tag']; |
| 168 |
} |
| 169 |
|
| 170 |
return $settings['tag'] ?? $default_html_tag; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Print safe HTML tag for the element based on the element settings. |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
protected function print_html_tag() { |
| 179 |
$html_tag = $this->get_html_tag(); |
| 180 |
Utils::print_validated_html_tag( $html_tag ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Print custom attributes if they exist. |
| 185 |
* |
| 186 |
* @return void |
| 187 |
*/ |
| 188 |
protected function print_custom_attributes() { |
| 189 |
$settings = $this->get_atomic_settings(); |
| 190 |
$attributes = $settings['attributes'] ?? ''; |
| 191 |
|
| 192 |
if ( isset( $settings['link']['attributes'] ) ) { |
| 193 |
$attributes .= ' ' . ( $settings['link']['attributes'] ?? '' ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( ! empty( $attributes ) && is_string( $attributes ) ) { |
| 197 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 198 |
echo ' ' . $attributes; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get default child type for container elements. |
| 204 |
* |
| 205 |
* @param array $element_data |
| 206 |
* @return mixed |
| 207 |
*/ |
| 208 |
protected function _get_default_child_type( array $element_data ) { |
| 209 |
$el_types = array_keys( Plugin::$instance->elements_manager->get_element_types() ); |
| 210 |
|
| 211 |
if ( in_array( $element_data['elType'], $el_types, true ) ) { |
| 212 |
return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( ! isset( $element_data['widgetType'] ) ) { |
| 216 |
return null; |
| 217 |
} |
| 218 |
|
| 219 |
return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Default before render for container elements. |
| 224 |
* |
| 225 |
* @return void |
| 226 |
*/ |
| 227 |
public function before_render() { |
| 228 |
?> |
| 229 |
<<?php $this->print_html_tag(); ?> <?php $this->print_render_attribute_string( '_wrapper' ); |
| 230 |
$this->print_custom_attributes(); ?>> |
| 231 |
<?php |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Default after render for container elements. |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function after_render() { |
| 240 |
?> |
| 241 |
</<?php $this->print_html_tag(); ?>> |
| 242 |
<?php |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Default content template - can be overridden by elements that need custom templates. |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
protected function content_template() { |
| 251 |
?> |
| 252 |
<?php |
| 253 |
} |
| 254 |
|
| 255 |
public static function generate() { |
| 256 |
return Element_Builder::make( static::get_type() ); |
| 257 |
} |
| 258 |
} |
| 259 |
|