Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test refactor of wp_video_shortcut
  • Loading branch information
dmsnell committed Jan 25, 2024
commit 97139af2bffb457d9790a738fdc58eb946218bd7
27 changes: 25 additions & 2 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2878,14 +2878,37 @@ public function set_attribute( $name, $value ) {
* > To represent a false value, the attribute has to be omitted altogether.
* - HTML5 spec, https://html.spec.whatwg.org/#boolean-attributes
*/
if ( false === $value ) {
if ( false === $value || null === $value ) {
return $this->remove_attribute( $name );
}

if ( true === $value ) {
$updated_attribute = $name;
} else {
$escaped_new_value = esc_attr( $value );
$tag_name = $this->get_tag();
$comparable_name = strtolower( $name );

/*
* Escape URL attributes.
*
* @see https://html.spec.whatwg.org/#attributes-3
*/
if (
! str_starts_with( $value, 'data:' ) && (
'cite' === $comparable_name ||
'formaction' === $comparable_name ||
'href' === $comparable_name ||
'ping' === $comparable_name ||
'src' === $comparable_name ||
( 'FORM' === $tag_name && 'action' === $comparable_name ) ||
( 'OBJECT' === $tag_name && 'data' === $comparable_name ) ||
( 'VIDEO' === $tag_name && 'poster' === $comparable_name )
)
) {
$escaped_new_value = esc_url( $value );
} else {
$escaped_new_value = esc_attr( $value );
}
$updated_attribute = "{$name}=\"{$escaped_new_value}\"";
}

Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/html-api/class-wp-html-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function render( $template, $args = array() ) {
$spread_name = substr( $attribute_name, 3 );
if ( isset( $args[ $spread_name ] ) && is_array( $args[ $spread_name ] ) ) {
foreach ( $args[ $spread_name ] as $key => $value ) {
if ( true === $value || null === $value || is_string( $value ) ) {
if ( true === $value || false === $value || null === $value || is_string( $value ) ) {
$processor->set_attribute( $key, $value );
}
}
Expand All @@ -128,7 +128,7 @@ public static function render( $template, $args = array() ) {

if ( array_key_exists( $name, $args ) ) {
$value = $args[ $name ];
if ( null === $value ) {
if ( false === $value || null === $value ) {
$processor->remove_attribute( $attribute_name );
} elseif ( true === $value ) {
$processor->set_attribute( $attribute_name, true );
Expand Down
68 changes: 37 additions & 31 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,10 @@ function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
* @return string HTML IMG element for given image attachment.
*/
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {

list( $img_src, $width, $height ) = image_downsize( $id, $size );
$hwstring = image_hwstring( $width, $height );

$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';

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

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

$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
$html = WP_HTML::render(
<<<'HTML'
<img src="</%src>" alt="</%alt>" title="</%title>" class="</%class>" height="</%height>" width="</%width>">
HTML,
array(
'alt' => $alt,
'class' => $class,
'height' => (string) $height,
'src' => $img_src,
'title' => empty( $title ) ? null : $title,
'width' => (string) $width,
)
);

/**
* Filters the HTML content for the image tag.
Expand Down Expand Up @@ -3603,37 +3611,24 @@ function wp_video_shortcode( $attr, $content = '' ) {
$html_atts = array(
'class' => $atts['class'],
'id' => sprintf( 'video-%d-%d', $post_id, $instance ),
'width' => absint( $atts['width'] ),
'height' => absint( $atts['height'] ),
'poster' => esc_url( $atts['poster'] ),
'width' => (string) absint( $atts['width'] ),
'height' => (string) absint( $atts['height'] ),
'poster' => empty( $atts['poster'] ) ? null : $atts['poster'],
'loop' => wp_validate_boolean( $atts['loop'] ),
'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
'muted' => wp_validate_boolean( $atts['muted'] ),
'preload' => $atts['preload'],
'preload' => empty( $atts['preload'] ) ? null : $attr['preload'],
);

// These ones should just be omitted altogether if they are blank.
foreach ( array( 'poster', 'loop', 'autoplay', 'preload', 'muted' ) as $a ) {
if ( empty( $html_atts[ $a ] ) ) {
unset( $html_atts[ $a ] );
}
}

$attr_strings = array();
foreach ( $html_atts as $k => $v ) {
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
}

$html = '';

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

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

$fileurl = '';
$source = '<source type="%s" src="%s" />';

foreach ( $default_types as $fallback ) {
if ( ! empty( $atts[ $fallback ] ) ) {
Expand All @@ -3647,8 +3642,14 @@ function wp_video_shortcode( $attr, $content = '' ) {
} else {
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
}
$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
$html .= sprintf( $source, $type['type'], esc_url( $url ) );

$html .= WP_HTML::render(
'<source type="</%source>" src="</%src>">',
array(
'source' => $type['type'],
'src' => add_query_arg( '_', $instance, $atts[ $fallback ] ),
)
);
}
}

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

$width_rule = '';
if ( ! empty( $atts['width'] ) ) {
$width_rule = sprintf( 'width: %dpx;', $atts['width'] );
}
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html );
$output = (
WP_HTML::render(
'<div class="wp-video" style="</%width>">',
array(
'width' => ! empty( $atts['width'] ) ? "width: {$atts['width']}px;" : null,
)
) .
$html .
'</div>'
);

/**
* Filters the output of the video shortcode.
Expand Down