| 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\Utils\Remap_Component_Instance_Ids; |
| 20 |
use Elementor\Modules\Components\Utils\Strip_Component_Instances; |
| 21 |
use Elementor\Modules\Components\Variants\Component_Variant_Class_Collector; |
| 22 |
use Elementor\Modules\Components\Widgets\Component_Instance; |
| 23 |
use Elementor\Modules\Components\Schema\Overridable_LLM_Filter; |
| 24 |
|
| 25 |
if ( ! defined( 'ABSPATH' ) ) { |
| 26 |
exit; // Exit if accessed directly. |
| 27 |
} |
| 28 |
|
| 29 |
class Module extends BaseModule { |
| 30 |
const EXPERIMENT_NAME = AtomicWidgetsModule::EXPERIMENT_NAME; |
| 31 |
const EXPERIMENT_VARIANTS_NAME = 'e_component_variants'; |
| 32 |
const PACKAGES = [ 'editor-components' ]; |
| 33 |
|
| 34 |
/** |
| 35 |
* Local kill switch for components import/export. Off by default: on export the |
| 36 |
* `elementor_component` post type is excluded, on import any `e-component` widgets |
| 37 |
* that survived from a foreign zip are stripped. Flip to `true` in the source |
| 38 |
* to unblock the flag-on branch when working on the real feature (`Remap_Component_Instance_Ids` |
| 39 |
* and its test cover that path today). |
| 40 |
* |
| 41 |
* Not an experiment on purpose: experiments are serialized into exported kits by |
| 42 |
* `Site_Settings::export_experiments()` and rehydrated on import, so a hidden experiment |
| 43 |
* here would let a source-site override silently turn the guard off on every destination site. |
| 44 |
* |
| 45 |
* Remove this constant, `is_import_export_supported()` and every `! is_import_export_supported()` |
| 46 |
* branch once components are a first-class part of import/export. |
| 47 |
*/ |
| 48 |
const IS_IMPORT_EXPORT_SUPPORTED = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* Variants meta must be persisted before `Global_Classes_Relations::on_document_save()` |
| 52 |
* (default priority 10) reads it via the `extract_class_ids_from_post` filter. |
| 53 |
*/ |
| 54 |
const SAVE_VARIANTS_PRIORITY = 9; |
| 55 |
|
| 56 |
public function get_name() { |
| 57 |
return 'components'; |
| 58 |
} |
| 59 |
|
| 60 |
public function __construct() { |
| 61 |
parent::__construct(); |
| 62 |
|
| 63 |
if ( ! self::is_experiment_active() ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$this->register_variants_experiment(); |
| 68 |
|
| 69 |
$this->register_component_post_type(); |
| 70 |
|
| 71 |
add_filter( 'elementor/editor/v2/packages', fn ( $packages ) => $this->add_packages( $packages ) ); |
| 72 |
add_filter( 'elementor/atomic-widgets/props-schema', fn ( $schema ) => $this->modify_props_schema( $schema ) ); |
| 73 |
add_action( 'elementor/documents/register', fn ( $documents_manager ) => $this->register_document_type( $documents_manager ) ); |
| 74 |
add_action( 'elementor/document/before_save', fn( Document $document, array $data ) => $this->validate_circular_dependencies( $document, $data ), 10, 2 ); |
| 75 |
add_action( 'elementor/document/after_save', fn( Document $document, array $data ) => $this->set_component_overridable_props( $document, $data ), 10, 2 ); |
| 76 |
|
| 77 |
if ( self::is_variants_experiment_active() ) { |
| 78 |
add_action( 'elementor/document/after_save', fn( Document $document, array $data ) => $this->set_component_variants( $document, $data ), self::SAVE_VARIANTS_PRIORITY, 2 ); |
| 79 |
add_filter( |
| 80 |
'elementor/global_classes/extract_class_ids_from_post', |
| 81 |
fn( array $ids, $post_id ) => $this->add_variant_class_ids( $ids, $post_id ), |
| 82 |
10, |
| 83 |
2 |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
add_filter( 'elementor/global_classes/additional_post_types', fn( $post_types ) => array_merge( $post_types, [ Component_Document::TYPE ] ) ); |
| 88 |
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 ); |
| 89 |
|
| 90 |
add_action( 'elementor/atomic-widgets/settings/transformers/register', fn ( $transformers ) => $this->register_settings_transformers( $transformers ) ); |
| 91 |
add_action( 'elementor/document/after_migrate', fn( Document $document, array $data ) => $this->after_component_migrate( $document, $data ), 10, 2 ); |
| 92 |
|
| 93 |
add_filter( |
| 94 |
'elementor/atomic-widgets/llm-json-schema', |
| 95 |
fn( array $schema ) => ( new Overridable_LLM_Filter() )->apply( $schema ) |
| 96 |
); |
| 97 |
|
| 98 |
( Component_Lock_Manager::get_instance()->register_hooks() ); |
| 99 |
( new Component_Styles() )->register_hooks(); |
| 100 |
( new Components_REST_API() )->register_hooks(); |
| 101 |
} |
| 102 |
|
| 103 |
public static function is_experiment_active() { |
| 104 |
return Plugin::$instance->experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME ); |
| 105 |
} |
| 106 |
|
| 107 |
public static function is_variants_experiment_active(): bool { |
| 108 |
return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_VARIANTS_NAME ); |
| 109 |
} |
| 110 |
|
| 111 |
public static function is_import_export_supported(): bool { |
| 112 |
return self::IS_IMPORT_EXPORT_SUPPORTED; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Single entry point for import runners to normalize the elements tree of an imported |
| 117 |
* document with respect to component instances. When components round-trip is enabled |
| 118 |
* (flag on) it rewrites source-site component ids to their destination-site equivalents; |
| 119 |
* when disabled (flag off) it strips any `e-component` widget that survived from a |
| 120 |
* legacy zip so the destination editor never opens a document with dangling instances. |
| 121 |
* |
| 122 |
* Kept as a static helper on the module so both `import-export-customization` and legacy |
| 123 |
* `import-export` import paths stay in sync when `IS_IMPORT_EXPORT_SUPPORTED` flips. |
| 124 |
*/ |
| 125 |
public static function prepare_imported_elements( array $elements, array $post_ids_map ): array { |
| 126 |
return self::is_import_export_supported() |
| 127 |
? Remap_Component_Instance_Ids::apply( $elements, $post_ids_map ) |
| 128 |
: Strip_Component_Instances::apply( $elements ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Post types that must be excluded from the import/export runners when components |
| 133 |
* round-trip is disabled. Same gating as `prepare_imported_elements()`. |
| 134 |
*/ |
| 135 |
public static function excluded_import_export_post_types(): array { |
| 136 |
return self::is_import_export_supported() ? [] : [ Component_Document::TYPE ]; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Dev-only gate that keeps per-instance Component Variants off trunk while the feature ships |
| 141 |
* across several tickets. Hidden experiments cannot declare dependencies on other experiments, |
| 142 |
* so activation is checked manually in the constructor after the parent atomic-elements gate. |
| 143 |
*/ |
| 144 |
private function register_variants_experiment() { |
| 145 |
Plugin::$instance->experiments->add_feature( [ |
| 146 |
'name' => self::EXPERIMENT_VARIANTS_NAME, |
| 147 |
'title' => esc_html__( 'Component Variants', 'elementor' ), |
| 148 |
'description' => esc_html__( 'Enable per-instance class variants on components.', 'elementor' ), |
| 149 |
'hidden' => true, |
| 150 |
'default' => Experiments_Manager::STATE_INACTIVE, |
| 151 |
'release_status' => Experiments_Manager::RELEASE_STATUS_DEV, |
| 152 |
] ); |
| 153 |
} |
| 154 |
|
| 155 |
public function get_widgets() { |
| 156 |
return [ |
| 157 |
'Component_Instance', |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
private function add_packages( $packages ) { |
| 162 |
return array_merge( $packages, self::PACKAGES ); |
| 163 |
} |
| 164 |
|
| 165 |
private function modify_props_schema( array $schema ) { |
| 166 |
return Overridable_Schema_Extender::make()->get_extended_schema( $schema ); |
| 167 |
} |
| 168 |
|
| 169 |
private function register_component_post_type() { |
| 170 |
register_post_type( Component_Document::TYPE, [ |
| 171 |
'label' => Component_Document::get_title(), |
| 172 |
'labels' => Component_Document::get_labels(), |
| 173 |
'public' => false, |
| 174 |
'supports' => Component_Document::get_supported_features(), |
| 175 |
] ); |
| 176 |
} |
| 177 |
|
| 178 |
private function register_document_type( $documents_manager ) { |
| 179 |
$documents_manager->register_document_type( |
| 180 |
Component_Document::TYPE, |
| 181 |
Component_Document::get_class_full_name() |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
private function validate_circular_dependencies( Document $document, array $data ) { |
| 186 |
if ( ! $document instanceof Component_Document ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! isset( $data['elements'] ) ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
$component_id = $document->get_main_id(); |
| 195 |
$elements = $data['elements']; |
| 196 |
|
| 197 |
$result = Circular_Dependency_Validator::make()->validate( $component_id, $elements ); |
| 198 |
|
| 199 |
if ( ! $result['success'] ) { |
| 200 |
throw new \Exception( esc_html__( "Can't add this component - components that contain each other can't be nested.", 'elementor' ) ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
private function set_component_overridable_props( Document $document, array $data ) { |
| 205 |
if ( ! isset( $data['settings'] ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
if ( ( ! $document instanceof Component_Document ) || |
| 209 |
( ! isset( $data['settings']['overridable_props'] ) ) |
| 210 |
) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! Components_Access_Controller::can_edit() ) { |
| 215 |
throw new \Exception( esc_html__( 'You do not have permission to edit component source.', 'elementor' ) ); |
| 216 |
} |
| 217 |
|
| 218 |
/* @var Component_Document $document */ |
| 219 |
$result = $document->update_overridable_props( $data['settings']['overridable_props'] ); |
| 220 |
|
| 221 |
if ( ! $result->is_valid() ) { |
| 222 |
throw new \Exception( esc_html( 'Settings validation failed for component overridable props: ' . $result->errors()->to_string() ) ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private function set_component_variants( Document $document, array $data ) { |
| 227 |
if ( ! isset( $data['settings'] ) ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
if ( ( ! $document instanceof Component_Document ) || |
| 231 |
( ! isset( $data['settings']['variants'] ) ) |
| 232 |
) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
if ( ! Components_Access_Controller::can_edit() ) { |
| 237 |
throw new \Exception( esc_html__( 'You do not have permission to edit component source.', 'elementor' ) ); |
| 238 |
} |
| 239 |
|
| 240 |
/* @var Component_Document $document */ |
| 241 |
$result = $document->update_variants( $data['settings']['variants'] ); |
| 242 |
|
| 243 |
if ( ! $result->is_valid() ) { |
| 244 |
throw new \Exception( esc_html( 'Settings validation failed for component variants: ' . $result->errors()->to_string() ) ); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
private function add_variant_class_ids( array $ids, $post_id ): array { |
| 249 |
$document = Plugin::$instance->documents->get( (int) $post_id ); |
| 250 |
|
| 251 |
if ( ! $document instanceof Component_Document ) { |
| 252 |
return $ids; |
| 253 |
} |
| 254 |
|
| 255 |
return array_merge( $ids, Component_Variant_Class_Collector::collect( $document->get_variants() ) ); |
| 256 |
} |
| 257 |
|
| 258 |
private function register_settings_transformers( Transformers_Registry $transformers ) { |
| 259 |
$transformers->register( Component_Instance_Prop_Type::get_key(), new Component_Instance_Transformer() ); |
| 260 |
$transformers->register( Overridable_Prop_Type::get_key(), new Overridable_Transformer() ); |
| 261 |
$transformers->register( Override_Prop_Type::get_key(), new Override_Transformer() ); |
| 262 |
} |
| 263 |
|
| 264 |
private function after_component_migrate( Document $document, array $data ) { |
| 265 |
if ( ! $document instanceof Component_Document ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$document->align_overridable_props_with_elements(); |
| 270 |
} |
| 271 |
|
| 272 |
private function get_inner_elements_for_search( array $inner_elements, array $element_data ): array { |
| 273 |
if ( ! $this->is_component_instance( $element_data ) ) { |
| 274 |
return $inner_elements; |
| 275 |
} |
| 276 |
|
| 277 |
$element_instance = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 278 |
|
| 279 |
if ( ! $element_instance instanceof Component_Instance ) { |
| 280 |
return []; |
| 281 |
} |
| 282 |
|
| 283 |
return $element_instance->get_inner_elements_data_for_search(); |
| 284 |
} |
| 285 |
|
| 286 |
private function is_component_instance( array $element_data ): bool { |
| 287 |
return isset( $element_data['elType'], $element_data['widgetType'] ) |
| 288 |
&& 'widget' === $element_data['elType'] |
| 289 |
&& Component_Instance::get_element_type() === $element_data['widgetType']; |
| 290 |
} |
| 291 |
} |
| 292 |
|