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