uilderModule::instance()->get_document( $post_id ); if ( ! $document ) { $results->add_error( $index, self::ACTION_UPDATE, 'not_a_theme_document', __( 'The given post_id is not a Theme Builder document.', 'elementor-pro' ) ); return; } $extra = [ 'post_id' => $post_id, ]; $has_updatable_field = false; if ( array_key_exists( 'conditions', $operation ) ) { $assign = $this->conditions_writer->write( $document, $post_id, $operation['conditions'] ); if ( is_wp_error( $assign ) ) { $results->add_error( $index, self::ACTION_UPDATE, $assign->get_error_code(), $assign->get_error_message() ); return; } $extra['conditions'] = $assign['conditions']; $extra['conflicts'] = $assign['conflicts']; $has_updatable_field = true; } if ( array_key_exists( 'display_settings', $operation ) ) { $display_settings = $this->display_settings_writer->write( $document, $operation['display_settings'] ); if ( is_wp_error( $display_settings ) ) { $results->add_error( $index, self::ACTION_UPDATE, $display_settings->get_error_code(), $display_settings->get_error_message() ); return; } $extra['display_settings'] = $display_settings; $has_updatable_field = true; } if ( ! $has_updatable_field ) { $results->add_error( $index, self::ACTION_UPDATE, 'no_updatable_fields', __( 'update requires at least one mutable field (conditions, display_settings).', 'elementor-pro' ) ); return; } $extra['post_status'] = get_post_status( $post_id ); $extra['type'] = ThemeBuilderModule::instance()->get_template_type( $post_id ); $results->add_success( $index, self::ACTION_UPDATE, $extra ); } private function handle_delete( int $index, array $operation, Bulk_Operations_Result $results ): void { $post_id = isset( $operation['post_id'] ) ? absint( $operation['post_id'] ) : 0; if ( ! $post_id ) { $results->add_error( $index, self::ACTION_DELETE, 'missing_post_id', __( 'post_id is required.', 'elementor-pro' ) ); return; } if ( ! current_user_can( 'delete_post', $post_id ) ) { $results->add_error( $index, self::ACTION_DELETE, 'insufficient_permissions', __( 'You do not have permission to delete this template.', 'elementor-pro' ) ); return; } $document = ThemeBuilderModule::instance()->get_document( $post_id ); if ( ! $document ) { $results->add_error( $index, self::ACTION_DELETE, 'not_a_theme_document', __( 'The given post_id is not a Theme Builder document.', 'elementor-pro' ) ); return; } $type = ThemeBuilderModule::instance()->get_template_type( $post_id ); $deleted = wp_delete_post( $post_id, true ); if ( ! $deleted ) { $results->add_error( $index, self::ACTION_DELETE, 'delete_failed', __( 'Failed to delete the site part.', 'elementor-pro' ) ); return; } ThemeBuilderModule::instance()->get_conditions_manager()->clear_location_cache(); $results->add_success( $index, self::ACTION_DELETE, [ 'post_id' => $post_id, 'type' => $type, ] ); } private function get_display_settings_properties_schema(): array { $properties = $this->display_settings_schema->get_input_schema(); $properties['triggers']['description'] = 'Triggers: what opens the popup. Example: { "page_load": "yes", "page_load_delay": 3 }. "click" counts clicks anywhere on the page; to open from one specific element, use the popup dynamic tag on that element link or the popup "open_selector" setting via update-page-settings instead.'; $properties['timing']['description'] = 'Advanced Rules: who may see the popup and when. Example: { "devices": "yes", "devices_devices": [ "desktop" ] }.'; return $properties; } private function get_supported_types(): array { $types_config = ThemeBuilderModule::instance()->get_types_manager()->get_types_config(); return array_keys( $types_config ); } private function check_create_permission(): ?\WP_Error { if ( ! current_user_can( Theme_Builder_Menu_Item::get_user_capability() ) ) { return new \WP_Error( 'cannot_create_theme_template', __( 'You do not have permission to create theme templates.', 'elementor-pro' ), [ 'status' => \WP_Http::FORBIDDEN ] ); } $post_type_object = get_post_type_object( self::TEMPLATE_LIBRARY_CPT ); if ( ! $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) { return new \WP_Error( 'cannot_create_theme_template', __( 'You do not have permission to create theme templates.', 'elementor-pro' ), [ 'status' => \WP_Http::FORBIDDEN ] ); } return null; } private function bad_request( string $message ): \WP_Error { return new \WP_Error( 'invalid_input', $message, [ 'status' => \WP_Http::BAD_REQUEST ] ); } private function emit_mcp_manage_site_parts_executed( int $started_at, array $operations, ?\WP_Error $top_level_error = null, array $response = [] ): void { if ( ! class_exists( Mcp_Event_Dispatcher::class ) || ! class_exists( Tool_Performance_Metrics::class ) ) { return; } $duration_ms = Tool_Performance_Metrics::duration_ms_since( $started_at ); [ 'status' => $status, 'error_code' => $error_code ] = Tool_Performance_Metrics::resolve_status( $response, $top_level_error ); $failed_results = array_filter( $response['results'] ?? [], fn( $r ) => 'error' === ( $r['status'] ?? '' ) ); $failed_count = count( $failed_results ); $failed_codes = array_values( array_unique( array_column( $failed_results, 'code' ) ) ); $ok_results = array_filter( $response['results'] ?? [], fn( $r ) => 'ok' === ( $r['status'] ?? '' ) ); $post_ids = array_values( array_filter( array_map( fn( $r ) => $r['id'] ?? $r['post_id'] ?? null, $ok_results ) ) ); $document_types = array_values( array_filter( array_column( $ok_results, 'type' ) ) ); $has_conditions = false; $conflicts = 0; foreach ( $ok_results as $row ) { if ( ! empty( $row['conditions'] ) ) { $has_conditions = true; } $conflicts += count( $row['conflicts'] ?? [] ); } $ops_count = count( $operations ); $by_action = []; foreach ( $operations as $op ) { $action = $op['action'] ?? ''; if ( is_string( $action ) && '' !== $action ) { $by_action[ $action ] = ( $by_action[ $action ] ?? 0 ) + 1; } } $dominant_action = ''; if ( ! empty( $by_action ) ) { arsort( $by_action ); $dominant_action = (string) array_key_first( $by_action ); } $payload = [ 'tool_name' => $this->get_ability_id(), 'status' => $status, 'duration_ms' => $duration_ms, 'action' => $dominant_action, 'operations_count' => $ops_count, 'operations_by_type' => $by_action, 'post_ids' => $post_ids, 'document_types' => $document_types, 'has_conditions' => $has_conditions, 'conflicts_count' => $conflicts, 'failed_operations_count' => $failed_count, 'failed_operation_codes' => $failed_codes, 'warning_count' => 0, 'warning_types' => [], ]; if ( null !== $error_code ) { $payload['error_code'] = $error_code; } Mcp_Event_Dispatcher::emit( 'mcp_manage_site_parts_executed', $payload ); } }