| 1 |
<?php |
| 2 |
namespace Elementor\Modules\AtomicWidgets\Base; |
| 3 |
|
| 4 |
use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 5 |
use Elementor\Widget_Base; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
abstract class Atomic_Widget_Base extends Widget_Base { |
| 12 |
protected $version = '0.0'; |
| 13 |
protected $styles = []; |
| 14 |
|
| 15 |
public function __construct( $data = [], $args = null ) { |
| 16 |
parent::__construct( $data, $args ); |
| 17 |
|
| 18 |
$this->version = $data['version'] ?? '0.0'; |
| 19 |
$this->styles = $data['styles'] ?? []; |
| 20 |
} |
| 21 |
|
| 22 |
public function get_atomic_controls() { |
| 23 |
$controls = $this->define_atomic_controls(); |
| 24 |
|
| 25 |
return $this->get_valid_controls( $controls ); |
| 26 |
} |
| 27 |
|
| 28 |
private function get_valid_controls( array $controls ): array { |
| 29 |
$valid_controls = []; |
| 30 |
$schema = static::get_props_schema(); |
| 31 |
|
| 32 |
foreach ( $controls as $control ) { |
| 33 |
if ( $control instanceof Section ) { |
| 34 |
$cloned_section = clone $control; |
| 35 |
|
| 36 |
$cloned_section->set_items( |
| 37 |
$this->get_valid_controls( $control->get_items() ) |
| 38 |
); |
| 39 |
|
| 40 |
$valid_controls[] = $cloned_section; |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! ( $control instanceof Atomic_Control_Base ) ) { |
| 45 |
$this->safe_throw( 'Control must be an instance of `Atomic_Control_Base`.' ); |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
$prop_name = $control->get_bind(); |
| 50 |
|
| 51 |
if ( ! $prop_name ) { |
| 52 |
$this->safe_throw( 'Control is missing a bound prop from the schema.' ); |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! array_key_exists( $prop_name, $schema ) ) { |
| 57 |
$this->safe_throw( "Prop `{$prop_name}` is not defined in the schema of `{$this->get_name()}`. Did you forget to define it?" ); |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$valid_controls[] = $control; |
| 62 |
} |
| 63 |
|
| 64 |
return $valid_controls; |
| 65 |
} |
| 66 |
|
| 67 |
private function safe_throw( string $message ) { |
| 68 |
if ( ! defined( 'ELEMENTOR_DEBUG' ) || ! ELEMENTOR_DEBUG ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
throw new \Exception( $message ); |
| 73 |
} |
| 74 |
|
| 75 |
abstract protected function define_atomic_controls(): array; |
| 76 |
|
| 77 |
final public function get_controls( $control_id = null ) { |
| 78 |
if ( ! empty( $control_id ) ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
return []; |
| 83 |
} |
| 84 |
|
| 85 |
final public function get_initial_config() { |
| 86 |
$config = parent::get_initial_config(); |
| 87 |
|
| 88 |
$config['atomic_controls'] = $this->get_atomic_controls(); |
| 89 |
$config['version'] = $this->version; |
| 90 |
|
| 91 |
return $config; |
| 92 |
} |
| 93 |
|
| 94 |
final public function get_data_for_save() { |
| 95 |
$data = parent::get_data_for_save(); |
| 96 |
|
| 97 |
$data['version'] = $this->version; |
| 98 |
|
| 99 |
return $data; |
| 100 |
} |
| 101 |
|
| 102 |
final public function get_raw_data( $with_html_content = false ) { |
| 103 |
$raw_data = parent::get_raw_data( $with_html_content ); |
| 104 |
|
| 105 |
$raw_data['styles'] = $this->styles; |
| 106 |
|
| 107 |
return $raw_data; |
| 108 |
} |
| 109 |
|
| 110 |
final public function get_stack( $with_common_controls = true ) { |
| 111 |
return [ |
| 112 |
'controls' => [], |
| 113 |
'tabs' => [], |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
final public function get_atomic_settings(): array { |
| 118 |
$schema = static::get_props_schema(); |
| 119 |
$raw_settings = $this->get_settings(); |
| 120 |
|
| 121 |
$transformed_settings = []; |
| 122 |
|
| 123 |
foreach ( $schema as $key => $prop ) { |
| 124 |
if ( array_key_exists( $key, $raw_settings ) ) { |
| 125 |
$transformed_settings[ $key ] = $raw_settings[ $key ]; |
| 126 |
} else { |
| 127 |
$transformed_settings[ $key ] = $prop->get_default(); |
| 128 |
} |
| 129 |
|
| 130 |
$transformed_settings[ $key ] = $this->transform_setting( $transformed_settings[ $key ] ); |
| 131 |
} |
| 132 |
|
| 133 |
return $transformed_settings; |
| 134 |
} |
| 135 |
|
| 136 |
public static function get_props_schema() { |
| 137 |
return static::define_props_schema(); |
| 138 |
} |
| 139 |
|
| 140 |
private function transform_setting( $setting ) { |
| 141 |
if ( ! $this->is_transformable( $setting ) ) { |
| 142 |
return $setting; |
| 143 |
} |
| 144 |
|
| 145 |
switch ( $setting['$$type'] ) { |
| 146 |
case 'classes': |
| 147 |
return is_array( $setting['value'] ) |
| 148 |
? join( ' ', $setting['value'] ) |
| 149 |
: ''; |
| 150 |
|
| 151 |
default: |
| 152 |
return null; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private function is_transformable( $setting ): bool { |
| 157 |
return ! empty( $setting['$$type'] ) && 'string' === getType( $setting['$$type'] ) && isset( $setting['value'] ); |
| 158 |
} |
| 159 |
|
| 160 |
abstract protected static function define_props_schema(): array; |
| 161 |
} |
| 162 |
|