| 1 |
<?php |
| 2 |
|
| 3 |
use \Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 4 |
|
| 5 |
/** |
| 6 |
* Required for class.media-extractor.php to match expected function naming convention. |
| 7 |
* |
| 8 |
* @param $url Can be just the $url or the whole $atts array |
| 9 |
* @return bool|mixed The Youtube video ID via jetpack_get_youtube_id |
| 10 |
*/ |
| 11 |
|
| 12 |
function jetpack_shortcode_get_youtube_id( $url ) { |
| 13 |
return jetpack_get_youtube_id( $url ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* @param $url Can be just the $url or the whole $atts array |
| 18 |
* @return bool|mixed The Youtube video ID |
| 19 |
*/ |
| 20 |
function jetpack_get_youtube_id( $url ) { |
| 21 |
// Do we have an $atts array? Get first att |
| 22 |
if ( is_array( $url ) ) { |
| 23 |
$url = reset( $url ); |
| 24 |
} |
| 25 |
|
| 26 |
$url = youtube_sanitize_url( $url ); |
| 27 |
$url = parse_url( $url ); |
| 28 |
$id = false; |
| 29 |
|
| 30 |
if ( ! isset( $url['query'] ) ) |
| 31 |
return false; |
| 32 |
|
| 33 |
parse_str( $url['query'], $qargs ); |
| 34 |
|
| 35 |
if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) ) |
| 36 |
return false; |
| 37 |
|
| 38 |
if ( isset( $qargs['list'] ) ) |
| 39 |
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] ); |
| 40 |
|
| 41 |
if ( empty( $id ) ) |
| 42 |
$id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] ); |
| 43 |
|
| 44 |
return $id; |
| 45 |
} |
| 46 |
|
| 47 |
if ( !function_exists( 'youtube_sanitize_url' ) ) : |
| 48 |
/** |
| 49 |
* Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands. |
| 50 |
* |
| 51 |
* @param string $url |
| 52 |
* @return string The normalized URL |
| 53 |
*/ |
| 54 |
function youtube_sanitize_url( $url ) { |
| 55 |
$url = trim( $url, ' "' ); |
| 56 |
$url = trim( $url ); |
| 57 |
$url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); |
| 58 |
|
| 59 |
// 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. |
| 60 |
$query_string_start = strpos( $url, "?" ); |
| 61 |
|
| 62 |
if ( false !== $query_string_start ) { |
| 63 |
$url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); |
| 64 |
} |
| 65 |
|
| 66 |
return $url; |
| 67 |
} |
| 68 |
endif; |
| 69 |
|
| 70 |
/** |
| 71 |
* Merge in three string helper functions from WPCOM. |
| 72 |
* |
| 73 |
* @see WPCOM/wp-content/mu-plugins/string-helpers.php |
| 74 |
*/ |
| 75 |
if ( ! function_exists( 'wp_startswith' ) ) : |
| 76 |
function wp_startswith( $haystack, $needle ) { |
| 77 |
return 0 === strpos( $haystack, $needle ); |
| 78 |
} |
| 79 |
endif; |
| 80 |
|
| 81 |
|
| 82 |
if ( ! function_exists( 'wp_endswith' ) ) : |
| 83 |
function wp_endswith( $haystack, $needle ) { |
| 84 |
return $needle === substr( $haystack, -strlen( $needle )); |
| 85 |
} |
| 86 |
endif; |
| 87 |
|
| 88 |
if ( ! function_exists( 'wp_in' ) ) : |
| 89 |
function wp_in( $needle, $haystack ) { |
| 90 |
return false !== strpos( $haystack, $needle ); |
| 91 |
} |
| 92 |
endif; |
| 93 |
|
| 94 |
/** |
| 95 |
* @deprecated 7.5 Use Connection_Manager instead. |
| 96 |
*/ |
| 97 |
function jetpack_sha1_base64( $text ) { |
| 98 |
$connection = new Connection_Manager(); |
| 99 |
return $connection->sha1_base64( $text ); |
| 100 |
} |
| 101 |
|