Skip to content

Commit f1f3106

Browse files
committed
Media: Ensure figcaption/figure IDs are unique.
Using the same image multiple times with a `figure` and `fig caption` would result in duplicate IDs, creating unclear relationships and breaking 3rd party expectations for anchor targets. Check id attributes and append a counter to ensure each attribute is unique, while retaining the current expectation of a counter-less ID attribute for the first instance. Update tests to verify. Developed in WordPress/wordpress-develop#11940 Props steelwagstaff, westonruter, jamesbregenzer, joedolson, mukesh27. Fixes #65315. Built from https://develop.svn.wordpress.org/trunk@62698 git-svn-id: http://core.svn.wordpress.org/trunk@61982 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 7ac9bfb commit f1f3106

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎wp-includes/media.php‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,16 @@ function img_caption_shortcode( $attr, $content = '' ) {
26562656
return $output;
26572657
}
26582658

2659+
/**
2660+
* @var array{
2661+
* id: string,
2662+
* caption_id: string,
2663+
* align: string,
2664+
* width: string,
2665+
* caption: string,
2666+
* class: string,
2667+
* } $atts
2668+
*/
26592669
$atts = shortcode_atts(
26602670
array(
26612671
'id' => '',
@@ -2675,24 +2685,30 @@ function img_caption_shortcode( $attr, $content = '' ) {
26752685
return $content;
26762686
}
26772687

2678-
$id = '';
2679-
$caption_id = '';
2680-
$describedby = '';
2688+
$id = '';
2689+
$caption_id = '';
2690+
$describedby = '';
2691+
$unique_id_value = '';
2692+
$caption_id_value = '';
26812693

26822694
if ( $atts['id'] ) {
2683-
$atts['id'] = sanitize_html_class( $atts['id'] );
2684-
$id = 'id="' . esc_attr( $atts['id'] ) . '" ';
2695+
$atts['id'] = sanitize_html_class( $atts['id'] );
2696+
$unique_id_value = (string) preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['id'] . '-' ) );
2697+
$id = 'id="' . esc_attr( $unique_id_value ) . '" ';
26852698
}
26862699

26872700
if ( $atts['caption_id'] ) {
2701+
// User explicitly provided a caption_id - make it unique.
26882702
$atts['caption_id'] = sanitize_html_class( $atts['caption_id'] );
2689-
} elseif ( $atts['id'] ) {
2690-
$atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] );
2703+
$caption_id_value = preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['caption_id'] . '-' ) );
2704+
} elseif ( $unique_id_value ) {
2705+
// Derive from the already-unique figure ID - guaranteed unique, no need for second call.
2706+
$caption_id_value = 'caption-' . str_replace( '_', '-', $unique_id_value );
26912707
}
26922708

2693-
if ( $atts['caption_id'] ) {
2694-
$caption_id = 'id="' . esc_attr( $atts['caption_id'] ) . '" ';
2695-
$describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" ';
2709+
if ( $caption_id_value ) {
2710+
$caption_id = 'id="' . esc_attr( $caption_id_value ) . '" ';
2711+
$describedby = 'aria-describedby="' . esc_attr( $caption_id_value ) . '" ';
26962712
}
26972713

26982714
$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );

‎wp-includes/version.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.1-alpha-62697';
19+
$wp_version = '7.1-alpha-62698';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)