first commit

This commit is contained in:
2025-08-04 16:00:02 +02:00
commit 807c9b262f
7069 changed files with 2784666 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Base;
use JsonSerializable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Atomic_Control_Base implements JsonSerializable {
private string $bind;
private $label = null;
private $description = null;
private $meta = null;
abstract public function get_type(): string;
abstract public function get_props(): array;
public static function bind_to( string $prop_name ) {
return new static( $prop_name );
}
protected function __construct( string $prop_name ) {
$this->bind = $prop_name;
}
public function get_bind() {
return $this->bind;
}
public function set_label( string $label ): self {
$this->label = $label;
return $this;
}
public function set_description( string $description ): self {
$this->description = $description;
return $this;
}
public function set_meta( $meta ): self {
$this->meta = $meta;
return $this;
}
public function jsonSerialize(): array {
return [
'type' => 'control',
'value' => [
'type' => $this->get_type(),
'bind' => $this->get_bind(),
'label' => $this->label,
'description' => $this->description,
'props' => $this->get_props(),
'meta' => $this->meta,
],
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Style_Transformer_Base {
/**
* Get the transformer type.
*
* @return string
*/
abstract public static function type(): string;
/**
* Transform the value.
*
* @param mixed $value
*
* @return mixed
*/
abstract public function transform( $value, callable $transform );
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls;
use JsonSerializable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Section implements JsonSerializable {
private ?string $id = null;
private $label = null;
private $description = null;
private array $items = [];
public static function make(): self {
return new static();
}
public function set_id( string $id ): self {
$this->id = $id;
return $this;
}
public function get_id() {
return $this->id;
}
public function set_label( string $label ): self {
$this->label = $label;
return $this;
}
public function set_description( string $description ): self {
$this->description = $description;
return $this;
}
public function set_items( array $items ): self {
$this->items = $items;
return $this;
}
public function add_item( $item ): self {
$this->items[] = $item;
return $this;
}
public function get_items() {
return $this->items;
}
public function jsonSerialize(): array {
return [
'type' => 'section',
'value' => [
'label' => $this->label,
'description' => $this->description,
'items' => $this->items,
],
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
use Elementor\Modules\AtomicWidgets\Image\Image_Sizes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Control extends Atomic_Control_Base {
private string $show_mode = 'all';
public function set_show_mode( string $show_mode ): self {
$this->show_mode = $show_mode;
return $this;
}
public function get_type(): string {
return 'image';
}
public function get_props(): array {
return [
'sizes' => Image_Sizes::get_all(),
'showMode' => $this->show_mode,
];
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
use Elementor\Modules\WpRest\Classes\Post_Query;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Link_Control extends Atomic_Control_Base {
private bool $allow_custom_values = true;
private int $minimum_input_length = 2;
private ?string $placeholder = null;
private array $query_options = [
'endpoint' => '',
'requestParams' => [],
];
public static function bind_to( string $prop_name ) {
$instance = parent::bind_to( $prop_name );
$instance->set_placeholder( __( 'Type or paste your URL', 'elementor' ) );
$instance->set_endpoint( Post_Query::NAMESPACE . '/' . Post_Query::ENDPOINT );
$instance->set_request_params( Post_Query::build_query_params( [
Post_Query::POST_KEYS_CONVERSION_MAP => [
'ID' => 'id',
'post_title' => 'label',
'post_type' => 'groupLabel',
],
] ) );
return $instance;
}
public function get_type(): string {
return 'link';
}
public function set_placeholder( string $placeholder ): self {
$this->placeholder = $placeholder;
return $this;
}
public function get_props(): array {
return [
'placeholder' => $this->placeholder,
'allowCustomValues' => $this->allow_custom_values,
'queryOptions' => $this->query_options,
'minInputLength' => $this->minimum_input_length,
];
}
public function set_allow_custom_values( bool $allow_custom_values ): self {
$this->allow_custom_values = $allow_custom_values;
return $this;
}
public function set_endpoint( string $url ): self {
$this->query_options['endpoint'] = $url;
return $this;
}
public function set_request_params( array $params ): self {
$this->query_options['requestParams'] = $params;
return $this;
}
public function set_minimum_input_length( int $input_length ): self {
$this->minimum_input_length = $input_length;
return $this;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Select_Control extends Atomic_Control_Base {
private array $options = [];
public function get_type(): string {
return 'select';
}
public function set_options( array $options ): self {
$this->options = $options;
return $this;
}
public function get_props(): array {
return [
'options' => $this->options,
];
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
use Elementor\Modules\AtomicWidgets\Image_Sizes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Svg_Control extends Atomic_Control_Base {
public function get_type(): string {
return 'svg-media';
}
public function get_props(): array {
return [
'type' => $this->get_type(),
];
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Switch_Control extends Atomic_Control_Base {
public function get_type(): string {
return 'switch';
}
public function get_props(): array {
return [];
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Text_Control extends Atomic_Control_Base {
private ?string $placeholder = null;
public function get_type(): string {
return 'text';
}
public function set_placeholder( string $placeholder ): self {
$this->placeholder = $placeholder;
return $this;
}
public function get_props(): array {
return [
'placeholder' => $this->placeholder,
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Textarea_Control extends Atomic_Control_Base {
private $placeholder = null;
public function get_type(): string {
return 'textarea';
}
public function set_placeholder( string $placeholder ): self {
$this->placeholder = $placeholder;
return $this;
}
public function get_props(): array {
return [
'placeholder' => $this->placeholder,
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Database;
use Elementor\Core\Database\Base_Database_Updater;
use Elementor\Modules\AtomicWidgets\Database\Migrations\Add_Capabilities;
class Atomic_Widgets_Database_Updater extends Base_Database_Updater {
const DB_VERSION = 1;
const OPTION_NAME = 'elementor_atomic_widgets_db_version';
protected function get_migrations(): array {
return [
1 => new Add_Capabilities(),
];
}
protected function get_db_version() {
return static::DB_VERSION;
}
protected function get_db_version_option_name(): string {
return static::OPTION_NAME;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Database\Migrations;
use Elementor\Core\Database\Base_Migration;
class Add_Capabilities extends Base_Migration {
const ACCESS_STYLES_TAB = 'elementor_atomic_widgets_access_styles_tab';
const EDIT_LOCAL_CSS_CLASS = 'elementor_atomic_widgets_edit_local_css_class';
public function up() {
$capabilities = [
self::ACCESS_STYLES_TAB => [ 'administrator', 'editor', 'author', 'contributor', 'shop_manager' ],
self::EDIT_LOCAL_CSS_CLASS => [ 'administrator', 'editor', 'author', 'contributor', 'shop_manager' ],
];
foreach ( $capabilities as $cap => $roles ) {
foreach ( $roles as $role_name ) {
$role = get_role( $role_name );
if ( $role ) {
$role->add_cap( $cap );
}
}
}
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
use Elementor\Modules\AtomicWidgets\Parsers\Props_Parser;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Prop_Type extends Plain_Prop_Type {
const META_KEY = 'dynamic';
/**
* Return a tuple that lets the developer ignore the dynamic prop type in the props schema
* using `Prop_Type::add_meta()`, e.g. `String_Prop_Type::make()->add_meta( Dynamic_Prop_Type::ignore() )`.
*/
public static function ignore(): array {
return [ static::META_KEY, false ];
}
public static function get_key(): string {
return 'dynamic';
}
public function categories( array $categories ) {
$this->settings['categories'] = $categories;
return $this;
}
public function get_categories() {
return $this->settings['categories'] ?? [];
}
protected function validate_value( $value ): bool {
$is_valid_structure = (
isset( $value['name'] ) &&
is_string( $value['name'] ) &&
isset( $value['settings'] ) &&
is_array( $value['settings'] )
);
if ( ! $is_valid_structure ) {
return false;
}
$tag = Dynamic_Tags_Module::instance()->registry->get_tag( $value['name'] );
if ( ! $tag || ! $this->is_tag_in_supported_categories( $tag ) ) {
return false;
}
return Props_Parser::make( $tag['props_schema'] )
->validate( $value['settings'] )
->is_valid();
}
protected function sanitize_value( $value ): array {
$tag = Dynamic_Tags_Module::instance()->registry->get_tag( $value['name'] );
$sanitized = Props_Parser::make( $tag['props_schema'] )
->sanitize( $value['settings'] )
->unwrap();
return [
'name' => $value['name'],
'settings' => $sanitized,
];
}
private function is_tag_in_supported_categories( array $tag ): bool {
$intersection = array_intersect(
$tag['categories'],
$this->get_categories()
);
return ! empty( $intersection );
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Url_Prop_Type;
use Elementor\Modules\DynamicTags\Module as V1_Dynamic_Tags_Module;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Prop_Types_Mapping {
public static function make(): self {
return new static();
}
/**
* @param array<string, Prop_Type> $schema
*
* @return array<string, Prop_Type>
*/
public function get_modified_prop_types( array $schema ): array {
$result = [];
foreach ( $schema as $key => $prop_type ) {
if ( ! ( $prop_type instanceof Prop_Type ) ) {
$result[ $key ] = $prop_type;
continue;
}
$result[ $key ] = $this->get_modified_prop_type( $prop_type );
}
return $result;
}
/**
* Change prop type into a union prop type if the original prop type supports dynamic tags.
*
* @param Prop_Type $prop_type
*
* @return Prop_Type|Union_Prop_Type
*/
private function get_modified_prop_type( Prop_Type $prop_type ) {
$transformable_prop_types = $prop_type instanceof Union_Prop_Type ?
$prop_type->get_prop_types() :
[ $prop_type ];
$categories = [];
foreach ( $transformable_prop_types as $transformable_prop_type ) {
if ( $transformable_prop_type instanceof Object_Prop_Type ) {
$transformable_prop_type->set_shape(
$this->get_modified_prop_types( $transformable_prop_type->get_shape() )
);
}
if ( $transformable_prop_type instanceof Array_Prop_Type ) {
$transformable_prop_type->set_item_type(
$this->get_modified_prop_type( $transformable_prop_type->get_item_type() )
);
}
// When the prop type is originally a union, we need to merge all the categories
// of each prop type in the union and create one dynamic prop type with all the categories.
$categories = array_merge( $categories, $this->get_related_categories( $transformable_prop_type ) );
}
if ( empty( $categories ) ) {
return $prop_type;
}
$union_prop_type = $prop_type instanceof Transformable_Prop_Type ?
Union_Prop_Type::create_from( $prop_type ) :
$prop_type;
$union_prop_type->add_prop_type(
Dynamic_Prop_Type::make()->categories( $categories )
);
return $union_prop_type;
}
private function get_related_categories( Transformable_Prop_Type $prop_type ): array {
if ( ! $prop_type->get_meta_item( Dynamic_Prop_Type::META_KEY, true ) ) {
return [];
}
if ( $prop_type instanceof Number_Prop_Type ) {
return [ V1_Dynamic_Tags_Module::NUMBER_CATEGORY ];
}
if ( $prop_type instanceof Image_Src_Prop_Type ) {
return [ V1_Dynamic_Tags_Module::IMAGE_CATEGORY ];
}
if ( $prop_type instanceof String_Prop_Type && empty( $prop_type->get_enum() ) ) {
return [ V1_Dynamic_Tags_Module::TEXT_CATEGORY ];
}
if ( $prop_type instanceof Url_Prop_Type ) {
return [ V1_Dynamic_Tags_Module::URL_CATEGORY ];
}
return [];
}
}

View File

@@ -0,0 +1,181 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Tags_Editor_Config {
private Dynamic_Tags_Schemas $schemas;
private ?array $tags = null;
public function __construct( Dynamic_Tags_Schemas $schemas ) {
$this->schemas = $schemas;
}
public function get_tags(): array {
if ( null !== $this->tags ) {
return $this->tags;
}
$atomic_tags = [];
$dynamic_tags = Plugin::$instance->dynamic_tags->get_tags_config();
foreach ( $dynamic_tags as $name => $tag ) {
$atomic_tag = $this->convert_dynamic_tag_to_atomic( $tag );
if ( $atomic_tag ) {
$atomic_tags[ $name ] = $atomic_tag;
}
}
$this->tags = $atomic_tags;
return $this->tags;
}
/**
* @param string $name
*
* @return null|array{
* name: string,
* categories: string[],
* label: string,
* group: string,
* atomic_controls: array,
* props_schema: array<string, Transformable_Prop_Type>
* }
*/
public function get_tag( string $name ): ?array {
$tags = $this->get_tags();
return $tags[ $name ] ?? null;
}
private function convert_dynamic_tag_to_atomic( $tag ) {
if ( empty( $tag['name'] ) || empty( $tag['categories'] ) ) {
return null;
}
$converted_tag = [
'name' => $tag['name'],
'categories' => $tag['categories'],
'label' => $tag['title'] ?? '',
'group' => $tag['group'] ?? '',
'atomic_controls' => [],
'props_schema' => $this->schemas->get( $tag['name'] ),
];
if ( ! isset( $tag['controls'] ) ) {
return $converted_tag;
}
try {
$atomic_controls = $this->convert_controls_to_atomic( $tag['controls'], $tag['force_convert_to_atomic'] ?? false );
} catch ( \Exception $e ) {
return null;
}
if ( null === $atomic_controls ) {
return null;
}
$converted_tag['atomic_controls'] = $atomic_controls;
return $converted_tag;
}
private function convert_controls_to_atomic( $controls, $force = false ) {
$atomic_controls = [];
foreach ( $controls as $control ) {
if ( 'section' === $control['type'] ) {
continue;
}
$atomic_control = $this->convert_control_to_atomic( $control );
if ( ! $atomic_control ) {
if ( $force ) {
continue;
}
return null;
}
$section_name = $control['section'];
if ( ! isset( $atomic_controls[ $section_name ] ) ) {
$atomic_controls[ $section_name ] = Section::make()
->set_label( $controls[ $section_name ]['label'] );
}
$atomic_controls[ $section_name ] = $atomic_controls[ $section_name ]->add_item( $atomic_control );
}
return array_values( $atomic_controls );
}
private function convert_control_to_atomic( $control ) {
$map = [
'select' => fn( $control ) => $this->convert_select_control_to_atomic( $control ),
'text' => fn( $control ) => $this->convert_text_control_to_atomic( $control ),
];
if ( ! isset( $map[ $control['type'] ] ) ) {
return null;
}
$is_convertable = ! isset( $control['name'], $control['section'], $control['label'], $control['default'] );
if ( $is_convertable ) {
throw new \Exception( 'Control must have name, section, label, and default' );
}
return $map[ $control['type'] ]( $control );
}
/**
* @param $control
*
* @return Select_Control
* @throws \Exception If control is missing options.
*/
private function convert_select_control_to_atomic( $control ) {
if ( empty( $control['options'] ) ) {
throw new \Exception( 'Select control must have options' );
}
$options = array_map(
fn( $key, $value ) => [
'value' => $key,
'label' => $value,
],
array_keys( $control['options'] ),
$control['options']
);
return Select_Control::bind_to( $control['name'] )
->set_label( $control['label'] )
->set_options( $options );
}
/**
* @param $control
*
* @return Text_Control
*/
private function convert_text_control_to_atomic( $control ) {
return Text_Control::bind_to( $control['name'] )
->set_label( $control['label'] );
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers_Registry;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Tags_Module {
private static ?self $instance = null;
public Dynamic_Tags_Editor_Config $registry;
private Dynamic_Tags_Schemas $schemas;
private function __construct() {
$this->schemas = new Dynamic_Tags_Schemas();
$this->registry = new Dynamic_Tags_Editor_Config( $this->schemas );
}
public static function instance( $fresh = false ): self {
if ( null === static::$instance || $fresh ) {
static::$instance = new static();
}
return static::$instance;
}
public static function fresh(): self {
return static::instance( true );
}
public function register_hooks() {
add_filter(
'elementor/editor/localize_settings',
fn( array $settings ) => $this->add_atomic_dynamic_tags_to_editor_settings( $settings )
);
add_filter(
'elementor/atomic-widgets/props-schema',
fn( array $schema ) => Dynamic_Prop_Types_Mapping::make()->get_modified_prop_types( $schema )
);
add_action(
'elementor/atomic-widgets/settings/transformers/register',
fn ( $transformers, $prop_resolver ) => $this->register_transformers( $transformers, $prop_resolver ),
10,
2
);
}
private function add_atomic_dynamic_tags_to_editor_settings( $settings ) {
if ( isset( $settings['dynamicTags']['tags'] ) ) {
$settings['atomicDynamicTags'] = [
'tags' => $this->registry->get_tags(),
'groups' => Plugin::$instance->dynamic_tags->get_config()['groups'],
];
}
return $settings;
}
private function register_transformers( Transformers_Registry $transformers, Render_Props_Resolver $props_resolver ) {
$transformers->register(
Dynamic_Prop_Type::get_key(),
new Dynamic_Transformer(
Plugin::$instance->dynamic_tags,
$this->schemas,
$props_resolver
)
);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Core\DynamicTags\Base_Tag;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Tags_Schemas {
private array $tags_schemas = [];
public function get( string $tag_name ) {
if ( isset( $this->tags_schemas[ $tag_name ] ) ) {
return $this->tags_schemas[ $tag_name ];
}
$tag = $this->get_tag( $tag_name );
$this->tags_schemas[ $tag_name ] = [];
foreach ( $tag->get_controls() as $control ) {
if ( ! isset( $control['type'] ) || 'section' === $control['type'] ) {
continue;
}
$prop_type = $this->convert_control_to_prop_type( $control );
if ( ! $prop_type ) {
continue;
}
$this->tags_schemas[ $tag_name ][ $control['name'] ] = $prop_type;
}
return $this->tags_schemas[ $tag_name ];
}
private function get_tag( string $tag_name ): Base_Tag {
$tag_info = Plugin::$instance->dynamic_tags->get_tag_info( $tag_name );
if ( ! $tag_info || empty( $tag_info['instance'] ) ) {
throw new \Exception( 'Tag not found' );
}
if ( ! $tag_info['instance'] instanceof Base_Tag ) {
throw new \Exception( 'Tag is not an instance of Tag' );
}
return $tag_info['instance'];
}
private function convert_control_to_prop_type( array $control ) {
$control_type = $control['type'];
if ( 'text' === $control_type ) {
return String_Prop_Type::make()
->default( $control['default'] ?? null );
}
if ( 'select' === $control_type ) {
return String_Prop_Type::make()
->default( $control['default'] ?? null )
->enum( array_keys( $control['options'] ?? [] ) );
}
return null;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
use Elementor\Core\DynamicTags\Manager as Dynamic_Tags_Manager;
use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dynamic_Transformer extends Transformer_Base {
private Dynamic_Tags_Manager $dynamic_tags_manager;
private Dynamic_Tags_Schemas $dynamic_tags_schemas;
private Render_Props_Resolver $props_resolver;
public function __construct(
Dynamic_Tags_Manager $dynamic_tags_manager,
Dynamic_Tags_Schemas $dynamic_tags_schemas,
Render_Props_Resolver $props_resolver
) {
$this->dynamic_tags_manager = $dynamic_tags_manager;
$this->dynamic_tags_schemas = $dynamic_tags_schemas;
$this->props_resolver = $props_resolver;
}
public function transform( $value, $key ) {
if ( ! isset( $value['name'] ) || ! is_string( $value['name'] ) ) {
throw new \Exception( 'Dynamic tag name must be a string' );
}
if ( isset( $value['settings'] ) && ! is_array( $value['settings'] ) ) {
throw new \Exception( 'Dynamic tag settings must be an array' );
}
$schema = $this->dynamic_tags_schemas->get( $value['name'] );
$settings = $this->props_resolver->resolve(
$schema,
$value['settings'] ?? []
);
return $this->dynamic_tags_manager->get_tag_data_content( null, $value['name'], $settings );
}
}

View File

@@ -0,0 +1,18 @@
{% if settings.text is not empty %}
{% set classes = settings.classes | merge( [ base_styles.base ] ) | join(' ') %}
{% set id_attribute = settings._cssid is not empty ? 'id=' ~ settings._cssid | e('html_attr') : '' %}
{% if settings.link.href %}
<a
href="{{ settings.link.href }}"
target="{{ settings.link.target }}"
class="{{ classes }}"
{{ id_attribute }}
>
{{ settings.text }}
</a>
{% else %}
<button class="{{ classes }}" {{ id_attribute }}>
{{ settings.text }}
</button>
{% endif %}
{% endif %}

View File

@@ -0,0 +1,141 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Button;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Color_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Atomic_Button extends Atomic_Widget_Base {
use Has_Template;
public static function get_element_type(): string {
return 'e-button';
}
public function get_title() {
return esc_html__( 'Button', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-e-button';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'text' => String_Prop_Type::make()
->default( __( 'Click here', 'elementor' ) ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$settings_section_items = [
Link_Control::bind_to( 'link' )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
return [
Section::make()
->set_label( __( 'Content', 'elementor' ) )
->set_items( [
Text_Control::bind_to( 'text' )
->set_label( __( 'Button text', 'elementor' ) )
->set_placeholder( __( 'Type your button text here', 'elementor' ) ),
] ),
Section::make()
->set_label( __( 'Settings', 'elementor' ) )
->set_items( $settings_section_items ),
];
}
protected function define_base_styles(): array {
$background_color_value = Background_Prop_Type::generate( [
'color' => Color_Prop_Type::generate( '#375EFB' ),
] );
$display_value = String_Prop_Type::generate( 'inline-block' );
$padding_value = Dimensions_Prop_Type::generate( [
'block-start' => Size_Prop_Type::generate( [
'size' => 12,
'unit' => 'px',
]),
'inline-end' => Size_Prop_Type::generate( [
'size' => 24,
'unit' => 'px',
]),
'block-end' => Size_Prop_Type::generate( [
'size' => 12,
'unit' => 'px',
]),
'inline-start' => Size_Prop_Type::generate( [
'size' => 24,
'unit' => 'px',
]),
]);
$border_radius_value = Size_Prop_Type::generate( [
'size' => 2,
'unit' => 'px',
] );
$border_width_value = Size_Prop_Type::generate( [
'size' => 0,
'unit' => 'px',
] );
$align_text_value = String_Prop_Type::generate( 'center' );
return [
'base' => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'background', $background_color_value )
->add_prop( 'display', $display_value )
->add_prop( 'padding', $padding_value )
->add_prop( 'border-radius', $border_radius_value )
->add_prop( 'border-width', $border_width_value )
->add_prop( 'text-align', $align_text_value )
),
];
}
protected function get_templates(): array {
return [
'elementor/elements/atomic-button' => __DIR__ . '/atomic-button.html.twig',
];
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
use Elementor\Element_Base;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Atomic_Element_Base extends Element_Base {
use Has_Atomic_Base;
protected $version = '0.0';
protected $styles = [];
protected $editor_settings = [];
public function __construct( $data = [], $args = null ) {
parent::__construct( $data, $args );
$this->version = $data['version'] ?? '0.0';
$this->styles = $data['styles'] ?? [];
$this->editor_settings = $data['editor_settings'] ?? [];
}
abstract protected function define_atomic_controls(): array;
public function get_global_scripts() {
return [];
}
final public function get_initial_config() {
$config = parent::get_initial_config();
$config['atomic_controls'] = $this->get_atomic_controls();
$config['atomic_props_schema'] = static::get_props_schema();
$config['base_styles'] = $this->get_base_styles();
$config['version'] = $this->version;
$config['show_in_panel'] = true;
$config['categories'] = [ 'v4-elements' ];
$config['hide_on_search'] = false;
$config['controls'] = [];
$config['keywords'] = $this->get_keywords();
return $config;
}
/**
* @return array<string, Prop_Type>
*/
abstract protected static function define_props_schema(): array;
/**
* Get Element keywords.
*
* Retrieve the element keywords.
*
* @since 3.29
* @access public
*
* @return array Element keywords.
*/
public function get_keywords() {
return [];
}
}

View File

@@ -0,0 +1,12 @@
{% if settings.title is not empty %}
{% set id_attribute = settings._cssid is not empty ? 'id=' ~ settings._cssid | e('html_attr') : '' %}
<{{ settings.tag | e('html_tag') }} class="{{ settings.classes | merge( [ base_styles.base ] ) | join(' ') }}" {{ id_attribute }}>
{% if settings.link.href %}
<a href="{{ settings.link.href }}" target="{{ settings.link.target }}" class="{{ base_styles['link-base'] }}">
{{ settings.title }}
</a>
{% else %}
{{ settings.title }}
{% endif %}
</{{ settings.tag | e('html_tag') }}>
{% endif %}

View File

@@ -0,0 +1,153 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Heading;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Textarea_Control;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Atomic_Heading extends Atomic_Widget_Base {
use Has_Template;
const LINK_BASE_STYLE_KEY = 'link-base';
public static function get_element_type(): string {
return 'e-heading';
}
public function get_title() {
return esc_html__( 'Heading', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-e-heading';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'tag' => String_Prop_Type::make()
->enum( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ] )
->default( 'h2' ),
'title' => String_Prop_Type::make()
->default( __( 'This is a title', 'elementor' ) ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$content_section = Section::make()
->set_label( __( 'Content', 'elementor' ) )
->set_items( [
Textarea_Control::bind_to( 'title' )
->set_label( __( 'Title', 'elementor' ) )
->set_placeholder( __( 'Type your title here', 'elementor' ) ),
] );
$settings_section_items = [
Select_Control::bind_to( 'tag' )
->set_label( esc_html__( 'Tag', 'elementor' ) )
->set_options( [
[
'value' => 'h1',
'label' => 'H1',
],
[
'value' => 'h2',
'label' => 'H2',
],
[
'value' => 'h3',
'label' => 'H3',
],
[
'value' => 'h4',
'label' => 'H4',
],
[
'value' => 'h5',
'label' => 'H5',
],
[
'value' => 'h6',
'label' => 'H6',
],
]),
Link_Control::bind_to( 'link' )->set_meta( [
'topDivider' => true,
] )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
$settings_section = Section::make()
->set_label( __( 'Settings', 'elementor' ) )
->set_items( $settings_section_items );
return [
$content_section,
$settings_section,
];
}
protected function define_base_styles(): array {
$margin_value = Size_Prop_Type::generate( [
'unit' => 'px',
'size' => 0 ,
] );
return [
'base' => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'margin', $margin_value )
),
self::LINK_BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'all', 'unset' )
->add_prop( 'cursor', 'pointer' )
),
];
}
protected function get_templates(): array {
return [
'elementor/elements/atomic-heading' => __DIR__ . '/atomic-heading.html.twig',
];
}
}

View File

@@ -0,0 +1,18 @@
{% if settings.image.src is not empty %}
{% set id_attribute = settings._cssid is not empty ? 'id=' ~ settings._cssid | e('html_attr') : '' %}
{% if settings.link.href %}
<a href="{{ settings.link.href }}" class="{{ base_styles['link-base'] }}" target="{{ settings.link.target }}">
{% endif %}
<img class="{{ base_styles['base'] }} {{ settings.classes | join(' ') }}" {{ id_attribute }}
{% for attr, value in settings.image %}
{% if attr == 'src' %}
src="{{ value | e('full_url') }}"
{% else %}
{{ attr | e('html_attr') }}="{{ value }}"
{% endif %}
{% endfor %}
/>
{% if settings.link.href %}
</a>
{% endif %}
{% endif %}

View File

@@ -0,0 +1,117 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Image;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\Controls\Types\Image_Control;
use Elementor\Modules\AtomicWidgets\Image\Placeholder_Image;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Atomic_Image extends Atomic_Widget_Base {
use Has_Template;
const LINK_BASE_STYLE_KEY = 'link-base';
const BASE_STYLE_KEY = 'base';
public static function get_element_type(): string {
return 'e-image';
}
public function get_title() {
return esc_html__( 'Image', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-e-image';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'image' => Image_Prop_Type::make()
->default_url( Placeholder_Image::get_placeholder_image() )
->default_size( 'full' ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$content_section = Section::make()
->set_label( esc_html__( 'Content', 'elementor' ) )
->set_items( [
Image_Control::bind_to( 'image' )
->set_show_mode( 'media' ),
] );
$settings_section_items = [
Image_Control::bind_to( 'image' )
->set_show_mode( 'sizes' ),
Link_Control::bind_to( 'link' )->set_meta( [
'topDivider' => true,
] )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
return [
$content_section,
Section::make()
->set_label( esc_html__( 'Settings', 'elementor' ) )
->set_items( $settings_section_items ),
];
}
protected function define_base_styles(): array {
return [
self::LINK_BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'display', 'inherit' )
->add_prop( 'width', 'fit-content' )
),
self::BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'display', 'block' )
),
];
}
protected function get_templates(): array {
return [
'elementor/elements/atomic-image' => __DIR__ . '/atomic-image.html.twig',
];
}
}

View File

@@ -0,0 +1,12 @@
{% if settings.paragraph is not empty %}
{% set id_attribute = settings._cssid is not empty ? 'id=' ~ settings._cssid | e('html_attr') : '' %}
<p class="{{ settings.classes | merge( [ base_styles.base ] ) | join(' ') }}" {{ id_attribute }}>
{% if settings.link.href %}
<a href="{{ settings.link.href }}" target="{{ settings.link.target }}" class="{{ base_styles['link-base'] }}">
{{ settings.paragraph }}
</a>
{% else %}
{{ settings.paragraph }}
{% endif %}
</p>
{% endif %}

View File

@@ -0,0 +1,115 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Textarea_Control;
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Atomic_Paragraph extends Atomic_Widget_Base {
use Has_Template;
const LINK_BASE_STYLE_KEY = 'link-base';
public static function get_element_type(): string {
return 'e-paragraph';
}
public function get_title() {
return esc_html__( 'Paragraph', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-paragraph';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'paragraph' => String_Prop_Type::make()
->default( __( 'Type your paragraph here', 'elementor' ) ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$settings_section_items = [
Link_Control::bind_to( 'link' )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
return [
Section::make()
->set_label( __( 'Content', 'elementor' ) )
->set_items( [
Textarea_Control::bind_to( 'paragraph' )
->set_label( __( 'Paragraph', 'elementor' ) )
->set_placeholder( __( 'Type your paragraph here', 'elementor' ) ),
] ),
Section::make()
->set_label( __( 'Settings', 'elementor' ) )
->set_items( $settings_section_items ),
];
}
protected function define_base_styles(): array {
$margin_value = Size_Prop_Type::generate( [
'unit' => 'px',
'size' => 0 ,
] );
return [
'base' => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'margin', $margin_value )
),
self::LINK_BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'all', 'unset' )
->add_prop( 'cursor', 'pointer' )
),
];
}
protected function get_templates(): array {
return [
'elementor/elements/atomic-paragraph' => __DIR__ . '/atomic-paragraph.html.twig',
];
}
}

View File

@@ -0,0 +1,191 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Svg;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Core\Utils\Svg\Svg_Sanitizer;
use Elementor\Modules\AtomicWidgets\Controls\Types\Svg_Control;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Atomic_Svg extends Atomic_Widget_Base {
const BASE_STYLE_KEY = 'base';
const DEFAULT_SVG = 'images/default-svg.svg';
const DEFAULT_SVG_PATH = ELEMENTOR_ASSETS_PATH . self::DEFAULT_SVG;
const DEFAULT_SVG_URL = ELEMENTOR_ASSETS_URL . self::DEFAULT_SVG;
public static function get_element_type(): string {
return 'e-svg';
}
public function get_title() {
return esc_html__( 'SVG', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-svg';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()->default( [] ),
'svg' => Image_Src_Prop_Type::make()->default_url( static::DEFAULT_SVG_URL ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$settings_section_items = [
Link_Control::bind_to( 'link' )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
return [
Section::make()
->set_label( esc_html__( 'Content', 'elementor' ) )
->set_items( [
Svg_Control::bind_to( 'svg' ),
] ),
Section::make()
->set_label( esc_html__( 'Settings', 'elementor' ) )
->set_items( $settings_section_items ),
];
}
protected function define_base_styles(): array {
$display_value = String_Prop_Type::generate( 'inline-block' );
$size = Size_Prop_Type::generate( [
'size' => 65,
'unit' => 'px',
] );
return [
self::BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'display', $display_value )
->add_prop( 'width', $size )
->add_prop( 'height', $size )
),
];
}
protected function render() {
$settings = $this->get_atomic_settings();
$svg = $this->get_svg_content( $settings );
if ( ! $svg ) {
return;
}
$svg = new \WP_HTML_Tag_Processor( $svg );
if ( ! $svg->next_tag( 'svg' ) ) {
return;
}
$svg->set_attribute( 'fill', 'currentColor' );
$this->add_svg_style( $svg, 'width: 100%; height: 100%; overflow: unset;' );
$svg_html = ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() );
$classes = array_filter( array_merge(
[ self::BASE_STYLE_KEY => $this->get_base_styles_dictionary()[ self::BASE_STYLE_KEY ] ],
$settings['classes']
) );
$classes_string = implode( ' ', $classes );
$cssid_attribute = ! empty( $settings['_cssid'] ) ? 'id="' . esc_attr( $settings['_cssid'] ) . '"' : '';
if ( isset( $settings['link'] ) && ! empty( $settings['link']['href'] ) ) {
$svg_html = sprintf(
'<a href="%s" target="%s" class="%s" %s>%s</a>',
$settings['link']['href'],
esc_attr( $settings['link']['target'] ),
esc_attr( $classes_string ),
$cssid_attribute,
$svg_html
);
} else {
$svg_html = sprintf( '<div class="%s" %s>%s</div>', esc_attr( $classes_string ), $cssid_attribute, $svg_html );
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $svg_html;
}
private function get_svg_content( $settings ) {
if ( isset( $settings['svg']['id'] ) ) {
$content = Utils::file_get_contents(
get_attached_file( $settings['svg']['id'] )
);
if ( $content ) {
return $content;
}
}
if (
isset( $settings['svg']['url'] ) &&
static::DEFAULT_SVG_URL !== $settings['svg']['url']
) {
$content = wp_safe_remote_get(
$settings['svg']['url']
);
if ( ! is_wp_error( $content ) ) {
return $content['body'];
}
}
$content = Utils::file_get_contents(
static::DEFAULT_SVG_PATH
);
return $content ? $content : null;
}
private function add_svg_style( &$svg, $new_style ) {
$svg_style = $svg->get_attribute( 'style' );
$svg_style = trim( (string) $svg_style );
if ( empty( $svg_style ) ) {
$svg_style = $new_style;
} else {
$svg_style = rtrim( $svg_style, ';' ) . '; ' . $new_style;
}
$svg->set_attribute( 'style', $svg_style );
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Widget_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Atomic_Widget_Base extends Widget_Base {
use Has_Atomic_Base;
protected $version = '0.0';
protected $styles = [];
protected $editor_settings = [];
public function __construct( $data = [], $args = null ) {
parent::__construct( $data, $args );
$this->version = $data['version'] ?? '0.0';
$this->styles = $data['styles'] ?? [];
$this->editor_settings = $data['editor_settings'] ?? [];
}
abstract protected function define_atomic_controls(): array;
public function get_global_scripts() {
return [];
}
public function get_initial_config() {
$config = parent::get_initial_config();
$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'] = static::get_props_schema();
$config['version'] = $this->version;
return $config;
}
public function get_categories(): array {
return [ 'v4-elements' ];
}
/**
* TODO: Removes the wrapper div from the widget.
*/
public function before_render() {}
public function after_render() {}
/**
* @return array<string, Prop_Type>
*/
abstract protected static function define_props_schema(): array;
}

View File

@@ -0,0 +1,17 @@
{% if settings.source is not empty %}
{% set classes = settings.classes | merge( [ base_styles.base ] ) | join(' ') %}
{% set data_settings = {
'source': settings.source,
'autoplay': settings.autoplay,
'mute': settings.mute,
'controls': settings.player_controls,
'cc_load_policy': settings.captions,
'loop': settings.loop,
'rel': settings.rel,
'start': settings.start,
'end': settings.end,
'privacy': settings.privacy_mode,
'lazyload': settings.lazyload,
} %}
<div data-id="{{ id }}" data-e-type="{{ type }}" class="{{ classes }}" data-settings="{{ data_settings|json_encode|e('html_attr') }}"></div>
{% endif %}

View File

@@ -0,0 +1,103 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Youtube;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Switch_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Atomic_Youtube extends Atomic_Widget_Base {
use Has_Template;
public static function get_element_type(): string {
return 'e-youtube';
}
public function get_title() {
return esc_html__( 'YouTube', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-e-youtube';
}
protected static function define_props_schema(): array {
return [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'source' => String_Prop_Type::make()
->default( 'https://www.youtube.com/watch?v=XHOmBV4js_E' ),
'start' => String_Prop_Type::make(),
'end' => String_Prop_Type::make(),
'autoplay' => Boolean_Prop_Type::make()->default( false ),
'mute' => Boolean_Prop_Type::make()->default( false ),
'loop' => Boolean_Prop_Type::make()->default( false ),
'lazyload' => Boolean_Prop_Type::make()->default( false ),
'player_controls' => Boolean_Prop_Type::make()->default( true ),
'captions' => Boolean_Prop_Type::make()->default( false ),
'privacy_mode' => Boolean_Prop_Type::make()->default( false ),
'rel' => Boolean_Prop_Type::make()->default( true ),
];
}
protected function define_atomic_controls(): array {
return [
Section::make()
->set_label( __( 'Content', 'elementor' ) )
->set_items( [
Text_Control::bind_to( 'source' )
->set_label( esc_html__( 'YouTube URL', 'elementor' ) )
->set_placeholder( esc_html__( 'Type or paste your URL', 'elementor' ) ),
Text_Control::bind_to( 'start' )->set_label( esc_html__( 'Start time', 'elementor' ) ),
Text_Control::bind_to( 'end' )->set_label( esc_html__( 'End time', 'elementor' ) ),
Switch_Control::bind_to( 'autoplay' )->set_label( esc_html__( 'Autoplay', 'elementor' ) ),
Switch_Control::bind_to( 'mute' )->set_label( esc_html__( 'Mute', 'elementor' ) ),
Switch_Control::bind_to( 'loop' )->set_label( esc_html__( 'Loop', 'elementor' ) ),
Switch_Control::bind_to( 'lazyload' )->set_label( esc_html__( 'Lazy load', 'elementor' ) ),
Switch_Control::bind_to( 'player_controls' )->set_label( esc_html__( 'Player controls', 'elementor' ) ),
Switch_Control::bind_to( 'captions' )->set_label( esc_html__( 'Captions', 'elementor' ) ),
Switch_Control::bind_to( 'privacy_mode' )->set_label( esc_html__( 'Privacy mode', 'elementor' ) ),
Switch_Control::bind_to( 'rel' )->set_label( esc_html__( 'Related videos', 'elementor' ) ),
] ),
];
}
protected function define_base_styles(): array {
return [
'base' => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'aspect-ratio', String_Prop_Type::generate( '16/9' ) )
->add_prop( 'overflow', String_Prop_Type::generate( 'hidden' ) )
),
];
}
public function get_script_depends() {
return [ 'elementor-youtube-handler' ];
}
protected function get_templates(): array {
return [
'elementor/elements/atomic-youtube' => __DIR__ . '/atomic-youtube.html.twig',
];
}
}

View File

@@ -0,0 +1,126 @@
import { register } from '@elementor/frontend-handlers';
const getYoutubeVideoIdFromUrl = ( url ) => {
const regex = /^(?:https?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?vi?=|(?:embed|v|vi|user|shorts)\/))([^?&"'>]+)/;
const match = url.match( regex );
return match ? match[ 1 ] : null;
};
const loadYouTubeAPI = () => {
return new Promise( ( resolve ) => {
if ( window.YT && window.YT.loaded ) {
resolve( window.YT );
return;
}
const YOUTUBE_IFRAME_API_URL = 'https://www.youtube.com/iframe_api';
if ( ! document.querySelector( `script[src="${ YOUTUBE_IFRAME_API_URL }"]` ) ) {
const tag = document.createElement( 'script' );
tag.src = YOUTUBE_IFRAME_API_URL;
const firstScriptTag = document.getElementsByTagName( 'script' )[ 0 ];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
}
const checkYT = () => {
if ( window.YT && window.YT.loaded ) {
resolve( window.YT );
} else {
setTimeout( checkYT, 350 );
}
};
checkYT();
} );
};
register( {
elementType: 'e-youtube',
uniqueId: 'e-youtube-handler',
callback: ( { element } ) => {
const youtubeElement = document.createElement( 'div' );
youtubeElement.style.height = '100%';
element.appendChild( youtubeElement );
const settingsAttr = element.getAttribute( 'data-settings' );
const parsedSettings = settingsAttr ? JSON.parse( settingsAttr ) : {};
const videoId = getYoutubeVideoIdFromUrl( parsedSettings.source );
if ( ! videoId ) {
return;
}
let player;
let observer;
const prepareYTVideo = ( YT ) => {
const playerOptions = {
videoId,
events: {
onReady: () => {
if ( parsedSettings.mute ) {
player.mute();
}
if ( parsedSettings.autoplay ) {
player.playVideo();
}
},
onStateChange: ( event ) => {
if ( event.data === YT.PlayerState.ENDED && parsedSettings.loop ) {
player.seekTo( parsedSettings.start || 0 );
}
},
},
playerVars: {
controls: parsedSettings.controls ? 1 : 0,
rel: parsedSettings.rel ? 0 : 1,
cc_load_policy: parsedSettings.cc_load_policy ? 1 : 0,
autoplay: parsedSettings.autoplay ? 1 : 0,
start: parsedSettings.start,
end: parsedSettings.end,
},
};
// To handle CORS issues, when the default host is changed, the origin parameter has to be set.
if ( parsedSettings.privacy ) {
playerOptions.host = 'https://www.youtube-nocookie.com';
playerOptions.origin = window.location.hostname;
}
player = new YT.Player( youtubeElement, playerOptions );
return player;
};
if ( parsedSettings.lazyload ) {
observer = new IntersectionObserver(
( entries ) => {
if ( entries[ 0 ].isIntersecting ) {
loadYouTubeAPI().then( ( apiObject ) => prepareYTVideo( apiObject ) );
observer.unobserve( element );
}
},
);
observer.observe( element );
} else {
loadYouTubeAPI().then( ( apiObject ) => prepareYTVideo( apiObject ) );
}
return () => {
if ( player && 'function' === typeof player.destroy ) {
player.destroy();
player = null;
}
if ( element.contains( youtubeElement ) ) {
element.removeChild( youtubeElement );
}
if ( observer && 'function' === typeof observer.disconnect ) {
observer.disconnect();
observer = null;
}
};
},
} );

View File

@@ -0,0 +1,211 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Div_Block;
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base;
use Elementor\Modules\AtomicWidgets\Controls\Section;
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control;
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Color_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
use Elementor\Plugin;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Div_Block extends Atomic_Element_Base {
const BASE_STYLE_KEY = 'base';
public static function get_type() {
return 'e-div-block';
}
public static function get_element_type(): string {
return 'e-div-block';
}
public function get_title() {
return esc_html__( 'Div Block', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-div-block';
}
protected static function define_props_schema(): array {
$props = [
'classes' => Classes_Prop_Type::make()
->default( [] ),
'tag' => String_Prop_Type::make()
->enum( [ 'div', 'header', 'section', 'article', 'aside', 'footer' ] )
->default( 'div' ),
'link' => Link_Prop_Type::make(),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$props['_cssid'] = String_Prop_Type::make();
}
return $props;
}
protected function define_atomic_controls(): array {
$settings_section_items = [
Select_Control::bind_to( 'tag' )
->set_label( esc_html__( 'HTML Tag', 'elementor' ) )
->set_options( [
[
'value' => 'div',
'label' => 'Div',
],
[
'value' => 'header',
'label' => 'Header',
],
[
'value' => 'section',
'label' => 'Section',
],
[
'value' => 'article',
'label' => 'Article',
],
[
'value' => 'aside',
'label' => 'Aside',
],
[
'value' => 'footer',
'label' => 'Footer',
],
]),
Link_Control::bind_to( 'link' )->set_meta( [
'topDivider' => true,
] )->set_label( __( 'Link', 'elementor' ) ),
];
if ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$settings_section_items[] = Text_Control::bind_to( '_cssid' )->set_label( __( 'ID', 'elementor' ) )->set_meta( [
'layout' => 'two-columns',
'topDivider' => true,
] );
}
return [
Section::make()
->set_label( __( 'Settings', 'elementor' ) )
->set_items( $settings_section_items ),
];
}
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'] );
}
return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
}
protected function content_template() {
?>
<?php
}
public function before_render() {
?>
<<?php $this->print_html_tag(); ?> <?php $this->print_render_attribute_string( '_wrapper' ); ?>>
<?php
}
public function after_render() {
?>
</<?php $this->print_html_tag(); ?>>
<?php
}
/**
* 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 );
}
protected function get_html_tag(): string {
$settings = $this->get_atomic_settings();
return ! empty( $settings['link']['href'] ) ? 'a' : ( $settings['tag'] ?? 'div' );
}
protected function define_base_styles(): array {
$display = String_Prop_Type::generate( 'block' );
return [
static::BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'display', $display )
->add_prop( 'padding', $this->get_base_padding() )
->add_prop( 'min-width', $this->get_base_min_width() )
),
];
}
protected function get_base_padding(): array {
return Size_Prop_Type::generate( [
'size' => 10,
'unit' => 'px',
] );
}
protected function get_base_min_width(): array {
return Size_Prop_Type::generate( [
'size' => 30,
'unit' => 'px',
] );
}
protected function add_render_attributes() {
parent::add_render_attributes();
$settings = $this->get_atomic_settings();
$base_style_class = $this->get_base_styles_dictionary()[ static::BASE_STYLE_KEY ];
$attributes = [
'class' => [
'e-con',
$base_style_class,
...( $settings['classes'] ?? [] ),
],
];
if ( ! empty( $settings['_cssid'] ) ) {
$attributes['id'] = esc_attr( $settings['_cssid'] );
}
if ( ! empty( $settings['link']['href'] ) ) {
$attributes = array_merge( $attributes, $settings['link'] );
}
$this->add_render_attribute( '_wrapper', $attributes );
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements\Flexbox;
use Elementor\Modules\AtomicWidgets\Elements\Div_Block\Div_Block;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Flexbox extends Div_Block {
public static function get_type() {
return 'e-flexbox';
}
public static function get_element_type(): string {
return 'e-flexbox';
}
public function get_title() {
return esc_html__( 'Flexbox', 'elementor' );
}
public function get_keywords() {
return [ 'ato', 'atom', 'atoms', 'atomic' ];
}
public function get_icon() {
return 'eicon-flexbox';
}
protected function define_base_styles(): array {
$display = String_Prop_Type::generate( 'flex' );
$flex_direction = String_Prop_Type::generate( 'row' );
return [
static::BASE_STYLE_KEY => Style_Definition::make()
->add_variant(
Style_Variant::make()
->add_prop( 'display', $display )
->add_prop( 'flex-direction', $flex_direction )
->add_prop( 'padding', $this->get_base_padding() )
),
];
}
}

View File

@@ -0,0 +1,185 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
use Elementor\Element_Base;
use Elementor\Modules\AtomicWidgets\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\Utils;
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 ) ) {
Utils::safe_throw( 'Control must be an instance of `Atomic_Control_Base`.' );
continue;
}
$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 $styles ): array {
$style_parser = Style_Parser::make( Style_Schema::get() );
foreach ( $styles as $style_id => $style ) {
$result = $style_parser->parse( $style );
if ( ! $result->is_valid() ) {
throw new \Exception( esc_html( "Styles validation failed for style `$style_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();
}
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 );
}
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['styles'] );
$data['editor_settings'] = $this->parse_editor_settings( $data['editor_settings'] );
return $data;
}
final public function get_raw_data( $with_html_content = false ) {
$raw_data = parent::get_raw_data( $with_html_content );
$raw_data['styles'] = $this->styles;
$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();
return Render_Props_Resolver::for_settings()->resolve( $schema, $props );
}
private 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 {
return apply_filters(
'elementor/atomic-widgets/props-schema',
static::define_props_schema()
);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
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 [];
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
use Elementor\Modules\AtomicWidgets\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(),
];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $renderer->render( $this->get_main_template(), $context );
} catch ( \Exception $e ) {
if ( Utils::is_elementor_debug() ) {
throw $e;
}
}
}
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;
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Image;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Sizes {
public static function get_keys() {
return array_map(
fn( $size ) => $size['value'],
static::get_all()
);
}
public static function get_all(): array {
$wp_image_sizes = static::get_wp_image_sizes();
$image_sizes = [];
foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
$control_title = ucwords( str_replace( '_', ' ', $size_key ) );
if ( is_array( $size_attributes ) ) {
$control_title .= sprintf( ' - %d*%d', $size_attributes['width'], $size_attributes['height'] );
}
$image_sizes[] = [
'label' => $control_title,
'value' => $size_key,
];
}
$image_sizes[] = [
'label' => esc_html__( 'Full', 'elementor' ),
'value' => 'full',
];
return $image_sizes;
}
private static function get_wp_image_sizes() {
$default_image_sizes = get_intermediate_image_sizes();
$additional_sizes = wp_get_additional_image_sizes();
$image_sizes = [];
foreach ( $default_image_sizes as $size ) {
$image_sizes[ $size ] = [
'width' => (int) get_option( $size . '_size_w' ),
'height' => (int) get_option( $size . '_size_h' ),
'crop' => (bool) get_option( $size . '_crop' ),
];
}
if ( $additional_sizes ) {
$image_sizes = array_merge( $image_sizes, $additional_sizes );
}
// /** This filter is documented in wp-admin/includes/media.php */
return apply_filters( 'image_size_names_choose', $image_sizes );
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Image;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Placeholder_Image {
public static function get_placeholder_image() {
return ELEMENTOR_ASSETS_URL . 'images/placeholder-v4.svg';
}
public static function get_background_placeholder_image() {
return ELEMENTOR_ASSETS_URL . 'images/background-placeholder.svg';
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Elementor\Modules\AtomicWidgets\ImportExport;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
use Elementor\Modules\AtomicWidgets\ImportExport\Modifiers\Settings_Props_Modifier;
use Elementor\Modules\AtomicWidgets\ImportExport\Modifiers\Styles_Ids_Modifier;
use Elementor\Modules\AtomicWidgets\ImportExport\Modifiers\Styles_Props_Modifier;
use Elementor\Modules\AtomicWidgets\PropsResolver\Import_Export_Props_Resolver;
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
use Elementor\Modules\AtomicWidgets\Utils;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Atomic_Import_Export {
public function register_hooks() {
add_filter(
'elementor/template_library/sources/local/import/elements',
fn( $elements ) => $this->run( $elements, Import_Export_Props_Resolver::for_import() )
);
add_filter(
'elementor/template_library/sources/cloud/import/elements',
fn( $elements ) => $this->run( $elements, Import_Export_Props_Resolver::for_import() )
);
add_filter(
'elementor/template_library/sources/local/export/elements',
fn( $elements ) => $this->run( $elements, Import_Export_Props_Resolver::for_export() )
);
add_filter(
'elementor/document/element/replace_id',
fn( $element ) => $this->replace_styles_ids( $element )
);
}
private function run( $elements, Import_Export_Props_Resolver $props_resolver ) {
if ( empty( $elements ) || ! is_array( $elements ) ) {
return $elements;
}
return Plugin::$instance->db->iterate_data( $elements, function ( $element ) use ( $props_resolver ) {
$element_instance = Plugin::$instance->elements_manager->create_element_instance( $element );
/** @var Atomic_Element_Base | Atomic_Widget_Base $element_instance */
if ( ! Utils::is_atomic( $element_instance ) ) {
return $element;
}
$runners = [
Settings_Props_Modifier::make( $props_resolver, $element_instance::get_props_schema() ),
Styles_Props_Modifier::make( $props_resolver, Style_Schema::get() ),
];
foreach ( $runners as $runner ) {
$element = $runner->run( $element );
}
return $element;
} );
}
private function replace_styles_ids( $element ) {
if ( empty( $element ) || ! is_array( $element ) ) {
return $element;
}
$element_instance = Plugin::$instance->elements_manager->create_element_instance( $element );
/** @var Atomic_Element_Base | Atomic_Widget_Base $element_instance */
if ( ! Utils::is_atomic( $element_instance ) ) {
return $element;
}
return Styles_Ids_Modifier::make()->run( $element );
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Elementor\Modules\AtomicWidgets\ImportExport\Modifiers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Import_Export_Props_Resolver;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Settings_Props_Modifier {
private Import_Export_Props_Resolver $props_resolver;
private array $schema;
public function __construct( Import_Export_Props_Resolver $props_resolver, array $schema ) {
$this->props_resolver = $props_resolver;
$this->schema = $schema;
}
public static function make( Import_Export_Props_Resolver $props_resolver, array $schema ) {
return new self( $props_resolver, $schema );
}
public function run( array $element ) {
if ( empty( $element['settings'] ) || ! is_array( $element['settings'] ) ) {
return $element;
}
$element['settings'] = $this->props_resolver->resolve(
$this->schema,
$element['settings']
);
return $element;
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Elementor\Modules\AtomicWidgets\ImportExport\Modifiers;
use Elementor\Core\Utils\Collection;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Styles_Ids_Modifier {
private Collection $old_to_new_ids;
public static function make() {
return new self();
}
public function run( array $element ) {
$this->old_to_new_ids = Collection::make();
$element = $this->replace_styles_ids( $element );
$element = $this->replace_references( $element );
return $element;
}
private function replace_styles_ids( array $element ) {
if ( empty( $element['styles'] ) || empty( $element['id'] ) ) {
return $element;
}
$styles = Collection::make( $element['styles'] )->map_with_keys( function ( $style, $id ) use ( $element ) {
$style['id'] = $this->generate_id( $element['id'], $id );
return [ $style['id'] => $style ];
} )->all();
$element['styles'] = $styles;
return $element;
}
private function replace_references( array $element ) {
if ( empty( $element['settings'] ) ) {
return $element;
}
$element['settings'] = Collection::make( $element['settings'] )->map( function ( $setting ) {
if ( ! $setting || ! Classes_Prop_Type::make()->validate( $setting ) ) {
return $setting;
}
$setting['value'] = Collection::make( $setting['value'] )
->map( fn( $style_id ) => $this->old_to_new_ids->get( $style_id ) ?? $style_id )
->all();
return $setting;
} )->all();
return $element;
}
private function generate_id( $element_id, $old_id ): string {
$id = Utils::generate_id( "e-{$element_id}-", $this->old_to_new_ids->values() );
$this->old_to_new_ids[ $old_id ] = $id;
return $id;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Elementor\Modules\AtomicWidgets\ImportExport\Modifiers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Import_Export_Props_Resolver;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Styles_Props_Modifier {
private Import_Export_Props_Resolver $props_resolver;
private array $schema;
public function __construct( Import_Export_Props_Resolver $props_resolver, array $schema ) {
$this->props_resolver = $props_resolver;
$this->schema = $schema;
}
public static function make( Import_Export_Props_Resolver $props_resolver, array $schema ) {
return new self( $props_resolver, $schema );
}
public function run( array $element ) {
if ( empty( $element['styles'] ) && ! is_array( $element['styles'] ) ) {
return $element;
}
foreach ( $element['styles'] as $style_key => $style ) {
if ( empty( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
continue;
}
foreach ( $style['variants'] as $variant_key => $variant ) {
if ( empty( $variant['props'] ) || ! is_array( $variant['props'] ) ) {
continue;
}
$element['styles'][ $style_key ]['variants'][ $variant_key ]['props'] = $this->props_resolver->resolve(
$this->schema,
$variant['props']
);
}
}
return $element;
}
}

View File

@@ -0,0 +1,315 @@
<?php
namespace Elementor\Modules\AtomicWidgets;
use Elementor\Core\Base\Module as BaseModule;
use Elementor\Core\Experiments\Manager as Experiments_Manager;
use Elementor\Core\Utils\Assets_Config_Provider;
use Elementor\Elements_Manager;
use Elementor\Modules\AtomicWidgets\DynamicTags\Dynamic_Tags_Module;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Youtube\Atomic_Youtube;
use Elementor\Modules\AtomicWidgets\Elements\Div_Block\Div_Block;
use Elementor\Modules\AtomicWidgets\Elements\Flexbox\Flexbox;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Heading\Atomic_Heading;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Image\Atomic_Image;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph\Atomic_Paragraph;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Button\Atomic_Button;
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Svg\Atomic_Svg;
use Elementor\Modules\AtomicWidgets\ImportExport\Atomic_Import_Export;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Combine_Array_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Export\Image_Src_Export_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Image_Src_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Image_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Import\Image_Src_Import_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Import_Export_Plain_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings\Classes_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings\Link_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Plain_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Color_Overlay_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Gradient_Overlay_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Color_Stop_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Multi_Props_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Shadow_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Size_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Stroke_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Image_Overlay_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Image_Overlay_Size_Scale_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Image_Position_Offset_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Overlay_Transformer;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers_Registry;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Color_Overlay_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Gradient_Overlay_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Image_Overlay_Size_Scale_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Image_Overlay_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Image_Position_Offset_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Overlay_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Box_Shadow_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Border_Radius_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Border_Width_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Color_Stop_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Gradient_Color_Stop_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Layout_Direction_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Shadow_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Stroke_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Atomic_Widget_Base_Styles;
use Elementor\Modules\AtomicWidgets\Styles\Atomic_Widget_Styles;
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
use Elementor\Modules\AtomicWidgets\Database\Atomic_Widgets_Database_Updater;
use Elementor\Plugin;
use Elementor\Widgets_Manager;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends BaseModule {
const EXPERIMENT_NAME = 'e_atomic_elements';
const EXPERIMENT_VERSION_3_30 = 'e_v_3_30';
const ENFORCE_CAPABILITIES_EXPERIMENT = 'atomic_widgets_should_enforce_capabilities';
const PACKAGES = [
'editor-canvas',
'editor-controls', // TODO: Need to be registered and not enqueued.
'editor-editing-panel',
'editor-elements', // TODO: Need to be registered and not enqueued.
'editor-props', // TODO: Need to be registered and not enqueued.
'editor-styles', // TODO: Need to be registered and not enqueued.
'editor-styles-repository',
];
public function get_name() {
return 'atomic-widgets';
}
public function __construct() {
parent::__construct();
if ( self::is_active() ) {
$this->register_experimental_features();
}
if ( Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) ) {
Dynamic_Tags_Module::instance()->register_hooks();
( new Atomic_Widget_Styles() )->register_hooks();
( new Atomic_Widget_Base_Styles() )->register_hooks();
( new Atomic_Import_Export() )->register_hooks();
( new Atomic_Widgets_Database_Updater() )->register();
add_filter( 'elementor/editor/v2/packages', fn( $packages ) => $this->add_packages( $packages ) );
add_filter( 'elementor/editor/localize_settings', fn( $settings ) => $this->add_styles_schema( $settings ) );
add_filter( 'elementor/widgets/register', fn( Widgets_Manager $widgets_manager ) => $this->register_widgets( $widgets_manager ) );
add_filter( 'elementor/usage/elements/element_title', fn( $title, $type ) => $this->get_element_usage_name( $title, $type ), 10, 2 );
add_action( 'elementor/elements/elements_registered', fn ( $elements_manager ) => $this->register_elements( $elements_manager ) );
add_action( 'elementor/editor/after_enqueue_scripts', fn() => $this->enqueue_scripts() );
add_action( 'elementor/frontend/after_register_scripts', fn() => $this->register_frontend_scripts() );
add_action( 'elementor/atomic-widgets/settings/transformers/register', fn ( $transformers ) => $this->register_settings_transformers( $transformers ) );
add_action( 'elementor/atomic-widgets/styles/transformers/register', fn ( $transformers ) => $this->register_styles_transformers( $transformers ) );
add_action( 'elementor/atomic-widgets/import/transformers/register', fn ( $transformers ) => $this->register_import_transformers( $transformers ) );
add_action( 'elementor/atomic-widgets/export/transformers/register', fn ( $transformers ) => $this->register_export_transformers( $transformers ) );
add_action( 'elementor/editor/templates/panel/category', fn () => $this->render_panel_category_chip() );
}
}
public static function get_experimental_data() {
return [
'name' => self::EXPERIMENT_NAME,
'title' => esc_html__( 'Atomic Widgets', 'elementor' ),
'description' => esc_html__( 'Enable atomic widgets.', 'elementor' ),
'hidden' => true,
'default' => Experiments_Manager::STATE_INACTIVE,
'release_status' => Experiments_Manager::RELEASE_STATUS_ALPHA,
];
}
private function register_experimental_features() {
Plugin::$instance->experiments->add_feature( [
'name' => 'e_indications_popover',
'title' => esc_html__( 'V4 Indications Popover', 'elementor' ),
'description' => esc_html__( 'Enable V4 Indication Popovers', 'elementor' ),
'hidden' => true,
'default' => Experiments_Manager::STATE_INACTIVE,
] );
Plugin::$instance->experiments->add_feature([
'name' => self::ENFORCE_CAPABILITIES_EXPERIMENT,
'title' => esc_html__( 'Enforce atomic widgets capabilities', 'elementor' ),
'description' => esc_html__( 'Enforce atomic widgets capabilities.', 'elementor' ),
'hidden' => true,
'default' => Experiments_Manager::STATE_ACTIVE,
'release_status' => Experiments_Manager::RELEASE_STATUS_DEV,
]);
Plugin::$instance->experiments->add_feature([
'name' => self::EXPERIMENT_VERSION_3_30,
'title' => esc_html__( 'Version 3.30', 'elementor' ),
'description' => esc_html__( 'Features for version 3.30.', 'elementor' ),
'hidden' => true,
'default' => Experiments_Manager::STATE_ACTIVE,
'release_status' => Experiments_Manager::RELEASE_STATUS_DEV,
]);
}
private function add_packages( $packages ) {
return array_merge( $packages, self::PACKAGES );
}
private function add_styles_schema( $settings ) {
if ( ! isset( $settings['atomic'] ) ) {
$settings['atomic'] = [];
}
$settings['atomic']['styles_schema'] = Style_Schema::get();
return $settings;
}
private function register_widgets( Widgets_Manager $widgets_manager ) {
$widgets_manager->register( new Atomic_Heading() );
$widgets_manager->register( new Atomic_Image() );
$widgets_manager->register( new Atomic_Paragraph() );
$widgets_manager->register( new Atomic_Svg() );
$widgets_manager->register( new Atomic_Button() );
$widgets_manager->register( new Atomic_Youtube() );
}
private function register_elements( Elements_Manager $elements_manager ) {
$elements_manager->register_element_type( new Div_Block() );
$elements_manager->register_element_type( new Flexbox() );
}
private function register_settings_transformers( Transformers_Registry $transformers ) {
$transformers->register_fallback( new Plain_Transformer() );
$transformers->register( Classes_Prop_Type::get_key(), new Classes_Transformer() );
$transformers->register( Image_Prop_Type::get_key(), new Image_Transformer() );
$transformers->register( Image_Src_Prop_Type::get_key(), new Image_Src_Transformer() );
$transformers->register( Link_Prop_Type::get_key(), new Link_Transformer() );
}
private function register_styles_transformers( Transformers_Registry $transformers ) {
$transformers->register_fallback( new Plain_Transformer() );
$transformers->register( Size_Prop_Type::get_key(), new Size_Transformer() );
$transformers->register( Box_Shadow_Prop_Type::get_key(), new Combine_Array_Transformer( ',' ) );
$transformers->register( Shadow_Prop_Type::get_key(), new Shadow_Transformer() );
$transformers->register( Stroke_Prop_Type::get_key(), new Stroke_Transformer() );
$transformers->register( Image_Prop_Type::get_key(), new Image_Transformer() );
$transformers->register( Image_Src_Prop_Type::get_key(), new Image_Src_Transformer() );
$transformers->register( Background_Image_Overlay_Prop_Type::get_key(), new Background_Image_Overlay_Transformer() );
$transformers->register( Background_Image_Overlay_Size_Scale_Prop_Type::get_key(), new Background_Image_Overlay_Size_Scale_Transformer() );
$transformers->register( Background_Image_Position_Offset_Prop_Type::get_key(), new Background_Image_Position_Offset_Transformer() );
$transformers->register( Background_Color_Overlay_Prop_Type::get_key(), new Background_Color_Overlay_Transformer() );
$transformers->register( Background_Overlay_Prop_Type::get_key(), new Background_Overlay_Transformer() );
$transformers->register( Background_Prop_Type::get_key(), new Background_Transformer() );
$transformers->register( Background_Gradient_Overlay_Prop_Type::get_key(), new Background_Gradient_Overlay_Transformer() );
$transformers->register( Color_Stop_Prop_Type::get_key(), new Color_Stop_Transformer() );
$transformers->register( Gradient_Color_Stop_Prop_Type::get_key(), new Combine_Array_Transformer( ',' ) );
$transformers->register(
Border_Radius_Prop_Type::get_key(),
new Multi_Props_Transformer( [ 'start-start', 'start-end', 'end-start', 'end-end' ], fn( $_, $key ) => "border-{$key}-radius" )
);
$transformers->register(
Border_Width_Prop_Type::get_key(),
new Multi_Props_Transformer( [ 'block-start', 'block-end', 'inline-start', 'inline-end' ], fn( $_, $key ) => "border-{$key}-width" )
);
$transformers->register(
Layout_Direction_Prop_Type::get_key(),
new Multi_Props_Transformer( [ 'column', 'row' ], fn( $prop_key, $key ) => "{$key}-{$prop_key}" )
);
$transformers->register(
Dimensions_Prop_Type::get_key(),
new Multi_Props_Transformer( [ 'block-start', 'block-end', 'inline-start', 'inline-end' ], fn( $prop_key, $key ) => "{$prop_key}-{$key}" )
);
}
public function register_import_transformers( Transformers_Registry $transformers ) {
$transformers->register_fallback( new Import_Export_Plain_Transformer() );
$transformers->register( Image_Src_Prop_Type::get_key(), new Image_Src_Import_Transformer() );
}
public function register_export_transformers( Transformers_Registry $transformers ) {
$transformers->register_fallback( new Import_Export_Plain_Transformer() );
$transformers->register( Image_Src_Prop_Type::get_key(), new Image_Src_Export_Transformer() );
}
public static function is_active(): bool {
return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME );
}
private function get_element_usage_name( $title, $type ) {
$element_instance = Plugin::$instance->elements_manager->get_element_types( $type );
$widget_instance = Plugin::$instance->widgets_manager->get_widget_types( $type );
if ( Utils::is_atomic( $element_instance ) || Utils::is_atomic( $widget_instance ) ) {
return $type;
}
return $title;
}
/**
* Enqueue the module scripts.
*
* @return void
*/
private function enqueue_scripts() {
wp_enqueue_script(
'elementor-atomic-widgets-editor',
$this->get_js_assets_url( 'atomic-widgets-editor' ),
[ 'elementor-editor' ],
ELEMENTOR_VERSION,
true
);
}
private function render_panel_category_chip() {
?><# if ( 'v4-elements' === name ) { #>
<span class="elementor-panel-heading-category-chip">
<?php echo esc_html__( 'Alpha', 'elementor' ); ?><i class="eicon-info"></i>
<span class="e-promotion-react-wrapper" data-promotion="v4_chip"></span>
</span>
<# } #><?php
}
private function register_frontend_scripts() {
$assets_config_provider = ( new Assets_Config_Provider() )
->set_path_resolver( function( $name ) {
return ELEMENTOR_ASSETS_PATH . "js/packages/{$name}/{$name}.asset.php";
} );
$assets_config_provider->load( 'frontend-handlers' );
$frontend_handlers_package_config = $assets_config_provider->get( 'frontend-handlers' );
if ( ! $frontend_handlers_package_config ) {
return;
}
wp_register_script(
$frontend_handlers_package_config['handle'],
$this->get_js_assets_url( 'packages/frontend-handlers/frontend-handlers' ),
$frontend_handlers_package_config['deps'],
ELEMENTOR_VERSION,
true
);
wp_register_script(
'elementor-youtube-handler',
$this->get_js_assets_url( 'youtube-handler' ),
[ $frontend_handlers_package_config['handle'] ],
ELEMENTOR_VERSION,
true
);
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Elementor\Modules\AtomicWidgets;
use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
use Elementor\Core\Experiments\Manager as Experiments_Manager;
use Elementor\Modules\GlobalClasses\Module as GlobalClassesModule;
use Elementor\Modules\NestedElements\Module as NestedElementsModule;
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule;
use Elementor\Plugin;
class Opt_In {
const EXPERIMENT_NAME = 'e_opt_in_v4';
const OPT_OUT_FEATURES = [
self::EXPERIMENT_NAME,
AtomicWidgetsModule::EXPERIMENT_NAME,
GlobalClassesModule::NAME,
];
const OPT_IN_FEATURES = [
self::EXPERIMENT_NAME,
'container',
NestedElementsModule::EXPERIMENT_NAME,
AtomicWidgetsModule::EXPERIMENT_NAME,
GlobalClassesModule::NAME,
];
public function init() {
$this->register_feature();
add_action( 'elementor/ajax/register_actions', fn( Ajax $ajax ) => $this->add_ajax_actions( $ajax ) );
}
private function register_feature() {
Plugin::$instance->experiments->add_feature([
'name' => self::EXPERIMENT_NAME,
'title' => esc_html__( 'Editor V4', 'elementor' ),
'description' => esc_html__( 'Enable Editor V4.', 'elementor' ),
'hidden' => true,
'default' => Experiments_Manager::STATE_INACTIVE,
'release_status' => Experiments_Manager::RELEASE_STATUS_ALPHA,
]);
}
private function opt_out_v4() {
foreach ( self::OPT_OUT_FEATURES as $feature ) {
$feature_key = Plugin::$instance->experiments->get_feature_option_key( $feature );
update_option( $feature_key, Experiments_Manager::STATE_INACTIVE );
}
}
private function opt_in_v4() {
foreach ( self::OPT_IN_FEATURES as $feature ) {
$feature_key = Plugin::$instance->experiments->get_feature_option_key( $feature );
update_option( $feature_key, Experiments_Manager::STATE_ACTIVE );
}
}
public function ajax_opt_out_v4() {
if ( ! current_user_can( 'manage_options' ) ) {
throw new \Exception( 'Permission denied' );
}
$this->opt_out_v4();
}
public function ajax_opt_in_v4() {
if ( ! current_user_can( 'manage_options' ) ) {
throw new \Exception( 'Permission denied' );
}
$this->opt_in_v4();
}
private function add_ajax_actions( Ajax $ajax ) {
$ajax->register_ajax_action( 'editor_v4_opt_in', fn() => $this->ajax_opt_in_v4() );
$ajax->register_ajax_action( 'editor_v4_opt_out', fn() => $this->ajax_opt_out_v4() );
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Parsers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Parse_Errors {
/**
* @var array<array{key: string, error: string}>
*/
private array $errors = [];
public static function make() {
return new static();
}
public function add( string $key, string $error ): self {
$this->errors[] = [
'key' => $key,
'error' => $error,
];
return $this;
}
public function is_empty(): bool {
return empty( $this->errors );
}
public function all(): array {
return $this->errors;
}
public function to_string(): string {
$errors = [];
foreach ( $this->errors as $error ) {
$errors[] = $error['key'] . ': ' . $error['error'];
}
return implode( ', ', $errors );
}
public function merge( Parse_Errors $errors, ?string $prefix = null ): self {
foreach ( $errors->all() as $error ) {
$new_key = $prefix ? "{$prefix}.{$error['key']}" : $error['key'];
$this->add( $new_key, $error['error'] );
}
return $this;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Parsers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Parse_Result {
private Parse_Errors $errors;
private $value;
public static function make() {
return new static();
}
public function __construct() {
$this->errors = Parse_Errors::make();
}
public function wrap( $value ): self {
$this->value = $value;
return $this;
}
public function unwrap() {
return $this->value;
}
public function is_valid(): bool {
return $this->errors->is_empty();
}
public function errors(): Parse_Errors {
return $this->errors;
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Parsers;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Props_Parser {
private array $schema;
public function __construct( array $schema ) {
$this->schema = $schema;
}
public static function make( array $schema ): self {
return new static( $schema );
}
/**
* @param array $props
* The key of each item represents the prop name (should match the schema),
* and the value is the prop value to validate
*/
public function validate( array $props ): Parse_Result {
$result = Parse_Result::make();
$validated = [];
foreach ( $this->schema as $key => $prop_type ) {
if ( ! ( $prop_type instanceof Prop_Type ) ) {
continue;
}
$value = $props[ $key ] ?? null;
$is_valid = $prop_type->validate( $value ?? $prop_type->get_default() );
if ( ! $is_valid ) {
$result->errors()->add( $key, 'invalid_value' );
continue;
}
if ( ! is_null( $value ) ) {
$validated[ $key ] = $value;
}
}
return $result->wrap( $validated );
}
/**
* @param array $props
* The key of each item represents the prop name (should match the schema),
* and the value is the prop value to sanitize
*/
public function sanitize( array $props ): Parse_Result {
$sanitized = [];
foreach ( $this->schema as $key => $prop_type ) {
if ( ! isset( $props[ $key ] ) ) {
continue;
}
$sanitized[ $key ] = $prop_type->sanitize( $props[ $key ] );
}
return Parse_Result::make()->wrap( $sanitized );
}
/**
* @param array $props
* The key of each item represents the prop name (should match the schema),
* and the value is the prop value to parse
*/
public function parse( array $props ): Parse_Result {
$validate_result = $this->validate( $props );
$sanitize_result = $this->sanitize( $validate_result->unwrap() );
$sanitize_result->errors()->merge( $validate_result->errors() );
return $sanitize_result;
}
}

View File

@@ -0,0 +1,233 @@
<?php
namespace Elementor\Modules\AtomicWidgets\Parsers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use Elementor\Modules\AtomicWidgets\Module;
use Elementor\Plugin;
class Style_Parser {
const VALID_TYPES = [
'class',
];
const VALID_STATES = [
'hover',
'active',
'focus',
null,
];
private array $schema;
public function __construct( array $schema ) {
$this->schema = $schema;
}
public static function make( array $schema ): self {
return new static( $schema );
}
/**
* @param array $style
* the style object to validate
*/
private function validate( array $style ): Parse_Result {
$validated_style = $style;
$result = Parse_Result::make();
if ( ! isset( $style['id'] ) || ! is_string( $style['id'] ) ) {
$result->errors()->add( 'id', 'missing_or_invalid' );
}
if ( ! isset( $style['type'] ) || ! in_array( $style['type'], self::VALID_TYPES, true ) ) {
$result->errors()->add( 'type', 'missing_or_invalid' );
}
if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) {
$result->errors()->add( 'label', 'missing_or_invalid' );
} elseif ( Plugin::$instance->experiments->is_feature_active( Module::EXPERIMENT_VERSION_3_30 ) ) {
$label_validation = $this->validate_style_label( $style['label'] );
if ( ! $label_validation['is_valid'] ) {
$result->errors()->add( 'label', $label_validation['error_message'] );
}
}
if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
$result->errors()->add( 'variants', 'missing_or_invalid' );
unset( $validated_style['variants'] );
return $result->wrap( $validated_style );
}
$props_parser = Props_Parser::make( $this->schema );
foreach ( $style['variants'] as $variant_index => $variant ) {
if ( ! isset( $variant['meta'] ) ) {
$result->errors()->add( 'meta', 'missing' );
continue;
}
$meta_result = $this->validate_meta( $variant['meta'] );
$result->errors()->merge( $meta_result->errors(), 'meta' );
if ( $meta_result->is_valid() ) {
$variant_result = $props_parser->validate( $variant['props'] );
$result->errors()->merge( $variant_result->errors(), "variants[$variant_index]" );
$validated_style['variants'][ $variant_index ]['props'] = $variant_result->unwrap();
} else {
unset( $validated_style['variants'][ $variant_index ] );
}
}
return $result->wrap( $validated_style );
}
private function validate_style_label( string $label ): array {
$label = strtolower( $label );
$reserved_class_names = [ 'container' ];
if ( strlen( $label ) > 50 ) {
return [
'is_valid' => false,
'error_message' => 'class_name_too_long',
];
}
if ( strlen( $label ) < 2 ) {
return [
'is_valid' => false,
'error_message' => 'class_name_too_short',
];
}
if ( in_array( $label, $reserved_class_names, true ) ) {
return [
'is_valid' => false,
'error_message' => 'reserved_class_name',
];
}
$regexes = [
[
'pattern' => '/^(|[^0-9].*)$/',
'message' => 'class_name_starts_with_digit',
],
[
'pattern' => '/^\S*$/',
'message' => 'class_name_contains_spaces',
],
[
'pattern' => '/^(|[a-zA-Z0-9_-]+)$/',
'message' => 'class_name_invalid_chars',
],
[
'pattern' => '/^(?!--).*/',
'message' => 'class_name_double_hyphen',
],
[
'pattern' => '/^(?!-[0-9])/',
'message' => 'class_name_starts_with_hyphen_digit',
],
];
foreach ( $regexes as $rule ) {
if ( ! preg_match( $rule['pattern'], $label ) ) {
return [
'is_valid' => false,
'error_message' => $rule['message'],
];
}
}
return [
'is_valid' => true,
'error_message' => null,
];
}
private function validate_meta( $meta ): Parse_Result {
$result = Parse_Result::make();
if ( ! is_array( $meta ) ) {
$result->errors()->add( 'meta', 'invalid_type' );
return $result;
}
if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) {
$result->errors()->add( 'state', 'missing_or_invalid_value' );
return $result;
}
// TODO: Validate breakpoint based on the existing breakpoints in the system [EDS-528]
if ( ! isset( $meta['breakpoint'] ) || ! is_string( $meta['breakpoint'] ) ) {
$result->errors()->add( 'breakpoint', 'missing_or_invalid_value' );
return $result;
}
return $result;
}
private function sanitize_meta( $meta ) {
if ( ! is_array( $meta ) ) {
return [];
}
if ( isset( $meta['breakpoint'] ) ) {
$meta['breakpoint'] = sanitize_key( $meta['breakpoint'] );
}
return $meta;
}
/**
* @param array $style
* the style object to sanitize
*/
private function sanitize( array $style ): Parse_Result {
$props_parser = Props_Parser::make( $this->schema );
if ( isset( $style['label'] ) ) {
$style['label'] = sanitize_text_field( $style['label'] );
}
if ( isset( $style['id'] ) ) {
$style['id'] = sanitize_key( $style['id'] );
}
if ( ! empty( $style['variants'] ) ) {
foreach ( $style['variants'] as $variant_index => $variant ) {
$style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] )->unwrap();
$style['variants'][ $variant_index ]['meta'] = $this->sanitize_meta( $variant['meta'] );
}
}
return Parse_Result::make()->wrap( $style );
}
/**
* @param array $style
* the style object to parse
*/
public function parse( array $style ): Parse_Result {
$validate_result = $this->validate( $style );
$sanitize_result = $this->sanitize( $validate_result->unwrap() );
$sanitize_result->errors()->merge( $validate_result->errors() );
return $sanitize_result;
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Color_Overlay_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background-color-overlay';
}
protected function define_shape(): array {
return [
'color' => Color_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Gradient_Overlay_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background-gradient-overlay';
}
protected function define_shape(): array {
return [
'type' => String_Prop_Type::make()->enum( [ 'linear', 'radial' ] ),
'angle' => Number_Prop_Type::make(),
'stops' => Gradient_Color_Stop_Prop_Type::make(),
'positions' => String_Prop_Type::make()->enum( self::get_position_enum_values() ),
];
}
private static function get_position_enum_values(): array {
return [
'center center',
'center left',
'center right',
'top center',
'top left',
'top right',
'bottom center',
'bottom left',
'bottom right',
];
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Image_Overlay_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background-image-overlay';
}
protected function define_shape(): array {
return [
'image' => Image_Prop_Type::make(),
'repeat' => String_Prop_Type::make()->enum( [ 'repeat', 'repeat-x', 'repeat-y', 'no-repeat' ] ),
'size' => Union_Prop_Type::make()
->add_prop_type( String_Prop_Type::make()->enum( [ 'auto', 'cover', 'contain' ] ) )
->add_prop_type( Background_Image_Overlay_Size_Scale_Prop_Type::make() ),
'position' => Union_Prop_Type::make()
->add_prop_type( String_Prop_Type::make()->enum( self::get_position_enum_values() ) )
->add_prop_type( Background_Image_Position_Offset_Prop_Type::make() ),
'attachment' => String_Prop_Type::make()->enum( [ 'fixed', 'scroll' ] ),
];
}
private static function get_position_enum_values(): array {
return [
'center center',
'center left',
'center right',
'top center',
'top left',
'top right',
'bottom center',
'bottom left',
'bottom right',
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Image_Overlay_Size_Scale_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background-image-size-scale';
}
protected function define_shape(): array {
return [
'width' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make() ),
'height' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make() ),
];
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Image_Position_Offset_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background-image-position-offset';
}
protected function define_shape(): array {
return [
'x' => Size_Prop_Type::make(),
'y' => Size_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Overlay_Prop_Type extends Array_Prop_Type {
public static function get_key(): string {
return 'background-overlay';
}
protected function define_item_type(): Prop_Type {
return Union_Prop_Type::make()
->add_prop_type( Background_Color_Overlay_Prop_Type::make() )
->add_prop_type( Background_Image_Overlay_Prop_Type::make() )
->add_prop_type( Background_Gradient_Overlay_Prop_Type::make() );
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Background_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'background';
}
protected function define_shape(): array {
return [
'background-overlay' => Background_Overlay_Prop_Type::make(),
'color' => Color_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Base;
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Array_Prop_Type implements Transformable_Prop_Type {
const KIND = 'array';
use Concerns\Has_Default;
use Concerns\Has_Generate;
use Concerns\Has_Meta;
use Concerns\Has_Required_Setting;
use Concerns\Has_Settings;
use Concerns\Has_Transformable_Validation;
protected Prop_Type $item_type;
public function __construct() {
$this->item_type = $this->define_item_type();
}
/**
* @return static
*/
public static function make() {
return new static();
}
/**
* @param Prop_Type $item_type
*
* @return $this
*/
public function set_item_type( Prop_Type $item_type ) {
$this->item_type = $item_type;
return $this;
}
public function get_item_type(): Prop_Type {
return $this->item_type;
}
public function validate( $value ): bool {
if ( is_null( $value ) ) {
return ! $this->is_required();
}
return (
$this->is_transformable( $value ) &&
$this->validate_value( $value['value'] )
);
}
protected function validate_value( $value ): bool {
if ( ! is_array( $value ) ) {
return false;
}
$prop_type = $this->get_item_type();
foreach ( $value as $item ) {
if ( $prop_type && ! $prop_type->validate( $item ) ) {
return false;
}
}
return true;
}
public function sanitize( $value ) {
$value['value'] = $this->sanitize_value( $value['value'] );
return $value;
}
public function sanitize_value( $value ) {
$prop_type = $this->get_item_type();
return array_map( function ( $item ) use ( $prop_type ) {
return $prop_type->sanitize( $item );
}, $value );
}
public function jsonSerialize(): array {
return [
'kind' => static::KIND,
'key' => static::get_key(),
'default' => $this->get_default(),
'meta' => (object) $this->get_meta(),
'settings' => (object) $this->get_settings(),
'item_prop_type' => $this->get_item_type(),
];
}
abstract protected function define_item_type(): Prop_Type;
}

View File

@@ -0,0 +1,141 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Base;
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Object_Prop_Type implements Transformable_Prop_Type {
const KIND = 'object';
use Concerns\Has_Default;
use Concerns\Has_Generate;
use Concerns\Has_Meta;
use Concerns\Has_Required_Setting;
use Concerns\Has_Settings;
use Concerns\Has_Transformable_Validation;
/**
* @var array<Prop_Type>
*/
protected array $shape;
public function __construct() {
$this->shape = $this->define_shape();
}
public function get_default() {
if ( null !== $this->default ) {
return $this->default;
}
foreach ( $this->get_shape() as $item ) {
// If the object has at least one property with default, return an empty object so
// it'll be iterable for processes like validation / transformation.
if ( $item->get_default() !== null ) {
return static::generate( [] );
}
}
return null;
}
/**
* @return static
*/
public static function make() {
return new static();
}
/**
* @param array $shape
*
* @return $this
*/
public function set_shape( array $shape ) {
$this->shape = $shape;
return $this;
}
public function get_shape(): array {
return $this->shape;
}
public function get_shape_field( $key ): ?Prop_Type {
return $this->shape[ $key ] ?? null;
}
public function validate( $value ): bool {
if ( is_null( $value ) ) {
return ! $this->is_required();
}
return (
$this->is_transformable( $value ) &&
$this->validate_value( $value['value'] )
);
}
protected function validate_value( $value ): bool {
if ( ! is_array( $value ) ) {
return false;
}
foreach ( $this->get_shape() as $key => $prop_type ) {
if ( ! ( $prop_type instanceof Prop_Type ) ) {
Utils::safe_throw( "Object prop type must have a prop type for key: $key" );
}
if ( ! $prop_type->validate( $value[ $key ] ?? $prop_type->get_default() ) ) {
return false;
}
}
return true;
}
public function sanitize( $value ) {
$value['value'] = $this->sanitize_value( $value['value'] );
return $value;
}
public function sanitize_value( $value ) {
foreach ( $this->get_shape() as $key => $prop_type ) {
if ( ! isset( $value[ $key ] ) ) {
continue;
}
$sanitized_value = $prop_type->sanitize( $value[ $key ] );
$value[ $key ] = $sanitized_value;
}
return $value;
}
public function jsonSerialize(): array {
$default = $this->get_default();
return [
'kind' => static::KIND,
'key' => static::get_key(),
'default' => is_array( $default ) ? (object) $default : $default,
'meta' => (object) $this->get_meta(),
'settings' => (object) $this->get_settings(),
'shape' => (object) $this->get_shape(),
];
}
/**
* @return array<Prop_Type>
*/
abstract protected function define_shape(): array;
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Base;
use Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Plain_Prop_Type implements Transformable_Prop_Type {
const KIND = 'plain';
use Concerns\Has_Default;
use Concerns\Has_Generate;
use Concerns\Has_Meta;
use Concerns\Has_Required_Setting;
use Concerns\Has_Settings;
use Concerns\Has_Transformable_Validation;
/**
* @return static
*/
public static function make() {
return new static();
}
public function validate( $value ): bool {
if ( is_null( $value ) ) {
return ! $this->is_required();
}
return (
$this->is_transformable( $value ) &&
$this->validate_value( $value['value'] )
);
}
public function sanitize( $value ) {
$value['value'] = $this->sanitize_value( $value['value'] );
return $value;
}
public function jsonSerialize(): array {
return [
'kind' => static::KIND,
'key' => static::get_key(),
'default' => $this->get_default(),
'meta' => (object) $this->get_meta(),
'settings' => (object) $this->get_settings(),
];
}
abstract public static function get_key(): string;
abstract protected function validate_value( $value ): bool;
abstract protected function sanitize_value( $value );
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Border_Radius_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'border-radius';
}
protected function define_shape(): array {
return [
'start-start' => Size_Prop_Type::make(),
'start-end' => Size_Prop_Type::make(),
'end-start' => Size_Prop_Type::make(),
'end-end' => Size_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Border_Width_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'border-width';
}
protected function define_shape(): array {
return [
'block-start' => Size_Prop_Type::make()->required(),
'block-end' => Size_Prop_Type::make()->required(),
'inline-start' => Size_Prop_Type::make()->required(),
'inline-end' => Size_Prop_Type::make()->required(),
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Box_Shadow_Prop_Type extends Array_Prop_Type {
public static function get_key(): string {
return 'box-shadow';
}
protected function define_item_type(): Prop_Type {
return Shadow_Prop_Type::make();
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Classes_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'classes';
}
protected function validate_value( $value ): bool {
if ( ! is_array( $value ) ) {
return false;
}
foreach ( $value as $class_name ) {
if ( ! is_string( $class_name ) || ! preg_match( '/^[a-z][a-z-_0-9]*$/i', $class_name ) ) {
return false;
}
}
return true;
}
protected function sanitize_value( $value ) {
if ( ! is_array( $value ) ) {
return null;
}
$sanitized = array_map(function ( $class_name ) {
if ( ! is_string( $class_name ) ) {
return null;
}
return sanitize_text_field( $class_name );
}, $value);
return array_filter($sanitized, function ( $class_name ) {
return ! empty( $class_name );
});
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Color_Prop_Type extends String_Prop_Type {
public static function get_key(): string {
return 'color';
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Color_Stop_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'color-stop';
}
protected function define_shape(): array {
return [
'color' => Color_Prop_Type::make(),
'offset' => Number_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Default {
protected $default = null;
/**
* @param $value
*
* @return $this
*/
public function default( $value ) {
$this->default = static::generate( $value );
return $this;
}
public function get_default() {
return $this->default;
}
abstract public static function generate( $value, $disable = false ): array;
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Generate {
public static function generate( $value, $disable = false ): array {
$value = [
'$$type' => static::get_key(),
'value' => $value,
];
if ( $disable ) {
$value['disabled'] = true;
}
return $value;
}
abstract public static function get_key(): string;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Meta {
protected array $meta = [];
/**
* @param $key
* @param $value
*
* @return $this
*/
public function meta( $key, $value = null ) {
$is_tuple = is_array( $key ) && 2 === count( $key );
if ( $is_tuple ) {
[ $key, $value ] = $key;
}
$this->meta[ $key ] = $value;
return $this;
}
public function get_meta(): array {
return $this->meta;
}
public function get_meta_item( $key, $default = null ) {
return array_key_exists( $key, $this->meta ) ? $this->meta[ $key ] : $default;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Required_Setting {
protected function is_required(): bool {
return $this->get_setting( 'required', false );
}
public function required() {
$this->setting( 'required', true );
return $this;
}
public function optional() {
$this->setting( 'required', false );
return $this;
}
abstract public function get_setting( string $key, $default = null );
abstract public function setting( $key, $value );
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Settings {
protected array $settings = [];
/**
* @param $key
* @param $value
*
* @return $this
*/
public function setting( $key, $value ) {
$this->settings[ $key ] = $value;
return $this;
}
public function get_settings(): array {
return $this->settings;
}
public function get_setting( string $key, $default = null ) {
return array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : $default;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Concerns;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
trait Has_Transformable_Validation {
protected function is_transformable( $value ): bool {
$satisfies_basic_shape = (
is_array( $value ) &&
array_key_exists( '$$type', $value ) &&
array_key_exists( 'value', $value ) &&
static::get_key() === $value['$$type']
);
$supports_disabling = (
! isset( $value['disabled'] ) ||
is_bool( $value['disabled'] )
);
return (
$satisfies_basic_shape &&
$supports_disabling
);
}
abstract public static function get_key(): string;
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Contracts;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
interface Prop_Type extends \JsonSerializable {
public function get_default();
public function validate( $value ): bool;
public function sanitize( $value );
public function get_meta(): array;
public function get_meta_item( string $key, $default = null );
public function get_settings(): array;
public function get_setting( string $key, $default = null );
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Contracts;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
interface Transformable_Prop_Type extends Prop_Type {
public static function get_key(): string;
public static function generate( $value, $disable = false ): array;
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Dimensions_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'dimensions';
}
protected function define_shape(): array {
return [
'block-start' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make()->enum( [ 'auto' ] ) ),
'inline-end' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make()->enum( [ 'auto' ] ) ),
'block-end' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make()->enum( [ 'auto' ] ) ),
'inline-start' => Union_Prop_Type::make()
->add_prop_type( Size_Prop_Type::make() )
->add_prop_type( String_Prop_Type::make()->enum( [ 'auto' ] ) ),
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Gradient_Color_Stop_Prop_Type extends Array_Prop_Type {
public static function get_key(): string {
return 'gradient-color-stop';
}
protected function define_item_type(): Prop_Type {
return Color_Stop_Prop_Type::make();
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Attachment_Id_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'image-attachment-id';
}
protected function validate_value( $value ): bool {
return is_numeric( $value );
}
protected function sanitize_value( $value ): int {
return (int) $value;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\Image\Image_Sizes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'image';
}
protected function define_shape(): array {
return [
'src' => Image_Src_Prop_Type::make()->required(),
'size' => String_Prop_Type::make()->enum( Image_Sizes::get_keys() )->required(),
];
}
public function default_url( string $url ): self {
$this->get_shape_field( 'src' )->default( [
'id' => null,
'url' => Url_Prop_Type::generate( $url ),
] );
return $this;
}
public function default_size( string $size ): self {
$this->get_shape_field( 'size' )->default( $size );
return $this;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Src_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'image-src';
}
protected function define_shape(): array {
return [
'id' => Image_Attachment_Id_Prop_Type::make(),
'url' => Url_Prop_Type::make(),
];
}
public function default_url( string $url ): self {
$this->default( [
'id' => null,
'url' => Url_Prop_Type::generate( $url ),
] );
return $this;
}
protected function validate_value( $value ): bool {
$only_one_key = count( array_filter( $value ) ) === 1;
return $only_one_key && parent::validate_value( $value );
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Layout_Direction_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'layout-direction';
}
protected function define_shape(): array {
return [
'column' => Size_Prop_Type::make(),
'row' => Size_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Link_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'link';
}
protected function define_shape(): array {
return [
'destination' => Union_Prop_Type::make()
->add_prop_type( Url_Prop_Type::make()->skip_validation() )
->add_prop_type( Number_Prop_Type::make() )
->required(),
'label' => Union_Prop_Type::make()
->add_prop_type( String_Prop_Type::make() ),
'isTargetBlank' => Boolean_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Primitives;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Boolean_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'boolean';
}
protected function validate_value( $value ): bool {
return is_bool( $value );
}
protected function sanitize_value( $value ) {
return (bool) $value;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Primitives;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Number_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'number';
}
protected function validate_value( $value ): bool {
return is_numeric( $value );
}
protected function sanitize_value( $value ) {
return (int) $value;
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes\Primitives;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class String_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'string';
}
public function enum( array $allowed_values ): self {
$all_are_strings = array_reduce(
$allowed_values,
fn ( $carry, $item ) => $carry && is_string( $item ),
true
);
if ( ! $all_are_strings ) {
Utils::safe_throw( 'All values in an enum must be strings.' );
}
$this->settings['enum'] = $allowed_values;
return $this;
}
public function get_enum() {
return $this->settings['enum'] ?? null;
}
public function regex( $pattern ) {
if ( ! is_string( $pattern ) ) {
Utils::safe_throw( 'Pattern must be a string, and valid regex pattern' );
}
$this->settings['regex'] = $pattern;
return $this;
}
public function get_regex() {
return $this->settings['regex'] ?? null;
}
protected function validate_value( $value ): bool {
return (
is_string( $value ) &&
( ! $this->get_enum() || $this->validate_enum( $value ) ) &&
( ! $this->get_regex() || $this->validate_regex( $value ) )
);
}
private function validate_enum( $value ): bool {
return in_array( $value, $this->settings['enum'], true );
}
private function validate_regex( $value ): bool {
return preg_match( $this->settings['regex'], $value );
}
protected function sanitize_value( $value ) {
return preg_replace_callback( '/^(\s*)(.*?)(\s*)$/', function ( $matches ) {
[, $leading, $value, $trailing ] = $matches;
return $leading . sanitize_text_field( $value ) . $trailing;
}, $value );
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Shadow_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'shadow';
}
protected function define_shape(): array {
return [
'hOffset' => Size_Prop_Type::make()->required(),
'vOffset' => Size_Prop_Type::make()->required(),
'blur' => Size_Prop_Type::make()->required(),
'spread' => Size_Prop_Type::make()->required(),
'color' => Color_Prop_Type::make()->required(),
'position' => String_Prop_Type::make()->enum( [ 'inset' ] ),
];
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Size_Prop_Type extends Object_Prop_Type {
const SUPPORTED_UNITS = [ 'px', 'em', 'rem', '%', 'vh', 'vw', 'vmin', 'vmax', 'auto', 'custom' ];
public static function get_key(): string {
return 'size';
}
protected function validate_value( $value ): bool {
if ( ! is_array( $value ) ||
! array_key_exists( 'size', $value ) ||
! array_key_exists( 'unit', $value ) ||
empty( $value['unit'] ) ||
! in_array( $value['unit'], static::SUPPORTED_UNITS, true )
) {
return false;
}
switch ( $value['unit'] ) {
case 'custom':
return null !== $value['size'];
case 'auto':
return ! $value['size'];
default:
return (
! in_array( $value['unit'], [ 'auto', 'custom' ], true ) &&
( ! empty( $value['size'] ) || 0 === $value['size'] ) &&
is_numeric( $value['size'] )
);
}
}
public function sanitize_value( $value ) {
$unit = sanitize_text_field( $value['unit'] );
if ( ! in_array( $value['unit'], [ 'auto', 'custom' ] ) ) {
return [
// The + operator cast the $value['size'] to numeric (either int or float - depends on the value)
'size' => +$value['size'],
'unit' => $unit,
];
}
return [
'size' => 'auto' === $value['unit'] ? '' : sanitize_text_field( $value['size'] ),
'unit' => $unit,
];
}
protected function define_shape(): array {
return [
'unit' => String_Prop_Type::make()->enum( self::SUPPORTED_UNITS )
->required(),
'size' => Union_Prop_Type::make()
->add_prop_type( String_Prop_Type::make() )
->add_prop_type( Number_Prop_Type::make() ),
];
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Stroke_Prop_Type extends Object_Prop_Type {
public static function get_key(): string {
return 'stroke';
}
protected function define_shape(): array {
return [
'color' => Color_Prop_Type::make(),
'width' => Size_Prop_Type::make(),
];
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Union_Prop_Type implements Prop_Type {
const KIND = 'union';
use Concerns\Has_Meta;
use Concerns\Has_Settings;
use Concerns\Has_Required_Setting;
protected $default = null;
/** @var Array<string, Transformable_Prop_Type> */
protected array $prop_types = [];
public static function make(): self {
return new static();
}
public static function create_from( Transformable_Prop_Type $prop_type ): self {
return static::make()
->add_prop_type( $prop_type )
->default( $prop_type->get_default() );
}
public function add_prop_type( Transformable_Prop_Type $prop_type ): self {
$this->prop_types[ $prop_type::get_key() ] = $prop_type;
return $this;
}
public function get_prop_types(): array {
return $this->prop_types;
}
public function get_prop_type( $type ): ?Transformable_Prop_Type {
return $this->prop_types[ $type ] ?? null;
}
private function get_prop_type_from_value( $value ): ?Prop_Type {
if ( isset( $value['$$type'] ) ) {
return $this->get_prop_type( $value['$$type'] );
}
if ( is_numeric( $value ) ) {
return $this->get_prop_type( 'number' );
}
if ( is_bool( $value ) ) {
return $this->get_prop_type( 'boolean' );
}
if ( is_string( $value ) ) {
return $this->get_prop_type( 'string' );
}
return null;
}
public function default( $value, ?string $type = null ): self {
$this->default = ! $type ?
$value :
[
'$$type' => $type,
'value' => $value,
];
return $this;
}
public function get_default() {
return $this->default;
}
public function validate( $value ): bool {
if ( is_null( $value ) ) {
return ! $this->is_required();
}
$prop_type = $this->get_prop_type_from_value( $value );
return $prop_type && $prop_type->validate( $value );
}
public function sanitize( $value ) {
$prop_type = $this->get_prop_type_from_value( $value );
return $prop_type ? $prop_type->sanitize( $value ) : null;
}
public function jsonSerialize(): array {
return [
'kind' => static::KIND,
'default' => $this->get_default(),
'meta' => $this->get_meta(),
'settings' => $this->get_settings(),
'prop_types' => $this->get_prop_types(),
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropTypes;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Url_Prop_Type extends Plain_Prop_Type {
public static function get_key(): string {
return 'url';
}
public function skip_validation(): self {
$this->settings['skip_validation'] = true;
return $this;
}
protected function validate_value( $value ): bool {
if ( ! empty( $this->settings['skip_validation'] ) ) {
return true;
}
return (bool) wp_http_validate_url( $value );
}
protected function sanitize_value( $value ) {
return esc_url_raw( $value );
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Import_Export_Props_Resolver extends Props_Resolver {
const CONTEXT_IMPORT = 'import';
const CONTEXT_EXPORT = 'export';
public static function for_import() {
return static::instance( self::CONTEXT_IMPORT );
}
public static function for_export() {
return static::instance( self::CONTEXT_EXPORT );
}
public function resolve( array $schema, array $props ): array {
$resolved = [];
foreach ( $schema as $key => $prop_type ) {
if ( ! ( $prop_type instanceof Prop_Type ) ) {
continue;
}
$value = $this->resolve_item( $props[ $key ] ?? null, $key, $prop_type );
if ( null === $value ) {
continue;
}
$resolved[ $key ] = $value;
}
return $resolved;
}
protected function resolve_item( $value, $key, Prop_Type $prop_type ) {
if ( null === $value ) {
return null;
}
if ( ! $this->is_transformable( $value ) ) {
return $value;
}
return $this->transform( $value, $key, $prop_type );
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Multi_Props {
public static function is( $value ) {
return (
! empty( $value['$$multi-props'] ) &&
true === $value['$$multi-props'] &&
array_key_exists( 'value', $value )
);
}
public static function generate( $value ) {
return [
'$$multi-props' => true,
'value' => $value,
];
}
public static function get_value( $value ) {
return $value['value'] ?? null;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Transformable_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Props_Resolver_Context {
private ?string $key = null;
private ?Transformable_Prop_Type $prop_type;
private bool $disabled = false;
public static function make(): self {
return new static();
}
public function set_key( ?string $key ): self {
$this->key = $key;
return $this;
}
public function set_disabled( bool $disabled ): self {
$this->disabled = $disabled;
return $this;
}
public function set_prop_type( Transformable_Prop_Type $prop_type ): self {
$this->prop_type = $prop_type;
return $this;
}
public function get_key(): ?string {
return $this->key;
}
public function is_disabled(): bool {
return $this->disabled;
}
public function get_prop_type(): ?Transformable_Prop_Type {
return $this->prop_type;
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Props_Resolver {
protected static array $instances = [];
protected Transformers_Registry $transformers_registry;
protected function __construct( Transformers_Registry $transformers_registry ) {
$this->transformers_registry = $transformers_registry;
}
protected static function instance( string $context ) {
if ( ! isset( static::$instances[ $context ] ) ) {
$instance = new static( new Transformers_Registry() );
static::$instances[ $context ] = $instance;
do_action(
"elementor/atomic-widgets/$context/transformers/register",
$instance->get_transformers_registry(),
$instance
);
}
return static::$instances[ $context ];
}
public static function reset(): void {
static::$instances = [];
}
public function get_transformers_registry(): Transformers_Registry {
return $this->transformers_registry;
}
protected function transform( $value, $key, Prop_Type $prop_type ) {
if ( $prop_type instanceof Union_Prop_Type ) {
$prop_type = $prop_type->get_prop_type( $value['$$type'] );
if ( ! $prop_type ) {
return null;
}
}
if ( $value['$$type'] !== $prop_type::get_key() ) {
return null;
}
if ( $prop_type instanceof Object_Prop_Type ) {
if ( ! is_array( $value['value'] ) ) {
return null;
}
$value['value'] = $this->resolve(
$prop_type->get_shape(),
$value['value']
);
}
if ( $prop_type instanceof Array_Prop_Type ) {
if ( ! is_array( $value['value'] ) ) {
return null;
}
$value['value'] = array_map(
fn( $item ) => $this->resolve_item( $item, null, $prop_type->get_item_type() ),
$value['value']
);
}
$transformer = $this->transformers_registry->get( $value['$$type'] );
if ( ! ( $transformer instanceof Transformer_Base ) ) {
return null;
}
try {
$context = Props_Resolver_Context::make()
->set_key( $key )
->set_disabled( (bool) ( $value['disabled'] ?? false ) )
->set_prop_type( $prop_type );
return $transformer->transform( $value['value'], $context );
} catch ( Exception $e ) {
return null;
}
}
protected function is_transformable( $value ): bool {
return (
! empty( $value['$$type'] ) &&
array_key_exists( 'value', $value )
);
}
abstract public function resolve( array $schema, array $props ): array;
abstract protected function resolve_item( $value, $key, Prop_Type $prop_type );
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Render_Props_Resolver extends Props_Resolver {
/**
* Each transformer can return a value that is also a transformable value,
* which means that it can be transformed again by another transformer.
* This constant defines the maximum depth of transformations to avoid infinite loops.
*/
const TRANSFORM_DEPTH_LIMIT = 3;
const CONTEXT_SETTINGS = 'settings';
const CONTEXT_STYLES = 'styles';
public static function for_styles(): self {
return static::instance( self::CONTEXT_STYLES );
}
public static function for_settings(): self {
return static::instance( self::CONTEXT_SETTINGS );
}
public function resolve( array $schema, array $props ): array {
$resolved = [];
foreach ( $schema as $key => $prop_type ) {
if ( ! ( $prop_type instanceof Prop_Type ) ) {
continue;
}
$transformed = $this->resolve_item(
$props[ $key ] ?? $prop_type->get_default(),
$key,
$prop_type
);
if ( Multi_Props::is( $transformed ) ) {
$resolved = array_merge( $resolved, Multi_Props::get_value( $transformed ) );
continue;
}
$resolved[ $key ] = $transformed;
}
return $resolved;
}
protected function resolve_item( $value, $key, Prop_Type $prop_type, int $depth = 0 ) {
if ( null === $value ) {
return null;
}
if ( ! $this->is_transformable( $value ) ) {
return $value;
}
if ( $depth >= self::TRANSFORM_DEPTH_LIMIT ) {
return null;
}
if ( isset( $value['disabled'] ) && true === $value['disabled'] ) {
return null;
}
$transformed = $this->transform( $value, $key, $prop_type );
return $this->resolve_item( $transformed, $key, $prop_type, $depth + 1 );
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Transformer_Base {
abstract public function transform( $value, Props_Resolver_Context $context );
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver;
use Elementor\Core\Utils\Collection;
use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Transformers_Registry extends Collection {
private ?Transformer_Base $fallback = null;
public function register( string $key, Transformer_Base $transformer ): self {
$this->items[ $key ] = $transformer;
return $this;
}
public function register_fallback( Transformer_Base $transformer ): self {
$this->fallback = $transformer;
return $this;
}
public function get( $key, $fallback = null ) {
return parent::get( $key, $fallback ?? $this->fallback );
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Array_Transformer extends Transformer_Base {
public function transform( $value, Props_Resolver_Context $context ) {
if ( ! is_array( $value ) ) {
return null;
}
return array_filter( $value );
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Combine_Array_Transformer extends Transformer_Base {
private string $separator;
public function __construct( string $separator ) {
$this->separator = $separator;
}
public function transform( $value, Props_Resolver_Context $context ) {
if ( ! is_array( $value ) ) {
return null;
}
return implode( $this->separator, array_filter( $value ) );
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Export;
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Src_Prop_Type;
use Elementor\Modules\AtomicWidgets\PropTypes\Url_Prop_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Src_Export_Transformer extends Transformer_Base {
public function transform( $value, Props_Resolver_Context $context ): ?array {
if ( ! empty( $value['url'] ) ) {
return Image_Src_Prop_Type::generate( [
'id' => null,
'url' => $value['url'],
], $context->is_disabled() );
}
if ( ! empty( $value['id'] ) && ! empty( $value['id']['value'] ) ) {
$image = wp_get_attachment_image_src( $value['id']['value'], 'full' );
if ( ! $image ) {
return null;
}
[ $src ] = $image;
return Image_Src_Prop_Type::generate( [
'id' => $value['id'],
'url' => Url_Prop_Type::generate( $src ),
], $context->is_disabled() );
}
return null;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Src_Transformer extends Transformer_Base {
/**
* This transformer (or rather this prop type) exists only to support dynamic images.
* Currently, the dynamic tags that return images return it with id & url no matter
* what, so we need to keep the same structure in the props.
*/
public function transform( $value, Props_Resolver_Context $context ) {
return [
'id' => isset( $value['id'] ) ? (int) $value['id'] : null,
'url' => $value['url'] ?? null,
];
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver_Context;
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Transformer extends Transformer_Base {
public function transform( $value, Props_Resolver_Context $context ) {
if ( ! empty( $value['src']['id'] ) ) {
$image_src = wp_get_attachment_image_src(
(int) $value['src']['id'],
$value['size'] ?? 'full'
);
if ( ! $image_src ) {
throw new \Exception( 'Cannot get image src.' );
}
[ $src, $width, $height ] = $image_src;
return [
'src' => $src,
'width' => (int) $width,
'height' => (int) $height,
'srcset' => wp_get_attachment_image_srcset( $value['src']['id'], $value['size'] ),
'alt' => get_post_meta( $value['src']['id'], '_wp_attachment_image_alt', true ),
];
}
if ( empty( $value['src']['url'] ) ) {
throw new \Exception( 'Invalid image URL.' );
}
return [
'src' => $value['src']['url'],
];
}
}

Some files were not shown because too many files have changed in this diff Show More