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