functions.compat.php 2.5 KB

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