bad_request( string $message ): \WP_Error { return new \WP_Error( 'invalid_input', $message, [ 'status' => \WP_Http::BAD_REQUEST ] ); } private function is_size_type( $type ): bool { return in_array( $type, [ self::TYPE_SIZE, self::TYPE_CUSTOM_SIZE ], true ); } private function is_size_variable( $id ): bool { return is_string( $id ) && $this->is_size_type( $this->get_service()->find_type( $id ) ); } private function can_manage_size_variables(): bool { return Utils::is_license_active(); } private function get_description(): string { return __( 'Manage V4 global variables (color, font, size, custom-size). Bulk create, update, or delete on the active kit (up to 50 operations).', 'elementor' ); } private function clear_cache(): void { if ( ! class_exists( Plugin::class ) || ! isset( Plugin::$instance ) ) { return; } Plugin::$instance->files_manager->clear_cache(); $this->flush_runtime_cache(); } private function flush_runtime_cache(): void { if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } } private function get_service(): Variables_Service { if ( $this->service ) { return $this->service; } $kit = Plugin::$instance->kits_manager->get_active_kit(); return new Variables_Service( new Variables_Repository( $kit ), new Batch_Processor() ); } private function emit_variable_events( array $batch_results ): void { $event_by_action = [ 'create' => 'variable_created', 'update' => 'variable_updated', ]; $var_type_labels = [ self::TYPE_COLOR => 'color', self::TYPE_FONT => 'font', self::TYPE_SIZE => 'size', self::TYPE_CUSTOM_SIZE => 'size', ]; foreach ( $batch_results as $result ) { if ( 'ok' !== ( $result['status'] ?? '' ) ) { continue; } $action = $result['action'] ?? ''; if ( ! isset( $event_by_action[ $action ] ) ) { continue; } $raw_type = $result['type'] ?? ''; $var_type = $var_type_labels[ $raw_type ] ?? $raw_type; Mcp_Event_Dispatcher::emit( $event_by_action[ $action ], [ 'id' => $result['id'] ?? '', 'name' => $result['label'] ?? '', 'var_type' => $var_type, ] ); } } private function emit_mcp_manage_global_variable_executed( int $started_at, array $operations, ?\WP_Error $top_level_error = null, array $response = [] ): void { $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' ) ) ); $ops_count = count( $operations ); $by_action = []; $variable_types = []; foreach ( $operations as $op ) { $action = $op['action'] ?? ''; if ( is_string( $action ) && '' !== $action ) { $by_action[ $action ] = ( $by_action[ $action ] ?? 0 ) + 1; } } $ok_results = array_filter( $response['results'] ?? [], fn( $r ) => 'ok' === ( $r['status'] ?? '' ) ); foreach ( $ok_results as $row ) { $type = $row['type'] ?? ''; if ( is_string( $type ) && '' !== $type ) { $variable_types[ $type ] = ( $variable_types[ $type ] ?? 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, 'variable_types' => $variable_types, '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_global_variable_executed', $payload ); } }