暫無描述

mixcloud.php 3.6KB

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