Completed
Push — update/soundcloud-phpcs ( f52eea )
by Jeremy
07:30
created

soundcloud.php ➔ soundcloud_shortcode()   F

Complexity

Conditions 19
Paths 242

Size

Total Lines 109

Duplication

Lines 7
Ratio 6.42 %

Importance

Changes 0
Metric Value
cc 19
nc 242
nop 2
dl 7
loc 109
rs 2.5266
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * SoundCloud Shortcode
4
 * Based on this plugin: https://wordpress.org/plugins/soundcloud-shortcode/
5
 *
6
 * Credits:
7
 * Original version: Johannes Wagener <[email protected]>
8
 * Options support: Tiffany Conroy <[email protected]>
9
 * HTML5 & oEmbed support: Tim Bormans <[email protected]>
10
 *
11
 * Examples:
12
 * [soundcloud]http://soundcloud.com/forss/flickermood[/soundcloud]
13
 * [soundcloud url="https://api.soundcloud.com/tracks/156661852" params="auto_play=false&amp;hide_related=false&amp;visual=false" width="100%" height="450" iframe="true" /]
14
 * [soundcloud url="https://api.soundcloud.com/tracks/156661852" params="auto_play=false&amp;hide_related=false&amp;visual=true" width="100%" height="450" iframe="true" /]
15
 * [soundcloud url="https://soundcloud.com/closetorgan/paul-is-dead" width=400 height=400]
16
 * [soundcloud url="https://soundcloud.com/closetorgan/sets/smells-like-lynx-africa-private"]
17
 * <iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/150745932&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
18
 *
19
 * @package Jetpack
20
 */
21
22
/**
23
 * SoundCloud shortcode handler
24
 *
25
 * @param  string|array $atts        The attributes passed to the shortcode like [soundcloud attr1="value" /].
26
 *                                   Is an empty string when no arguments are given.
27
 * @param  string       $content     The content between non-self closing [soundcloud]...[/soundcloud] tags.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $content not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
28
 *
29
 * @return string                  Widget embed code HTML
30
 */
31
function soundcloud_shortcode( $atts, $content = null ) {
32
33
	// Custom shortcode options.
34
	$shortcode_options = array_merge(
35
		array( 'url' => trim( $content ) ),
36
		is_array( $atts ) ? $atts : array()
37
	);
38
39
	// The "url" option is required.
40 View Code Duplication
	if ( empty( $shortcode_options['url'] ) ) {
41
		if ( current_user_can( 'edit_posts' ) ) {
42
			return esc_html__( 'Please specify a Soundcloud URL.', 'jetpack' );
43
		} else {
44
			return '<!-- Missing Soundcloud URL -->';
45
		}
46
	}
47
48
	// Turn shortcode option "param" (param=value&param2=value) into array of params.
49
	$shortcode_params = array();
50
	if ( isset( $shortcode_options['params'] ) ) {
51
		parse_str( html_entity_decode( $shortcode_options['params'] ), $shortcode_params );
52
		$shortcode_options = array_merge(
53
			$shortcode_options,
54
			$shortcode_params
55
		);
56
		unset( $shortcode_options['params'] );
57
	}
58
59
	$options = shortcode_atts(
60
		// This list used to include an 'iframe' option. We don't include it anymore as we don't support the Flash player anymore.
61
		array(
62
			'url'           => '',
63
			'width'         => soundcloud_get_option( 'player_width' ),
64
			'height'        => soundcloud_url_has_tracklist( $shortcode_options['url'] ) ? soundcloud_get_option( 'player_height_multi' ) : soundcloud_get_option( 'player_height' ),
65
			'auto_play'     => soundcloud_get_option( 'auto_play' ),
66
			'hide_related'  => false,
67
			'visual'        => false,
68
			'show_comments' => soundcloud_get_option( 'show_comments' ),
69
			'color'         => soundcloud_get_option( 'color' ),
70
			'show_user'     => false,
71
			'show_reposts'  => false,
72
		),
73
		$shortcode_options,
74
		'soundcloud'
75
	);
76
77
	// "width" needs to be an integer.
78
	if ( ! empty( $options['width'] ) && ! preg_match( '/^\d+$/', $options['width'] ) ) {
79
		// set to 0 so oEmbed will use the default 100% and WordPress themes will leave it alone.
80
		$options['width'] = 0;
81
	}
82
	// Set default width if not defined.
83
	$width = ! empty( $options['width'] ) ? absint( $options['width'] ) : '100%';
84
85
	// Set default height if not defined.
86
	if (
87
		empty( $options['height'] )
88
		|| (
89
			// "height" needs to be an integer.
90
			! empty( $options['height'] )
91
			&& ! preg_match( '/^\d+$/', $options['height'] )
92
		)
93
	) {
94
		if (
95
			soundcloud_url_has_tracklist( $options['url'] )
96
			|| 'true' === $options['visual']
97
		) {
98
			$height = 450;
99
		} else {
100
			$height = 166;
101
		}
102
	} else {
103
		$height = absint( $options['height'] );
104
	}
105
106
	// Set visual to false when displaying the smallest player.
107
	if ( '20' === $options['height'] ) {
108
		$options['visual'] = false;
109
	}
110
111
	// Build our list of Souncloud parameters.
112
	$query_args = array(
113
		'url' => rawurlencode( $options['url'] ),
114
	);
115
116
	// Add our options, if they are set to true or false.
117
	foreach ( $options as $name => $value ) {
118
		if ( 'true' === $value ) {
119
			$query_args[ $name ] = 'true';
120
		}
121
122
		if ( 'false' === $value || false === $value ) {
123
			$query_args[ $name ] = 'false';
124
		}
125
	}
126
127
	// Build final embed URL.
128
	$url = add_query_arg(
129
		$query_args,
130
		'https://w.soundcloud.com/player/'
131
	);
132
133
	return sprintf(
134
		'<iframe width="%1$s" height="%2$d" scrolling="no" frameborder="no" src="%3$s"></iframe>',
135
		esc_attr( $width ),
136
		esc_attr( $height ),
137
		$url
138
	);
139
}
140
add_shortcode( 'soundcloud', 'soundcloud_shortcode' );
141
142
/**
143
 * Plugin options getter
144
 *
145
 * @param  string|array $option  Option name.
146
 * @param  mixed        $default Default value.
147
 *
148
 * @return mixed                   Option value
149
 */
