PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.3
Jetpack – WP Security, Backup, Speed, & Growth v7.2.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 / mixcloud.php

mixcloud.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.3, at modules/shortcodes/mixcloud.php

76 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ),
57 $atts
58 );
59
60 // remove falsey values
61 $atts = array_filter( $atts );
62
63 $query_args = array( 'url' => $mixcloud_url );
64 $query_args = array_merge( $query_args, $atts );
65
66 $url = add_query_arg( urlencode_deep( $query_args ), 'https://www.mixcloud.com/oembed/' );
67 $mixcloud_response = wp_remote_get( $url, array( 'redirection' => 0 ) );
68 if ( is_wp_error( $mixcloud_response ) || 200 !== $mixcloud_response['response']['code'] || empty( $mixcloud_response['body'] ) ) {
69 return '<!-- mixcloud error: invalid mixcloud resource -->';
70 }
71
72 $response_body = json_decode( $mixcloud_response['body'] );
73
74 return $response_body->html;
75 }
76