/modules/shortcodes/gist.php

  1. <?php 
  2.  
  3. /** 
  4. * GitHub's Gist site supports oEmbed but their oembed provider only 
  5. * returns raw HTML (no styling) and the first little bit of the code. 
  6. * 
  7. * Their Javascript-based embed method is a lot better, so that's what we're using. 
  8. */ 
  9. wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([a-zA-Z0-9]+)#', 'github_gist_embed_handler' ); 
  10. add_shortcode( 'gist', 'github_gist_shortcode' ); 
  11.  
  12. function github_gist_embed_handler( $matches, $attr, $url, $rawattr ) { 
  13. // Let the shortcode callback do all the work 
  14. return github_gist_shortcode( $attr, $url ); 
  15.  
  16. function github_gist_shortcode( $atts, $content = '' ) { 
  17.  
  18. if ( empty( $atts[0] ) && empty( $content ) ) 
  19. return '<!-- Missing Gist ID -->'; 
  20.  
  21. $id = ( ! empty( $content ) ) ? $content : $atts[0]; 
  22.  
  23. // Parse a URL 
  24. if ( ! is_numeric( $id ) ) 
  25. $id = preg_replace( '#https?://gist.github.com/([a-zA-Z0-9]+)#', '$1', $id ); 
  26.  
  27. if ( ! $id ) 
  28. return '<!-- Invalid Gist ID -->'; 
  29.  
  30. $embed_url = "https://gist.github.com/{$id}.js"; 
  31.  
  32. if ( ! empty( $atts['file'] ) ) 
  33. $embed_url = add_query_arg( 'file', urlencode( $atts['file'] ), $embed_url ); 
  34. // inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables 
  35. return '<style>.gist table { margin-bottom: 0; }</style>' . '<script src="' . esc_url( $embed_url ) . '"></script>'; 
.