PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / mcp / abilities / build-composition / subtree-builder.php

subtree-builder.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/mcp/abilities/build-composition/subtree-builder.php

114 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Abilities\Build_Composition;
4
5 use Elementor\Modules\Mcp\Abilities\Appliers\V3_Node_Bridge;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Subtree_Builder {
12
13 private Xml_Parser $xml_parser;
14
15 public function __construct( Xml_Parser $xml_parser ) {
16 $this->xml_parser = $xml_parser;
17 }
18
19 /**
20 * @param \DOMDocument $dom XML document.
21 * @param array<string, array> $widget_configs Resolved type configs indexed by tag.
22 * @return array[]
23 */
24 public function build( \DOMDocument $dom, array $widget_configs ): array {
25 $root = $this->xml_parser->get_root( $dom );
26 if ( ! $root ) {
27 return [];
28 }
29
30 $subtrees = [];
31 foreach ( $this->xml_parser->get_child_elements( $root ) as $child ) {
32 $subtrees[] = $this->build_subtree( $child, $widget_configs );
33 }
34
35 return $subtrees;
36 }
37
38 /**
39 * Build an index of configuration-id -> reference to subtree node.
40 *
41 * @param array[] $subtrees Built subtrees (mutated by reference downstream).
42 * @param \DOMDocument $dom Source DOM.
43 * @return array<string, array&>
44 */
45 public function index_by_config_id( array &$subtrees, \DOMDocument $dom ): array {
46 $root = $this->xml_parser->get_root( $dom );
47 if ( ! $root ) {
48 return [];
49 }
50
51 $dom_roots = $this->xml_parser->get_child_elements( $root );
52 $index = [];
53
54 foreach ( $subtrees as $i => &$subtree ) {
55 if ( isset( $dom_roots[ $i ] ) ) {
56 $this->index_recursive( $subtree, $dom_roots[ $i ], $index );
57 }
58 }
59 unset( $subtree );
60
61 return $index;
62 }
63
64 private function build_subtree( \DOMElement $node, array $widget_configs ): array {
65 $config = $widget_configs[ $this->xml_parser->get_tag_name( $node ) ];
66
67 $children = [];
68 foreach ( $this->xml_parser->get_child_elements( $node ) as $child ) {
69 $children[] = $this->build_subtree( $child, $widget_configs );
70 }
71
72 $element = [
73 'elType' => $config['elType'],
74 'settings' => [],
75 'elements' => $children,
76 'editor_settings' => [],
77 ];
78
79 if ( ! empty( $config['widgetType'] ) ) {
80 $element['widgetType'] = $config['widgetType'];
81 }
82
83 $configuration_id = $this->xml_parser->get_configuration_id( $node );
84 if ( null !== $configuration_id ) {
85 $element['editor_settings']['title'] = $configuration_id;
86 }
87
88 if ( ! empty( $config['controls'] ) && V3_Node_Bridge::is_v3_node( $element ) ) {
89 V3_Node_Bridge::seed_dynamic_defaults( $element, $config['controls'] );
90 }
91
92 return $element;
93 }
94
95 private function index_recursive( array &$subtree, \DOMElement $node, array &$index ): void {
96 $configuration_id = $this->xml_parser->get_configuration_id( $node );
97 if ( null !== $configuration_id ) {
98 $index[ $configuration_id ] = &$subtree;
99 }
100
101 if ( ! isset( $subtree['elements'] ) || ! is_array( $subtree['elements'] ) ) {
102 return;
103 }
104
105 $child_elements = $this->xml_parser->get_child_elements( $node );
106 foreach ( $subtree['elements'] as $i => &$child_subtree ) {
107 if ( isset( $child_elements[ $i ] ) ) {
108 $this->index_recursive( $child_subtree, $child_elements[ $i ], $index );
109 }
110 }
111 unset( $child_subtree );
112 }
113 }
114