registry = $registry ?? V3_Serializer_Registry_Factory::create(); $this->renderer = $renderer ?? new Block_Renderer(); } public function serialize( array $settings, string $widget_type, array $widget_config ): string { $overrides = V3_Widget_Bridge_Registry::get_style_overrides( $widget_type ); $controls = $widget_config['controls'] ?? []; $generic = V3_Style_Settings_Index::build( is_array( $controls ) ? $controls : [], $overrides ); $blocks = new V3_Block_Accumulator(); foreach ( $overrides as $match_key => $entry ) { [ $property, $state ] = $this->split_match_key( (string) $match_key ); $this->dispatch_entry( $blocks, $settings, $entry, $property, $state ); } foreach ( $generic as $match_key => $entry ) { [ $property, $state ] = $this->split_match_key( (string) $match_key ); $this->dispatch_entry( $blocks, $settings, $entry, $property, $state ); } $mapped_css = $this->renderer->render( $blocks ); $custom_css = $this->unwrap_custom_css( $settings['custom_css'] ?? null ); if ( '' === $mapped_css ) { return $custom_css; } if ( '' === $custom_css ) { return $mapped_css; } return $mapped_css . ' ' . $custom_css; } /** * @param mixed $custom_css */ private function unwrap_custom_css( $custom_css ): string { if ( ! is_string( $custom_css ) ) { return ''; } $custom_css = trim( $custom_css ); if ( '' === $custom_css ) { return ''; } if ( preg_match( '/^\s*selector\s*\{\s*([\s\S]*?)\s*\}\s*$/i', $custom_css, $matches ) ) { return trim( $matches[1] ); } return $custom_css; } private function dispatch_entry( V3_Block_Accumulator $blocks, array $settings, array $entry, string $property, ?string $state ): void { foreach ( $this->registry->all() as $serializer ) { if ( ! $serializer->is_supported( $entry, $property, $state ) ) { continue; } $serializer->emit( $blocks, $settings, $entry, $property, $state ); return; } } /** * @return array{0: string, 1: string|null} */ private function split_match_key( string $match_key ): array { if ( false === strpos( $match_key, '@' ) ) { return [ $match_key, null ]; } [ $property, $state ] = explode( '@', $match_key, 2 ); return [ $property, '' === $state ? null : $state ]; } }