• File: composition-compiler.php
  • Full Path: /home/slvgwrnz/public_html/wp-content/plugins/elementor/modules/mcp/abilities/utils/composition-compiler.php
  • Date Modified: 11/05/2026 9:17 AM
  • File size: 12.35 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace Elementor\Modules\Mcp\Abilities\Utils;

use Elementor\Core\Base\Document;
use Elementor\Modules\AtomicWidgets\CssConverter\Converter_Registry_Factory;
use Elementor\Modules\AtomicWidgets\CssConverter\Css_Converter;
use Elementor\Modules\AtomicWidgets\CssConverter\Expander_Registry_Factory;
use Elementor\Modules\AtomicWidgets\CssConverter\Metrics\Null_Failure_Reporter;
use Elementor\Modules\AtomicWidgets\CssConverter\Variable_Prop_Value_Transformer;
use Elementor\Modules\AtomicWidgets\Module as AtomicWidgetsModule;
use Elementor\Modules\AtomicWidgets\PlainResolvers\Plain_Values_Resolver;
use Elementor\Modules\GlobalClasses\Global_Classes_Repository;
use Elementor\Modules\Interactions\Module as Interactions_Module;
use Elementor\Modules\Mcp\Abilities\Appliers\Class_Applier;
use Elementor\Modules\Mcp\Abilities\Appliers\Element_Config_Applier;
use Elementor\Modules\Mcp\Abilities\Appliers\Interactions_Applier;
use Elementor\Modules\Mcp\Abilities\Appliers\Style_Applier;
use Elementor\Modules\Mcp\Abilities\Build_Composition\Form_Structure_Validator;
use Elementor\Modules\Mcp\Abilities\Build_Composition\Subtree_Builder;
use Elementor\Modules\Mcp\Abilities\Build_Composition\Widget_Type_Resolver;
use Elementor\Modules\Mcp\Abilities\Build_Composition\Xml_Parser;
use Elementor\Modules\Variables\Module as Variables_Module;
use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Processor;
use Elementor\Modules\Variables\Services\Variables_Service;
use Elementor\Modules\Variables\Storage\Variables_Repository;
use Elementor\Plugin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Composition_Compiler {

	private const DEFAULT_PARENT_ID = 'document';
	private const DOCUMENT_ROOT_WRAPPER = 'e-div-block';
	private const DOCUMENT_ROOT_V3_WRAPPER = 'container';

	private const NAME_COMES_FROM_CONFIGURATION_ID_HINT = 'The layer name is the element\'s configuration-id; rename it afterwards with elementor/manage-elements editor_settings.name.';

	public const COMPONENT_PARENT_ID = 'component';

	public static function make(): self {
		return new self();
	}

	/**
	 * @param array     $input         Composition input using the `elementor/build-composition` shapes.
	 * @param ?Document $document      Context for resolving dynamic values, when a target document exists.
	 * @param array     $document_tree Existing document tree used for insertion-context validation.
	 * @param string    $parent_id     Target parent used for insertion-context validation.
	 *
	 * @return array{elements: array[], warnings: Warnings_Bag, dom: \DOMDocument, xml_parser: Xml_Parser, pro_features_discovery: \WP_Error[]}|\WP_Error
	 */
	public function compile(
		array $input,
		?Document $document = null,
		array $document_tree = [],
		string $parent_id = self::DEFAULT_PARENT_ID
	) {
		$xml_parser = new Xml_Parser();
		$type_resolver = new Widget_Type_Resolver( $xml_parser );
		$subtree_builder = new Subtree_Builder( $xml_parser );

		$dom = $xml_parser->parse( (string) ( $input['xml_structure'] ?? '' ) );
		if ( is_wp_error( $dom ) ) {
			return $dom;
		}

		$pro_features_discovery = ( new Pro_Elements_Remover( $xml_parser ) )->remove_and_collect_errors( $dom );

		[
			'configs' => $widget_configs,
			'unknown_tag_errors' => $unknown_widget_tag_errors,
		] = $type_resolver->collect_referenced_widget_configs( $dom );

		$validation_error = $this->validate_xml_before_apply(
			$dom,
			$xml_parser,
			$document_tree,
			$parent_id,
			$unknown_widget_tag_errors
		);
		if ( $validation_error ) {
			return $validation_error;
		}

		$warnings = Warnings_Bag::make();

		$wrapping_result = $this->wrap_document_root_content( $dom, $widget_configs, $parent_id, $type_resolver, $xml_parser );
		if ( is_wp_error( $wrapping_result ) ) {
			return $wrapping_result;
		}

		$widget_configs = $wrapping_result['widget_configs'];
		$warnings->merge( $wrapping_result['warnings'] );

		$child_type_errors = $type_resolver->collect_child_type_and_required_child_errors( $dom, $widget_configs );
		if ( ! empty( $child_type_errors ) ) {
			return new \WP_Error(
				'elementor_invalid_child_type',
				implode( ' ', $child_type_errors ),
				[ 'status' => \WP_Http::BAD_REQUEST ]
			);
		}

		$subtrees = $subtree_builder->build( $dom, $widget_configs );
		if ( empty( $subtrees ) ) {
			return $pro_features_discovery[0] ?? new \WP_Error(
				'empty_composition',
				__( 'xml_structure did not contain any elements. Pass raw XML tags (e.g. <e-div-block configuration-id="..."></e-div-block>) — do not wrap the value in <![CDATA[...]]> or other text-only content.', 'elementor' ),
				[ 'status' => \WP_Http::BAD_REQUEST ]
			);
		}

		$index = $subtree_builder->index_by_config_id( $subtrees, $dom );

		$variables_service = $this->create_variables_service();

		$config_applier = new Element_Config_Applier( $type_resolver, $this->get_plain_values_resolver() );
		$config_result = $config_applier->apply( $index, $this->as_map( $input['element_config'] ?? [] ), $widget_configs, $document );
		if ( $config_result['error'] ) {
			return $config_result['error'];
		}
		$warnings->merge( $config_result['warnings'] );

		$warnings->merge( $this->apply_editor_settings( $index, $this->as_map( $input['editor_settings'] ?? [] ) ) );

		$class_applier = new Class_Applier( $this->create_global_classes_repository() );
		$warnings->merge( $class_applier->apply( $index, $this->as_map( $input['classes'] ?? [] ) )['warnings'] );

		$style_applier = new Style_Applier( $this->create_css_converter( $variables_service ), $this->get_active_breakpoints(), $variables_service );
		$warnings->merge( $style_applier->apply( $index, $this->as_map( $input['style'] ?? [] ), 'patch' )['warnings'] );
		Grid_Empty_Rows_Warnings::report( $index, $warnings );

		$warnings->merge( $this->apply_interactions( $index, $this->as_map( $input['interactions'] ?? [] ) )['warnings'] );

		return [
			'elements' => $subtrees,
			'warnings' => $warnings,
			'dom' => $dom,
			'xml_parser' => $xml_parser,
			'pro_features_discovery' => $pro_features_discovery,
		];
	}

	/**
	 * @param \DOMDocument $dom
	 * @param Xml_Parser   $xml_parser
	 * @param array        $document_tree
	 * @param string       $parent_id
	 * @param string[]     $unknown_widget_tag_errors
	 *
	 * @return \WP_Error|null
	 */
	private function validate_xml_before_apply(
		\DOMDocument $dom,
		Xml_Parser $xml_parser,
		array $document_tree,
		string $parent_id,
		array $unknown_widget_tag_errors
	) {
		$errors = array_merge(
			$this->tag_errors( 'elementor_duplicate_configuration_id', $xml_parser->collect_duplicate_configuration_id_errors( $dom ) ),
			$this->tag_errors( 'elementor_invalid_form_structure', ( new Form_Structure_Validator( $xml_parser ) )->collect_errors( $dom, $document_tree, $parent_id ) ),
			$this->tag_errors( 'elementor_unknown_type', $unknown_widget_tag_errors ),
		);

		if ( empty( $errors ) ) {
			return null;
		}

		$status = [ 'status' => \WP_Http::BAD_REQUEST ];
		$joined = implode( ' ', array_column( $errors, 'message' ) );

		$aggregated = new \WP_Error( $errors[0]['code'], $joined, $status );
		foreach ( array_slice( $errors, 1 ) as $error ) {
			$aggregated->add( $error['code'], $error['message'], $status );
		}

		return $aggregated;
	}

	/**
	 * @param string   $code     Error code to tag each message with.
	 * @param string[] $messages List of error messages.
	 *
	 * @return array<int, array{code: string, message: string}>
	 */
	private function tag_errors( string $code, array $messages ): array {
		return array_map(
			fn ( $message ) => [
				'code' => $code,
				'message' => $message,
			],
			$messages
		);
	}

	private function wrap_document_root_content(
		\DOMDocument $dom,
		array $widget_configs,
		string $parent_id,
		Widget_Type_Resolver $type_resolver,
		Xml_Parser $xml_parser
	) {
		$unwrapped = [
			'widget_configs' => $widget_configs,
			'warnings' => Warnings_Bag::make(),
		];

		if ( self::DEFAULT_PARENT_ID !== $parent_id ) {
			return $unwrapped;
		}

		$root = $xml_parser->get_root( $dom );
		if ( ! $root ) {
			return $unwrapped;
		}

		$root_children = $xml_parser->get_child_elements( $root );
		$has_widget = false;
		foreach ( $root_children as $child ) {
			$tag = $xml_parser->get_tag_name( $child );
			$config = $widget_configs[ $tag ] ?? [];

			if ( 'widget' !== ( $config['elType'] ?? null ) ) {
				continue;
			}

			$has_widget = true;
			break;
		}

		if ( ! $has_widget ) {
			return $unwrapped;
		}

		$wrapper_tag = $this->resolve_document_root_wrapper();
		$wrapper_config = $type_resolver->resolve_type_config( $wrapper_tag );
		if ( is_wp_error( $wrapper_config ) ) {
			return $wrapper_config;
		}

		$widget_configs[ $wrapper_tag ] = $wrapper_config;

		$wrapper = $dom->createElement( $wrapper_tag );
		$root->appendChild( $wrapper );

		foreach ( $root_children as $child ) {
			$wrapper->appendChild( $child );
		}

		return [
			'widget_configs' => $widget_configs,
			'warnings' => Warnings_Bag::make()->add(
				'root_auto_wrapped',
				sprintf(
					/* translators: %s: wrapper element tag */
					__( 'Direct document-root content was wrapped in a %s element.', 'elementor' ),
					$wrapper_tag
				)
			),
		];
	}

	private function resolve_document_root_wrapper(): string {
		if ( Widget_Context_Helper::is_v3_supported( self::DOCUMENT_ROOT_V3_WRAPPER ) ) {
			return self::DOCUMENT_ROOT_V3_WRAPPER;
		}

		return self::DOCUMENT_ROOT_WRAPPER;
	}

	/**
	 * @param array<string, array&>               $index           Index of subtree refs.
	 * @param array<string, array<string, mixed>> $editor_settings Per-config-id editor-only fields.
	 */
	private function apply_editor_settings( array $index, array $editor_settings ): Warnings_Bag {
		$warnings = Warnings_Bag::make();

		foreach ( $editor_settings as $config_id => $values ) {
			if ( ! isset( $index[ $config_id ] ) ) {
				continue;
			}

			Editor_Settings::apply(
				$index[ $config_id ],
				$this->as_map( $values ),
				(string) $config_id,
				$warnings,
				[ Editor_Settings::NAME => self::NAME_COMES_FROM_CONFIGURATION_ID_HINT ]
			);
		}

		return $warnings;
	}

	private function as_map( $value ): array {
		if ( is_object( $value ) ) {
			$value = (array) $value;
		}

		return is_array( $value ) ? $value : [];
	}

	private function get_active_breakpoints(): array {
		return array_keys( Plugin::$instance->breakpoints->get_active_breakpoints() );
	}

	private function create_css_converter( ?Variables_Service $variables_service ): Css_Converter {
		$variable_transformer = $variables_service
			? new Variable_Prop_Value_Transformer( $variables_service )
			: null;

		return new Css_Converter(
			Converter_Registry_Factory::create( $variables_service ),
			new Null_Failure_Reporter(),
			Expander_Registry_Factory::create( $variables_service ),
			$variable_transformer
		);
	}

	private function create_variables_service(): ?Variables_Service {
		if ( ! $this->is_variables_active() ) {
			return null;
		}

		$kit = Plugin::$instance->kits_manager->get_active_kit();

		if ( ! $kit ) {
			return null;
		}

		return new Variables_Service(
			new Variables_Repository( $kit ),
			new Batch_Processor()
		);
	}

	private function get_plain_values_resolver(): Plain_Values_Resolver {
		return AtomicWidgetsModule::instance()->get_settings_plain_values_resolver();
	}

	/**
	 * @param array<string, array&>            $index
	 * @param array<string, array<int, array>> $interactions
	 *
	 * @return array{error: null, warnings: Warnings_Bag}
	 */
	private function apply_interactions( array &$index, array $interactions ): array {
		if ( empty( $interactions ) ) {
			return [
				'error' => null,
				'warnings' => Warnings_Bag::make(),
			];
		}

		if ( ! Plugin::$instance->experiments->is_feature_active( Interactions_Module::EXPERIMENT_NAME ) ) {
			return [
				'error' => null,
				'warnings' => Warnings_Bag::make()->add(
					'interactions_experiment_off',
					__( 'Interactions experiment is not active. Interactions were not applied.', 'elementor' )
				),
			];
		}

		$applier = new Interactions_Applier( $this->get_plain_values_resolver() );

		return $applier->apply( $index, $interactions );
	}

	private function is_variables_active(): bool {
		$experiments = Plugin::$instance->experiments;

		return $experiments->is_feature_active( Variables_Module::EXPERIMENT_NAME )
			&& $experiments->is_feature_active( AtomicWidgetsModule::EXPERIMENT_NAME );
	}

	private function create_global_classes_repository(): Global_Classes_Repository {
		$kit = Plugin::$instance->kits_manager->get_active_kit();

		return Global_Classes_Repository::make( $kit );
	}
}