$language, 'code' => $code_content, 'file_name' => $file_name ] ]; // Normalize every entry up front. `code_variants` arrives from block attributes // (and from Pro's API-docs generator), so a caller can legitimately hand over an // entry missing `language` or `code` — reading them directly emitted PHP 8 // warnings mid-render. The tab template already guards this way. $variants = array_values( array_filter( array_map( function ( $variant ) { if ( ! is_array( $variant ) ) { return null; } return [ 'language' => isset( $variant['language'] ) ? (string) $variant['language'] : '', 'code' => isset( $variant['code'] ) ? (string) $variant['code'] : '', 'file_name' => isset( $variant['file_name'] ) ? (string) $variant['file_name'] : '' ]; }, $variants ) ) ); // Everything below indexes $variants[0]; keep the single-entry fallback so an // empty or fully-invalid list still renders rather than fatalling. if ( empty( $variants ) ) { $variants = [ [ 'language' => $language, 'code' => (string) $code_content, 'file_name' => $file_name ] ]; } $is_multi = count( $variants ) > 1; $active = $variants[0]; $active_lang = sanitize_text_field( $active['language'] ); // Dropdown/tab label for an entry: a custom filename overrides (so the API-docs // cURL sample reads "cURL", not "Bash"), otherwise the language's display name. // The block default filename ("filename.js") never wins. $lang_label = function ( $variant ) { $file = isset( $variant['file_name'] ) ? (string) $variant['file_name'] : ''; if ( '' !== $file && 'filename.js' !== $file ) { return $file; } return Helper::get_language_label( sanitize_text_field( isset( $variant['language'] ) ? $variant['language'] : '' ) ); }; // Enqueue necessary assets wp_enqueue_script( 'betterdocs-code-snippet' ); wp_enqueue_style( 'betterdocs-code-snippet' ); ?>