'...', * ] * * @var array */ private static array $cache = []; /** * Lädt eine SVG-Datei und gibt ihren Inhalt inline zurück. * * @param string $path Absoluter Pfad zur SVG-Datei. * @param array $args Optionale Modifikationen: * - class (string) : zusätzliche CSS-Klasse für das -Element * - title (string) : -Tag für Accessibility * - role (string) : ARIA role, z.B. 'img' * - aria_hidden (bool): ob aria-hidden="true" gesetzt werden soll * * @return string Der Inline-SVG-Code oder ein leerer String bei Fehlern. */ public static function get( string $path, array $args = [] ): string { if ( '' === $path ) { return ''; } if ( function_exists( 'wp_normalize_path' ) ) { $path = wp_normalize_path( $path ); } if ( strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ) !== 'svg' ) { return ''; } if ( ! file_exists( $path ) || ! is_readable( $path ) ) { if ( function_exists( 'error_log' ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Only reachable for a missing/unreadable file path, not routine production logging. error_log( sprintf( 'Svg_Loader: SVG file not found or not readable: %s', $path ) ); } return ''; } $cache_key = md5( $path . '|' . serialize( $args ) ); if ( isset( self::$cache[ $cache_key ] ) ) { return self::$cache[ $cache_key ]; } $svg = file_get_contents( $path ); if ( false === $svg ) { return ''; } if ( stripos( $svg, '-Element optionale Attribute hinzu (class, title, ARIA). * * @param string $svg SVG-Rohstring. * @param array $args Siehe self::get(). * * @return string */ private static function maybe_inject_attributes( string $svg, array $args ): string { $defaults = [ 'class' => '', 'title' => '', 'role' => '', 'aria_hidden' => null, // null = nicht setzen, true/false = explizit setzen ]; $args = array_merge( $defaults, $args ); $extra_attrs = ''; if ( $args['class'] !== '' ) { $extra_attrs .= ' class="' . esc_attr( $args['class'] ) . '"'; } if ( $args['role'] !== '' ) { $extra_attrs .= ' role="' . esc_attr( $args['role'] ) . '"'; } if ( $args['aria_hidden'] !== null ) { $extra_attrs .= ' aria-hidden="' . ( $args['aria_hidden'] ? 'true' : 'false' ) . '"'; } if ( $extra_attrs !== '' ) { $svg = preg_replace( '/]*)>/', '', $svg, 1 ); } if ( $args['title'] !== '' ) { $title = esc_html( $args['title'] ); if ( stripos( $svg, ']*)>/', '' . $title . '', $svg, 1 ); } } return $svg; } }