| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Required for class.media-extractor.php to match expected function naming convention. |
| 5 |
* |
| 6 |
* @param $url Can be just the $url or the whole $atts array |
| 7 |
* @return bool|mixed The Youtube video ID via jetpack_get_youtube_id |
| 8 |
*/ |
| 9 |
|
| 10 |
function jetpack_shortcode_get_youtube_id( $url ) { |
| 11 |
return jetpack_get_youtube_id( $url ); |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* @param $url Can be just the $url or the whole $atts array |
| 16 |
* @return bool|mixed The Youtube video ID |
| 17 |
*/ |
| 18 |
function jetpack_get_youtube_id( $url ) { |
| 19 |
// Do we have an $atts array? Get first att |
| 20 |
if ( is_array( $url ) ) |
| 21 |
$url = $url[0]; |
| 22 |
|
| 23 |
$url = youtube_sanitize_url( $url ); |
| 24 |
$url = parse_url( $url ); |
| 25 |
$id = false; |
| 26 |
|
| 27 |
if ( ! isset( $url['query'] ) ) |
| 28 |
return false; |
| 29 |
|
| 30 |
parse_str( $url['query'], $qargs ); |
| 31 |
|
| 32 |
if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) |
| 33 |
return false; |
| 34 |
|
| 35 |
if ( isset( $qargs['list'] ) ) |
| 36 |
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] ); |
| 37 |
|
| 38 |
if ( empty( $id ) ) |
| 39 |
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] ); |
| 40 |
|
| 41 |
return $id; |
| 42 |
} |
| 43 |
|
| 44 |
if ( !function_exists( 'youtube_sanitize_url' ) ) : |
| 45 |
/** |
| 46 |
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands. |
| 47 |
* |
| 48 |
* @param string $url |
| 49 |
* @return string The normalized URL |
| 50 |
*/ |
| 51 |
function youtube_sanitize_url( $url ) { |
| 52 |
$url = trim( $url, ' "' ); |
| 53 |
$url = trim( $url ); |
| 54 |
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); |
| 55 |
|
| 56 |
// Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in. |
| 57 |
$query_string_start = strpos( $url, "?" ); |
| 58 |
|
| 59 |
if ( false !== $query_string_start ) { |
| 60 |
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); |
| 61 |
} |
| 62 |
|
| 63 |
return $url; |
| 64 |
} |
| 65 |
endif; |
| 66 |
|