)#s'; public function __construct( private BlockAttributes $block_attributes ) { } public function replace( string $content, string $link_url, string $replacement_url ): string { return (string) preg_replace_callback( self::DELIMITER_PATTERN, fn( array $delimiter ): string => $this->replaced_delimiter( $delimiter, $link_url, $replacement_url ), $content ); } /** @param array{0: string, opening: string, attributes: string, closing: string} $delimiter */ private function replaced_delimiter( array $delimiter, string $link_url, string $replacement_url ): string { $attributes = $this->decoded( $delimiter['attributes'] ); $replaced = $this->block_attributes->replace_in( $attributes, $link_url, $replacement_url ); if ( $replaced === $attributes ) { return $delimiter[0]; } return $delimiter['opening'] . serialize_block_attributes( $replaced ) . $delimiter['closing']; } /** @return array */ private function decoded( string $json ): array { $attributes = json_decode( $json, true ); return is_array( $attributes ) ? $attributes : array(); } }