@@ -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}
0 commit comments