| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Elements\Base; |
| 4 |
|
| 5 |
use Elementor\Element_Base; |
| 6 |
use Elementor\Modules\AtomicWidgets\Controls\Base\Atomic_Control_Base; |
| 7 |
use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver; |
| 9 |
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 10 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; |
| 11 |
use Elementor\Modules\AtomicWidgets\Parsers\Props_Parser; |
| 12 |
use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser; |
| 13 |
use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 14 |
use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type; |
| 15 |
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 16 |
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 17 |
use Elementor\Utils; |
| 18 |
use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 19 |
use Elementor\Modules\AtomicWidgets\Styles\Atomic_Widget_Styles; |
| 20 |
|
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; // Exit if accessed directly. |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @mixin Element_Base |
| 27 |
*/ |
| 28 |
trait Has_Atomic_Base { |
| 29 |
use Has_Base_Styles; |
| 30 |
|
| 31 |
public function has_widget_inner_wrapper(): bool { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
abstract public static function get_element_type(): string; |
| 36 |
|
| 37 |
final public function get_name() { |
| 38 |
return static::get_element_type(); |
| 39 |
} |
| 40 |
|
| 41 |
private function get_valid_controls( array $schema, array $controls ): array { |
| 42 |
$valid_controls = []; |
| 43 |
|
| 44 |
foreach ( $controls as $control ) { |
| 45 |
if ( $control instanceof Section ) { |
| 46 |
$cloned_section = clone $control; |
| 47 |
|
| 48 |
$cloned_section->set_items( |
| 49 |
$this->get_valid_controls( $schema, $control->get_items() ) |
| 50 |
); |
| 51 |
|
| 52 |
$valid_controls[] = $cloned_section; |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ( $control instanceof Atomic_Control_Base ) ) { |
| 57 |
$prop_name = $control->get_bind(); |
| 58 |
|
| 59 |
if ( ! $prop_name ) { |
| 60 |
Utils::safe_throw( 'Control is missing a bound prop from the schema.' ); |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! array_key_exists( $prop_name, $schema ) ) { |
| 65 |
Utils::safe_throw( "Prop `{$prop_name}` is not defined in the schema of `{$this->get_name()}`." ); |
| 66 |
continue; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$valid_controls[] = $control; |
| 71 |
} |
| 72 |
|
| 73 |
return $valid_controls; |
| 74 |
} |
| 75 |
|
| 76 |
private static function validate_schema( array $schema ) { |
| 77 |
$widget_name = static::class; |
| 78 |
|
| 79 |
foreach ( $schema as $key => $prop ) { |
| 80 |
if ( ! ( $prop instanceof Prop_Type ) ) { |
| 81 |
Utils::safe_throw( "Prop `$key` must be an instance of `Prop_Type` in `{$widget_name}`." ); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private function parse_atomic_styles( array $data ): array { |
| 87 |
$styles = $data['styles'] ?? []; |
| 88 |
$style_parser = Style_Parser::make( Style_Schema::get() ); |
| 89 |
|
| 90 |
foreach ( $styles as $style_id => $style ) { |
| 91 |
$result = $style_parser->parse( $style ); |
| 92 |
|
| 93 |
if ( ! $result->is_valid() ) { |
| 94 |
$widget_id = $data['id'] ?? 'unknown'; |
| 95 |
throw new \Exception( esc_html( "Styles validation failed for style `$style_id`. Widget ID: `$widget_id`. " . $result->errors()->to_string() ) ); |
| 96 |
} |
| 97 |
|
| 98 |
$styles[ $style_id ] = $result->unwrap(); |
| 99 |
} |
| 100 |
|
| 101 |
return $styles; |
| 102 |
} |
| 103 |
|
| 104 |
private function parse_atomic_settings( array $settings ): array { |
| 105 |
$schema = static::get_props_schema(); |
| 106 |
$props_parser = Props_Parser::make( $schema ); |
| 107 |
|
| 108 |
$result = $props_parser->parse( $settings ); |
| 109 |
|
| 110 |
if ( ! $result->is_valid() ) { |
| 111 |
throw new \Exception( esc_html( 'Settings validation failed. ' . $result->errors()->to_string() ) ); |
| 112 |
} |
| 113 |
|
| 114 |
return $result->unwrap(); |
| 115 |
} |
| 116 |
|
| 117 |
private function parse_atomic_interactions( $interactions ) { |
| 118 |
|
| 119 |
if ( empty( $interactions ) ) { |
| 120 |
return []; |
| 121 |
} |
| 122 |
|
| 123 |
if ( is_string( $interactions ) ) { |
| 124 |
$decoded = json_decode( $interactions, true ); |
| 125 |
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { |
| 126 |
return $decoded; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if ( is_array( $interactions ) ) { |
| 131 |
return $interactions; |
| 132 |
} |
| 133 |
|
| 134 |
return []; |
| 135 |
} |
| 136 |
|
| 137 |
private function extract_prop_value( $data, $key, $default = '' ) { |
| 138 |
if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { |
| 139 |
return $default; |
| 140 |
} |
| 141 |
|
| 142 |
$value = $data[ $key ]; |
| 143 |
|
| 144 |
if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) { |
| 145 |
return $value['value']; |
| 146 |
} |
| 147 |
|
| 148 |
return null !== $value ? $value : $default; |
| 149 |
} |
| 150 |
|
| 151 |
public function get_atomic_controls() { |
| 152 |
$controls = apply_filters( |
| 153 |
'elementor/atomic-widgets/controls', |
| 154 |
$this->define_atomic_controls(), |
| 155 |
$this |
| 156 |
); |
| 157 |
|
| 158 |
$schema = static::get_props_schema(); |
| 159 |
|
| 160 |
// Validate the schema only in the Editor. |
| 161 |
static::validate_schema( $schema ); |
| 162 |
|
| 163 |
return $this->get_valid_controls( $schema, $controls ); |
| 164 |
} |
| 165 |
|
| 166 |
protected function get_css_id_control_meta(): array { |
| 167 |
return [ |
| 168 |
'layout' => 'two-columns', |
| 169 |
'topDivider' => true, |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
final public function get_controls( $control_id = null ) { |
| 174 |
if ( ! empty( $control_id ) ) { |
| 175 |
return null; |
| 176 |
} |
| 177 |
|
| 178 |
return []; |
| 179 |
} |
| 180 |
|
| 181 |
final public function get_data_for_save() { |
| 182 |
$data = parent::get_data_for_save(); |
| 183 |
|
| 184 |
$data['version'] = $this->version; |
| 185 |
$data['settings'] = $this->parse_atomic_settings( $data['settings'] ); |
| 186 |
$data['styles'] = $this->parse_atomic_styles( $data ); |
| 187 |
$data['editor_settings'] = $this->parse_editor_settings( $data['editor_settings'] ); |
| 188 |
|
| 189 |
if ( isset( $data['interactions'] ) && ! empty( $data['interactions'] ) ) { |
| 190 |
$data['interactions'] = $this->transform_interactions_for_save( $data['interactions'] ); |
| 191 |
} else { |
| 192 |
$data['interactions'] = []; |
| 193 |
} |
| 194 |
return $data; |
| 195 |
} |
| 196 |
|
| 197 |
private function transform_interactions_for_save( $interactions ) { |
| 198 |
$decoded = $this->decode_interactions_data( $interactions ); |
| 199 |
|
| 200 |
if ( empty( $decoded['items'] ) ) { |
| 201 |
return []; |
| 202 |
} |
| 203 |
return $decoded; |
| 204 |
} |
| 205 |
|
| 206 |
private function decode_interactions_data( $interactions ) { |
| 207 |
if ( is_array( $interactions ) ) { |
| 208 |
return $interactions; |
| 209 |
} |
| 210 |
|
| 211 |
if ( is_string( $interactions ) ) { |
| 212 |
$decoded = json_decode( $interactions, true ); |
| 213 |
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { |
| 214 |
return $decoded; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return [ |
| 219 |
'items' => [], |
| 220 |
'version' => 1, |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
final public function get_raw_data( $with_html_content = false ) { |
| 225 |
$raw_data = parent::get_raw_data( $with_html_content ); |
| 226 |
|
| 227 |
$raw_data['styles'] = Atomic_Widget_Styles::get_license_based_filtered_styles( $this->styles ?? [] ); |
| 228 |
$raw_data['interactions'] = $this->interactions ?? []; |
| 229 |
$raw_data['editor_settings'] = $this->editor_settings; |
| 230 |
|
| 231 |
return $raw_data; |
| 232 |
} |
| 233 |
|
| 234 |
final public function get_stack( $with_common_controls = true ) { |
| 235 |
return [ |
| 236 |
'controls' => [], |
| 237 |
'tabs' => [], |
| 238 |
]; |
| 239 |
} |
| 240 |
|
| 241 |
public function get_atomic_settings(): array { |
| 242 |
$schema = static::get_props_schema(); |
| 243 |
$props = $this->get_settings(); |
| 244 |
$initial_attributes = $this->get_initial_attributes(); |
| 245 |
|
| 246 |
$props['attributes'] = Attributes_Prop_Type::generate( array_merge( |
| 247 |
$initial_attributes['value'] ?? [], |
| 248 |
$props['attributes']['value'] ?? [] |
| 249 |
) ); |
| 250 |
|
| 251 |
return Render_Props_Resolver::for_settings()->resolve( $schema, $props ); |
| 252 |
} |
| 253 |
|
| 254 |
protected function get_initial_attributes() { |
| 255 |
return Attributes_Prop_Type::generate( [ |
| 256 |
Key_Value_Prop_Type::generate( [ |
| 257 |
'key' => String_Prop_Type::generate( 'data-e-type' ), |
| 258 |
'value' => $this->get_type(), |
| 259 |
] ), |
| 260 |
Key_Value_Prop_Type::generate( [ |
| 261 |
'key' => String_Prop_Type::generate( 'data-id' ), |
| 262 |
'value' => $this->get_id(), |
| 263 |
] ), |
| 264 |
] ); |
| 265 |
} |
| 266 |
|
| 267 |
public function get_atomic_setting( string $key ) { |
| 268 |
$schema = static::get_props_schema(); |
| 269 |
|
| 270 |
if ( ! isset( $schema[ $key ] ) ) { |
| 271 |
return null; |
| 272 |
} |
| 273 |
|
| 274 |
$props = $this->get_settings(); |
| 275 |
$prop_value = $props[ $key ] ?? null; |
| 276 |
|
| 277 |
$single_schema = [ $key => $schema[ $key ] ]; |
| 278 |
$single_props = [ $key => $prop_value ]; |
| 279 |
|
| 280 |
$resolved = Render_Props_Resolver::for_settings()->resolve( $single_schema, $single_props ); |
| 281 |
|
| 282 |
return $resolved[ $key ] ?? null; |
| 283 |
} |
| 284 |
|
| 285 |
protected function parse_editor_settings( array $data ): array { |
| 286 |
$editor_data = []; |
| 287 |
|
| 288 |
if ( isset( $data['title'] ) && is_string( $data['title'] ) ) { |
| 289 |
$editor_data['title'] = sanitize_text_field( $data['title'] ); |
| 290 |
} |
| 291 |
|
| 292 |
return $editor_data; |
| 293 |
} |
| 294 |
|
| 295 |
public static function get_props_schema(): array { |
| 296 |
$schema = static::define_props_schema(); |
| 297 |
$schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); |
| 298 |
|
| 299 |
return apply_filters( |
| 300 |
'elementor/atomic-widgets/props-schema', |
| 301 |
$schema |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
protected function set_render_context( array $context_pairs ): void { |
| 306 |
foreach ( $context_pairs as $context_pair ) { |
| 307 |
$context_key = $context_pair['context_key'] ?? static::class; |
| 308 |
$context = $context_pair['context']; |
| 309 |
Render_Context::push( $context_key, $context ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
protected function clear_render_context( array $context_pairs ): void { |
| 314 |
foreach ( $context_pairs as $context_pair ) { |
| 315 |
$context_key = $context_pair['context_key'] ?? static::class; |
| 316 |
Render_Context::pop( $context_key ); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
public function print_content() { |
| 321 |
$defined_context = $this->define_render_context(); |
| 322 |
|
| 323 |
if ( empty( $defined_context ) ) { |
| 324 |
return parent::print_content(); |
| 325 |
} |
| 326 |
|
| 327 |
$this->set_render_context( $defined_context ); |
| 328 |
|
| 329 |
parent::print_content(); |
| 330 |
|
| 331 |
$this->clear_render_context( $defined_context ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Define the context for element's Render_Context. |
| 336 |
* |
| 337 |
* @return array Array of context pairs. Each pair is an associative array with: |
| 338 |
* - 'context_key' (optional): The context key. Defaults to static::class if not provided. |
| 339 |
* - 'context' (required): The context value (can be any type). |
| 340 |
* |
| 341 |
* @example |
| 342 |
* [ |
| 343 |
* [ |
| 344 |
* 'context_key' => 'custom-key', |
| 345 |
* 'context' => ['some' => 'data'], |
| 346 |
* ], |
| 347 |
* [ |
| 348 |
* 'context' => ['instance_id' => $this->get_id()], |
| 349 |
* ], |
| 350 |
* ] |
| 351 |
*/ |
| 352 |
protected function define_render_context(): array { |
| 353 |
return []; |
| 354 |
} |
| 355 |
|
| 356 |
protected function get_link_attributes( $link_settings, $add_key_to_result = false ) { |
| 357 |
$tag = $link_settings['tag'] ?? Link_Prop_Type::DEFAULT_TAG; |
| 358 |
$href = $link_settings['href']; |
| 359 |
$target = $link_settings['target'] ?? '_self'; |
| 360 |
|
| 361 |
$is_button = 'button' === $tag; |
| 362 |
$href_attribute_key = $is_button ? 'data-action-link' : 'href'; |
| 363 |
|
| 364 |
$result = [ |
| 365 |
$href_attribute_key => $href, |
| 366 |
'target' => $target, |
| 367 |
]; |
| 368 |
|
| 369 |
if ( $add_key_to_result ) { |
| 370 |
$result['key'] = $href_attribute_key; |
| 371 |
} |
| 372 |
|
| 373 |
return $result; |
| 374 |
} |
| 375 |
} |
| 376 |
|