gist.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * GitHub's Gist site supports oEmbed but their oembed provider only
  4. * returns raw HTML (no styling) and the first little bit of the code.
  5. *
  6. * Their JavaScript-based embed method is a lot better, so that's what we're using.
  7. */
  8. wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([a-zA-Z0-9/]+)(\#file\-[a-zA-Z0-9\_\-]+)?#', 'github_gist_embed_handler' );
  9. add_shortcode( 'gist', 'github_gist_shortcode' );
  10. /**
  11. * Handle gist embeds.
  12. *
  13. * @since 2.8.0
  14. *
  15. * @global WP_Embed $wp_embed
  16. *
  17. * @param array $matches Results after parsing the URL using the regex in wp_embed_register_handler().
  18. * @param array $attr Embed attributes.
  19. * @param string $url The original URL that was matched by the regex.
  20. * @param array $rawattr The original unmodified attributes.
  21. * @return string The embed HTML.
  22. */
  23. function github_gist_embed_handler( $matches, $attr, $url, $rawattr ) {
  24. // Let the shortcode callback do all the work
  25. return github_gist_shortcode( $matches, $url );
  26. }
  27. /**
  28. * Callback for gist shortcode.
  29. *
  30. * @since 2.8.0
  31. *
  32. * @param array $atts Attributes found in the shortcode.
  33. * @param string $content Content enclosed by the shortcode.
  34. *
  35. * @return string The gist HTML.
  36. */
  37. function github_gist_shortcode( $atts, $content = '' ) {
  38. if ( empty( $atts[0] ) && empty( $content ) ) {
  39. return '<!-- Missing Gist ID -->';
  40. }
  41. $id = ( ! empty( $content ) ) ? $content : $atts[0];
  42. // Parse a URL
  43. if ( ! is_numeric( $id ) ) {
  44. $id = preg_replace( '#https?://gist.github.com/([a-zA-Z0-9]+)#', '$1', $id );
  45. }
  46. if ( ! $id ) {
  47. return '<!-- Invalid Gist ID -->';
  48. }
  49. wp_enqueue_script(
  50. 'jetpack-gist-embed',
  51. Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
  52. array( 'jquery' ),
  53. false,
  54. true
  55. );
  56. if ( false !== strpos( $id, '#file-' ) ) {
  57. // URL points to a specific file in the gist
  58. $id = str_replace( '#file-', '.json?file=', $id );
  59. $id = preg_replace( '/\-(?!.*\-)/', '.', $id );
  60. } else {
  61. $file = ( ! empty( $atts['file'] ) ) ? '?file=' . urlencode( $atts['file'] ) : '';
  62. // URL points to the entire gist
  63. $id .= ".json$file";
  64. }
  65. // inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables
  66. $return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>';
  67. if ( isset( $_POST[ 'type' ] ) && 'embed' === $_POST[ 'type' ] &&
  68. isset( $_POST[ 'action' ] ) && 'parse-embed' === $_POST['action'] ) {
  69. return github_gist_simple_embed( $id );
  70. }
  71. return $return;
  72. }
  73. /**
  74. * Use script tag to load shortcode in editor.
  75. *
  76. * @since 3.9.0
  77. *
  78. * @param string $id The ID of the gist.
  79. *
  80. * @return string
  81. */
  82. function github_gist_simple_embed( $id ) {
  83. $id = str_replace( 'json', 'js', $id );
  84. return '<script type="text/javascript" src="https://gist.github.com/' . $id . '"></script>';
  85. }