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