Skip to content

Commit 33567c5

Browse files
committed
Test refactor of wp_video_shortcut
1 parent 2a964f4 commit 33567c5

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
@@ -2812,14 +2812,37 @@ public function set_attribute( $name, $value ) {
28122812
* > To represent a false value, the attribute has to be omitted altogether.
28132813
* - HTML5 spec, https://html.spec.whatwg.org/#boolean-attributes
28142814
*/
2815-
if ( false === $value ) {
2815+
if ( false === $value || null === $value ) {
28162816
return $this->remove_attribute( $name );
28172817
}
28182818

28192819
if ( true === $value ) {
28202820
$updated_attribute = $name;
28212821
} else {
2822-
$escaped_new_value = esc_attr( $value );
2822+
$tag_name = $this->get_tag();
2823+
$comparable_name = strtolower( $name );
2824+
2825+
/*
2826+
* Escape URL attributes.
2827+
*
2828+
* @see https://html.spec.whatwg.org/#attributes-3
2829+
*/
2830+
if (
2831+
! str_starts_with( $value, 'data:' ) && (
2832+
'cite' === $comparable_name ||
2833+
'formaction' === $comparable_name ||
2834+
'href' === $comparable_name ||
2835+
'ping' === $comparable_name ||
2836+
'src' === $comparable_name ||
2837+
( 'FORM' === $tag_name && 'action' === $comparable_name ) ||
2838+
( 'OBJECT' === $tag_name && 'data' === $comparable_name ) ||
2839+
( 'VIDEO' === $tag_name && 'poster' === $comparable_name )
2840+
)
2841+
) {
2842+
$escaped_new_value = esc_url( $value );
2843+
} else {
2844+
$escaped_new_value = esc_attr( $value );
2845+
}
28232846
$updated_attribute = "{$name}=\"{$escaped_new_value}\"";
28242847
}
28252848

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.
@@ -3596,37 +3604,24 @@ function wp_video_shortcode( $attr, $content = '' ) {
35963604
$html_atts = array(
35973605
'class' => $atts['class'],
35983606
'id' => sprintf( 'video-%d-%d', $post_id, $instance ),
3599-
'width' => absint( $atts['width'] ),
3600-
'height' => absint( $atts['height'] ),
3601-
'poster' => esc_url( $atts['poster'] ),
3607+
'width' => (string) absint( $atts['width'] ),
3608+
'height' => (string) absint( $atts['height'] ),
3609+
'poster' => empty( $atts['poster'] ) ? null : $atts['poster'],
36023610
'loop' => wp_validate_boolean( $atts['loop'] ),
36033611
'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
36043612
'muted' => wp_validate_boolean( $atts['muted'] ),
3605-
'preload' => $atts['preload'],
3613+
'preload' => empty( $atts['preload'] ) ? null : $attr['preload'],
36063614
);
36073615

3608-
// These ones should just be omitted altogether if they are blank.
3609-
foreach ( array( 'poster', 'loop', 'autoplay', 'preload', 'muted' ) as $a ) {
3610-
if ( empty( $html_atts[ $a ] ) ) {
3611-
unset( $html_atts[ $a ] );
3612-
}
3613-
}
3614-
3615-
$attr_strings = array();
3616-
foreach ( $html_atts as $k => $v ) {
3617-
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
3618-
}
3619-
36203616
$html = '';
36213617

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

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

36283624
$fileurl = '';
3629-
$source = '<source type="%s" src="%s" />';
36303625

36313626
foreach ( $default_types as $fallback ) {
36323627
if ( ! empty( $atts[ $fallback ] ) ) {
@@ -3640,8 +3635,14 @@ function wp_video_shortcode( $attr, $content = '' ) {
36403635
} else {
36413636
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
36423637
}
3643-
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
3644-
$html .= sprintf( $source, $type['type'], esc_url( $url ) );
3638+
3639+
$html .= WP_HTML::render(
3640+
'<source type="</%source>" src="</%src>">',
3641+
array(
3642+
'source' => $type['type'],
3643+
'src' => add_query_arg( '_', $instance, $atts[ $fallback ] ),
3644+
)
3645+
);
36453646
}
36463647
}
36473648

@@ -3657,11 +3658,16 @@ function wp_video_shortcode( $attr, $content = '' ) {
36573658
}
36583659
$html .= '</video>';
36593660

3660-
$width_rule = '';
3661-
if ( ! empty( $atts['width'] ) ) {
3662-
$width_rule = sprintf( 'width: %dpx;', $atts['width'] );
3663-
}
3664-
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html );
3661+
$output = (
3662+
WP_HTML::render(
3663+
'<div class="wp-video" style="</%width>">',
3664+
array(
3665+
'width' => ! empty( $atts['width'] ) ? "width: {$atts['width']}px;" : null,
3666+
)
3667+
) .
3668+
$html .
3669+
'</div>'
3670+
);
36653671

36663672
/**
36673673
* Filters the output of the video shortcode.

0 commit comments

Comments
 (0)