|
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 = reset( $url ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$url = youtube_sanitize_url( $url ); |
|
25
|
|
|
$url = parse_url( $url ); |
|
26
|
|
|
$id = false; |
|
27
|
|
|
|
|
28
|
|
|
if ( ! isset( $url['query'] ) ) |
|
29
|
|
|
return false; |
|
30
|
|
|
|
|
31
|
|
|
parse_str( $url['query'], $qargs ); |
|
32
|
|
|
|
|
33
|
|
|
if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) |
|
34
|
|
|
return false; |
|
35
|
|
|
|
|
36
|
|
|
if ( isset( $qargs['list'] ) ) |
|
37
|
|
|
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] ); |
|
38
|
|
|
|
|
39
|
|
|
if ( empty( $id ) ) |
|
40
|
|
|
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] ); |
|
41
|
|
|
|
|
42
|
|
|
return $id; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
if ( !function_exists( 'youtube_sanitize_url' ) ) : |
|
46
|
|
|
/** |
|
47
|
|
|
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $url |
|
50
|
|
|
* @return string The normalized URL |
|
51
|
|
|
*/ |
|
52
|
|
|
function youtube_sanitize_url( $url ) { |
|
53
|
|
|
$url = trim( $url, ' "' ); |
|
54
|
|
|
$url = trim( $url ); |
|
55
|
|
|
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); |
|
56
|
|
|
|
|
57
|
|
|
// 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. |
|
58
|
|
|
$query_string_start = strpos( $url, "?" ); |
|
59
|
|
|
|
|
60
|
|
|
if ( false !== $query_string_start ) { |
|
61
|
|
|
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $url; |
|
65
|
|
|
} |
|
66
|
|
|
endif; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Merge in three string helper functions from WPCOM. |
|
70
|
|
|
* |
|
71
|
|
|
* @see WPCOM/wp-content/mu-plugins/string-helpers.php |
|
72
|
|
|
*/ |
|
73
|
|
|
if ( ! function_exists( 'wp_startswith' ) ) : |
|
74
|
|
|
function wp_startswith( $haystack, $needle ) { |
|
75
|
|
|
return 0 === strpos( $haystack, $needle ); |
|
76
|
|
|
} |
|
77
|
|
|
endif; |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if ( ! function_exists( 'wp_endswith' ) ) : |
|
81
|
|
|
function wp_endswith( $haystack, $needle ) { |
|
82
|
|
|
return $needle === substr( $haystack, -strlen( $needle )); |
|
83
|
|
|
} |
|
84
|
|
|
endif; |
|
85
|
|
|
|
|
86
|
|
|
if ( ! function_exists( 'wp_in' ) ) : |
|
87
|
|
|
function wp_in( $needle, $haystack ) { |
|
88
|
|
|
return false !== strpos( $haystack, $needle ); |
|
89
|
|
|
} |
|
90
|
|
|
endif; |
|
91
|
|
|
|
|
92
|
|
|
if ( ! function_exists( 'hash_equals' ) ) : |
|
93
|
|
|
function hash_equals( $str1, $str2 ) { |
|
94
|
|
|
if ( strlen( $str1 ) !== strlen( $str2 ) ) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} else { |
|
97
|
|
|
$res = $str1 ^ $str2; |
|
98
|
|
|
$ret = 0; |
|
99
|
|
|
for ( $i = strlen( $res ) - 1; $i >= 0; $i-- ) { |
|
100
|
|
|
$ret |= ord( $res[ $i ] ); |
|
101
|
|
|
} |
|
102
|
|
|
return ! $ret; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
endif; |
|
106
|
|
|
|