PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.7.6
Jetpack – WP Security, Backup, Speed, & Growth v7.7.6
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.7.6, at modules/shortcodes/mixcloud.php

85 lines 2.8 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 * @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.
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 return $response_body->html;
83 }
84 add_shortcode( 'mixcloud', 'mixcloud_shortcode' );
85