Completed
Push — fix/tiled-gallery-amp-mosaic-c... ( b708e7...398dc2 )
by Yaroslav
08:06
created

mixcloud.php ➔ mixcloud_shortcode()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 13
nop 2
dl 0
loc 77
rs 6.0751
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
 * 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
 * @package Jetpack
15
 */
16
17
/*
18
 * Register oEmbed provider
19
 * Example URL: http://www.mixcloud.com/oembed/?url=http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/
20
 */
21
wp_oembed_add_provider( '#https?://(?:www\.)?mixcloud\.com/\S*#i', 'https://www.mixcloud.com/oembed', true );
22
23
/**
24
 * Register mixcloud shortcode.
25
 *
26
 * @param array  $atts    Shortcode atttributes.
27
 * @param string $content Post content.
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
function mixcloud_shortcode( $atts, $content = null ) {
30
31
	if ( empty( $atts[0] ) && empty( $content ) ) {
32
		return '<!-- mixcloud error: invalid mixcloud resource -->';
33
	}
34
35
	$regular_expression = '/((?<=mixcloud\.com\/)[\w\-\/]+$)|(^[\w\-\/]+$)/i';
36
	preg_match( $regular_expression, $content, $match );
37
	if ( ! empty( $match ) ) {
38
		$resource_id = trim( $match[0] );
39
	} else {
40
		preg_match( $regular_expression, $atts[0], $match );
41
		if ( ! empty( $match ) ) {
42
			$resource_id = trim( $match[0] );
43
		}
44
	}
45
46
	if ( empty( $resource_id ) ) {
47
		return '<!-- mixcloud error: invalid mixcloud resource -->';
48
	}
49
50
	$mixcloud_url = 'https://mixcloud.com/' . $resource_id;
51
52
	$atts = shortcode_atts(
53
		array(
54
			'width'          => false,
55
			'height'         => false,
56
			'color'          => false,
57
			'light'          => false,
58
			'dark'           => false,
59
			'hide_tracklist' => false,
60
			'hide_cover'     => false,
61
			'mini'           => false,
62
			'hide_followers' => false,
63
			'hide_artwork'   => false,
64
		),
65
		$atts
66
	);
67
68
	// remove falsey values.
69
	$atts = array_filter( $atts );
70
71
	$query_args = array( 'url' => $mixcloud_url );
72
	$query_args = array_merge( $query_args, $atts );
73
74
	$url               = add_query_arg( urlencode_deep( $query_args ), 'https://www.mixcloud.com/oembed/' );
75
	$mixcloud_response = wp_remote_get( $url, array( 'redirection' => 0 ) );
76
	if ( is_wp_error( $mixcloud_response ) || 200 !== $mixcloud_response['response']['code'] || empty( $mixcloud_response['body'] ) ) {
77
		return '<!-- mixcloud error: invalid mixcloud resource -->';
78
	}
79
80
	$response_body = json_decode( $mixcloud_response['body'] );
81
82
	$html = $response_body->html;
83
84
	preg_match( '/sandbox="([^"]*)"/', $html, $matches );
85
86
	if ( empty( $matches ) ) { // MixCloud doesn't use sandbox attribute.
87
		$html = preg_replace( '/>/', ' sandbox="allow-popups allow-scripts allow-same-origin allow-presentation">', $html, 1 );
88
	} else { // MixCloud uses sandbox attribute.
89
90
		$allowed_values = array();
91
		// Here we make sure that these string are not repeated in the sandbox attribute.
92
		$attrs = array( 'allow-popups', 'allow-scripts', 'allow-same-origin', 'allow-presentation' );
93
		foreach ( $attrs as $attr ) {
94
			if ( false === strpos( $matches[1], $attr ) ) {
95
				$allowed_values[] = $attr;
96
			}
97
		}
98
99
		$sandbox_value = $matches[1] . ' ' . implode( ' ', $allowed_values );
100
101
		$html = preg_replace( '/sandbox="([^"]*)"/', "sandbox=\"$sandbox_value\"", $html );
102
	}
103
104
	return $html;
105
}
106
add_shortcode( 'mixcloud', 'mixcloud_shortcode' );
107