Skip to content

Commit c01fabd

Browse files
committed
Fix header image filtering and YouTube header video detection
* Make sure that any get_header_image_tag filters from theme get applied. Fixes quality issue in Twenty Seventeen. * Fix detection of YouTube short URLs. * Dequeue wp-custom-header instead of deregistering to prevent Query Monitor complaining.
1 parent 0f3491a commit c01fabd

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

includes/class-amp-theme-support.php

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ public static function add_hooks() {
369369
add_action( 'comment_form', array( __CLASS__, 'amend_comment_form' ), 100 );
370370
remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
371371
add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'whitelist_layout_in_wp_kses_allowed_html' ), 10 );
372-
add_filter( 'get_header_image_tag', array( __CLASS__, 'conditionally_output_header' ), 10, 3 );
372+
add_filter( 'get_header_image_tag', array( __CLASS__, 'amend_header_image_with_video_header' ), PHP_INT_MAX );
373+
add_action( 'wp_print_footer_scripts', function() {
374+
wp_dequeue_script( 'wp-custom-header' );
375+
}, 0 );
373376
add_action( 'wp_enqueue_scripts', function() {
374377
wp_dequeue_script( 'comment-reply' ); // Handled largely by AMP_Comments_Sanitizer and *reply* methods in this class.
375378
} );
@@ -1356,46 +1359,25 @@ public static function enqueue_assets() {
13561359
* Conditionally replace the header image markup with a header video or image.
13571360
*
13581361
* This is JS-driven in Core themes like Twenty Sixteen and Twenty Seventeen.
1359-
* So in order for the header video to display,
1360-
* this replaces the markup of the header image.
1361-
*
1362-
* @since 1.0
1363-
* @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L54
1364-
* @param string $html The image markup to filter.
1365-
* @param array $header The header config array.
1366-
* @param array $atts The image markup attributes.
1367-
* @return string $html Filtered markup.
1368-
*/
1369-
public static function conditionally_output_header( $html, $header, $atts ) {
1370-
unset( $header );
1371-
if ( ! is_header_video_active() ) {
1372-
return $html;
1373-
};
1374-
1375-
if ( ! has_header_video() ) {
1376-
return AMP_HTML_Utils::build_tag( 'amp-img', $atts );
1377-
}
1378-
1379-
return self::output_header_video( $atts );
1380-
}
1381-
1382-
/**
1383-
* Replace the header image markup with a header video.
1362+
* So in order for the header video to display, this replaces the markup of the header image.
13841363
*
13851364
* @since 1.0
13861365
* @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L54
13871366
* @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L78
13881367
*
1389-
* @param array $atts The header tag attributes array.
1368+
* @param string $image_markup The image markup to filter.
13901369
* @return string $html Filtered markup.
13911370
*/
1392-
public static function output_header_video( $atts ) {
1393-
// Remove the script for video.
1394-
wp_deregister_script( 'wp-custom-header' );
1395-
$video_settings = get_header_video_settings();
1371+
public static function amend_header_image_with_video_header( $image_markup ) {
13961372

1373+
// If there is no video, just pass the image through.
1374+
if ( ! has_header_video() || ! is_header_video_active() ) {
1375+
return $image_markup;
1376+
};
1377+
1378+
$video_settings = get_header_video_settings();
13971379
$parsed_url = wp_parse_url( $video_settings['videoUrl'] );
1398-
$query = isset( $parsed_url['query'] ) ? wp_parse_args( $parsed_url['query'] ) : null;
1380+
$query = isset( $parsed_url['query'] ) ? wp_parse_args( $parsed_url['query'] ) : array();
13991381
$video_attributes = array(
14001382
'media' => '(min-width: ' . $video_settings['minWidth'] . 'px)',
14011383
'width' => $video_settings['width'],
@@ -1405,25 +1387,31 @@ public static function output_header_video( $atts ) {
14051387
'id' => 'wp-custom-header-video',
14061388
);
14071389

1408-
// Create image banner to stay behind the video.
1409-
$image_header = AMP_HTML_Utils::build_tag( 'amp-img', $atts );
1390+
$youtube_id = null;
1391+
if ( isset( $parsed_url['host'] ) && preg_match( '/(^|\.)(youtube\.com|youtu\.be)$/', $parsed_url['host'], $host_matches ) ) {
1392+
if ( 'youtu.be' === $parsed_url['host'] && ! empty( $parsed_url['path'] ) ) {
1393+
$youtube_id = trim( $parsed_url['path'], '/' );
1394+
} elseif ( isset( $query['v'] ) ) {
1395+
$youtube_id = $query['v'];
1396+
}
1397+
}
14101398

14111399
// If the video URL is for YouTube, return an <amp-youtube> element.
1412-
if ( isset( $parsed_url['host'], $query['v'] ) && ( false !== strpos( $parsed_url['host'], 'youtube' ) ) ) {
1413-
$video_header = AMP_HTML_Utils::build_tag(
1400+
if ( ! empty( $youtube_id ) ) {
1401+
$video_markup = AMP_HTML_Utils::build_tag(
14141402
'amp-youtube',
14151403
array_merge(
14161404
$video_attributes,
14171405
array(
1418-
'data-videoid' => $query['v'],
1406+
'data-videoid' => $youtube_id,
14191407
'data-param-rel' => '0', // Don't show related videos.
14201408
'data-param-showinfo' => '0', // Don't show video title at the top.
14211409
'data-param-controls' => '0', // Don't show video controls.
14221410
)
14231411
)
14241412
);
14251413
} else {
1426-
$video_header = AMP_HTML_Utils::build_tag(
1414+
$video_markup = AMP_HTML_Utils::build_tag(
14271415
'amp-video',
14281416
array_merge(
14291417
$video_attributes,
@@ -1434,6 +1422,6 @@ public static function output_header_video( $atts ) {
14341422
);
14351423
}
14361424

1437-
return $image_header . $video_header;
1425+
return $image_markup . $video_markup;
14381426
}
14391427
}

tests/test-class-amp-theme-support.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public function test_add_hooks() {
386386
$this->assertEquals( 10, has_filter( 'cancel_comment_reply_link', array( self::TESTED_CLASS, 'filter_cancel_comment_reply_link' ) ) );
387387
$this->assertEquals( 100, has_action( 'comment_form', array( self::TESTED_CLASS, 'amend_comment_form' ) ) );
388388
$this->assertFalse( has_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ) );
389-
$this->assertEquals( 10, has_filter( 'get_header_image_tag', array( self::TESTED_CLASS, 'conditionally_output_header' ) ) );
389+
$this->assertEquals( PHP_INT_MAX, has_filter( 'get_header_image_tag', array( self::TESTED_CLASS, 'amend_header_image_with_video_header' ) ) );
390390
}
391391

392392
/**
@@ -1338,15 +1338,15 @@ public function test_whitelist_layout_in_wp_kses_allowed_html() {
13381338
/**
13391339
* Test AMP_Theme_Support::conditionally_output_header().
13401340
*
1341-
* @see AMP_Theme_Support::conditionally_output_header()
1341+
* @see AMP_Theme_Support::amend_header_image_with_video_header()
13421342
*/
13431343
public function conditionally_output_header() {
13441344
$mock_image = '<img src="https://example.com/flower.jpeg">';
13451345

13461346
// If there's no theme support for 'custom-header', the callback should simply return the image.
13471347
$this->assertEquals(
13481348
$mock_image,
1349-
AMP_Theme_Support::conditionally_output_header( $mock_image )
1349+
AMP_Theme_Support::amend_header_image_with_video_header( $mock_image )
13501350
);
13511351

13521352
// If theme support is present, but there isn't a header video selected, the callback should again return the image.
@@ -1358,7 +1358,7 @@ public function conditionally_output_header() {
13581358
set_theme_mod( 'external_header_video', 'https://www.youtube.com/watch?v=a8NScvBhVnc' );
13591359
$this->assertEquals(
13601360
'<amp-youtube width="0" height="0" layout="responsive" autoplay id="wp-custom-header-video" data-videoid="a8NScvBhVnc" data-param-rel="0" data-param-showinfo="0"></amp-youtube>',
1361-
AMP_Theme_Support::conditionally_output_header( $mock_image )
1361+
AMP_Theme_Support::amend_header_image_with_video_header( $mock_image )
13621362
);
13631363
}
13641364
}

0 commit comments

Comments
 (0)