PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1
Jetpack – WP Security, Backup, Speed, & Growth v7.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / gist.php

gist.php in Jetpack – WP Security, Backup, Speed, & Growth 7.1, at modules/shortcodes/gist.php

142 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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