PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.3.4
Jetpack – WP Security, Backup, Speed, & Growth v7.3.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / spotify.php
spotify.php
100 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Spotify shortcode.
4 *
5 * Usage:
6 * [spotify id="spotify:track:4bz7uB4edifWKJXSDxwHcs" width="400" height="100"]
7 *
8 * @package Jetpack
9 */
10
11 if ( ! shortcode_exists( 'spotify' ) ) {
12 add_shortcode( 'spotify', 'jetpack_spotify_shortcode' );
13
14 if ( get_option( 'embed_autourls' ) ) {
15 // If user enabled autourls, also convert syntax like spotify:track:4bz7uB4edifWKJXSDxwHcs.
16 add_filter( 'the_content', 'jetpack_spotify_embed_ids', 7 );
17 }
18 }
19
20 /**
21 * Parse shortcode arguments and render its output.
22 *
23 * @since 4.5.0
24 *
25 * @param array $atts Shortcode attributes.
26 * @param string $content Post Content.
27 *
28 * @return string
29 */
30 function jetpack_spotify_shortcode( $atts = array(), $content = '' ) {
31
32 if ( ! empty( $content ) ) {
33 $id = $content;
34 } elseif ( ! empty( $atts['id'] ) ) {
35 $id = $atts['id'];
36 } elseif ( ! empty( $atts[0] ) ) {
37 $id = $atts[0];
38 } else {
39 return '<!-- Missing Spotify ID -->';
40 }
41
42 if ( empty( $atts['width'] ) ) {
43 $atts['width'] = 300;
44 }
45
46 if ( empty( $atts['height'] ) ) {
47 $atts['height'] = 380;
48 }
49
50 $atts['width'] = (int) $atts['width'];
51 $atts['height'] = (int) $atts['height'];
52
53 // Spotify accepts both URLs and their Spotify ID format, so let them sort it out and validate.
54 $embed_url = add_query_arg( 'uri', rawurlencode( $id ), 'https://embed.spotify.com/' );
55
56 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>';
57 }
58
59 /**
60 * Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs
61 * The core WordPress embed functionality only works with URLs
62 * Modified version of WP_Embed::autoembed()
63 *
64 * @since 4.5.0
65 *
66 * @param string $content Post content.
67 *
68 * @return string
69 */
70 function jetpack_spotify_embed_ids( $content ) {
71 $textarr = wp_html_split( $content );
72
73 foreach ( $textarr as &$element ) {
74 if ( '' === $element || '<' === $element[0] ) {
75 continue;
76 }
77
78 if ( substr( ltrim( $element ), 0, 8 ) !== 'spotify:' ) {
79 continue;
80 }
81
82 $element = preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'jetpack_spotify_embed_ids_callback', $element );
83 }
84
85 return implode( '', $textarr );
86 }
87
88 /**
89 * Call shortcode with ID provided by matching pattern.
90 *
91 * @since 4.5.0
92 *
93 * @param array $matches Array of matches for Spofify links.
94 *
95 * @return string
96 */
97 function jetpack_spotify_embed_ids_callback( $matches ) {
98 return "\n" . jetpack_spotify_shortcode( array(), $matches[1] ) . "\n";
99 }
100