css
1 year ago
images
2 years ago
img
2 years ago
js
1 year ago
archiveorg-book.php
5 years ago
archiveorg.php
5 years ago
archives.php
5 years ago
bandcamp.php
3 years ago
brightcove.php
5 years ago
cartodb.php
5 years ago
class.filter-embedded-html-objects.php
2 years ago
codepen.php
5 years ago
crowdsignal.php
1 year ago
dailymotion.php
3 years ago
descript.php
4 years ago
facebook.php
1 year ago
flatio.php
5 years ago
flickr.php
1 year ago
getty.php
2 years ago
gist.php
1 year ago
googleapps.php
2 years ago
googlemaps.php
2 years ago
googleplus.php
5 years ago
gravatar.php
2 years ago
houzz.php
5 years ago
inline-pdfs.php
4 years ago
instagram.php
1 year ago
kickstarter.php
3 years ago
mailchimp.php
3 years ago
medium.php
3 years ago
mixcloud.php
2 years ago
others.php
1 year ago
pinterest.php
2 years ago
presentations.php
2 years ago
quiz.php
4 years ago
recipe.php
1 year ago
scribd.php
5 years ago
sitemap.php
5 years ago
slideshare.php
5 years ago
slideshow.php
1 year ago
smartframe.php
3 years ago
soundcloud.php
3 years ago
spotify.php
2 years ago
ted.php
5 years ago
tweet.php
2 years ago
twitchtv.php
5 years ago
twitter-timeline.php
5 years ago
twitter.php
1 year ago
unavailable.php
3 years ago
untappd-menu.php
3 years ago
upcoming-events.php
1 year ago
ustream.php
5 years ago
videopress.php
4 years ago
vimeo.php
2 years ago
vine.php
5 years ago
vr.php
2 years ago
wufoo.php
3 years ago
youtube.php
1 year ago
spotify.php
120 lines
| 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 | |
| 11 | if ( ! shortcode_exists( 'spotify' ) ) { |
| 12 | add_shortcode( 'spotify', 'jetpack_spotify_shortcode' ); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Parse shortcode arguments and render its output. |
| 17 | * |
| 18 | * @since 4.5.0 |
| 19 | * |
| 20 | * @param array $atts Shortcode attributes. |
| 21 | * @param string $content Post Content. |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | function jetpack_spotify_shortcode( $atts = array(), $content = '' ) { |
| 26 | if ( ! is_array( $atts ) ) { |
| 27 | $atts = array(); |
| 28 | } |
| 29 | |
| 30 | if ( ! empty( $content ) ) { |
| 31 | $id = $content; |
| 32 | } elseif ( ! empty( $atts['id'] ) ) { |
| 33 | $id = $atts['id']; |
| 34 | } elseif ( ! empty( $atts[0] ) ) { |
| 35 | $id = $atts[0]; |
| 36 | } else { |
| 37 | return '<!-- Missing Spotify ID -->'; |
| 38 | } |
| 39 | |
| 40 | if ( empty( $atts['width'] ) ) { |
| 41 | $atts['width'] = 300; |
| 42 | } |
| 43 | |
| 44 | if ( empty( $atts['height'] ) ) { |
| 45 | $atts['height'] = 380; |
| 46 | } |
| 47 | |
| 48 | $atts['width'] = (int) $atts['width']; |
| 49 | $atts['height'] = (int) $atts['height']; |
| 50 | |
| 51 | // Spotify accepts both URLs and their Spotify ID format, so let them sort it out and validate. |
| 52 | $embed_url = add_query_arg( 'uri', rawurlencode( $id ), 'https://embed.spotify.com/' ); |
| 53 | |
| 54 | // If the shortcode is displayed in a WPCOM notification, display a simple link only. |
| 55 | // When the shortcode is displayed in the WPCOM Reader, use iframe instead. |
| 56 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 57 | require_once WP_CONTENT_DIR . '/lib/display-context.php'; |
| 58 | $context = A8C\Display_Context\get_current_context(); |
| 59 | if ( A8C\Display_Context\NOTIFICATIONS === $context ) { |
| 60 | return sprintf( |
| 61 | '<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>', |
| 62 | esc_url( $id ) |
| 63 | ); |
| 64 | } elseif ( A8C\Display_Context\READER === $context ) { |
| 65 | return sprintf( |
| 66 | '<iframe src="%1$s" height="%2$s" width="%3$s"></iframe>', |
| 67 | esc_url( $embed_url ), |
| 68 | esc_attr( $atts['height'] ), |
| 69 | esc_attr( $atts['width'] ) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | 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>'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs |
| 79 | * The core WordPress embed functionality only works with URLs |
| 80 | * Modified version of WP_Embed::autoembed() |
| 81 | * |
| 82 | * @since 4.5.0 |
| 83 | * |
| 84 | * @param string $content Post content. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | function jetpack_spotify_embed_ids( $content ) { |
| 89 | $textarr = wp_html_split( $content ); |
| 90 | |
| 91 | foreach ( $textarr as &$element ) { |
| 92 | if ( '' === $element || '<' === $element[0] ) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | // If this element does not contain a Spotify embed, continue. |
| 97 | if ( ! str_contains( $element, 'spotify:' ) ) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | $element = preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'jetpack_spotify_embed_ids_callback', $element ); |
| 102 | } |
| 103 | |
| 104 | return implode( '', $textarr ); |
| 105 | } |
| 106 | add_filter( 'the_content', 'jetpack_spotify_embed_ids', 7 ); |
| 107 | |
| 108 | /** |
| 109 | * Call shortcode with ID provided by matching pattern. |
| 110 | * |
| 111 | * @since 4.5.0 |
| 112 | * |
| 113 | * @param array $matches Array of matches for Spofify links. |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | function jetpack_spotify_embed_ids_callback( $matches ) { |
| 118 | return "\n" . jetpack_spotify_shortcode( array(), $matches[1] ) . "\n"; |
| 119 | } |
| 120 |