| @@ -1,5 +1,5 @@ | ||
| 1 | -<?php | |
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase | |
| 2 | 2 | /** |
| 3 | 3 | * Compatibility functions for YouTube URLs and WP.com helper functions. |
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Required for class.media-extractor.php to match expected function naming convention. |
| 12 | 12 | * |
| 13 | - * @param $url Can be just the $url or the whole $atts array. | |
| 13 | + * @param string|array $url Can be just the $url or the whole $atts array. | |
| 14 | 14 | * @return bool|mixed The Youtube video ID via jetpack_get_youtube_id |
| 15 | 15 | */ |
| 16 | 16 | function jetpack_shortcode_get_youtube_id( $url ) { |
| 17 | 17 | return jetpack_get_youtube_id( $url ); |
| @@ -19,9 +19,9 @@ | ||
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Extract video ID from a YouTube url. |
| 22 | 22 | * |
| 23 | - * @param string $url YouTube URL. | |
| 23 | + * @param string|array $url YouTube URL. | |
| 24 | 24 | * @return bool|mixed The Youtube video ID |
| 25 | 25 | */ |
| 26 | 26 | function jetpack_get_youtube_id( $url ) { |
| 27 | 27 | // Do we have an $atts array? Get first att |
| @@ -28,9 +28,9 @@ | ||
| 28 | 28 | if ( is_array( $url ) ) { |
| 29 | 29 | $url = reset( $url ); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - $url = youtube_sanitize_url( $url ); | |
| 32 | + $url = jetpack_youtube_sanitize_url( $url ); | |
| 33 | 33 | $url = wp_parse_url( $url ); |
| 34 | 34 | $id = false; |
| 35 | 35 | |
| 36 | 36 | if ( ! isset( $url['query'] ) ) { |
| @@ -53,21 +53,28 @@ | ||
| 53 | 53 | |
| 54 | 54 | return $id; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | -if ( ! function_exists( 'youtube_sanitize_url' ) ) : | |
| 57 | +if ( ! function_exists( 'jetpack_youtube_sanitize_url' ) ) : | |
| 58 | 58 | /** |
| 59 | 59 | * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands. |
| 60 | 60 | * |
| 61 | - * @param string $url YouTube URL. | |
| 62 | - * @return string The normalized URL | |
| 61 | + * @param string|array $url YouTube URL. | |
| 62 | + * @return string|false The normalized URL or false if input is invalid. | |
| 63 | 63 | */ |
| 64 | - function youtube_sanitize_url( $url ) { | |
| 64 | + function jetpack_youtube_sanitize_url( $url ) { | |
| 65 | + if ( is_array( $url ) && isset( $url['url'] ) ) { | |
| 66 | + $url = $url['url']; | |
| 67 | + } | |
| 68 | + if ( ! is_string( $url ) ) { | |
| 69 | + return false; | |
| 70 | + } | |
| 71 | + | |
| 65 | 72 | $url = trim( $url, ' "' ); |
| 66 | 73 | $url = trim( $url ); |
| 67 | - $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); | |
| 74 | + $url = str_replace( array( 'youtu.be/', '/v/', '/shorts/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '/watch?v=', '?v=', '&', '&', 'videoseries' ), $url ); | |
| 68 | 75 | |
| 69 | - // Replace any extra question marks with ampersands - the result of a URL like "https://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in. | |
| 76 | + // Replace any extra question marks with ampersands - the result of a URL like "https://www.youtube.com/v/dQw4w9WgXcQ?fs=1&hl=en_US" being passed in. | |
| 70 | 77 | $query_string_start = strpos( $url, '?' ); |
| 71 | 78 | |
| 72 | 79 | if ( false !== $query_string_start ) { |
| 73 | 80 | $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) ); |
| @@ -97,12 +104,9 @@ | ||
| 97 | 104 | |
| 98 | 105 | $haystack = (string) $haystack; |
| 99 | 106 | $needle = (string) $needle; |
| 100 | 107 | |
| 101 | - if ( function_exists( 'str_starts_with' ) ) { // remove when PHP 8.0 is the minimum supported. | |
| 102 | - return str_starts_with( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions | |
| 103 | - } | |
| 104 | - return 0 === strpos( $haystack, $needle ); | |
| 108 | + return str_starts_with( $haystack, $needle ); | |
| 105 | 109 | } |
| 106 | 110 | endif; |
| 107 | 111 | |
| 108 | 112 | if ( ! function_exists( 'wp_endswith' ) ) : |
| @@ -120,13 +124,9 @@ | ||
| 120 | 124 | |
| 121 | 125 | $haystack = (string) $haystack; |
| 122 | 126 | $needle = (string) $needle; |
| 123 | 127 | |
| 124 | - if ( function_exists( 'str_ends_with' ) ) { // remove when PHP 8.0 is the minimum supported. | |
| 125 | - return str_ends_with( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions | |
| 126 | - | |
| 127 | - } | |
| 128 | - return $needle === substr( $haystack, -strlen( $needle ) ); | |
| 128 | + return str_ends_with( $haystack, $needle ); | |
| 129 | 129 | } |
| 130 | 130 | endif; |
| 131 | 131 | |
| 132 | 132 | if ( ! function_exists( 'wp_in' ) ) : |
| @@ -144,13 +144,9 @@ | ||
| 144 | 144 | |
| 145 | 145 | $haystack = (string) $haystack; |
| 146 | 146 | $needle = (string) $needle; |
| 147 | 147 | |
| 148 | - if ( function_exists( 'str_contains' ) ) { // remove when PHP 8.0 is the minimum supported. | |
| 149 | - return str_contains( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions | |
| 150 | - } | |
| 151 | - | |
| 152 | - return false !== strpos( $haystack, $needle ); | |
| 148 | + return str_contains( $haystack, $needle ); | |
| 153 | 149 | } |
| 154 | 150 | endif; |
| 155 | 151 | |
| 156 | 152 | /** |