jetpack_soundcloud_embed_reversal

SoundCloud Embed Reversal.

Description

jetpack_soundcloud_embed_reversal( $content ); 

Converts a generic HTML embed code from SoundClound into a WordPress.com-compatibly shortcode.

Parameters (1)

0. $content
The content.

Usage

  1. if ( !function_exists( 'jetpack_soundcloud_embed_reversal' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/shortcodes/soundcloud.php'; 
  3.  
  4. // The content. 
  5. $content = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = jetpack_soundcloud_embed_reversal($content); 
  9.  

Defined (1)

The function is defined in the following location(s).

/modules/shortcodes/soundcloud.php  
  1. function jetpack_soundcloud_embed_reversal( $content ) { 
  2. if ( false === stripos( $content, 'w.soundcloud.com/player' ) ) 
  3. return $content; 
  4.  
  5. /** Sample embed code: 
  6.   
  7. <iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/150745932&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&visual=true"></iframe> 
  8. */ 
  9.  
  10. $regexes = array(); 
  11.  
  12. $regexes[] = '#<iframe[^>]+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"[^>]*+>\s*?</iframe>#i'; 
  13. $regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src="((?:https?:)?//w\.soundcloud\.com/player/[^"\']++)"(?:[^&]|&(?!gt;))*+>\s*?</iframe>#i'; 
  14.  
  15. foreach ( $regexes as $regex ) { 
  16. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) 
  17. continue; 
  18.  
  19. foreach ( $matches as $match ) { 
  20.  
  21. // if pasted from the visual editor - prevent double encoding 
  22. $match[1] = str_replace( '&amp;', '&', $match[1] ); 
  23.  
  24. $args = parse_url( html_entity_decode( $match[1] ), PHP_URL_QUERY ); 
  25. $args = wp_parse_args( $args ); 
  26.  
  27. if ( ! preg_match( '#^(?:https?:)?//api\.soundcloud\.com/.+$#i', $args['url'], $url_matches ) ) 
  28. continue; 
  29.  
  30. if ( ! preg_match( '#height="(\d+)"#i', $match[0], $hmatch ) ) { 
  31. $height = ''; 
  32. } else { 
  33. $height = " height='" . intval( $hmatch[1] ) . "'"; 
  34.  
  35. unset( $args['url'] ); 
  36. $params = 'params="'; 
  37. if ( count( $args ) > 0 ) { 
  38. foreach ( $args as $key => $value ) { 
  39. $params .= esc_html( $key ) . '=' . esc_html( $value ) . '&'; 
  40. $params = substr( $params, 0, -5 ); 
  41. $params .= '"'; 
  42.  
  43. $shortcode = '[soundcloud url="' . esc_url( $url_matches[0] ) . '" ' . $params . ' width="100%"' . $height . ' iframe="true" /]'; 
  44.  
  45. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); 
  46. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content ); 
  47. /** This action is documented in modules/shortcodes/youtube.php */ 
  48. do_action( 'jetpack_embed_to_shortcode', 'soundcloud', $url_matches[0] ); 
  49.  
  50. return $content;