$blocks Trusted semantic block HTML. * @param string[] $mandatory_blocks Mandatory semantic block IDs. */ public function compile( TemplateVersion $template, array $blocks, array $mandatory_blocks ): string { $this->validator->validate( $template, $mandatory_blocks ); $html = $template->template_html(); preg_match_all( '/\{\{BLOCK:([A-Z0-9_]+)\}\}/', $html, $matches ); foreach ( array_values( array_unique( $matches[1] ?? array() ) ) as $id ) { if ( ! array_key_exists( $id, $blocks ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Exception text is not rendered; callers control any presentation. throw new \RuntimeException( 'Template requests an unavailable semantic block: ' . $id ); } $markup = '
' . $blocks[ $id ] . '
'; $html = str_replace( '{{BLOCK:' . $id . '}}', $markup, $html ); } if ( str_contains( $html, '{{' ) ) { throw new \RuntimeException( 'Compiled template still contains unresolved placeholders.' ); } // Self-contained renderer input only; never emitted as a WordPress browser page. return '' . $html . ''; } }