$model * @param int $post_id * @param string $subtree_id * @return array */ protected function analyze( array $model, int $post_id, string $subtree_id ): array { $targets = array(); foreach ( (array) $model['rows'] as $row ) { foreach ( (array) $row['lanes'] as $lane ) { $types = array_map( 'strval', (array) $lane['widget_types'] ); if ( array( 'image' ) !== $types ) { continue; } $settings = (array) $lane['settings']; if ( ! empty( $settings['background_background'] ) || ! empty( $settings['background_image'] ) ) { continue; } $targets[] = (string) $lane['id']; } } if ( array() === $targets ) { return Design_Mutator::report( array( 'changed' => array(), 'applied' => false ), '', __( 'No lane in scope holds a single image widget without already having a background of its own, so nothing was changed.', 'acrossai-abilities-manager' ) ); } // Read the image out of each target lane before writing, so the background is set // from the widget that is about to be hidden rather than from a half-edited tree. $images = array(); \AcrossAI_Abilities_Manager\Includes\Abilities\Utilities\Elementor\Document_Repository::walk_tree( (array) $model['scoped_data'], static function ( array $element, array $path ) use ( &$images, $targets ): void { if ( 'widget' !== ( $element['elType'] ?? '' ) || 'image' !== ( $element['widgetType'] ?? '' ) ) { return; } foreach ( $path as $ancestor ) { if ( in_array( (string) $ancestor, $targets, true ) ) { $settings = isset( $element['settings'] ) && is_array( $element['settings'] ) ? $element['settings'] : array(); if ( isset( $settings['image'] ) ) { $images[ (string) $ancestor ] = array( 'image' => $settings['image'], 'widget_id' => (string) ( $element['id'] ?? '' ), ); } return; } } } ); if ( array() === $images ) { return Design_Mutator::report( array( 'changed' => array(), 'applied' => false ), '', __( 'The candidate lanes hold image widgets with no image selected, so there is nothing to move to a background.', 'acrossai-abilities-manager' ) ); } $widget_ids = array_map( static fn( array $i ): string => $i['widget_id'], $images ); $result = Design_Mutator::edit( $model, static function ( array &$settings, array $element ) use ( $images, $widget_ids ): array { $id = (string) ( $element['id'] ?? '' ); if ( isset( $images[ $id ] ) ) { $changes = Design_Mutator::set( $settings, 'background_background', 'classic' ); $changes = array_merge( $changes, Design_Mutator::set( $settings, 'background_image', $images[ $id ]['image'] ) ); $changes = array_merge( $changes, Design_Mutator::set( $settings, 'background_size', 'cover' ) ); return $changes; } if ( in_array( $id, $widget_ids, true ) ) { // Hidden, not removed — every setting stays recoverable in the editor. return Design_Mutator::set( $settings, 'hide_desktop', 'hidden-desktop' ); } return array(); } ); if ( is_wp_error( $result ) ) { return $result; } return Design_Mutator::report( $result, __( 'Moved lone images onto their lane as a cover background and hid the original widgets rather than deleting them, so the change can be reversed by hand.', 'acrossai-abilities-manager' ), __( 'Nothing matched, so nothing was changed.', 'acrossai-abilities-manager' ) ); } }