spotify.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Spotify shortcode.
  4. *
  5. * Usage:
  6. * [spotify id="spotify:track:4bz7uB4edifWKJXSDxwHcs" width="400" height="100"]
  7. */
  8. if ( ! shortcode_exists( 'spotify' ) ) {
  9. add_shortcode( 'spotify', 'jetpack_spotify_shortcode' );
  10. if ( get_option( 'embed_autourls' ) ) {
  11. // If user enabled autourls, also convert syntax like spotify:track:4bz7uB4edifWKJXSDxwHcs
  12. add_filter( 'the_content', 'jetpack_spotify_embed_ids', 7 );
  13. }
  14. }
  15. /**
  16. * Parse shortcode arguments and render its output.
  17. *
  18. * @since 4.5.0
  19. *
  20. * @param array $atts
  21. * @param string $content
  22. *
  23. * @return string
  24. */
  25. function jetpack_spotify_shortcode( $atts = array(), $content = '' ) {
  26. if ( ! empty( $content ) ) {
  27. $id = $content;
  28. } elseif ( ! empty( $atts['id'] ) ) {
  29. $id = $atts['id'];
  30. } elseif ( ! empty( $atts[0] ) ) {
  31. $id = $atts[0];
  32. } else {
  33. return '<!-- Missing Spotify ID -->';
  34. }
  35. if ( empty( $atts['width'] ) ) {
  36. $atts['width'] = 300;
  37. }
  38. if ( empty( $atts['height'] ) ) {
  39. $atts['height'] = 380;
  40. }
  41. $atts['width'] = (int) $atts['width'];
  42. $atts['height'] = (int) $atts['height'];
  43. // Spotify accepts both URLs and their Spotify ID format, so let them sort it out and validate
  44. $embed_url = add_query_arg( 'uri', urlencode( $id ), 'https://embed.spotify.com/' );
  45. return '<iframe src="' . esc_url( $embed_url ) . '" style="display:block; margin:0 auto; width:' . esc_attr( $atts['width'] ) . 'px; height:' . esc_attr( $atts['height'] ) . 'px;" frameborder="0" allowtransparency="true"></iframe>';
  46. }
  47. /**
  48. * Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs
  49. * The core WordPress embed functionality only works with URLs
  50. * Modified version of WP_Embed::autoembed()
  51. *
  52. * @since 4.5.0
  53. *
  54. * @param $content
  55. *
  56. * @return string
  57. */
  58. function jetpack_spotify_embed_ids( $content ) {
  59. $textarr = wp_html_split( $content );
  60. foreach ( $textarr as &$element ) {
  61. if ( '' == $element || '<' === $element[0] ) {
  62. continue;
  63. }
  64. if ( substr( ltrim( $element ), 0, 8 ) !== 'spotify:' ) {
  65. continue;
  66. }
  67. $element = preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'jetpack_spotify_embed_ids_callback', $element );
  68. }
  69. return implode( '', $textarr );
  70. }
  71. /**
  72. * Call shortcode with ID provided by matching pattern.
  73. *
  74. * @since 4.5.0
  75. *
  76. * @param array $matches
  77. *
  78. * @return string
  79. */
  80. function jetpack_spotify_embed_ids_callback( $matches ) {
  81. return "\n" . jetpack_spotify_shortcode( array(), $matches[1] ) . "\n";
  82. }