| @@ -18,8 +18,30 @@ | ||
| 18 | 18 | if ( $should_load_view_script ) { |
| 19 | 19 | wp_enqueue_script( 'wp-block-file-view' ); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | + // Update object's aria-label attribute if present in block HTML. | |
| 23 | + | |
| 24 | + // Match an aria-label attribute from an object tag. | |
| 25 | + $pattern = '@<object.+(?<attribute>aria-label="(?<filename>[^"]+)?")@i'; | |
| 26 | + $content = preg_replace_callback( | |
| 27 | + $pattern, | |
| 28 | + function ( $matches ) { | |
| 29 | + $filename = ! empty( $matches['filename'] ) ? $matches['filename'] : ''; | |
| 30 | + $has_filename = ! empty( $filename ) && 'PDF embed' !== $filename; | |
| 31 | + $label = $has_filename ? | |
| 32 | + sprintf( | |
| 33 | + /* translators: %s: filename. */ | |
| 34 | + __( 'Embed of %s.' ), | |
| 35 | + $filename | |
| 36 | + ) | |
| 37 | + : __( 'PDF embed' ); | |
| 38 | + | |
| 39 | + return str_replace( $matches['attribute'], sprintf( 'aria-label="%s"', $label ), $matches[0] ); | |
| 40 | + }, | |
| 41 | + $content | |
| 42 | + ); | |
| 43 | + | |
| 22 | 44 | return $content; |
| 23 | 45 | } |
| 24 | 46 | |
| 25 | 47 | /** |