Completed
Push — add/improve-histogram ( bde1c8...c8533f )
by
unknown
43:24 queued 36:14
created

gist.php ➔ github_gist_shortcode()   D

Complexity

Conditions 19
Paths 77

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 77
nop 2
dl 0
loc 88
rs 4.5166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	if ( Jetpack_AMP_Support::is_amp_request() ) {
58
		/*
59
		 * According to <https://www.ampproject.org/docs/reference/components/amp-gist#height-(required)>:
60
		 *
61
		 * > Note: You must find the height of the gist by inspecting it with your browser (e.g., Chrome Developer Tools).
62
		 *
63
		 * However, this does not seem to be the case any longer. The actual height of the content does get set in the
64
		 * page after loading. So this is just the initial height.
65
		 * See <https://github.com/ampproject/amphtml/pull/17738>.
66
		 */
67
		$height = 240;
68
69
		$parsed_url = wp_parse_url( $id );
70
71
		// Strip Github user name.
72
		$id = preg_replace( '#^.*/(?=[a-z0-9]+)#', '', $parsed_url['path'] );
73
74
		$file = null;
75
		if ( ! empty( $parsed_url['query'] ) ) {
76
			$query_args = wp_parse_args( $parsed_url['query'] );
77
			if ( isset( $query_args['file'] ) ) {
78
				$file = $query_args['file'];
79
			}
80
		}
81
		if ( ! $file && ! empty( $parsed_url['fragment'] ) && preg_match( '#^file-(.+)#', $parsed_url['fragment'], $matches ) ) {
82
			$file = $matches[1];
83
84
			// Make best guess of file for fragment that was slugified.
85
			$file = preg_replace( '/-(\w+)/', '.$1', $file );
86
		}
87
88
		$amp_tag = sprintf(
89
			'<amp-gist layout="fixed-height" data-gistid="%s" height="%s"',
90
			esc_attr( basename( $id, '.json' ) ),
91
			esc_attr( $height )
92
		);
93
		if ( ! empty( $file ) ) {
94
			$amp_tag .= sprintf( ' data-file="%s"', esc_attr( $file ) );
95
		}
96
		$amp_tag .= '></amp-gist>';
97
		return $amp_tag;
98
	}
99
100
	wp_enqueue_script(
101
		'jetpack-gist-embed',
102
		Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
103
		array( 'jquery' ),
104
		false,
105
		true
106
	);
107
108
	if ( false !== strpos( $id, '#file-' ) ) {
109
		// URL points to a specific file in the gist
110
		$id = str_replace( '#file-', '.json?file=', $id );
111
		$id = preg_replace( '/\-(?!.*\-)/', '.', $id );
112
	} else {
113
		$file = ( ! empty( $atts['file'] ) ) ? '?file=' . urlencode( $atts['file'] ) : '';
114
		// URL points to the entire gist
115
		$id .= ".json$file";
116
	}
117
118
	// inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables
119
	$return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>';
120
121
	if ( isset( $_POST['type'] ) && 'embed' === $_POST['type'] &&
122
		 isset( $_POST['action'] ) && 'parse-embed' === $_POST['action'] ) {
123
		return github_gist_simple_embed( $id );
124
	}
125
126
	return $return;
127
}
128
129
/**
130
 * Use script tag to load shortcode in editor.
131
 *
132
 * @since 3.9.0
133
 *
134
 * @param string $id The ID of the gist.
135
 *
136
 * @return string
137
 */
138
function github_gist_simple_embed( $id ) {
139
	$id = str_replace( 'json', 'js', $id );
140
	return '<script type="text/javascript" src="https://gist.github.com/' . $id . '"></script>';
141
}
142