Completed
Push — add/connection_plugins_active_... ( 2ac671...32c5d9 )
by
unknown
104:49 queued 97:47
created

spotify.php ➔ jetpack_spotify_embed_ids()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 18
loc 18
rs 9.3554
c 0
b 0
f 0
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
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 View Code Duplication
	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
	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>';
52
}
53
54
/**
55
 * Turn text like this on it's own line into an embed: spotify:track:4bz7uB4edifWKJXSDxwHcs
56
 * The core WordPress embed functionality only works with URLs
57
 * Modified version of WP_Embed::autoembed()
58
 *
59
 * @since 4.5.0
60
 *
61
 * @param string $content Post content.
62
 *
63
 * @return string
64
 */
65 View Code Duplication
function jetpack_spotify_embed_ids( $content ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
	$textarr = wp_html_split( $content );
67
68
	foreach ( $textarr as &$element ) {
69
		if ( '' === $element || '<' === $element[0] ) {
70
			continue;
71
		}
72
73
		// If this element does not contain a Spotify embed, continue.
74
		if ( false === strpos( $element, 'spotify:' ) ) {
75
			continue;
76
		}
77
78
		$element = preg_replace_callback( '|^\s*(spotify:[^\s"]+:[^\s"]+)\s*$|im', 'jetpack_spotify_embed_ids_callback', $element );
79
	}
80
81
	return implode( '', $textarr );
82
}
83
add_filter( 'the_content', 'jetpack_spotify_embed_ids', 7 );
84
85
/**
86
 * Call shortcode with ID provided by matching pattern.
87
 *
88
 * @since 4.5.0
89
 *
90
 * @param array $matches Array of matches for Spofify links.
91
 *
92
 * @return string
93
 */
94
function jetpack_spotify_embed_ids_callback( $matches ) {
95
	return "\n" . jetpack_spotify_shortcode( array(), $matches[1] ) . "\n";
96
}
97