150
function soundcloud_get_option( $option, $default = false ) {
151
	$value = get_option( 'soundcloud_' . $option );
152
153
	return '' === $value ? $default : $value;
154
}
155
156
/**
157
 * Decide if a url has a tracklist
158
 *
159
 * @param string $url Soundcloud URL.
160
 *
161
 * @return boolean
162
 */
163
function soundcloud_url_has_tracklist( $url ) {
164
	return preg_match( '/^(.+?)\/(sets|groups|playlists)\/(.+?)$/', $url );
165
}
166
167
/**
168
 * SoundCloud Embed Reversal
169
 *
170
 * Converts a generic HTML embed code from SoundClound into a
171
 * WordPress.com-compatibly shortcode.
172
 *
173
 * @param string $content HTML content.
174
 *
175
 * @return string Parsed content.
176
 */
177
function jetpack_soundcloud_embed_reversal( $content ) {
178
	if ( ! is_string( $content ) || false === stripos( $content, 'w.soundcloud.com/player' ) ) {
179
		return $content;
180
	}
181
182
	$regexes = array();
183
184
	$regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i';
185
	$regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+&gt;\s*?&lt;/iframe&gt;#i';
186
187
	foreach ( $regexes as $regex ) {
188
		if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
189
			continue;
190
		}
191
192
		foreach ( $matches as $match ) {
193
194
			// if pasted from the visual editor - prevent double encoding.
195
			$match[1] = str_replace( '&amp;amp;', '&amp;', $match[1] );
196
197
			$args = wp_parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY );
198
			$args = wp_parse_args( $args );
199
200
			if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) ) {
201
				continue;
202
			}
203
204
			if ( ! preg_match( '#height="(\d+)"#i', $match[0], $hmatch ) ) {
205
				$height = '';
206
			} else {
207
				$height = ' height="' . intval( $hmatch[1] ) . '"';
208
			}
209
210
			unset( $args['url'] );
211
			$params = 'params="';
212
			if ( count( $args ) > 0 ) {
213
				foreach ( $args as $key => $value ) {
214
					$params .= esc_html( $key ) . '=' . esc_html( $value ) . '&amp;';
215
				}
216
				$params = substr( $params, 0, -5 );
217
			}
218
			$params .= '"';
219
220
			$shortcode = '[soundcloud url="' . esc_url( $url_matches[0] ) . '" ' . $params . ' width="100%"' . $height . ' iframe="true" /]';
221
222
			$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
223
			$content       = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
224
225
			/** This action is documented in modules/shortcodes/youtube.php */
226
			do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] );
227
		}
228
	}
229
230
	return $content;
231
}
232
add_filter( 'pre_kses', 'jetpack_soundcloud_embed_reversal' );
233