PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.3
Jetpack – WP Security, Backup, Speed, & Growth v12.3
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 in Jetpack – WP Security, Backup, Speed, & Growth 12.3, at modules/shortcodes/spotify.php

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