Nenhuma Descrição

spotify.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Spotify shortcode.
  4. *
  5. * Usage:
  6. * [spotify id="spotify:track:4bz7uB4edifWKJXSDxwHcs" width="400" height="100"]
  7. *
  8. * @package automattic/jetpack
  9. */
  10. if ( ! shortcode_exists( 'spotify' ) ) {
  11. add_shortcode( 'spotify', 'jetpack_spotify_shortcode' );
  12. }
  13. /**
  14. * Parse shortcode arguments and render its output.
  15. *
  16. * @since 4.5.0
  17. *
  18. * @param array $atts Shortcode attributes.
  19. * @param string $content Post Content.
  20. *
  21. * @return string
  22. */
  23. function jetpack_spotify_shortcode( $atts = array(), $content = '' ) {
  24. if ( ! empty( $content ) ) {
  25. $id = $content;
  26. } elseif ( ! empty( $atts['id'] ) ) {
  27. $id = $atts['id'];
  28. } elseif ( ! empty( $atts[0] ) ) {
  29. $id = $atts[0];
  30. } else {
  31. return '<!-- Missing Spotify ID -->';
  32. }
  33. if ( empty( $atts['width'] ) ) {
  34. $atts['width'] = 300;
  35. }
  36. if ( empty( $atts['height'] ) ) {
  37. $atts['height'] = 380;
  38. }
  39. $atts['width'] = (int) $atts['width'];
  40. $atts['height'] = (int) $atts['height'];
  41. // Spotify accepts both URLs and their Spotify ID format, so let them sort it out and validate.
  42. $embed_url = add_query_arg( 'uri', rawurlencode( $id ), 'https://embed.spotify.com/' );
  43. // If the shortcode is displayed in a WPCOM notification, display a simple link only.
  44. // When the shortcode is displayed in the WPCOM Reader, use iframe instead.
  45. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  46. require_once WP_CONTENT_DIR . '/lib/display-context.php';
  47. $context = A8C\Display_Context\get_current_context();
  48. if ( A8C\Display_Context\NOTIFICATIONS === $context ) {
  49. return sprintf(
  50. '<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>',
  51. esc_url( $id )
  52. );
  53. } elseif ( A8C\Display_Context\READER === $context ) {
  54. return sprintf(
  55. '<iframe src="%1$s" height="%2$s" width="%3$s"></iframe>',
  56. esc_url( $embed_url ),
  57. esc_attr( $atts['height'] ),
  58. esc_attr( $atts['width'] )
  59. );
  60. }
  61. }
  62. 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" loading="lazy"></iframe>';
  63. }
  64. /**
  65. * Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs
  66. * The core WordPress embed functionality only works with URLs
  67. * Modified version of WP_Embed::autoembed()
  68. *
  69. * @since 4.5.0
  70. *
  71. * @param string $content Post content.
  72. *
  73. * @return string
  74. */
  75. function jetpack_spotify_embed_ids( $content ) {
  76. $textarr = wp_html_split( $content );
  77. foreach ( $textarr as &$element ) {
  78. if ( '' === $element || '<' === $element[0] ) {
  79. continue;
  80. }
  81. // If this element does not contain a Spotify embed, continue.
  82. if ( false === strpos( $element, 'spotify:' ) ) {
  83. continue;
  84. }
  85. $element = preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'jetpack_spotify_embed_ids_callback', $element );
  86. }
  87. return implode( '', $textarr );
  88. }
  89. add_filter( 'the_content', 'jetpack_spotify_embed_ids', 7 );
  90. /**
  91. * Call shortcode with ID provided by matching pattern.
  92. *
  93. * @since 4.5.0
  94. *
  95. * @param array $matches Array of matches for Spofify links.
  96. *
  97. * @return string
  98. */
  99. function jetpack_spotify_embed_ids_callback( $matches ) {
  100. return "\n" . jetpack_spotify_shortcode( array(), $matches[1] ) . "\n";
  101. }