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