$block Block data. * @return string */ public static function render_advanced_code( string $block_content, array $block ): string { if ( ( $block['blockName'] ?? '' ) !== 'core/code' ) { return $block_content; } if ( ! self::is_advanced_enabled( $block ) ) { return $block_content; } $code = self::extract_code_content( $block_content ); return self::build_enhanced_block( $code, $block ); } /** * Whether advanced features apply to this block. * * @param array $block Block data. */ private static function is_advanced_enabled( array $block ): bool { $global_enabled = (bool) get_option( 'forwp_advanced_code_enabled', true ); $block_enabled = $block['attrs']['advancedEnabled'] ?? true; return $global_enabled && $block_enabled; } /** * Extract code text from saved core/code HTML. */ private static function extract_code_content( string $block_content ): string { if ( preg_match( '/]*>(.*?)<\/code>/s', $block_content, $matches ) ) { return html_entity_decode( wp_strip_all_tags( $matches[1] ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } return ''; } /** * Build enhanced markup shared with the custom block render template. * * @param string $code Code sample text. * @param array $block Block data. */ public static function build_enhanced_block( string $code, array $block ): string { $attrs = $block['attrs'] ?? array(); $language = $attrs['language'] ?? get_option( 'forwp_advanced_code_default_language', 'auto' ); if ( 'auto' === $language ) { $language = Seo_Handler::detect_language( $code ); } $theme = $attrs['theme'] ?? get_option( 'forwp_advanced_code_theme', 'light' ); $show_copy = $attrs['showCopy'] ?? true; $show_share = $attrs['showShare'] ?? true; $note = $attrs['note'] ?? ''; $block_id = 'forwp-code-' . wp_generate_uuid4(); ob_start(); ?>