-
Notifications
You must be signed in to change notification settings - Fork 382
Fix header image filtering and YouTube header video detection #1208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,7 +369,10 @@ public static function add_hooks() { | |
| add_action( 'comment_form', array( __CLASS__, 'amend_comment_form' ), 100 ); | ||
| remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ); | ||
| add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'whitelist_layout_in_wp_kses_allowed_html' ), 10 ); | ||
| add_filter( 'get_header_image_tag', array( __CLASS__, 'conditionally_output_header' ), 10, 3 ); | ||
| add_filter( 'get_header_image_tag', array( __CLASS__, 'amend_header_image_with_video_header' ), PHP_INT_MAX ); | ||
| add_action( 'wp_print_footer_scripts', function() { | ||
| wp_dequeue_script( 'wp-custom-header' ); | ||
| }, 0 ); | ||
| add_action( 'wp_enqueue_scripts', function() { | ||
| wp_dequeue_script( 'comment-reply' ); // Handled largely by AMP_Comments_Sanitizer and *reply* methods in this class. | ||
| } ); | ||
|
|
@@ -1356,46 +1359,25 @@ public static function enqueue_assets() { | |
| * Conditionally replace the header image markup with a header video or image. | ||
| * | ||
| * This is JS-driven in Core themes like Twenty Sixteen and Twenty Seventeen. | ||
| * So in order for the header video to display, | ||
| * this replaces the markup of the header image. | ||
| * | ||
| * @since 1.0 | ||
| * @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L54 | ||
| * @param string $html The image markup to filter. | ||
| * @param array $header The header config array. | ||
| * @param array $atts The image markup attributes. | ||
| * @return string $html Filtered markup. | ||
| */ | ||
| public static function conditionally_output_header( $html, $header, $atts ) { | ||
| unset( $header ); | ||
| if ( ! is_header_video_active() ) { | ||
| return $html; | ||
| }; | ||
|
|
||
| if ( ! has_header_video() ) { | ||
| return AMP_HTML_Utils::build_tag( 'amp-img', $atts ); | ||
| } | ||
|
|
||
| return self::output_header_video( $atts ); | ||
| } | ||
|
|
||
| /** | ||
| * Replace the header image markup with a header video. | ||
| * So in order for the header video to display, this replaces the markup of the header image. | ||
| * | ||
| * @since 1.0 | ||
| * @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L54 | ||
| * @link https://github.com/WordPress/wordpress-develop/blob/d002fde80e5e3a083e5f950313163f566561517f/src/wp-includes/js/wp-custom-header.js#L78 | ||
| * | ||
| * @param array $atts The header tag attributes array. | ||
| * @param string $image_markup The image markup to filter. | ||
| * @return string $html Filtered markup. | ||
| */ | ||
| public static function output_header_video( $atts ) { | ||
| // Remove the script for video. | ||
| wp_deregister_script( 'wp-custom-header' ); | ||
| $video_settings = get_header_video_settings(); | ||
| public static function amend_header_image_with_video_header( $image_markup ) { | ||
|
|
||
| // If there is no video, just pass the image through. | ||
| if ( ! has_header_video() || ! is_header_video_active() ) { | ||
| return $image_markup; | ||
| }; | ||
|
|
||
| $video_settings = get_header_video_settings(); | ||
| $parsed_url = wp_parse_url( $video_settings['videoUrl'] ); | ||
| $query = isset( $parsed_url['query'] ) ? wp_parse_args( $parsed_url['query'] ) : null; | ||
| $query = isset( $parsed_url['query'] ) ? wp_parse_args( $parsed_url['query'] ) : array(); | ||
| $video_attributes = array( | ||
| 'media' => '(min-width: ' . $video_settings['minWidth'] . 'px)', | ||
| 'width' => $video_settings['width'], | ||
|
|
@@ -1405,25 +1387,31 @@ public static function output_header_video( $atts ) { | |
| 'id' => 'wp-custom-header-video', | ||
| ); | ||
|
|
||
| // Create image banner to stay behind the video. | ||
| $image_header = AMP_HTML_Utils::build_tag( 'amp-img', $atts ); | ||
| $youtube_id = null; | ||
| if ( isset( $parsed_url['host'] ) && preg_match( '/(^|\.)(youtube\.com|youtu\.be)$/', $parsed_url['host'] ) ) { | ||
| if ( 'youtu.be' === $parsed_url['host'] && ! empty( $parsed_url['path'] ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's nice how this accounts for URLs like https://youtu.be/v5zg_Yv048s |
||
| $youtube_id = trim( $parsed_url['path'], '/' ); | ||
| } elseif ( isset( $query['v'] ) ) { | ||
| $youtube_id = $query['v']; | ||
| } | ||
| } | ||
|
|
||
| // If the video URL is for YouTube, return an <amp-youtube> element. | ||
| if ( isset( $parsed_url['host'], $query['v'] ) && ( false !== strpos( $parsed_url['host'], 'youtube' ) ) ) { | ||
| $video_header = AMP_HTML_Utils::build_tag( | ||
| if ( ! empty( $youtube_id ) ) { | ||
| $video_markup = AMP_HTML_Utils::build_tag( | ||
| 'amp-youtube', | ||
| array_merge( | ||
| $video_attributes, | ||
| array( | ||
| 'data-videoid' => $query['v'], | ||
| 'data-videoid' => $youtube_id, | ||
| 'data-param-rel' => '0', // Don't show related videos. | ||
| 'data-param-showinfo' => '0', // Don't show video title at the top. | ||
| 'data-param-controls' => '0', // Don't show video controls. | ||
| ) | ||
| ) | ||
| ); | ||
| } else { | ||
| $video_header = AMP_HTML_Utils::build_tag( | ||
| $video_markup = AMP_HTML_Utils::build_tag( | ||
| 'amp-video', | ||
| array_merge( | ||
| $video_attributes, | ||
|
|
@@ -1434,6 +1422,6 @@ public static function output_header_video( $atts ) { | |
| ); | ||
| } | ||
|
|
||
| return $image_header . $video_header; | ||
| return $image_markup . $video_markup; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nice how this allows all other filters to run first.