mixcloud.php
| 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 | */ |
| 13 | |
| 14 | // Register oEmbed provider |
| 15 | // Example URL: http://www.mixcloud.com/oembed/?url=http://www.mixcloud.com/MalibuRum/play-6-kissy-sellouts-winter-sun-house-party-mix/ |
| 16 | wp_oembed_add_provider('#https?://(?:www\.)?mixcloud\.com/\S*#i', 'http://www.mixcloud.com/oembed', true); |
| 17 | |
| 18 | // Register mixcloud shortcode |
| 19 | add_shortcode( 'mixcloud', 'mixcloud_shortcode' ); |
| 20 | function mixcloud_shortcode( $atts, $content = null ) { |
| 21 | |
| 22 | if ( empty( $atts[0] ) && empty( $content ) ) |
| 23 | return "<!-- mixcloud error: invalid mixcloud resource -->"; |
| 24 | |
| 25 | $regular_expression = '#((?<=mixcloud.com/)([A-Za-z0-9%-]+/[A-Za-z0-9%-]+))|^([A-Za-z0-9%-]+/[A-Za-z0-9%-]+)#i'; |
| 26 | preg_match( $regular_expression, $content, $match ); |
| 27 | if ( ! empty( $match ) ) { |
| 28 | $resource_id = trim( $match[0] ); |
| 29 | } else { |
| 30 | preg_match( $regular_expression, $atts[0], $match ); |
| 31 | if ( ! empty( $match ) ) |
| 32 | $resource_id = trim( $match[0] ); |
| 33 | } |
| 34 | |
| 35 | if ( empty( $resource_id ) ) |
| 36 | return "<!-- mixcloud error: invalid mixcloud resource -->"; |
| 37 | |
| 38 | $atts = shortcode_atts( array( |
| 39 | 'width' => 300, |
| 40 | 'height' => 300, |
| 41 | ), $atts, 'mixcloud' ); |
| 42 | |
| 43 | |
| 44 | // Build URL |
| 45 | $url = add_query_arg( $atts, "http://api.mixcloud.com/$resource_id/embed-html/" ); |
| 46 | $head = wp_remote_head( $url ); |
| 47 | if ( is_wp_error( $head ) || 200 != $head['response']['code'] ) |
| 48 | return "<!-- mixcloud error: invalid mixcloud resource -->"; |
| 49 | |
| 50 | return sprintf( '<iframe width="%d" height="%d" scrolling="no" frameborder="no" src="%s"></iframe>', $atts['width'], $atts['height'], esc_url( $url ) ); |
| 51 | |
| 52 | } |
| 53 |