Completed
Push — master-stable ( 4a8b26...01e321 )
by
unknown
09:43
created

modules/shortcodes/gist.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ) {
0 ignored issues
show
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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( 'jetpack-gist-embed', plugins_url( 'js/gist.js', __FILE__ ), array( 'jquery' ), false, true );
58
59
	if ( false !== strpos( $id, '#file-' ) ) {
60
		// URL points to a specific file in the gist
61
		$id = str_replace( '#file-', '.json?file=', $id );
62
		$id = preg_replace( '/\-(?!.*\-)/', '.', $id );
63
	} else {
64
		$file = ( ! empty( $atts['file'] ) ) ? '?file=' . urlencode( $atts['file'] ) : '';
65
		// URL points to the entire gist
66
		$id .= ".json$file";
67
	}
68
69
	// inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables
70
	$return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>';
71
72
	if ( isset( $_POST[ 'type' ] ) && 'embed' === $_POST[ 'type' ] &&
73
		 isset( $_POST[ 'action' ] ) && 'parse-embed' === $_POST['action'] ) {
74
		return github_gist_simple_embed( $id );
75
	}
76
77
	return $return;
78
}
79
80
/**
81
 * Use script tag to load shortcode in editor.
82
 *
83
 * @since 3.9.0
84
 *
85
 * @param string $id The ID of the gist.
86
 *
87
 * @return string
88
 */
89
function github_gist_simple_embed( $id ) {
90
	$id = str_replace( 'json', 'js', $id );
91
	return '<script type="text/javascript" src="https://gist.github.com/' . $id . '"></script>';
92
}