Aucune description

functions.compat.php 2.6KB

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