← All changes
|
modules/mcp/abilities/build-composition/composition-persister.php
+80
-8
4.3.0-beta1
→
4.3.2
View file →
| @@ -3,8 +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; | |
| 8 | +use Elementor\Modules\Mcp\Abilities\Utils\Document_Mutation_Save; | |
| 9 | +use Elementor\Modules\Mcp\Events\Mcp_Event_Dispatcher; | |
| 7 | 10 | |
| 8 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 12 | exit; |
| 10 | 13 | } |
| @@ -53,20 +56,14 @@ | ||
| 53 | 56 | $tree = $new_tree; |
| 54 | 57 | $root_ids[] = $this->find_last_root_id( $tree, $parent_id ); |
| 55 | 58 | } |
| 56 | 59 | |
| 57 | - $save_result = $this->mutator->save_as_draft( $document, $tree ); | |
| 60 | + $save_result = Document_Mutation_Save::elements_preserving_live_status( $this->mutator, $document, $tree ); | |
| 58 | 61 | if ( is_wp_error( $save_result ) ) { |
| 59 | 62 | return $save_result; |
| 60 | 63 | } |
| 61 | 64 | |
| 62 | - if ( ! $save_result ) { | |
| 63 | - return new \WP_Error( | |
| 64 | - 'save_failed', | |
| 65 | - __( 'Could not save document.', 'elementor' ), | |
| 66 | - [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] | |
| 67 | - ); | |
| 68 | - } | |
| 65 | + $this->emit_insertion_events( $tree, $parent_id, $root_ids ); | |
| 69 | 66 | |
| 70 | 67 | return [ |
| 71 | 68 | 'tree' => $tree, |
| 72 | 69 | 'root_ids' => $root_ids, |
| @@ -174,6 +171,81 @@ | ||
| 174 | 171 | if ( isset( $child_subtrees[ $index ] ) && is_array( $child_subtrees[ $index ] ) ) { |
| 175 | 172 | $this->apply_ids_recursive( $child, $child_subtrees[ $index ] ); |
| 176 | 173 | } |
| 177 | 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'] ?? ''; | |
| 178 | 250 | } |
| 179 | 251 | } |