Initial commit: Atomaste website
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?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;
|
||||
|
||||
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 jsonSerialize(): array {
|
||||
return [
|
||||
'type' => 'control',
|
||||
'value' => [
|
||||
'type' => $this->get_type(),
|
||||
'bind' => $this->get_bind(),
|
||||
'label' => $this->label,
|
||||
'description' => $this->description,
|
||||
'props' => $this->get_props(),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\Controls;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Section implements JsonSerializable {
|
||||
private $label = null;
|
||||
private $description = null;
|
||||
private array $items = [];
|
||||
|
||||
public static function make(): self {
|
||||
return new static();
|
||||
}
|
||||
|
||||
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,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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 {
|
||||
|
||||
public function get_type(): string {
|
||||
return 'image';
|
||||
}
|
||||
|
||||
public function get_props(): array {
|
||||
return [
|
||||
'sizes' => Image_Sizes::get_all(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Controls\Types;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
|
||||
use Elementor\Modules\WpRest\Classes\WP_Post;
|
||||
|
||||
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( __( 'Paste URL or type', 'elementor' ) );
|
||||
$instance->set_endpoint( WP_Post::ENDPOINT );
|
||||
$instance->set_request_params( WP_Post::build_query_params( [
|
||||
WP_Post::KEYS_FORMAT_MAP_KEY => [
|
||||
'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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
[ $is_valid ] = Props_Parser::make( $tag['props_schema'] )->validate( $value['settings'] );
|
||||
|
||||
return $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'] );
|
||||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -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 [];
|
||||
}
|
||||
}
|
||||
@@ -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'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\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, Props_Resolver $props_resolver ) {
|
||||
$transformers->register(
|
||||
Dynamic_Prop_Type::get_key(),
|
||||
new Dynamic_Transformer(
|
||||
Plugin::$instance->dynamic_tags,
|
||||
$this->schemas,
|
||||
$props_resolver
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\DynamicTags;
|
||||
|
||||
use Elementor\Core\DynamicTags\Manager as Dynamic_Tags_Manager;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\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 Props_Resolver $props_resolver;
|
||||
|
||||
public function __construct(
|
||||
Dynamic_Tags_Manager $dynamic_tags_manager,
|
||||
Dynamic_Tags_Schemas $dynamic_tags_schemas,
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{% set classes = settings.classes | merge( [ base_styles.base ] ) | join(' ') %}
|
||||
|
||||
{% if settings.link.href %}
|
||||
<a
|
||||
href="{{ settings.link.href | e('full_url') }}"
|
||||
target="{{ settings.link.target }}"
|
||||
class="{{ classes }}"
|
||||
>
|
||||
{{ settings.text }}
|
||||
</a>
|
||||
{% else %}
|
||||
<button class="{{ classes }}">
|
||||
{{ settings.text }}
|
||||
</button>
|
||||
{% endif %}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?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\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\Modules\WpRest\Classes\WP_Post;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Atomic_Button extends Atomic_Widget_Base {
|
||||
use Has_Template;
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'a-button';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Atomic Button', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-e-button';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'classes' => Classes_Prop_Type::make()
|
||||
->default( [] ),
|
||||
|
||||
'text' => String_Prop_Type::make()
|
||||
->default( __( 'Click here', 'elementor' ) ),
|
||||
|
||||
'link' => Link_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
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' ) ),
|
||||
|
||||
Link_Control::bind_to( 'link' ),
|
||||
] ),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_base_styles(): array {
|
||||
$color_value = Color_Prop_Type::generate( 'white' );
|
||||
$font_family_value = String_Prop_Type::generate( 'Poppins' );
|
||||
$font_size_value = Size_Prop_Type::generate( [
|
||||
'size' => 16,
|
||||
'unit' => 'px',
|
||||
] );
|
||||
$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( [
|
||||
'top' => Size_Prop_Type::generate( [
|
||||
'size' => 12,
|
||||
'unit' => 'px',
|
||||
]),
|
||||
'right' => Size_Prop_Type::generate( [
|
||||
'size' => 24,
|
||||
'unit' => 'px',
|
||||
]),
|
||||
'bottom' => Size_Prop_Type::generate( [
|
||||
'size' => 12,
|
||||
'unit' => 'px',
|
||||
]),
|
||||
'left' => 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',
|
||||
] );
|
||||
$text_align_value = String_Prop_Type::generate( 'center' );
|
||||
$font_weight_value = String_Prop_Type::generate( '500' );
|
||||
|
||||
return [
|
||||
'base' => Style_Definition::make()
|
||||
->add_variant(
|
||||
Style_Variant::make()
|
||||
->add_prop( 'color', $color_value )
|
||||
->add_prop( 'font-family', $font_family_value )
|
||||
->add_prop( 'font-size', $font_size_value )
|
||||
->add_prop( 'background', $background_color_value )
|
||||
->add_prop( 'display', $display_value )
|
||||
->add_prop( 'font-weight', $font_weight_value )
|
||||
->add_prop( 'padding', $padding_value )
|
||||
->add_prop( 'text-align', $text_align_value )
|
||||
->add_prop( 'border-radius', $border_radius_value )
|
||||
->add_prop( 'border-width', $border_width_value )
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_templates(): array {
|
||||
return [
|
||||
'elementor/elements/atomic-button' => __DIR__ . '/atomic-button.html.twig',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements;
|
||||
|
||||
use Elementor\Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
|
||||
|
||||
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 = [];
|
||||
|
||||
public function __construct( $data = [], $args = null ) {
|
||||
parent::__construct( $data, $args );
|
||||
|
||||
$this->version = $data['version'] ?? '0.0';
|
||||
$this->styles = $data['styles'] ?? [];
|
||||
}
|
||||
|
||||
abstract protected function define_atomic_controls(): array;
|
||||
|
||||
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'] = [];
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Prop_Type>
|
||||
*/
|
||||
abstract protected static function define_props_schema(): array;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<{{ settings.tag | e('html_tag') }}
|
||||
class="{{ settings.classes | merge( [ base_styles.base ] ) | join(' ') }}"
|
||||
>
|
||||
{% if settings.link.href %}
|
||||
<a href="{{ settings.link.href | e('full_url') }}" target="{{ settings.link.target }}">
|
||||
{{ settings.title }}
|
||||
</a>
|
||||
{% else %}
|
||||
{{ settings.title }}
|
||||
{% endif %}
|
||||
</{{ settings.tag | e('html_tag') }}>
|
||||
@@ -0,0 +1,123 @@
|
||||
<?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\Textarea_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\Color_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\Modules\WpRest\Classes\WP_Post;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Atomic_Heading extends Atomic_Widget_Base {
|
||||
use Has_Template;
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'a-heading';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Atomic Heading', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-e-heading';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'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( __( 'Your Title Here', 'elementor' ) ),
|
||||
|
||||
'link' => Link_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
return [
|
||||
Section::make()
|
||||
->set_label( __( 'Content', 'elementor' ) )
|
||||
->set_items( [
|
||||
Textarea_Control::bind_to( 'title' )
|
||||
->set_label( __( 'Title', 'elementor' ) )
|
||||
->set_placeholder( __( 'Type your title here', 'elementor' ) ),
|
||||
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' ),
|
||||
] ),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_base_styles(): array {
|
||||
$color_value = Color_Prop_Type::generate( 'black' );
|
||||
$font_family_value = String_Prop_Type::generate( 'Inter' );
|
||||
$font_size_value = Size_Prop_Type::generate( [
|
||||
'size' => 3,
|
||||
'unit' => 'rem',
|
||||
] );
|
||||
$line_height_value = String_Prop_Type::generate( '1.1' );
|
||||
$font_weight_value = String_Prop_Type::generate( '600' );
|
||||
|
||||
return [
|
||||
'base' => Style_Definition::make()
|
||||
->add_variant(
|
||||
Style_Variant::make()
|
||||
->add_prop( 'color', $color_value )
|
||||
->add_prop( 'font-family', $font_family_value )
|
||||
->add_prop( 'font-size', $font_size_value )
|
||||
->add_prop( 'line-height', $line_height_value )
|
||||
->add_prop( 'font-weight', $font_weight_value )
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_templates(): array {
|
||||
return [
|
||||
'elementor/elements/atomic-heading' => __DIR__ . '/atomic-heading.html.twig',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{% if settings.link.href %}
|
||||
<a href="{{ settings.link.href | e('full_url') }}" target="{{ settings.link.target }}">
|
||||
{% endif %}
|
||||
<img class="{{ settings.classes | join(' ') }}"
|
||||
{% 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 %}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Image;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Has_Template;
|
||||
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;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Atomic_Image extends Atomic_Widget_Base {
|
||||
use Has_Template;
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'a-image';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Atomic Image', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-e-image';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'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(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
$content_section = Section::make()
|
||||
->set_label( esc_html__( 'Content', 'elementor' ) )
|
||||
->set_items( [
|
||||
Image_Control::bind_to( 'image' ),
|
||||
|
||||
Link_Control::bind_to( 'link' )
|
||||
->set_placeholder( __( 'Paste URL or type', 'elementor' ) ),
|
||||
] );
|
||||
|
||||
return [
|
||||
$content_section,
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_templates(): array {
|
||||
return [
|
||||
'elementor/elements/atomic-image' => __DIR__ . '/atomic-image.html.twig',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<p class="{{ settings.classes | merge( [ base_styles.base ] ) | join(' ') }}">
|
||||
{% if settings.link.href %}
|
||||
<a href="{{ settings.link.href | e('full_url') }}" target="{{ settings.link.target }}">
|
||||
{{ settings.paragraph }}
|
||||
</a>
|
||||
{% else %}
|
||||
{{ settings.paragraph }}
|
||||
{% endif %}
|
||||
</p>
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph;
|
||||
|
||||
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\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\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\Modules\WpRest\Classes\WP_Post;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Atomic_Paragraph extends Atomic_Widget_Base {
|
||||
use Has_Template;
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'a-paragraph';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Atomic Paragraph', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-paragraph';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'classes' => Classes_Prop_Type::make()
|
||||
->default( [] ),
|
||||
|
||||
'paragraph' => String_Prop_Type::make()
|
||||
->default( __( 'Type your paragraph here', 'elementor' ) ),
|
||||
|
||||
'link' => Link_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
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' ) ),
|
||||
|
||||
Link_Control::bind_to( 'link' ),
|
||||
] ),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_base_styles(): array {
|
||||
$color_value = Color_Prop_Type::generate( 'black' );
|
||||
$font_family_value = String_Prop_Type::generate( 'Poppins' );
|
||||
$font_size_value = Size_Prop_Type::generate( [
|
||||
'size' => 1.2,
|
||||
'unit' => 'rem',
|
||||
] );
|
||||
$line_height_value = String_Prop_Type::generate( '1.5' );
|
||||
|
||||
return [
|
||||
'base' => Style_Definition::make()
|
||||
->add_variant(
|
||||
Style_Variant::make()
|
||||
->add_prop( 'color', $color_value )
|
||||
->add_prop( 'font-family', $font_family_value )
|
||||
->add_prop( 'font-size', $font_size_value )
|
||||
->add_prop( 'line-height', $line_height_value )
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function get_templates(): array {
|
||||
return [
|
||||
'elementor/elements/atomic-paragraph' => __DIR__ . '/atomic-paragraph.html.twig',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?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\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\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_Svg extends Atomic_Widget_Base {
|
||||
const BASE_STYLE_KEY = 'base';
|
||||
const DEFAULT_SIZE = 'full';
|
||||
const DEFAULT_SVG_PATH = ELEMENTOR_ASSETS_URL . 'images/a-default-svg.svg';
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'a-svg';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Atomic SVG', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-svg';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'classes' => Classes_Prop_Type::make()->default( [] ),
|
||||
'svg' => Image_Src_Prop_Type::make()->default_url( self::DEFAULT_SVG_PATH ),
|
||||
'link' => Link_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
return [
|
||||
Section::make()
|
||||
->set_label( esc_html__( 'Content', 'elementor' ) )
|
||||
->set_items( [
|
||||
Svg_Control::bind_to( 'svg' ),
|
||||
Link_Control::bind_to( 'link' )
|
||||
->set_placeholder( __( 'Paste URL or type', 'elementor' ) ),
|
||||
] ),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_base_styles(): array {
|
||||
$width = Size_Prop_Type::generate( [
|
||||
'size' => 65,
|
||||
'unit' => 'px',
|
||||
] );
|
||||
$height = Size_Prop_Type::generate( [
|
||||
'size' => 65,
|
||||
'unit' => 'px',
|
||||
] );
|
||||
|
||||
return [
|
||||
self::BASE_STYLE_KEY => Style_Definition::make()
|
||||
->add_variant(
|
||||
Style_Variant::make()
|
||||
->add_prop( 'width', $width )
|
||||
->add_prop( 'height', $height )
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function render() {
|
||||
$settings = $this->get_atomic_settings();
|
||||
$svg_url = isset( $settings['svg']['url'] ) ? $settings['svg']['url'] : null;
|
||||
|
||||
if ( ! $svg_url && isset( $settings['svg']['id'] ) ) {
|
||||
$attachment = wp_get_attachment_image_src( $settings['svg']['id'], self::DEFAULT_SIZE );
|
||||
$svg_url = isset( $attachment[0] ) ? $attachment[0] : null;
|
||||
}
|
||||
|
||||
$svg = file_get_contents( $svg_url );
|
||||
$svg = $svg ? new \WP_HTML_Tag_Processor( $svg ) : null;
|
||||
|
||||
if ( $svg && $svg->next_tag( 'svg' ) ) {
|
||||
$this->set_svg_attributes( $svg, $settings );
|
||||
}
|
||||
|
||||
if ( $svg ) {
|
||||
$svg_html = ( new Svg_Sanitizer() )->sanitize( $svg->get_updated_html() );
|
||||
}
|
||||
|
||||
$svg_html = $svg_html ?? file_get_contents( self::DEFAULT_SVG_PATH );
|
||||
|
||||
if ( isset( $settings['link'] ) && ! empty( $settings['link']['href'] ) ) {
|
||||
$svg_html = sprintf( '<a href="%s" target="%s"> %s </a>', esc_url( $settings['link']['href'] ), esc_attr( $settings['link']['target'] ), $svg_html );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $svg_html;
|
||||
}
|
||||
|
||||
private function set_svg_attributes( \WP_HTML_Tag_Processor $svg, $settings ) {
|
||||
$svg->set_attribute( 'fill', 'currentColor' );
|
||||
$string_classes = implode( ' ', $settings['classes'] );
|
||||
$svg->add_class( $string_classes );
|
||||
$base_styles = $this->get_base_styles_dictionary()[ self::BASE_STYLE_KEY ];
|
||||
$svg->add_class( $base_styles );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?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 = [];
|
||||
|
||||
public function __construct( $data = [], $args = null ) {
|
||||
parent::__construct( $data, $args );
|
||||
|
||||
$this->version = $data['version'] ?? '0.0';
|
||||
$this->styles = $data['styles'] ?? [];
|
||||
}
|
||||
|
||||
abstract protected function define_atomic_controls(): array;
|
||||
|
||||
final 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['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;
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\Elements\Div_Block;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_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\PropTypes\Classes_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
|
||||
use Elementor\Plugin;
|
||||
use Elementor\Utils;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Div_Block extends Atomic_Element_Base {
|
||||
public static function get_type() {
|
||||
return 'div-block';
|
||||
}
|
||||
|
||||
public static function get_element_type(): string {
|
||||
return 'div-block';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return esc_html__( 'Div Block', 'elementor' );
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-div-block';
|
||||
}
|
||||
|
||||
protected static function define_props_schema(): array {
|
||||
return [
|
||||
'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(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function define_atomic_controls(): array {
|
||||
return [
|
||||
Section::make()
|
||||
->set_label( __( 'Settings', 'elementor' ) )
|
||||
->set_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_placeholder( __( 'Paste URL or type', 'elementor' ) ),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function get_style_depends() {
|
||||
return [ 'div-block' ];
|
||||
}
|
||||
|
||||
protected function _get_default_child_type( array $element_data ) {
|
||||
if ( 'div-block' === $element_data['elType'] ) {
|
||||
return Plugin::$instance->elements_manager->get_element_types( 'div-block' );
|
||||
}
|
||||
|
||||
return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
|
||||
}
|
||||
|
||||
protected function content_template() {
|
||||
?>
|
||||
<?php
|
||||
}
|
||||
|
||||
protected function add_render_attributes() {
|
||||
parent::add_render_attributes();
|
||||
$settings = $this->get_atomic_settings();
|
||||
|
||||
$attributes = [
|
||||
'class' => [
|
||||
'e-con',
|
||||
'e-div-block',
|
||||
...( $settings['classes'] ?? [] ),
|
||||
],
|
||||
];
|
||||
|
||||
if ( ! empty( $settings['link']['href'] ) ) {
|
||||
$attributes = array_merge( $attributes, $settings['link'] );
|
||||
}
|
||||
|
||||
$this->add_render_attribute( '_wrapper', $attributes );
|
||||
}
|
||||
|
||||
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' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?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\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 ) {
|
||||
[ $is_valid, $sanitized_style, $errors ] = $style_parser->parse( $style );
|
||||
|
||||
if ( ! $is_valid ) {
|
||||
throw new \Exception( esc_html( 'Styles validation failed. Invalid keys: ' . join( ', ', $errors ) ) );
|
||||
}
|
||||
|
||||
$styles[ $style_id ] = $sanitized_style;
|
||||
}
|
||||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
private function parse_atomic_settings( array $settings ): array {
|
||||
$schema = static::get_props_schema();
|
||||
$props_parser = Props_Parser::make( $schema );
|
||||
|
||||
[ $is_valid, $parsed, $errors ] = $props_parser->parse( $settings );
|
||||
|
||||
if ( ! $is_valid ) {
|
||||
throw new \Exception( esc_html( 'Settings validation failed. Invalid keys: ' . join( ', ', $errors ) ) );
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
public function get_atomic_controls() {
|
||||
$controls = $this->define_atomic_controls();
|
||||
$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'] );
|
||||
|
||||
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;
|
||||
|
||||
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 Props_Resolver::for_settings()->resolve( $schema, $props );
|
||||
}
|
||||
|
||||
public static function get_props_schema(): array {
|
||||
return apply_filters(
|
||||
'elementor/atomic-widgets/props-schema',
|
||||
static::define_props_schema()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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 {
|
||||
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_main_template() {
|
||||
$templates = $this->get_templates();
|
||||
|
||||
if ( count( $templates ) > 1 ) {
|
||||
Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' );
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ( $templates as $key => $path ) {
|
||||
// Returns first key in the array.
|
||||
return $key;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
abstract protected function get_templates(): array;
|
||||
}
|
||||
@@ -0,0 +1,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 );
|
||||
}
|
||||
}
|
||||
@@ -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.png';
|
||||
}
|
||||
|
||||
public static function get_background_placeholder_image() {
|
||||
return ELEMENTOR_ASSETS_URL . 'images/background-placeholder.png';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets;
|
||||
|
||||
use Elementor\Core\Base\Module as BaseModule;
|
||||
use Elementor\Core\Experiments\Manager as Experiments_Manager;
|
||||
use Elementor\Elements_Manager;
|
||||
use Elementor\Modules\AtomicWidgets\DynamicTags\Dynamic_Tags_Module;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Div_Block\Div_Block;
|
||||
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\PropsResolver\Transformers\Array_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Combine_Array_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings\Image_Src_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings\Image_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings\Link_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Primitive_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Color_Overlay_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Background_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Edge_Sizes_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Corner_Sizes_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Dimensions_Transformer;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles\Layout_Direction_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_Registry;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Background_Color_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\Layout_Direction_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Color_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Image_Attachment_Id_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\Primitives\Number_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Shadow_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Stroke_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Url_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\Plugin;
|
||||
use Elementor\Widgets_Manager;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Module extends BaseModule {
|
||||
const EXPERIMENT_NAME = 'atomic_widgets';
|
||||
|
||||
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-panels',
|
||||
'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 ( 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();
|
||||
|
||||
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_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/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_styles', fn() => $this->register_styles() );
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_experimental_data(): array {
|
||||
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 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() );
|
||||
}
|
||||
|
||||
private function register_elements( Elements_Manager $elements_manager ) {
|
||||
$elements_manager->register_element_type( new Div_Block() );
|
||||
}
|
||||
|
||||
private function register_settings_transformers( Transformers_Registry $transformers ) {
|
||||
// Primitives
|
||||
$transformers->register( Boolean_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( Number_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( String_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
|
||||
// Other
|
||||
$transformers->register( Classes_Prop_Type::get_key(), new Array_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( Image_Attachment_Id_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( Url_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( Link_Prop_Type::get_key(), new Link_Transformer() );
|
||||
}
|
||||
|
||||
private function register_styles_transformers( Transformers_Registry $transformers ) {
|
||||
// Primitives
|
||||
$transformers->register( Boolean_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( Number_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( String_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
|
||||
// Other
|
||||
$transformers->register( Dimensions_Prop_Type::get_key(), new Dimensions_Transformer() );
|
||||
$transformers->register( Size_Prop_Type::get_key(), new Size_Transformer() );
|
||||
$transformers->register( Color_Prop_Type::get_key(), new Primitive_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( Border_Radius_Prop_Type::get_key(), new Corner_Sizes_Transformer( fn( $corner ) => 'border-' . $corner . '-radius' ) );
|
||||
$transformers->register( Border_Width_Prop_Type::get_key(), new Edge_Sizes_Transformer( fn( $edge ) => 'border-' . $edge . '-width' ) );
|
||||
$transformers->register( Stroke_Prop_Type::get_key(), new Stroke_Transformer() );
|
||||
$transformers->register( Layout_Direction_Prop_Type::get_key(), new Layout_Direction_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( Image_Attachment_Id_Prop_Type::get_key(), new Primitive_Transformer() );
|
||||
$transformers->register( Url_Prop_Type::get_key(), new Primitive_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 Combine_Array_Transformer( ',' ) );
|
||||
$transformers->register( Background_Prop_Type::get_key(), new Background_Transformer() );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
}
|
||||
|
||||
public function register_styles() {
|
||||
wp_register_style(
|
||||
'div-block',
|
||||
$this->get_css_assets_url( 'div-block', 'assets/css/' ),
|
||||
[ 'elementor-frontend' ],
|
||||
ELEMENTOR_VERSION
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?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;
|
||||
private array $errors_bag = [];
|
||||
|
||||
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
|
||||
*
|
||||
* @return array{
|
||||
* 0: bool,
|
||||
* 1: array<string, mixed>,
|
||||
* 2: array<string>
|
||||
* }
|
||||
*/
|
||||
public function validate( array $props ): array {
|
||||
$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 ) {
|
||||
$this->errors_bag[] = $key;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! is_null( $value ) ) {
|
||||
$validated[ $key ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$is_valid = empty( $this->errors_bag );
|
||||
|
||||
return [
|
||||
$is_valid,
|
||||
$validated,
|
||||
$this->errors_bag,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function sanitize( array $props ): array {
|
||||
$sanitized = [];
|
||||
|
||||
foreach ( $this->schema as $key => $prop_type ) {
|
||||
if ( ! isset( $props[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sanitized[ $key ] = $prop_type->sanitize( $props[ $key ] );
|
||||
}
|
||||
|
||||
return $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
|
||||
*
|
||||
* @return array{
|
||||
* 0: bool,
|
||||
* 1: array<string, mixed>,
|
||||
* 2: array<string>
|
||||
* }
|
||||
*/
|
||||
public function parse( array $props ): array {
|
||||
[ $is_valid, $validated, $errors_bag ] = $this->validate( $props );
|
||||
|
||||
return [
|
||||
$is_valid,
|
||||
$this->sanitize( $validated ),
|
||||
$errors_bag,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Parsers;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Style_Parser {
|
||||
const VALID_TYPES = [
|
||||
'class',
|
||||
];
|
||||
|
||||
const VALID_STATES = [
|
||||
'hover',
|
||||
'active',
|
||||
'focus',
|
||||
null,
|
||||
];
|
||||
|
||||
private array $schema;
|
||||
private array $errors_bag = [];
|
||||
private $should_validate_id = true;
|
||||
|
||||
public function __construct( array $schema ) {
|
||||
$this->schema = $schema;
|
||||
}
|
||||
|
||||
public static function make( array $schema ): self {
|
||||
return new static( $schema );
|
||||
}
|
||||
|
||||
public function without_id() {
|
||||
$this->should_validate_id = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $style
|
||||
* the style object to validate
|
||||
*
|
||||
* @return array{
|
||||
* 0: bool,
|
||||
* 1: array<string, mixed>,
|
||||
* 2: array<string>
|
||||
* }
|
||||
*/
|
||||
public function validate( array $style ): array {
|
||||
$validated_style = $style;
|
||||
|
||||
if ( $this->should_validate_id && ( ! isset( $style['id'] ) || ! is_string( $style['id'] ) ) ) {
|
||||
$this->errors_bag[] = 'id';
|
||||
}
|
||||
|
||||
if ( ! isset( $style['type'] ) || ! in_array( $style['type'], self::VALID_TYPES, true ) ) {
|
||||
$this->errors_bag[] = 'type';
|
||||
}
|
||||
|
||||
if ( ! isset( $style['label'] ) || ! is_string( $style['label'] ) ) {
|
||||
$this->errors_bag[] = 'label';
|
||||
}
|
||||
|
||||
if ( ! isset( $style['variants'] ) || ! is_array( $style['variants'] ) ) {
|
||||
$this->errors_bag[] = 'variants';
|
||||
unset( $validated_style['variants'] );
|
||||
|
||||
return [
|
||||
false,
|
||||
$validated_style,
|
||||
$this->errors_bag,
|
||||
];
|
||||
}
|
||||
|
||||
$props_parser = Props_Parser::make( $this->schema );
|
||||
|
||||
foreach ( $style['variants'] as $variant_index => $variant ) {
|
||||
if ( ! isset( $variant['meta'] ) ) {
|
||||
$this->errors_bag[] = 'meta';
|
||||
continue;
|
||||
}
|
||||
|
||||
$is_variant_meta_valid = $this->validate_meta( $variant['meta'] );
|
||||
|
||||
if ( $is_variant_meta_valid ) {
|
||||
[, $validated_props, $variant_errors] = $props_parser->validate( $variant['props'] );
|
||||
$this->errors_bag = array_merge( $this->errors_bag, $variant_errors );
|
||||
|
||||
$validated_style['variants'][ $variant_index ]['props'] = $validated_props;
|
||||
} else {
|
||||
unset( $validated_style['variants'][ $variant_index ] );
|
||||
}
|
||||
}
|
||||
|
||||
$is_valid = empty( $this->errors_bag );
|
||||
|
||||
return [
|
||||
$is_valid,
|
||||
$validated_style,
|
||||
$this->errors_bag,
|
||||
];
|
||||
}
|
||||
|
||||
public function validate_meta( $meta ): bool {
|
||||
if ( ! is_array( $meta ) ) {
|
||||
$this->errors_bag[] = 'meta';
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'state', $meta ) || ! in_array( $meta['state'], self::VALID_STATES, true ) ) {
|
||||
$this->errors_bag[] = 'meta';
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Validate breakpoint based on the existing breakpoints in the system [EDS-528]
|
||||
if ( ! isset( $meta['breakpoint'] ) || ! is_string( $meta['breakpoint'] ) ) {
|
||||
$this->errors_bag[] = 'meta';
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $style
|
||||
* the style object to sanitize
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function sanitize( array $style ): array {
|
||||
$props_parser = Props_Parser::make( $this->schema );
|
||||
|
||||
if ( ! empty( $style['variants'] ) ) {
|
||||
foreach ( $style['variants'] as $variant_index => $variant ) {
|
||||
$style['variants'][ $variant_index ]['props'] = $props_parser->sanitize( $variant['props'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $style
|
||||
* the style object to parse
|
||||
*
|
||||
* @return array{
|
||||
* 0: bool,
|
||||
* 1: array<string, mixed>,
|
||||
* 2: array<string>
|
||||
* }
|
||||
*/
|
||||
public function parse( array $style ): array {
|
||||
[ $is_valid, $validated, $errors_bag ] = $this->validate( $style );
|
||||
|
||||
return [
|
||||
$is_valid,
|
||||
$this->sanitize( $validated ),
|
||||
$errors_bag,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropTypes;
|
||||
|
||||
class Background_Color_Overlay_Prop_Type extends Color_Prop_Type {
|
||||
public static function get_key(): string {
|
||||
return 'background-color-overlay';
|
||||
}
|
||||
}
|
||||
@@ -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\String_Prop_Type;
|
||||
|
||||
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',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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_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' => Size_Prop_Type::make(),
|
||||
'height' => Size_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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_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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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() );
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
@@ -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 [
|
||||
'top-left' => Size_Prop_Type::make(),
|
||||
'top-right' => Size_Prop_Type::make(),
|
||||
'bottom-right' => Size_Prop_Type::make(),
|
||||
'bottom-left' => Size_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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 [
|
||||
'top' => Size_Prop_Type::make(),
|
||||
'right' => Size_Prop_Type::make(),
|
||||
'bottom' => Size_Prop_Type::make(),
|
||||
'left' => Size_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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 Color_Gradient_Prop_Type extends Object_Prop_Type {
|
||||
public static function get_key(): string {
|
||||
return 'color-gradient';
|
||||
}
|
||||
|
||||
protected function define_shape(): array {
|
||||
return [
|
||||
'color' => Color_Prop_Type::make()->required(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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 ) {
|
||||
return [
|
||||
'$$type' => static::get_key(),
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
|
||||
abstract public static function get_key(): string;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?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;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropTypes;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_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 [
|
||||
'top' => Size_Prop_Type::make(),
|
||||
'right' => Size_Prop_Type::make(),
|
||||
'bottom' => Size_Prop_Type::make(),
|
||||
'left' => Size_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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 ) && Plugin::$instance->wp->wp_attachment_is_image( $value );
|
||||
}
|
||||
|
||||
protected function sanitize_value( $value ): int {
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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() )
|
||||
->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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?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 sanitize_text_field( $value );
|
||||
}
|
||||
}
|
||||
@@ -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' ] ),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropTypes;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Plain_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 Plain_Prop_Type {
|
||||
const SUPPORTED_UNITS = [ 'px', 'em', 'rem', '%', 'vh', 'vw', 'vmin', 'vmax' ];
|
||||
|
||||
public static function get_key(): string {
|
||||
return 'size';
|
||||
}
|
||||
|
||||
protected function validate_value( $value ): bool {
|
||||
return (
|
||||
is_array( $value ) &&
|
||||
array_key_exists( 'size', $value ) &&
|
||||
is_numeric( $value['size'] ) &&
|
||||
! empty( $value['unit'] ) &&
|
||||
in_array( $value['unit'], static::SUPPORTED_UNITS, true )
|
||||
);
|
||||
}
|
||||
|
||||
protected function sanitize_value( $value ) {
|
||||
return [
|
||||
'size' => (int) $value['size'],
|
||||
'unit' => sanitize_text_field( $value['unit'] ),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?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 static function validate_url( $value ): bool {
|
||||
return (bool) wp_http_validate_url( $value );
|
||||
}
|
||||
|
||||
protected function validate_value( $value ): bool {
|
||||
return self::validate_url( $value );
|
||||
}
|
||||
|
||||
protected function sanitize_value( $value ) {
|
||||
return esc_url_raw( $value );
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
<?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 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';
|
||||
|
||||
/**
|
||||
* @var array<string, Props_Resolver>
|
||||
*/
|
||||
private static array $instances = [];
|
||||
|
||||
private Transformers_Registry $transformers_registry;
|
||||
|
||||
private function __construct( Transformers_Registry $transformers_registry ) {
|
||||
$this->transformers_registry = $transformers_registry;
|
||||
}
|
||||
|
||||
public static function for_styles(): self {
|
||||
return self::instance( self::CONTEXT_STYLES );
|
||||
}
|
||||
|
||||
public static function for_settings(): self {
|
||||
return self::instance( self::CONTEXT_SETTINGS );
|
||||
}
|
||||
|
||||
private static function instance( string $context ): self {
|
||||
if ( ! isset( self::$instances[ $context ] ) ) {
|
||||
$instance = new self( new Transformers_Registry() );
|
||||
|
||||
self::$instances[ $context ] = $instance;
|
||||
|
||||
do_action(
|
||||
"elementor/atomic-widgets/$context/transformers/register",
|
||||
$instance->get_transformers_registry(),
|
||||
$instance
|
||||
);
|
||||
}
|
||||
|
||||
return self::$instances[ $context ];
|
||||
}
|
||||
|
||||
public static function reset(): void {
|
||||
self::$instances = [];
|
||||
}
|
||||
|
||||
public function get_transformers_registry(): Transformers_Registry {
|
||||
return $this->transformers_registry;
|
||||
}
|
||||
|
||||
public function resolve( array $schema, array $props ): array {
|
||||
$resolved = [];
|
||||
|
||||
foreach ( $schema as $key => $prop_type ) {
|
||||
if ( ! ( $prop_type instanceof Prop_Type ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$resolved[ $key ] = $props[ $key ] ?? $prop_type->get_default();
|
||||
}
|
||||
|
||||
return $this->assign_values( $resolved, $schema );
|
||||
}
|
||||
|
||||
private function transform( $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;
|
||||
}
|
||||
|
||||
if ( $prop_type instanceof Union_Prop_Type ) {
|
||||
$prop_type = $prop_type->get_prop_type( $value['$$type'] );
|
||||
|
||||
if ( ! $prop_type ) {
|
||||
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'] = $this->assign_values(
|
||||
$value['value'],
|
||||
$prop_type->get_item_type()
|
||||
);
|
||||
}
|
||||
|
||||
$transformer = $this->transformers_registry->get( $value['$$type'] );
|
||||
|
||||
if ( ! ( $transformer instanceof Transformer_Base ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$transformed_value = $transformer->transform( $value['value'], $key );
|
||||
|
||||
return $this->transform( $transformed_value, $key, $prop_type, $depth + 1 );
|
||||
} catch ( Exception $e ) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function is_transformable( $value ): bool {
|
||||
return (
|
||||
! empty( $value['$$type'] ) &&
|
||||
array_key_exists( 'value', $value )
|
||||
);
|
||||
}
|
||||
|
||||
private function assign_values( $values, $schema ) {
|
||||
$assigned = [];
|
||||
|
||||
foreach ( $values as $key => $value ) {
|
||||
$prop_type = $schema instanceof Prop_Type ? $schema : $schema[ $key ];
|
||||
|
||||
$transformed = $this->transform( $value, $key, $prop_type );
|
||||
|
||||
if ( Multi_Props::is( $transformed ) ) {
|
||||
$assigned = array_merge( $assigned, Multi_Props::get_value( $transformed ) );
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$assigned[ $key ] = $transformed;
|
||||
}
|
||||
|
||||
return $assigned;
|
||||
}
|
||||
}
|
||||
@@ -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, $key );
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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 {
|
||||
public function register( string $key, Transformer_Base $transformer ): self {
|
||||
if ( isset( $this->items[ $key ] ) ) {
|
||||
Utils::safe_throw( "{$key} transformer is already registered." );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->items[ $key ] = $transformer;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
|
||||
|
||||
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, $key ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array_filter( $value );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
|
||||
|
||||
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, $key ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return implode( $this->separator, array_filter( $value ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Primitive_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings;
|
||||
|
||||
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, $key ) {
|
||||
return [
|
||||
'id' => isset( $value['id'] ) ? (int) $value['id'] : null,
|
||||
'url' => $value['url'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings;
|
||||
|
||||
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, $key ) {
|
||||
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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Settings;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Url_Prop_Type;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Link_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ): ?array {
|
||||
$url = $this->extract_url( $value );
|
||||
|
||||
if ( ! Url_Prop_Type::validate_url( $url ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$link_attrs = [
|
||||
'href' => esc_url( $url ),
|
||||
'target' => $value['isTargetBlank'] ? '_blank' : '_self',
|
||||
];
|
||||
|
||||
return array_filter( $link_attrs );
|
||||
}
|
||||
|
||||
private function extract_url( $value ): ?string {
|
||||
$destination = $value['destination'];
|
||||
$post = is_numeric( $destination ) ? get_post( $destination ) : null;
|
||||
|
||||
return $post ? $post->guid : $destination;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Background_Color_Overlay_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$color = $value;
|
||||
return "linear-gradient($color, $color)";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Background_Image_Overlay_Size_Scale_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ): string {
|
||||
$default_custom_size = 'auto';
|
||||
$width = $value['width'] ?? $default_custom_size;
|
||||
$height = $value['height'] ?? $default_custom_size;
|
||||
|
||||
return $width . ' ' . $height;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Background_Image_Overlay_Transformer extends Transformer_Base {
|
||||
const DEFAULT_POSITION = '0% 0%';
|
||||
|
||||
public function transform( $value, $key ) {
|
||||
if ( ! isset( $value['image'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$image_url = $value['image']['src'];
|
||||
|
||||
$background_style = "url(\" $image_url \")";
|
||||
|
||||
if ( ! empty( $value['repeat'] ) ) {
|
||||
$background_style .= ' ' . $value['repeat'];
|
||||
}
|
||||
|
||||
if ( ! empty( $value['attachment'] ) ) {
|
||||
$background_style .= ' ' . $value['attachment'];
|
||||
}
|
||||
|
||||
$position_and_size_style = $this->get_position_and_size_style( $value );
|
||||
|
||||
if ( ! empty( $position_and_size_style ) ) {
|
||||
$background_style .= ' ' . $position_and_size_style;
|
||||
}
|
||||
|
||||
return $background_style;
|
||||
}
|
||||
|
||||
private function get_position_and_size_style( array $value ): string {
|
||||
if ( ! isset( $value['size'] ) && ! isset( $value['position'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( ! isset( $value['size'] ) ) {
|
||||
return $value['position'];
|
||||
}
|
||||
|
||||
$position = $value['position'] ?? self::DEFAULT_POSITION;
|
||||
|
||||
return $position . ' / ' . $value['size'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
|
||||
class Background_Image_Position_Offset_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ): string {
|
||||
return ( $value['x'] ?? '0px' ) . ' ' . ( $value['y'] ?? '0px' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Background_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$overlay = $value['background-overlay'] ?? '';
|
||||
$color = $value['color'] ?? '';
|
||||
|
||||
return trim( "$overlay $color" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Multi_Props;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Corner_Sizes_Transformer extends Transformer_Base {
|
||||
private $key_generator;
|
||||
|
||||
public function __construct( callable $key_generator ) {
|
||||
$this->key_generator = $key_generator;
|
||||
}
|
||||
|
||||
public function transform( $value, $key ) {
|
||||
$corners = Collection::make( $value )
|
||||
->only( [ 'top-left', 'top-right', 'bottom-right', 'bottom-left' ] )
|
||||
->filter()
|
||||
->map_with_keys( fn( $corner, $corner_key ) => [ call_user_func( $this->key_generator, $corner_key ) => $corner ] )
|
||||
->all();
|
||||
|
||||
return Multi_Props::generate( $corners );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Multi_Props;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Dimensions_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$dimensions = Collection::make( $value )
|
||||
->only( [ 'top', 'right', 'bottom', 'left' ] )
|
||||
->filter()
|
||||
->map_with_keys( fn( $dimension, $side ) => [ $key . '-' . $side => $dimension ] )
|
||||
->all();
|
||||
|
||||
return Multi_Props::generate( $dimensions );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Multi_Props;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Edge_Sizes_Transformer extends Transformer_Base {
|
||||
private $key_generator;
|
||||
|
||||
public function __construct( callable $key_generator ) {
|
||||
$this->key_generator = $key_generator;
|
||||
}
|
||||
|
||||
public function transform( $value, $key ) {
|
||||
$edges = Collection::make( $value )
|
||||
->only( [ 'top', 'right', 'bottom', 'left' ] )
|
||||
->filter()
|
||||
->map_with_keys( fn( $edge, $edge_key ) => [ call_user_func( $this->key_generator, $edge_key ) => $edge ] )
|
||||
->all();
|
||||
|
||||
return Multi_Props::generate( $edges );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Multi_Props;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Layout_Direction_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ): array {
|
||||
$gap = Collection::make( $value )
|
||||
->only( [ 'row', 'column' ] )
|
||||
->filter()
|
||||
->map_with_keys( fn( $gap, $gap_direction ) => [ $gap_direction . '-' . $key => $gap ] )
|
||||
->all();
|
||||
|
||||
return Multi_Props::generate( $gap );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Shadow_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$val = array_filter( [
|
||||
$value['hOffset'],
|
||||
$value['vOffset'],
|
||||
$value['blur'],
|
||||
$value['spread'],
|
||||
$value['color'],
|
||||
$value['position'] ?? '',
|
||||
] );
|
||||
|
||||
return implode( ' ', $val );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Size_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$size = (int) $value['size'];
|
||||
$unit = $value['unit'];
|
||||
|
||||
return $size . $unit;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\PropsResolver\Transformers\Styles;
|
||||
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformer_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Stroke_Transformer extends Transformer_Base {
|
||||
public function transform( $value, $key ) {
|
||||
$width = $value['width'];
|
||||
$color = $value['color'];
|
||||
|
||||
return $width . ' ' . $color;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
use Elementor\Core\Files\CSS\Post as Post_CSS;
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
|
||||
use Elementor\Plugin;
|
||||
|
||||
class Atomic_Widget_Base_Styles {
|
||||
public function register_hooks() {
|
||||
add_action( 'elementor/css-file/post/parse', fn( Post_CSS $post ) => $this->inject_elements_base_styles( $post ), 10 );
|
||||
}
|
||||
|
||||
private function inject_elements_base_styles( Post_CSS $post ) {
|
||||
if ( ! Plugin::$instance->kits_manager->is_kit( $post->get_post_id() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$elements = Plugin::$instance->elements_manager->get_element_types();
|
||||
$widgets = Plugin::$instance->widgets_manager->get_widget_types();
|
||||
|
||||
$base_styles = Collection::make( $elements )
|
||||
->merge( $widgets )
|
||||
->filter( fn( $element ) => $element instanceof Atomic_Widget_Base || $element instanceof Atomic_Element_Base )
|
||||
->map( fn( $element ) => $element->get_base_styles() )
|
||||
->flatten()
|
||||
->all();
|
||||
|
||||
$css = Styles_Renderer::make(
|
||||
Plugin::$instance->breakpoints->get_breakpoints_config()
|
||||
)->on_prop_transform( function( $key, $value ) use ( &$post ) {
|
||||
if ( 'font-family' !== $key ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post->add_font( $value );
|
||||
} )->render( $base_styles );
|
||||
|
||||
$post->get_stylesheet()->add_raw_css( $css );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
use Elementor\Core\Files\CSS\Post;
|
||||
use Elementor\Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base;
|
||||
use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
|
||||
use Elementor\Plugin;
|
||||
|
||||
class Atomic_Widget_Styles {
|
||||
public function register_hooks() {
|
||||
add_action( 'elementor/element/parse_css', fn( Post $post, Element_Base $element ) => $this->parse_element_style( $post, $element ), 10, 2 );
|
||||
}
|
||||
|
||||
private function parse_element_style( Post $post, Element_Base $element ) {
|
||||
if ( ! ( $element instanceof Atomic_Widget_Base || $element instanceof Atomic_Element_Base )
|
||||
|| Post::class !== get_class( $post ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$styles = $element->get_raw_data()['styles'];
|
||||
|
||||
if ( empty( $styles ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$css = Styles_Renderer::make(
|
||||
Plugin::$instance->breakpoints->get_breakpoints_config()
|
||||
)->on_prop_transform( function( $key, $value ) use ( &$post ) {
|
||||
if ( 'font-family' !== $key ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post->add_font( $value );
|
||||
} )->render( $styles );
|
||||
|
||||
$post->get_stylesheet()->add_raw_css( $css );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
class Style_Definition {
|
||||
private string $type = 'class';
|
||||
private string $label = '';
|
||||
|
||||
/** @var Style_Variant[] */
|
||||
private array $variants = [];
|
||||
|
||||
public static function make(): self {
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function set_type( string $type ): self {
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set_label( string $label ): self {
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function add_variant( Style_Variant $variant ): self {
|
||||
$this->variants[] = $variant->build();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build( string $id ): array {
|
||||
return [
|
||||
'id' => $id,
|
||||
'type' => $this->type,
|
||||
'label' => $this->label,
|
||||
'variants' => $this->variants,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
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_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Layout_Direction_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Stroke_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
|
||||
use Elementor\Modules\AtomicWidgets\PropTypes\Gap_Prop_Type;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Style_Schema {
|
||||
public static function get() {
|
||||
return array_merge(
|
||||
self::get_size_props(),
|
||||
self::get_position_props(),
|
||||
self::get_typography_props(),
|
||||
self::get_spacing_props(),
|
||||
self::get_border_props(),
|
||||
self::get_background_props(),
|
||||
self::get_effects_props(),
|
||||
self::get_layout_props(),
|
||||
self::get_alignment_props(),
|
||||
);
|
||||
}
|
||||
|
||||
private static function get_size_props() {
|
||||
return [
|
||||
'width' => Size_Prop_Type::make(),
|
||||
'height' => Size_Prop_Type::make(),
|
||||
'min-width' => Size_Prop_Type::make(),
|
||||
'min-height' => Size_Prop_Type::make(),
|
||||
'max-width' => Size_Prop_Type::make(),
|
||||
'max-height' => Size_Prop_Type::make(),
|
||||
'overflow' => String_Prop_Type::make()->enum([
|
||||
'visible',
|
||||
'hidden',
|
||||
'auto',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_position_props() {
|
||||
return [
|
||||
'position' => String_Prop_Type::make()->enum([
|
||||
'static',
|
||||
'relative',
|
||||
'absolute',
|
||||
'fixed',
|
||||
'sticky',
|
||||
]),
|
||||
'top' => Size_Prop_Type::make(),
|
||||
'right' => Size_Prop_Type::make(),
|
||||
'bottom' => Size_Prop_Type::make(),
|
||||
'left' => Size_Prop_Type::make(),
|
||||
'z-index' => Number_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_typography_props() {
|
||||
return [
|
||||
'font-family' => String_Prop_Type::make(),
|
||||
'font-weight' => String_Prop_Type::make()->enum([
|
||||
'100',
|
||||
'200',
|
||||
'300',
|
||||
'400',
|
||||
'500',
|
||||
'600',
|
||||
'700',
|
||||
'800',
|
||||
'900',
|
||||
'normal',
|
||||
'bold',
|
||||
'bolder',
|
||||
'lighter',
|
||||
]),
|
||||
'font-size' => Size_Prop_Type::make(),
|
||||
'color' => Color_Prop_Type::make(),
|
||||
'letter-spacing' => Size_Prop_Type::make(),
|
||||
'word-spacing' => Size_Prop_Type::make(),
|
||||
'line-height' => Size_Prop_Type::make(),
|
||||
'text-align' => String_Prop_Type::make()->enum([
|
||||
'start',
|
||||
'center',
|
||||
'end',
|
||||
'justify',
|
||||
]),
|
||||
'font-style' => String_Prop_Type::make()->enum([
|
||||
'normal',
|
||||
'italic',
|
||||
'oblique',
|
||||
]),
|
||||
// TODO: validate text-decoration in more specific way [EDS-524]
|
||||
'text-decoration' => String_Prop_Type::make(),
|
||||
'text-transform' => String_Prop_Type::make()->enum([
|
||||
'none',
|
||||
'capitalize',
|
||||
'uppercase',
|
||||
'lowercase',
|
||||
]),
|
||||
'direction' => String_Prop_Type::make()->enum([
|
||||
'ltr',
|
||||
'rtl',
|
||||
]),
|
||||
'-webkit-text-stroke' => Stroke_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_spacing_props() {
|
||||
return [
|
||||
'padding' => Union_Prop_Type::make()->add_prop_type( Dimensions_Prop_Type::make() )->add_prop_type( Size_Prop_Type::make() ),
|
||||
'margin' => Union_Prop_Type::make()->add_prop_type( Dimensions_Prop_Type::make() )->add_prop_type( Size_Prop_Type::make() ),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_border_props() {
|
||||
return [
|
||||
'border-radius' => Union_Prop_Type::make()->add_prop_type(
|
||||
Size_Prop_Type::make()
|
||||
)->add_prop_type(
|
||||
Border_Radius_Prop_Type::make()
|
||||
),
|
||||
'border-width' => Union_Prop_Type::make()->add_prop_type( Size_Prop_Type::make() )->add_prop_type( Border_Width_Prop_Type::make() ),
|
||||
'border-color' => Color_Prop_Type::make(),
|
||||
'border-style' => String_Prop_Type::make()->enum([
|
||||
'none',
|
||||
'hidden',
|
||||
'dotted',
|
||||
'dashed',
|
||||
'solid',
|
||||
'double',
|
||||
'groove',
|
||||
'ridge',
|
||||
'inset',
|
||||
'outset',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_background_props() {
|
||||
return [
|
||||
'background' => Background_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_effects_props() {
|
||||
return [
|
||||
'box-shadow' => Box_Shadow_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_layout_props() {
|
||||
return [
|
||||
'display' => String_Prop_Type::make()->enum([
|
||||
'block',
|
||||
'inline',
|
||||
'inline-block',
|
||||
'flex',
|
||||
'inline-flex',
|
||||
'grid',
|
||||
'inline-grid',
|
||||
'flow-root',
|
||||
'none',
|
||||
'contents',
|
||||
]),
|
||||
'flex-direction' => String_Prop_Type::make()->enum([
|
||||
'row',
|
||||
'row-reverse',
|
||||
'column',
|
||||
'column-reverse',
|
||||
]),
|
||||
'gap' => Union_Prop_Type::make()
|
||||
->add_prop_type( Layout_Direction_Prop_Type::make() )
|
||||
->add_prop_type( Size_Prop_Type::make() ),
|
||||
'flex-wrap' => String_Prop_Type::make()->enum([
|
||||
'wrap',
|
||||
'nowrap',
|
||||
'wrap-reverse',
|
||||
]),
|
||||
'flex-grow' => Number_Prop_Type::make(),
|
||||
'flex-shrink' => Number_Prop_Type::make(),
|
||||
'flex-basis' => Size_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function get_alignment_props() {
|
||||
return [
|
||||
'justify-content' => String_Prop_Type::make()->enum([
|
||||
'center',
|
||||
'start',
|
||||
'end',
|
||||
'flex-start',
|
||||
'flex-end',
|
||||
'left',
|
||||
'right',
|
||||
'normal',
|
||||
'space-between',
|
||||
'space-around',
|
||||
'space-evenly',
|
||||
'stretch',
|
||||
]),
|
||||
'align-items' => String_Prop_Type::make()->enum([
|
||||
'normal',
|
||||
'stretch',
|
||||
'center',
|
||||
'start',
|
||||
'end',
|
||||
'flex-start',
|
||||
'flex-end',
|
||||
'self-start',
|
||||
'self-end',
|
||||
'anchor-center',
|
||||
]),
|
||||
'align-self' => String_Prop_Type::make()->enum([
|
||||
'auto',
|
||||
'normal',
|
||||
'center',
|
||||
'start',
|
||||
'end',
|
||||
'self-start',
|
||||
'self-end',
|
||||
'flex-start',
|
||||
'flex-end',
|
||||
'anchor-center',
|
||||
'baseline',
|
||||
'first baseline',
|
||||
'last baseline',
|
||||
'stretch',
|
||||
]),
|
||||
'order' => Number_Prop_Type::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
class Style_Variant {
|
||||
private ?string $breakpoint = null;
|
||||
private ?string $state = null;
|
||||
|
||||
/** @var array<string, array> */
|
||||
private array $props = [];
|
||||
|
||||
public static function make(): self {
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function set_breakpoint( string $breakpoint ): self {
|
||||
$this->breakpoint = $breakpoint;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set_state( string $state ): self {
|
||||
$this->state = $state;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function add_prop( string $key, $value ): self {
|
||||
$this->props[ $key ] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build(): array {
|
||||
return [
|
||||
'meta' => [
|
||||
'breakpoint' => $this->breakpoint,
|
||||
'state' => $this->state,
|
||||
],
|
||||
'props' => $this->props,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace Elementor\Modules\AtomicWidgets\Styles;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver;
|
||||
|
||||
class Styles_Renderer {
|
||||
const DEFAULT_SELECTOR_PREFIX = '.elementor';
|
||||
|
||||
/**
|
||||
* @var array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}>
|
||||
*/
|
||||
private array $breakpoints;
|
||||
|
||||
private $on_prop_transform;
|
||||
|
||||
private string $selector_prefix;
|
||||
|
||||
/**
|
||||
* @param array<string, array{direction: 'min' | 'max', value: int, is_enabled: boolean}> $breakpoints
|
||||
* @param string $selector_prefix
|
||||
*/
|
||||
private function __construct( array $breakpoints, string $selector_prefix = self::DEFAULT_SELECTOR_PREFIX ) {
|
||||
$this->breakpoints = $breakpoints;
|
||||
$this->selector_prefix = $selector_prefix;
|
||||
}
|
||||
|
||||
public static function make( array $breakpoints, string $selector_prefix = self::DEFAULT_SELECTOR_PREFIX ): self {
|
||||
return new self( $breakpoints, $selector_prefix );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the styles to a CSS string.
|
||||
*
|
||||
* Styles format:
|
||||
* array<int, array{
|
||||
* id: string,
|
||||
* type: string,
|
||||
* variants: array<int, array{
|
||||
* props: array<string, mixed>,
|
||||
* meta: array<string, mixed>
|
||||
* }>
|
||||
* }>
|
||||
*
|
||||
* @param array $styles Array of style definitions.
|
||||
*
|
||||
* @return string Rendered CSS string.
|
||||
*/
|
||||
public function render( array $styles ): string {
|
||||
$css_style = [];
|
||||
|
||||
foreach ( $styles as $style_def ) {
|
||||
$style = $this->style_definition_to_css_string( $style_def );
|
||||
$css_style[] = $style;
|
||||
}
|
||||
|
||||
return implode( '', $css_style );
|
||||
}
|
||||
|
||||
public function on_prop_transform( callable $callback ): self {
|
||||
$this->on_prop_transform = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function style_definition_to_css_string( array $style ): string {
|
||||
$base_selector = $this->get_base_selector( $style );
|
||||
|
||||
if ( ! $base_selector ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$stylesheet = [];
|
||||
|
||||
foreach ( $style['variants'] as $variant ) {
|
||||
$style_declaration = $this->variant_to_css_string( $base_selector, $variant );
|
||||
|
||||
if ( $style_declaration ) {
|
||||
$stylesheet[] = $style_declaration;
|
||||
}
|
||||
}
|
||||
|
||||
return implode( '', $stylesheet );
|
||||
}
|
||||
|
||||
private function get_base_selector( array $style_def ): ?string {
|
||||
$map = [
|
||||
'class' => '.',
|
||||
];
|
||||
|
||||
if (
|
||||
isset( $style_def['type'] ) &&
|
||||
isset( $style_def['id'] ) &&
|
||||
isset( $map[ $style_def['type'] ] ) &&
|
||||
$style_def['id']
|
||||
) {
|
||||
$type = $map[ $style_def['type'] ];
|
||||
$id = $style_def['id'];
|
||||
|
||||
$selector_parts = array_filter( [
|
||||
$this->selector_prefix,
|
||||
"{$type}{$id}",
|
||||
] );
|
||||
|
||||
return implode( ' ', $selector_parts );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function variant_to_css_string( string $base_selector, array $variant ): string {
|
||||
$css = $this->props_to_css_string( $variant['props'] );
|
||||
|
||||
if ( ! $css ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$state = isset( $variant['meta']['state'] ) ? ':' . $variant['meta']['state'] : '';
|
||||
$selector = $base_selector . $state;
|
||||
|
||||
$style_declaration = $selector . '{' . $css . '}';
|
||||
|
||||
if ( isset( $variant['meta']['breakpoint'] ) ) {
|
||||
$style_declaration = $this->wrap_with_media_query( $variant['meta']['breakpoint'], $style_declaration );
|
||||
}
|
||||
|
||||
return $style_declaration;
|
||||
}
|
||||
|
||||
private function props_to_css_string( array $props ): string {
|
||||
$schema = Style_Schema::get();
|
||||
|
||||
return Collection::make( Props_Resolver::for_styles()->resolve( $schema, $props ) )
|
||||
->filter()
|
||||
->map( function ( $value, $prop ) {
|
||||
if ( $this->on_prop_transform ) {
|
||||
call_user_func( $this->on_prop_transform, $prop, $value );
|
||||
}
|
||||
|
||||
return $prop . ':' . $value . ';';
|
||||
} )
|
||||
->implode( '' );
|
||||
}
|
||||
|
||||
private function wrap_with_media_query( string $breakpoint_id, string $css ): string {
|
||||
if ( ! isset( $this->breakpoints[ $breakpoint_id ] ) ) {
|
||||
return $css;
|
||||
}
|
||||
|
||||
$breakpoint = $this->breakpoints[ $breakpoint_id ];
|
||||
if ( isset( $breakpoint['is_enabled'] ) && ! $breakpoint['is_enabled'] ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$size = $this->get_breakpoint_size( $this->breakpoints[ $breakpoint_id ] );
|
||||
|
||||
return $size ? '@media(' . $size . '){' . $css . '}' : $css;
|
||||
}
|
||||
|
||||
private function get_breakpoint_size( array $breakpoint ): ?string {
|
||||
$bound = 'min' === $breakpoint['direction'] ? 'min-width' : 'max-width';
|
||||
$width = $breakpoint['value'] . 'px';
|
||||
|
||||
return "{$bound}:{$width}";
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user