Skip to content

Commit 97139af

Browse files
committed
Test refactor of wp_video_shortcut
1 parent fdfd521 commit 97139af

3 files changed

Lines changed: 64 additions & 35 deletions

File tree

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,14 +2878,37 @@ public function set_attribute( $name, $value ) {
28782878
* > To represent a false value, the attribute has to be omitted altogether.
28792879
* - HTML5 spec, https://html.spec.whatwg.org/#boolean-attributes
28802880
*/
2881-
if ( false === $value ) {
2881+
if ( false === $value || null === $value ) {
28822882
return $this->remove_attribute( $name );
28832883
}
28842884

28852885
if ( true === $value ) {
28862886
$updated_attribute = $name;
28872887
} else {
2888-
$escaped_new_value = esc_attr( $value );
2888+
$tag_name = $this->get_tag();
2889+
$comparable_name = strtolower( $name );
2890+
2891+
/*
2892+
* Escape URL attributes.
2893+
*
2894+
* @see https://html.spec.whatwg.org/#attributes-3
2895+
*/
2896+
if (
2897+
! str_starts_with( $value, 'data:' ) && (
2898+
'cite' === $comparable_name ||
2899+
'formaction' === $comparable_name ||
2900+
'href' === $comparable_name ||
2901+
'ping' === $comparable_name ||
2902+
'src' === $comparable_name ||
2903+
( 'FORM' === $tag_name && 'action' === $comparable_name ) ||
2904+
( 'OBJECT' === $tag_name && 'data' === $comparable_name ) ||
2905+
( 'VIDEO' === $tag_name && 'poster' === $comparable_name )
2906+
)
2907+
) {
2908+
$escaped_new_value = esc_url( $value );
2909+
} else {
2910+
$escaped_new_value = esc_attr( $value );
2911+
}
28892912
$updated_attribute = "{$name}=\"{$escaped_new_value}\"";
28902913
}
28912914

src/wp-includes/html-api/class-wp-html-template.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static function render( $template, $args = array() ) {
108108
$spread_name = substr( $attribute_name, 3 );
109109
if ( isset( $args[ $spread_name ] ) && is_array( $args[ $spread_name ] ) ) {
110110
foreach ( $args[ $spread_name ] as $key => $value ) {
111-
if ( true === $value || null === $value || is_string( $value ) ) {
111+
if ( true === $value || false === $value || null === $value || is_string( $value ) ) {
112112
$processor->set_attribute( $key, $value );
113113
}
114114
}
@@ -128,7 +128,7 @@ public static function render( $template, $args = array() ) {
128128

129129
if ( array_key_exists( $name, $args ) ) {
130130
$value = $args[ $name ];
131-
if ( null === $value ) {
131+
if ( false === $value || null === $value ) {
132132
$processor->remove_attribute( $attribute_name );
133133
} elseif ( true === $value ) {
134134
$processor->set_attribute( $attribute_name, true );

src/wp-includes/media.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,10 @@ function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
381381
* @return string HTML IMG element for given image attachment.
382382
*/
383383
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
384-
385384
list( $img_src, $width, $height ) = image_downsize( $id, $size );
386-
$hwstring = image_hwstring( $width, $height );
387-
388-
$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
389385

390386
$size_class = is_array( $size ) ? implode( 'x', $size ) : $size;
391-
$class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id;
387+
$class = "align{$align} size-{$size_class} wp-image-{$id}";
392388

393389
/**
394390
* Filters the value of the attachment's image tag class attribute.
@@ -403,7 +399,19 @@ function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
403399
*/
404400
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
405401

406-
$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
402+
$html = WP_HTML::render(
403+
<<<'HTML'
404+
<img src="</%src>" alt="</%alt>" title="</%title>" class="</%class>" height="</%height>" width="</%width>">
405+
HTML,
406+
array(
407+
'alt' => $alt,
408+
'class' => $class,
409+
'height' => (string) $height,
410+
'src' => $img_src,
411+
'title' => empty( $title ) ? null : $title,
412+
'width' => (string) $width,
413+
)
414+
);
407415

408416
/**
409417
* Filters the HTML content for the image tag.
@@ -3603,37 +3611,24 @@ function wp_video_shortcode( $attr, $content = '' ) {
36033611
$html_atts = array(
36043612
'class' => $atts['class'],
36053613
'id' => sprintf( 'video-%d-%d', $post_id, $instance ),
3606-
'width' => absint( $atts['width'] ),
3607-
'height' => absint( $atts['height'] ),
3608-
'poster' => esc_url( $atts['poster'] ),
3614+
'width' => (string) absint( $atts['width'] ),
3615+
'height' => (string) absint( $atts['height'] ),
3616+
'poster' => empty( $atts['poster'] ) ? null : $atts['poster'],
36093617
'loop' => wp_validate_boolean( $atts['loop'] ),
36103618
'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
36113619
'muted' => wp_validate_boolean( $atts['muted'] ),
3612-
'preload' => $atts['preload'],
3620+
'preload' => empty( $atts['preload'] ) ? null : $attr['preload'],
36133621
);
36143622

3615-
// These ones should just be omitted altogether if they are blank.
3616-
foreach ( array( 'poster', 'loop', 'autoplay', 'preload', 'muted' ) as $a ) {
3617-
if ( empty( $html_atts[ $a ] ) ) {
3618-
unset( $html_atts[ $a ] );
3619-
}
3620-
}
3621-
3622-
$attr_strings = array();
3623-
foreach ( $html_atts as $k => $v ) {
3624-
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
3625-
}
3626-
36273623
$html = '';
36283624

36293625
if ( 'mediaelement' === $library && 1 === $instance ) {
36303626
$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
36313627
}
36323628

3633-
$html .= sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) );
3629+
$html .= WP_HTML::render( '<video controls="controls" ...args>', array( 'args' => $html_atts ) );
36343630

36353631
$fileurl = '';
3636-
$source = '<source type="%s" src="%s" />';
36373632

36383633
foreach ( $default_types as $fallback ) {
36393634
if ( ! empty( $atts[ $fallback ] ) ) {
@@ -3647,8 +3642,14 @@ function wp_video_shortcode( $attr, $content = '' ) {
36473642
} else {
36483643
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
36493644
}
3650-
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3651-
$html .= sprintf( $source, $type['type'], esc_url( $url ) );
3645+
3646+
$html .= WP_HTML::render(
3647+
'<source type="</%source>" src="</%src>">',
3648+
array(
3649+
'source' => $type['type'],
3650+
'src' => add_query_arg( '_', $instance, $atts[ $fallback ] ),
3651+
)
3652+
);
36523653
}
36533654
}
36543655

@@ -3664,11 +3665,16 @@ function wp_video_shortcode( $attr, $content = '' ) {
36643665
}
36653666
$html .= '</video>';
36663667

3667-
$width_rule = '';
3668-
if ( ! empty( $atts['width'] ) ) {
3669-
$width_rule = sprintf( 'width: %dpx;', $atts['width'] );
3670-
}
3671-
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html );
3668+
$output = (
3669+
WP_HTML::render(
3670+
'<div class="wp-video" style="</%width>">',
3671+
array(
3672+
'width' => ! empty( $atts['width'] ) ? "width: {$atts['width']}px;" : null,
3673+
)
3674+
) .
3675+
$html .
3676+
'</div>'
3677+
);
36723678

36733679
/**
36743680
* Filters the output of the video shortcode.

0 commit comments

Comments
 (0)