first commit
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
use Elementor\Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Loader\Frontend_Assets_Loader;
|
||||
use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns\Has_Meta;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
abstract class Atomic_Element_Base extends Element_Base {
|
||||
use Has_Atomic_Base;
|
||||
use Has_Meta;
|
||||
|
||||
protected $version = '0.0';
|
||||
protected $styles = [];
|
||||
protected $interactions = [];
|
||||
protected $editor_settings = [];
|
||||
|
||||
public static $widget_description = null;
|
||||
|
||||
|
||||
public function __construct( $data = [], $args = null ) {
|
||||
parent::__construct( $data, $args );
|
||||
|
||||
$this->version = $data['version'] ?? '0.0';
|
||||
$this->styles = $data['styles'] ?? [];
|
||||
$this->interactions = $this->parse_atomic_interactions( $data['interactions'] ?? [] );
|
||||
$this->editor_settings = $data['editor_settings'] ?? [];
|
||||
$this->add_script_depends( Frontend_Assets_Loader::ATOMIC_WIDGETS_HANDLER );
|
||||
if ( static::$widget_description ) {
|
||||
$this->description( static::$widget_description );
|
||||
}
|
||||
}
|
||||
|
||||
private function parse_atomic_interactions( $interactions ) {
|
||||
if ( empty( $interactions ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( is_string( $interactions ) ) {
|
||||
$decoded = json_decode( $interactions, true );
|
||||
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
|
||||
$interactions = $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $interactions ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $interactions;
|
||||
}
|
||||
|
||||
private function convert_prop_type_interactions_to_legacy_for_runtime( $interactions ) {
|
||||
$legacy_items = [];
|
||||
|
||||
foreach ( $interactions['items'] as $item ) {
|
||||
if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) {
|
||||
$legacy_item = $this->extract_legacy_interaction_from_prop_type( $item );
|
||||
if ( $legacy_item ) {
|
||||
$legacy_items[] = $legacy_item;
|
||||
}
|
||||
} else {
|
||||
$legacy_items[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'version' => $interactions['version'] ?? 1,
|
||||
'items' => $legacy_items,
|
||||
];
|
||||
}
|
||||
|
||||
abstract protected function define_atomic_controls(): array;
|
||||
|
||||
protected function define_atomic_style_states(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_global_scripts() {
|
||||
return [];
|
||||
}
|
||||
|
||||
final public function get_initial_config() {
|
||||
$config = parent::get_initial_config();
|
||||
$props_schema = static::get_props_schema();
|
||||
|
||||
$config['atomic_controls'] = $this->get_atomic_controls();
|
||||
$config['atomic_props_schema'] = $props_schema;
|
||||
$config['atomic_style_states'] = $this->define_atomic_style_states();
|
||||
$config['dependencies_per_target_mapping'] = Dependency_Manager::get_source_to_dependents( $props_schema );
|
||||
$config['base_styles'] = $this->get_base_styles();
|
||||
$config['version'] = $this->version;
|
||||
$config['show_in_panel'] = $this->should_show_in_panel();
|
||||
$config['categories'] = [ 'v4-elements' ];
|
||||
$config['hide_on_search'] = false;
|
||||
$config['controls'] = [];
|
||||
$config['keywords'] = $this->get_keywords();
|
||||
$config['default_children'] = $this->define_default_children();
|
||||
$config['initial_attributes'] = $this->define_initial_attributes();
|
||||
$config['include_in_widgets_config'] = true;
|
||||
$config['default_html_tag'] = $this->define_default_html_tag();
|
||||
$config['meta'] = $this->get_meta();
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
protected function should_show_in_panel() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function define_default_children() {
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function define_default_html_tag() {
|
||||
return 'div';
|
||||
}
|
||||
|
||||
protected function define_initial_attributes() {
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function add_render_attributes() {
|
||||
parent::add_render_attributes();
|
||||
|
||||
$interaction_ids = $this->get_interactions_ids();
|
||||
|
||||
if ( ! empty( $interaction_ids ) ) {
|
||||
$this->add_render_attribute( '_wrapper', 'data-interaction-id', $this->get_id() );
|
||||
$this->add_render_attribute( '_wrapper', 'data-interactions', json_encode( $interaction_ids ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Element keywords.
|
||||
*
|
||||
* Retrieve the element keywords.
|
||||
*
|
||||
* @since 3.29
|
||||
* @access public
|
||||
*
|
||||
* @return array Element keywords.
|
||||
*/
|
||||
public function get_keywords() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Prop_Type>
|
||||
*/
|
||||
abstract protected static function define_props_schema(): array;
|
||||
|
||||
/**
|
||||
* Get the HTML tag for rendering.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_html_tag(): string {
|
||||
$settings = $this->get_atomic_settings();
|
||||
$default_html_tag = $this->define_default_html_tag();
|
||||
|
||||
return ! empty( $settings['link']['href'] ) ? $settings['link']['tag'] : ( $settings['tag'] ?? $default_html_tag );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print safe HTML tag for the element based on the element settings.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function print_html_tag() {
|
||||
$html_tag = $this->get_html_tag();
|
||||
Utils::print_validated_html_tag( $html_tag );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print custom attributes if they exist.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function print_custom_attributes() {
|
||||
$settings = $this->get_atomic_settings();
|
||||
$attributes = $settings['attributes'] ?? '';
|
||||
if ( ! empty( $attributes ) && is_string( $attributes ) ) {
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo ' ' . $attributes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default child type for container elements.
|
||||
*
|
||||
* @param array $element_data
|
||||
* @return mixed
|
||||
*/
|
||||
protected function _get_default_child_type( array $element_data ) {
|
||||
$el_types = array_keys( Plugin::$instance->elements_manager->get_element_types() );
|
||||
|
||||
if ( in_array( $element_data['elType'], $el_types, true ) ) {
|
||||
return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] );
|
||||
}
|
||||
|
||||
if ( ! isset( $element_data['widgetType'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default before render for container elements.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function before_render() {
|
||||
?>
|
||||
<<?php $this->print_html_tag(); ?> <?php $this->print_render_attribute_string( '_wrapper' );
|
||||
$this->print_custom_attributes(); ?>>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Default after render for container elements.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function after_render() {
|
||||
?>
|
||||
</<?php $this->print_html_tag(); ?>>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Default content template - can be overridden by elements that need custom templates.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function content_template() {
|
||||
?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function generate() {
|
||||
return Element_Builder::make( static::get_type() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Loader\Frontend_Assets_Loader;
|
||||
use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns\Has_Meta;
|
||||
use Elementor\Widget_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
abstract class Atomic_Widget_Base extends Widget_Base {
|
||||
use Has_Atomic_Base;
|
||||
use Has_Meta;
|
||||
|
||||
public static $widget_description = null;
|
||||
|
||||
protected $version = '0.0';
|
||||
protected $styles = [];
|
||||
protected $interactions = [];
|
||||
protected $editor_settings = [];
|
||||
|
||||
public function __construct( $data = [], $args = null ) {
|
||||
parent::__construct( $data, $args );
|
||||
|
||||
$this->version = $data['version'] ?? '0.0';
|
||||
$this->styles = $data['styles'] ?? [];
|
||||
$this->interactions = $this->parse_atomic_interactions( $data['interactions'] ?? [] );
|
||||
$this->editor_settings = $data['editor_settings'] ?? [];
|
||||
if ( static::$widget_description ) {
|
||||
$this->description( static::$widget_description );
|
||||
}
|
||||
}
|
||||
|
||||
private function parse_atomic_interactions( $interactions ) {
|
||||
if ( empty( $interactions ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( is_string( $interactions ) ) {
|
||||
$decoded = json_decode( $interactions, true );
|
||||
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
|
||||
$interactions = $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $interactions ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $interactions;
|
||||
}
|
||||
|
||||
private function convert_prop_type_interactions_to_legacy_for_runtime( $interactions ) {
|
||||
$legacy_items = [];
|
||||
|
||||
foreach ( $interactions['items'] as $item ) {
|
||||
if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) {
|
||||
$legacy_item = $this->extract_legacy_interaction_from_prop_type( $item );
|
||||
if ( $legacy_item ) {
|
||||
$legacy_items[] = $legacy_item;
|
||||
}
|
||||
} else {
|
||||
$legacy_items[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'version' => $interactions['version'] ?? 1,
|
||||
'items' => $legacy_items,
|
||||
];
|
||||
}
|
||||
|
||||
abstract protected function define_atomic_controls(): array;
|
||||
|
||||
public function get_global_scripts() {
|
||||
return [];
|
||||
}
|
||||
|
||||
public function get_initial_config() {
|
||||
$config = parent::get_initial_config();
|
||||
$props_schema = static::get_props_schema();
|
||||
|
||||
$config['atomic'] = true;
|
||||
$config['atomic_controls'] = $this->get_atomic_controls();
|
||||
$config['base_styles'] = $this->get_base_styles();
|
||||
$config['base_styles_dictionary'] = $this->get_base_styles_dictionary();
|
||||
$config['atomic_props_schema'] = $props_schema;
|
||||
$config['dependencies_per_target_mapping'] = Dependency_Manager::get_source_to_dependents( $props_schema );
|
||||
$config['version'] = $this->version;
|
||||
$config['meta'] = $this->get_meta();
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
public function get_categories(): array {
|
||||
return [ 'v4-elements' ];
|
||||
}
|
||||
|
||||
public function before_render() {}
|
||||
|
||||
public function after_render() {}
|
||||
|
||||
abstract protected static function define_props_schema(): array;
|
||||
|
||||
public static function generate() {
|
||||
return Widget_Builder::make( static::get_element_type() );
|
||||
}
|
||||
|
||||
public function get_script_depends() {
|
||||
return [ Frontend_Assets_Loader::ATOMIC_WIDGETS_HANDLER ];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
class Element_Builder {
|
||||
protected $element_type;
|
||||
protected $settings = [];
|
||||
protected $is_locked = false;
|
||||
protected $children = [];
|
||||
protected $editor_settings = [];
|
||||
|
||||
public static function make( string $element_type ) {
|
||||
return new self( $element_type );
|
||||
}
|
||||
|
||||
private function __construct( string $element_type ) {
|
||||
$this->element_type = $element_type;
|
||||
}
|
||||
|
||||
public function settings( array $settings ) {
|
||||
$this->settings = $settings;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function is_locked( $is_locked ) {
|
||||
$this->is_locked = $is_locked;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function editor_settings( array $editor_settings ) {
|
||||
$this->editor_settings = $editor_settings;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function children( array $children ) {
|
||||
$this->children = $children;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build() {
|
||||
$element_data = [
|
||||
'elType' => $this->element_type,
|
||||
'settings' => $this->settings,
|
||||
'isLocked' => $this->is_locked,
|
||||
'editor_settings' => $this->editor_settings,
|
||||
'elements' => $this->children,
|
||||
];
|
||||
|
||||
return $element_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
use Elementor\Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Controls\Base\Atomic_Control_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Controls\Section;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
|
||||
use Elementor\Modules\AtomicWidgets\Parsers\Props_Parser;
|
||||
use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
|
||||
use Elementor\Utils;
|
||||
use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\Styles\Atomic_Widget_Styles;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* @mixin Element_Base
|
||||
*/
|
||||
trait Has_Atomic_Base {
|
||||
use Has_Base_Styles;
|
||||
|
||||
public function has_widget_inner_wrapper(): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
abstract public static function get_element_type(): string;
|
||||
|
||||
final public function get_name() {
|
||||
return static::get_element_type();
|
||||
}
|
||||
|
||||
private function get_valid_controls( array $schema, array $controls ): array {
|
||||
$valid_controls = [];
|
||||
|
||||
foreach ( $controls as $control ) {
|
||||
if ( $control instanceof Section ) {
|
||||
$cloned_section = clone $control;
|
||||
|
||||
$cloned_section->set_items(
|
||||
$this->get_valid_controls( $schema, $control->get_items() )
|
||||
);
|
||||
|
||||
$valid_controls[] = $cloned_section;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ( $control instanceof Atomic_Control_Base ) ) {
|
||||
$prop_name = $control->get_bind();
|
||||
|
||||
if ( ! $prop_name ) {
|
||||
Utils::safe_throw( 'Control is missing a bound prop from the schema.' );
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( $prop_name, $schema ) ) {
|
||||
Utils::safe_throw( "Prop `{$prop_name}` is not defined in the schema of `{$this->get_name()}`." );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$valid_controls[] = $control;
|
||||
}
|
||||
|
||||
return $valid_controls;
|
||||
}
|
||||
|
||||
private static function validate_schema( array $schema ) {
|
||||
$widget_name = static::class;
|
||||
|
||||
foreach ( $schema as $key => $prop ) {
|
||||
if ( ! ( $prop instanceof Prop_Type ) ) {
|
||||
Utils::safe_throw( "Prop `$key` must be an instance of `Prop_Type` in `{$widget_name}`." );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function parse_atomic_styles( array $data ): array {
|
||||
$styles = $data['styles'] ?? [];
|
||||
$style_parser = Style_Parser::make( Style_Schema::get() );
|
||||
|
||||
foreach ( $styles as $style_id => $style ) {
|
||||
$result = $style_parser->parse( $style );
|
||||
|
||||
if ( ! $result->is_valid() ) {
|
||||
$widget_id = $data['id'] ?? 'unknown';
|
||||
throw new \Exception( esc_html( "Styles validation failed for style `$style_id`. Widget ID: `$widget_id`. " . $result->errors()->to_string() ) );
|
||||
}
|
||||
|
||||
$styles[ $style_id ] = $result->unwrap();
|
||||
}
|
||||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
private function parse_atomic_settings( array $settings ): array {
|
||||
$schema = static::get_props_schema();
|
||||
$props_parser = Props_Parser::make( $schema );
|
||||
|
||||
$result = $props_parser->parse( $settings );
|
||||
|
||||
if ( ! $result->is_valid() ) {
|
||||
throw new \Exception( esc_html( 'Settings validation failed. ' . $result->errors()->to_string() ) );
|
||||
}
|
||||
|
||||
return $result->unwrap();
|
||||
}
|
||||
|
||||
private function parse_atomic_interactions( $interactions ) {
|
||||
|
||||
if ( empty( $interactions ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ( is_string( $interactions ) ) {
|
||||
$decoded = json_decode( $interactions, true );
|
||||
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_array( $interactions ) ) {
|
||||
return $interactions;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private function convert_prop_type_interactions_to_legacy( $interactions ) {
|
||||
$legacy_items = [];
|
||||
|
||||
foreach ( $interactions['items'] as $item ) {
|
||||
if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) {
|
||||
$legacy_item = $this->extract_legacy_interaction_from_prop_type( $item );
|
||||
if ( $legacy_item ) {
|
||||
$legacy_items[] = $legacy_item;
|
||||
}
|
||||
} else {
|
||||
$legacy_items[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'version' => $interactions['version'] ?? 1,
|
||||
'items' => $legacy_items,
|
||||
];
|
||||
}
|
||||
|
||||
private function extract_legacy_interaction_from_prop_type( $item ) {
|
||||
if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$item_value = $item['value'];
|
||||
|
||||
$interaction_id = $this->extract_prop_value( $item_value, 'interaction_id' );
|
||||
$trigger = $this->extract_prop_value( $item_value, 'trigger' );
|
||||
$animation = $this->extract_prop_value( $item_value, 'animation' );
|
||||
|
||||
if ( ! is_array( $animation ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$effect = $this->extract_prop_value( $animation, 'effect' );
|
||||
$type = $this->extract_prop_value( $animation, 'type' );
|
||||
$direction = $this->extract_prop_value( $animation, 'direction' );
|
||||
$timing_config = $this->extract_prop_value( $animation, 'timing_config' );
|
||||
|
||||
$duration = 300;
|
||||
$delay = 0;
|
||||
|
||||
if ( is_array( $timing_config ) ) {
|
||||
$duration = $this->extract_prop_value( $timing_config, 'duration', 300 );
|
||||
$delay = $this->extract_prop_value( $timing_config, 'delay', 0 );
|
||||
}
|
||||
|
||||
$animation_id = implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay ] );
|
||||
|
||||
return [
|
||||
'interaction_id' => $interaction_id,
|
||||
'animation' => [
|
||||
'animation_id' => $animation_id,
|
||||
'animation_type' => 'full-preset',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function extract_prop_value( $data, $key, $default = '' ) {
|
||||
if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$value = $data[ $key ];
|
||||
|
||||
if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) {
|
||||
return $value['value'];
|
||||
}
|
||||
|
||||
return null !== $value ? $value : $default;
|
||||
}
|
||||
|
||||
public function get_atomic_controls() {
|
||||
$controls = apply_filters(
|
||||
'elementor/atomic-widgets/controls',
|
||||
$this->define_atomic_controls(),
|
||||
$this
|
||||
);
|
||||
|
||||
$schema = static::get_props_schema();
|
||||
|
||||
// Validate the schema only in the Editor.
|
||||
static::validate_schema( $schema );
|
||||
|
||||
return $this->get_valid_controls( $schema, $controls );
|
||||
}
|
||||
|
||||
protected function get_css_id_control_meta(): array {
|
||||
return [
|
||||
'layout' => 'two-columns',
|
||||
'topDivider' => true,
|
||||
];
|
||||
}
|
||||
|
||||
final public function get_controls( $control_id = null ) {
|
||||
if ( ! empty( $control_id ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
final public function get_data_for_save() {
|
||||
$data = parent::get_data_for_save();
|
||||
|
||||
$data['version'] = $this->version;
|
||||
$data['settings'] = $this->parse_atomic_settings( $data['settings'] );
|
||||
$data['styles'] = $this->parse_atomic_styles( $data );
|
||||
$data['editor_settings'] = $this->parse_editor_settings( $data['editor_settings'] );
|
||||
|
||||
if ( isset( $data['interactions'] ) && ! empty( $data['interactions'] ) ) {
|
||||
$data['interactions'] = $this->transform_interactions_for_save( $data['interactions'] );
|
||||
} else {
|
||||
$data['interactions'] = [];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function transform_interactions_for_save( $interactions ) {
|
||||
$decoded = $this->decode_interactions_data( $interactions );
|
||||
|
||||
if ( empty( $decoded['items'] ) ) {
|
||||
return [];
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
private function decode_interactions_data( $interactions ) {
|
||||
if ( is_array( $interactions ) ) {
|
||||
return $interactions;
|
||||
}
|
||||
|
||||
if ( is_string( $interactions ) ) {
|
||||
$decoded = json_decode( $interactions, true );
|
||||
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'items' => [],
|
||||
'version' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
final public function get_raw_data( $with_html_content = false ) {
|
||||
$raw_data = parent::get_raw_data( $with_html_content );
|
||||
|
||||
$raw_data['styles'] = Atomic_Widget_Styles::get_license_based_filtered_styles( $this->styles ?? [] );
|
||||
$raw_data['interactions'] = $this->interactions ?? [];
|
||||
$raw_data['editor_settings'] = $this->editor_settings;
|
||||
|
||||
return $raw_data;
|
||||
}
|
||||
|
||||
final public function get_stack( $with_common_controls = true ) {
|
||||
return [
|
||||
'controls' => [],
|
||||
'tabs' => [],
|
||||
];
|
||||
}
|
||||
|
||||
public function get_atomic_settings(): array {
|
||||
$schema = static::get_props_schema();
|
||||
$props = $this->get_settings();
|
||||
$initial_attributes = $this->get_initial_attributes();
|
||||
|
||||
$props['attributes'] = Attributes_Prop_Type::generate( array_merge(
|
||||
$initial_attributes['value'] ?? [],
|
||||
$props['attributes']['value'] ?? []
|
||||
) );
|
||||
|
||||
return Render_Props_Resolver::for_settings()->resolve( $schema, $props );
|
||||
}
|
||||
|
||||
protected function get_initial_attributes() {
|
||||
return Attributes_Prop_Type::generate( [
|
||||
Key_Value_Prop_Type::generate( [
|
||||
'key' => String_Prop_Type::generate( 'data-e-type' ),
|
||||
'value' => $this->get_type(),
|
||||
] ),
|
||||
Key_Value_Prop_Type::generate( [
|
||||
'key' => String_Prop_Type::generate( 'data-id' ),
|
||||
'value' => $this->get_id(),
|
||||
] ),
|
||||
] );
|
||||
}
|
||||
|
||||
public function get_atomic_setting( string $key ) {
|
||||
$schema = static::get_props_schema();
|
||||
|
||||
if ( ! isset( $schema[ $key ] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$props = $this->get_settings();
|
||||
$prop_value = $props[ $key ] ?? null;
|
||||
|
||||
$single_schema = [ $key => $schema[ $key ] ];
|
||||
$single_props = [ $key => $prop_value ];
|
||||
|
||||
$resolved = Render_Props_Resolver::for_settings()->resolve( $single_schema, $single_props );
|
||||
|
||||
return $resolved[ $key ] ?? null;
|
||||
}
|
||||
|
||||
protected function parse_editor_settings( array $data ): array {
|
||||
$editor_data = [];
|
||||
|
||||
if ( isset( $data['title'] ) && is_string( $data['title'] ) ) {
|
||||
$editor_data['title'] = sanitize_text_field( $data['title'] );
|
||||
}
|
||||
|
||||
return $editor_data;
|
||||
}
|
||||
|
||||
public static function get_props_schema(): array {
|
||||
$schema = static::define_props_schema();
|
||||
$schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() );
|
||||
|
||||
return apply_filters(
|
||||
'elementor/atomic-widgets/props-schema',
|
||||
$schema
|
||||
);
|
||||
}
|
||||
|
||||
public function get_interactions_ids() {
|
||||
$animation_ids = [];
|
||||
|
||||
$list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) )
|
||||
? $this->interactions['items']
|
||||
: [];
|
||||
|
||||
foreach ( $list_of_interactions as $interaction ) {
|
||||
if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) {
|
||||
$animation_id = $this->extract_animation_id_from_prop_type( $interaction );
|
||||
if ( $animation_id ) {
|
||||
$animation_ids[] = $animation_id;
|
||||
}
|
||||
} elseif ( isset( $interaction['animation']['animation_id'] ) ) {
|
||||
$animation_ids[] = $interaction['animation']['animation_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $animation_ids;
|
||||
}
|
||||
|
||||
private function extract_animation_id_from_prop_type( $item ) {
|
||||
if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$item_value = $item['value'];
|
||||
|
||||
$trigger = $this->extract_prop_value( $item_value, 'trigger' );
|
||||
$animation = $this->extract_prop_value( $item_value, 'animation' );
|
||||
|
||||
if ( ! is_array( $animation ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$effect = $this->extract_prop_value( $animation, 'effect' );
|
||||
$type = $this->extract_prop_value( $animation, 'type' );
|
||||
$direction = $this->extract_prop_value( $animation, 'direction' );
|
||||
$timing_config = $this->extract_prop_value( $animation, 'timing_config' );
|
||||
$config = $this->extract_prop_value( $animation, 'config' );
|
||||
|
||||
$duration = 300;
|
||||
$delay = 0;
|
||||
$replay = 0;
|
||||
$relative_to = 'viewport';
|
||||
$offset_top = 15;
|
||||
$offset_bottom = 85;
|
||||
|
||||
if ( is_array( $timing_config ) ) {
|
||||
$duration = $this->extract_prop_value( $timing_config, 'duration', 300 );
|
||||
$delay = $this->extract_prop_value( $timing_config, 'delay', 0 );
|
||||
}
|
||||
|
||||
if ( is_array( $config ) ) {
|
||||
$relative_to = $this->extract_prop_value( $config, 'relative_to', 'viewport' );
|
||||
$offset_top = $this->extract_prop_value( $config, 'offset_top', 15 );
|
||||
$offset_bottom = $this->extract_prop_value( $config, 'offset_bottom', 85 );
|
||||
|
||||
$replay = $this->extract_prop_value( $config, 'replay', 0 );
|
||||
if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) {
|
||||
$replay = 0;
|
||||
}
|
||||
} else {
|
||||
$replay = 0;
|
||||
}
|
||||
|
||||
return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] );
|
||||
}
|
||||
|
||||
public function print_content() {
|
||||
$defined_context = $this->define_render_context();
|
||||
|
||||
$context_key = $defined_context['context_key'] ?? static::class;
|
||||
$element_context = $defined_context['context'] ?? [];
|
||||
|
||||
$has_context = ! empty( $element_context );
|
||||
|
||||
if ( ! $has_context ) {
|
||||
return parent::print_content();
|
||||
}
|
||||
|
||||
Render_Context::push( $context_key, $element_context );
|
||||
|
||||
parent::print_content();
|
||||
|
||||
Render_Context::pop( $context_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the context for element's Render_Context.
|
||||
*
|
||||
* @return array{context_key: ?string, context: array}
|
||||
*/
|
||||
protected function define_render_context(): array {
|
||||
return [
|
||||
'context_key' => null,
|
||||
'context' => [],
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_link_attributes( $link_settings, $add_key_to_result = false ) {
|
||||
$tag = $link_settings['tag'] ?? Link_Prop_Type::DEFAULT_TAG;
|
||||
$href = $link_settings['href'];
|
||||
$target = $link_settings['target'] ?? '_self';
|
||||
|
||||
$is_button = 'button' === $tag;
|
||||
$href_attribute_key = $is_button ? 'data-action-link' : 'href';
|
||||
|
||||
$result = [
|
||||
$href_attribute_key => $href,
|
||||
'target' => $target,
|
||||
];
|
||||
|
||||
if ( $add_key_to_result ) {
|
||||
$result['key'] = $href_attribute_key;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* @mixin Has_Atomic_Base
|
||||
*/
|
||||
trait Has_Base_Styles {
|
||||
public function get_base_styles() {
|
||||
$base_styles = $this->define_base_styles();
|
||||
$style_definitions = [];
|
||||
|
||||
foreach ( $base_styles as $key => $style ) {
|
||||
$id = $this->generate_base_style_id( $key );
|
||||
|
||||
$style_definitions[ $id ] = $style->build( $id );
|
||||
}
|
||||
|
||||
return $style_definitions;
|
||||
}
|
||||
|
||||
public function get_base_styles_dictionary() {
|
||||
$result = [];
|
||||
|
||||
$base_styles = array_keys( $this->define_base_styles() );
|
||||
|
||||
foreach ( $base_styles as $key ) {
|
||||
$result[ $key ] = $this->generate_base_style_id( $key );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function generate_base_style_id( string $key ): string {
|
||||
return static::get_element_type() . '-' . $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Style_Definition>
|
||||
*/
|
||||
protected function define_base_styles(): array {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\Elements\TemplateRenderer\Template_Renderer;
|
||||
use Elementor\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* @mixin Has_Atomic_Base
|
||||
*/
|
||||
trait Has_Template {
|
||||
|
||||
public function get_initial_config() {
|
||||
$config = parent::get_initial_config();
|
||||
|
||||
$config['twig_main_template'] = $this->get_main_template();
|
||||
$config['twig_templates'] = $this->get_templates_contents();
|
||||
return $config;
|
||||
}
|
||||
|
||||
protected function render() {
|
||||
try {
|
||||
$renderer = Template_Renderer::instance();
|
||||
|
||||
foreach ( $this->get_templates() as $name => $path ) {
|
||||
if ( $renderer->is_registered( $name ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$renderer->register( $name, $path );
|
||||
}
|
||||
|
||||
$context = [
|
||||
'id' => $this->get_id(),
|
||||
'type' => $this->get_name(),
|
||||
'settings' => $this->get_atomic_settings(),
|
||||
'base_styles' => $this->get_base_styles_dictionary(),
|
||||
'interactions' => $this->get_interactions_ids(),
|
||||
];
|
||||
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $renderer->render( $this->get_main_template(), $context );
|
||||
} catch ( \Exception $e ) {
|
||||
if ( Utils::is_elementor_debug() ) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function get_interactions_ids() {
|
||||
$animation_ids = [];
|
||||
|
||||
$list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) )
|
||||
? $this->interactions['items']
|
||||
: [];
|
||||
|
||||
foreach ( $list_of_interactions as $interaction ) {
|
||||
|
||||
if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) {
|
||||
$animation_id = $this->extract_animation_id_from_prop_type( $interaction );
|
||||
if ( $animation_id ) {
|
||||
$animation_ids[] = $animation_id;
|
||||
}
|
||||
} elseif ( isset( $interaction['animation']['animation_id'] ) ) {
|
||||
$animation_ids[] = $interaction['animation']['animation_id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $animation_ids;
|
||||
}
|
||||
|
||||
private function extract_animation_id_from_prop_type( $item ) {
|
||||
if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$item_value = $item['value'];
|
||||
|
||||
$trigger = $this->extract_prop_value_simple( $item_value, 'trigger' );
|
||||
$animation = $this->extract_prop_value_simple( $item_value, 'animation' );
|
||||
|
||||
if ( ! is_array( $animation ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$effect = $this->extract_prop_value_simple( $animation, 'effect' );
|
||||
$type = $this->extract_prop_value_simple( $animation, 'type' );
|
||||
$direction = $this->extract_prop_value_simple( $animation, 'direction' );
|
||||
$timing_config = $this->extract_prop_value_simple( $animation, 'timing_config' );
|
||||
$config = $this->extract_prop_value_simple( $animation, 'config' );
|
||||
|
||||
$duration = 300;
|
||||
$delay = 0;
|
||||
$replay = 0;
|
||||
$relative_to = 'viewport';
|
||||
$offset_top = 15;
|
||||
$offset_bottom = 85;
|
||||
|
||||
if ( is_array( $timing_config ) ) {
|
||||
$duration = $this->extract_prop_value_simple( $timing_config, 'duration', 300 );
|
||||
$delay = $this->extract_prop_value_simple( $timing_config, 'delay', 0 );
|
||||
}
|
||||
|
||||
if ( is_array( $config ) ) {
|
||||
$relative_to = $this->extract_prop_value_simple( $config, 'relative_to', 'viewport' );
|
||||
$offset_top = $this->extract_prop_value_simple( $config, 'offset_top', 15 );
|
||||
$offset_bottom = $this->extract_prop_value_simple( $config, 'offset_bottom', 85 );
|
||||
$replay = $this->extract_prop_value_simple( $config, 'replay', 0 );
|
||||
if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) {
|
||||
$replay = 0;
|
||||
}
|
||||
} else {
|
||||
$replay = 0;
|
||||
}
|
||||
return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] );
|
||||
}
|
||||
|
||||
private function extract_prop_value_simple( $data, $key, $default = '' ) {
|
||||
if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$value = $data[ $key ];
|
||||
|
||||
if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) {
|
||||
return $value['value'];
|
||||
}
|
||||
|
||||
return null !== $value ? $value : $default;
|
||||
}
|
||||
|
||||
protected function get_templates_contents() {
|
||||
return array_map(
|
||||
fn ( $path ) => Utils::file_get_contents( $path ),
|
||||
$this->get_templates()
|
||||
);
|
||||
}
|
||||
|
||||
protected function get_main_template() {
|
||||
$templates = $this->get_templates();
|
||||
|
||||
if ( count( $templates ) > 1 ) {
|
||||
Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' );
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ( $templates as $key => $path ) {
|
||||
// Returns first key in the array.
|
||||
return $key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
abstract protected function get_templates(): array;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Render_Context {
|
||||
private static $context_stack = [];
|
||||
|
||||
public static function push( string $key, array $context ): void {
|
||||
if ( ! self::get( $key ) ) {
|
||||
self::$context_stack[ $key ] = [];
|
||||
}
|
||||
|
||||
self::$context_stack[ $key ][] = $context;
|
||||
}
|
||||
|
||||
public static function pop( string $key ): void {
|
||||
if ( isset( self::$context_stack[ $key ] ) && ! empty( self::$context_stack[ $key ] ) ) {
|
||||
array_pop( self::$context_stack[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
public static function get( string $key ): array {
|
||||
if ( ! isset( self::$context_stack[ $key ] ) || empty( self::$context_stack[ $key ] ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$last_key = array_key_last( self::$context_stack[ $key ] );
|
||||
return self::$context_stack[ $key ][ $last_key ];
|
||||
}
|
||||
|
||||
public static function clear(): void {
|
||||
self::$context_stack = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Base;
|
||||
|
||||
class Widget_Builder {
|
||||
protected $widget_type;
|
||||
protected $settings = [];
|
||||
protected $is_locked = false;
|
||||
protected $editor_settings = [];
|
||||
|
||||
public static function make( string $widget_type ) {
|
||||
return new self( $widget_type );
|
||||
}
|
||||
|
||||
private function __construct( string $widget_type ) {
|
||||
$this->widget_type = $widget_type;
|
||||
}
|
||||
|
||||
public function settings( array $settings ) {
|
||||
$this->settings = $settings;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function is_locked( $is_locked ) {
|
||||
$this->is_locked = $is_locked;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function editor_settings( array $editor_settings ) {
|
||||
$this->editor_settings = $editor_settings;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build() {
|
||||
$widget_data = [
|
||||
'elType' => 'widget',
|
||||
'widgetType' => $this->widget_type,
|
||||
'settings' => $this->settings,
|
||||
'isLocked' => $this->is_locked,
|
||||
'editor_settings' => $this->editor_settings,
|
||||
];
|
||||
|
||||
return $widget_data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user