PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.1
Elementor Website Builder – more than just a page builder v4.3.1
4.3.2 4.3.1 4.3.0 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 All 455 releases
← All changes | modules/mcp/abilities/build-composition/composition-persister.php +79 -0 4.3.0-beta2 → 4.3.1 View file →
@@ -3,9 +3,11 @@
3 3 namespace Elementor\Modules\Mcp\Abilities\Build_Composition;
4 4
5 5 use Elementor\Core\Base\Document;
6 6 use Elementor\Core\Utils\Document\Document_Mutator;
7 +use Elementor\Modules\Components\Components_Repository;
7 8 use Elementor\Modules\Mcp\Abilities\Utils\Document_Mutation_Save;
9 +use Elementor\Modules\Mcp\Events\Mcp_Event_Dispatcher;
8 10
9 11 if ( ! defined( 'ABSPATH' ) ) {
10 12 exit;
11 13 }
@@ -59,8 +61,10 @@
59 61 if ( is_wp_error( $save_result ) ) {
60 62 return $save_result;
61 63 }
62 64
65 + $this->emit_insertion_events( $tree, $parent_id, $root_ids );
66 +
63 67 return [
64 68 'tree' => $tree,
65 69 'root_ids' => $root_ids,
66 70 'removed_ids' => $removed_ids,
@@ -167,6 +171,81 @@
167 171 if ( isset( $child_subtrees[ $index ] ) && is_array( $child_subtrees[ $index ] ) ) {
168 172 $this->apply_ids_recursive( $child, $child_subtrees[ $index ] );
169 173 }
170 174 }
175 + }
176 +
177 + private function emit_insertion_events( array $tree, string $parent_id, array $root_ids ): void {
178 + $top_element_type = $this->resolve_top_element_type( $tree, $parent_id );
179 +
180 + foreach ( $root_ids as $root_id ) {
181 + $root_node = $this->mutator->find_by_id( $tree, $root_id );
182 +
183 + if ( null === $root_node ) {
184 + continue;
185 + }
186 +
187 + $this->walk_and_emit_insertion_events( $root_node, $top_element_type );
188 + }
189 + }
190 +
191 + private function walk_and_emit_insertion_events( array $node, string $top_element_type ): void {
192 + $element_name = $node['widgetType'] ?? $node['elType'] ?? '';
193 +
194 + if ( '' !== $element_name ) {
195 + Mcp_Event_Dispatcher::emit( 'element_added', [
196 + 'element_name' => $element_name,
197 + ] );
198 + }
199 +
200 + if ( 'e-component' === ( $node['widgetType'] ?? '' ) ) {
201 + [ 'id' => $component_id, 'name' => $component_name ] = $this->resolve_component_instance_meta( $node );
202 +
203 + Mcp_Event_Dispatcher::emit( 'component_instance_added', [
204 + 'id' => $component_id,
205 + 'name' => $component_name,
206 + 'top_element_type' => $top_element_type,
207 + ] );
208 + }
209 +
210 + foreach ( $node['elements'] ?? [] as $child ) {
211 + $this->walk_and_emit_insertion_events( $child, $top_element_type );
212 + }
213 + }
214 +
215 + /**
216 + * @return array{id: string, name: string}
217 + */
218 + private function resolve_component_instance_meta( array $node ): array {
219 + $raw_id = $node['settings']['component_instance']['value']['component_id']['value'] ?? null;
220 +
221 + if ( null === $raw_id || '' === $raw_id ) {
222 + return [
223 + 'id' => '',
224 + 'name' => '',
225 + ];
226 + }
227 +
228 + $component_id = (int) $raw_id;
229 + $component = Components_Repository::make()->get( $component_id, false );
230 + $name = $component ? $component->get_post()->post_title : '';
231 +
232 + return [
233 + 'id' => (string) $component_id,
234 + 'name' => (string) $name,
235 + ];
236 + }
237 +
238 + private function resolve_top_element_type( array $tree, string $parent_id ): string {
239 + if ( self::DOCUMENT_ROOT === $parent_id ) {
240 + return 'document';
241 + }
242 +
243 + $parent = $this->mutator->find_by_id( $tree, $parent_id );
244 +
245 + if ( null === $parent ) {
246 + return '';
247 + }
248 +
249 + return $parent['widgetType'] ?? $parent['elType'] ?? '';
171 250 }
172 251 }