Completed
Push — add/gdpr-ads-compliance ( c52a1e...cda5c5 )
by
unknown
26:32 queued 14:46
created

mixcloud.php ➔ mixcloud_shortcode()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 36
nc 10
nop 2
dl 0
loc 54
rs 7.255
c 0
b 0
f 0

How to fix   Long Method   

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
 * Mixcloud embeds
4
 *
5
 * examples:
6
 * [mixcloud MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/ /]
7
 * [mixcloud MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/ width=640 height=480 /]
8
 * [mixcloud http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/ /]
9
 * [mixcloud http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/ width=640 height=480 /]
10
 * [mixcloud]http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/[/mixcloud]
11
 * [mixcloud]MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/[/mixcloud]
12
 * [mixcloud http://www.mixcloud.com/mat/playlists/classics/ width=660 height=208 hide_cover=1 hide_tracklist=1]
13
*/
14
15
// Register oEmbed provider
16
// Example URL: http://www.mixcloud.com/oembed/?url=http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/
17
wp_oembed_add_provider( '#https?://(?:www\.)?mixcloud\.com/\S*#i', 'https://www.mixcloud.com/oembed', true );
18
19
// Register mixcloud shortcode
20
add_shortcode( 'mixcloud', 'mixcloud_shortcode' );
21
function mixcloud_shortcode( $atts, $content = null ) {
22
23
	if ( empty( $atts[0] ) && empty( $content ) ) {
24
		return '<!-- mixcloud error: invalid mixcloud resource -->';
25
	}
26
27
	$regular_expression = '/((?<=mixcloud\\.com\\/)[\\w-\\/]+$)|(^[\\w-\\/]+$)/i';
28
	preg_match( $regular_expression, $content, $match );
29
	if ( ! empty( $match ) ) {
30
		$resource_id = trim( $match[0] );
31
	} else {
32
		preg_match( $regular_expression, $atts[0], $match );
33
		if ( ! empty( $match ) ) {
34
			$resource_id = trim( $match[0] );
35
		}
36
	}
37
38
	if ( empty( $resource_id ) ) {
39
		return '<!-- mixcloud error: invalid mixcloud resource -->';
40
	}
41
42
	$mixcloud_url = 'https://mixcloud.com/' . $resource_id;
43
44
	$atts = shortcode_atts(
45
		array(
46
			'width'          => false,
47
			'height'         => false,
48
			'color'          => false,
49
			'light'          => false,
50
			'dark'           => false,
51
			'hide_tracklist' => false,
52
			'hide_cover'     => false,
53
			'mini'           => false,
54
			'hide_followers' => false,
55
			'hide_artwork'   => false,
56
		), $atts
57
	);
58
59
	// remove falsey values
60
	$atts = array_filter( $atts );
61
62
	$query_args = array( 'url' => $mixcloud_url );
63
	$query_args = array_merge( $query_args, $atts );
64
65
	$url               = add_query_arg( urlencode_deep( $query_args ), 'https://www.mixcloud.com/oembed/' );
66
	$mixcloud_response = wp_remote_get( $url, array( 'redirection' => 0 ) );
67
	if ( is_wp_error( $mixcloud_response ) || 200 !== $mixcloud_response['response']['code'] || empty( $mixcloud_response['body'] ) ) {
68
		return '<!-- mixcloud error: invalid mixcloud resource -->';
69
	}
70
71
	$response_body = json_decode( $mixcloud_response['body'] );
72
73
	return $response_body->html;
74
}
75