| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Components; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module as BaseModule; |
| 5 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 6 |
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropsResolver\Transformers_Registry; |
| 9 |
use Elementor\Modules\Components\Styles\Component_Styles; |
| 10 |
use Elementor\Modules\Components\Documents\Component as Component_Document; |
| 11 |
use Elementor\Modules\Components\Component_Lock_Manager; |
| 12 |
use Elementor\Modules\Components\PropTypes\Component_Instance_Prop_Type; |
| 13 |
use Elementor\Modules\Components\Transformers\Component_Instance_Transformer; |
| 14 |
use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 15 |
use Elementor\Modules\Components\Transformers\Overridable_Transformer; |
| 16 |
use Elementor\Core\Base\Document; |
| 17 |
use Elementor\Modules\Components\PropTypes\Override_Prop_Type; |
| 18 |
use Elementor\Modules\Components\Transformers\Override_Transformer; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; // Exit if accessed directly. |
| 22 |
} |
| 23 |
|
| 24 |
class Module extends BaseModule { |
| 25 |
const EXPERIMENT_NAME = 'e_components'; |
| 26 |
const PACKAGES = [ 'editor-components' ]; |
| 27 |
|
| 28 |
public function get_name() { |
| 29 |
return 'components'; |
| 30 |
} |
| 31 |
|
| 32 |
public function __construct() { |
| 33 |
parent::__construct(); |
| 34 |
|
| 35 |
if ( ! $this->is_experiment_active() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$this->register_component_post_type(); |
| 40 |
|
| 41 |
add_filter( 'elementor/editor/v2/packages', fn ( $packages ) => $this->add_packages( $packages ) ); |
| 42 |
add_filter( 'elementor/atomic-widgets/props-schema', fn ( $schema ) => $this->modify_props_schema( $schema ) ); |
| 43 |
add_action( 'elementor/documents/register', fn ( $documents_manager ) => $this->register_document_type( $documents_manager ) ); |
| 44 |
add_action( 'elementor/document/before_save', fn( Document $document, array $data ) => $this->validate_circular_dependencies( $document, $data ), 10, 2 ); |
| 45 |
add_action( 'elementor/document/after_save', fn( Document $document, array $data ) => $this->set_component_overridable_props( $document, $data ), 10, 2 ); |
| 46 |
add_filter( 'elementor/global_classes/additional_post_types', fn( $post_types ) => array_merge( $post_types, [ Component_Document::TYPE ] ) ); |
| 47 |
|
| 48 |
add_action( 'elementor/atomic-widgets/settings/transformers/register', fn ( $transformers ) => $this->register_settings_transformers( $transformers ) ); |
| 49 |
add_action( 'elementor/document/after_migrate', fn( Document $document, array $data ) => $this->after_component_migrate( $document, $data ), 10, 2 ); |
| 50 |
|
| 51 |
( Component_Lock_Manager::get_instance()->register_hooks() ); |
| 52 |
( new Component_Styles() )->register_hooks(); |
| 53 |
( new Components_REST_API() )->register_hooks(); |
| 54 |
} |
| 55 |
|
| 56 |
public function is_experiment_active() { |
| 57 |
return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ) |
| 58 |
&& Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); |
| 59 |
} |
| 60 |
|
| 61 |
public static function get_experimental_data() { |
| 62 |
return [ |
| 63 |
'name' => self::EXPERIMENT_NAME, |
| 64 |
'title' => esc_html__( 'Components', 'elementor' ), |
| 65 |
'description' => esc_html__( 'Enable components.', 'elementor' ), |
| 66 |
'hidden' => true, |
| 67 |
'default' => Experiments_Manager::STATE_ACTIVE, |
| 68 |
'release_status' => Experiments_Manager::RELEASE_STATUS_BETA, |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
public function get_widgets() { |
| 73 |
return [ |
| 74 |
'Component_Instance', |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
private function add_packages( $packages ) { |
| 79 |
return array_merge( $packages, self::PACKAGES ); |
| 80 |
} |
| 81 |
|
| 82 |
private function modify_props_schema( array $schema ) { |
| 83 |
return Overridable_Schema_Extender::make()->get_extended_schema( $schema ); |
| 84 |
} |
| 85 |
|
| 86 |
private function register_component_post_type() { |
| 87 |
register_post_type( Component_Document::TYPE, [ |
| 88 |
'label' => Component_Document::get_title(), |
| 89 |
'labels' => Component_Document::get_labels(), |
| 90 |
'public' => false, |
| 91 |
'supports' => Component_Document::get_supported_features(), |
| 92 |
] ); |
| 93 |
} |
| 94 |
|
| 95 |
private function register_document_type( $documents_manager ) { |
| 96 |
$documents_manager->register_document_type( |
| 97 |
Component_Document::TYPE, |
| 98 |
Component_Document::get_class_full_name() |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
private function validate_circular_dependencies( Document $document, array $data ) { |
| 103 |
if ( ! $document instanceof Component_Document ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! isset( $data['elements'] ) ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$component_id = $document->get_main_id(); |
| 112 |
$elements = $data['elements']; |
| 113 |
|
| 114 |
$result = Circular_Dependency_Validator::make()->validate( $component_id, $elements ); |
| 115 |
|
| 116 |
if ( ! $result['success'] ) { |
| 117 |
throw new \Exception( esc_html__( "Can't add this component - components that contain each other can't be nested.", 'elementor' ) ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private function set_component_overridable_props( Document $document, array $data ) { |
| 122 |
if ( ! isset( $data['settings'] ) ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
if ( ( ! $document instanceof Component_Document ) || |
| 126 |
( ! isset( $data['settings']['overridable_props'] ) ) |
| 127 |
) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! Components_Access_Controller::can_edit() ) { |
| 132 |
throw new \Exception( esc_html__( 'You do not have permission to edit component source.', 'elementor' ) ); |
| 133 |
} |
| 134 |
|
| 135 |
/* @var Component_Document $document */ |
| 136 |
$result = $document->update_overridable_props( $data['settings']['overridable_props'] ); |
| 137 |
|
| 138 |
if ( ! $result->is_valid() ) { |
| 139 |
throw new \Exception( esc_html( 'Settings validation failed for component overridable props: ' . $result->errors()->to_string() ) ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private function register_settings_transformers( Transformers_Registry $transformers ) { |
| 144 |
$transformers->register( Component_Instance_Prop_Type::get_key(), new Component_Instance_Transformer() ); |
| 145 |
$transformers->register( Overridable_Prop_Type::get_key(), new Overridable_Transformer() ); |
| 146 |
$transformers->register( Override_Prop_Type::get_key(), new Override_Transformer() ); |
| 147 |
} |
| 148 |
|
| 149 |
private function after_component_migrate( Document $document, array $data ) { |
| 150 |
if ( ! $document instanceof Component_Document ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$document->align_overridable_props_with_elements(); |
| 155 |
} |
| 156 |
} |
| 157 |
|