PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.8.3
Jetpack – WP Security, Backup, Speed, & Growth v7.8.3
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.8.3, at modules/shortcodes/gist.php

228 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Automattic\Jetpack\Assets;
4
5 /**
6 * GitHub's Gist site supports oEmbed but their oembed provider only
7 * returns raw HTML (no styling) and the first little bit of the code.
8 *
9 * Their JavaScript-based embed method is a lot better, so that's what we're using.
10 *
11 * Supported formats:
12 * Full URL: https://gist.github.com/57cc50246aab776e110060926a2face2
13 * Full URL with username: https://gist.github.com/jeherve/57cc50246aab776e110060926a2face2
14 * Full URL linking to specific file: https://gist.github.com/jeherve/57cc50246aab776e110060926a2face2#file-wp-config-php
15 * Full URL, no username, linking to specific file: https://gist.github.com/57cc50246aab776e110060926a2face2#file-wp-config-php
16 * Gist ID: [gist]57cc50246aab776e110060926a2face2[/gist]
17 * Gist ID within tag: [gist 57cc50246aab776e110060926a2face2]
18 * Gist ID with username: [gist jeherve/57cc50246aab776e110060926a2face2]
19 * Gist private ID with username: [gist xknown/fc5891af153e2cf365c9]
20 *
21 * @package Jetpack
22 */
23
24 wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([a-zA-Z0-9/]+)(\#file\-[a-zA-Z0-9\_\-]+)?#', 'github_gist_embed_handler' );
25 add_shortcode( 'gist', 'github_gist_shortcode' );
26
27 /**
28 * Handle gist embeds.
29 *
30 * @since 2.8.0
31 *
32 * @global WP_Embed $wp_embed
33 *
34 * @param array $matches Results after parsing the URL using the regex in wp_embed_register_handler().
35 * @param array $attr Embed attributes.
36 * @param string $url The original URL that was matched by the regex.
37 * @param array $rawattr The original unmodified attributes.
38 * @return string The embed HTML.
39 */
40 function github_gist_embed_handler( $matches, $attr, $url, $rawattr ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
41 // Let the shortcode callback do all the work.
42 return github_gist_shortcode( $matches, $url );
43 }
44
45 /**
46 * Extract an ID from a Gist shortcode or a full Gist URL.
47 *
48 * @since 7.3.0
49 *
50 * @param string $gist Gist shortcode or full Gist URL.
51 *
52 * @return array $gist_info {
53 * Array of information about our gist.
54 * @type string $id Unique identifier for the gist.
55 * @type string $file File name if the gist links to a specific file.
56 * }
57 */
58 function jetpack_gist_get_shortcode_id( $gist = '' ) {
59 $gist_info = array(
60 'id' => '',
61 'file' => '',
62 );
63 // Simple shortcode, with just an ID.
64 if ( ctype_alnum( $gist ) ) {
65 $gist_info['id'] = $gist;
66 }
67
68 // Full URL? Only keep the relevant parts.
69 $parsed_url = wp_parse_url( $gist );
70 if (
71 ! empty( $parsed_url )
72 && is_array( $parsed_url )
73 && isset( $parsed_url['scheme'], $parsed_url['host'], $parsed_url['path'] )
74 ) {
75 // Not a Gist URL? Bail.
76 if ( 'gist.github.com' !== $parsed_url['host'] ) {
77 return array(
78 'id' => '',
79 'file' => '',
80 );
81 }
82
83 // Keep the file name if there was one.
84 if ( ! empty( $parsed_url['fragment'] ) ) {
85 $gist_info['file'] = preg_replace( '/(?:file-)(.+)/', '$1', $parsed_url['fragment'] );
86 }
87
88 // Keep the unique identifier without any leading or trailing slashes.
89 if ( ! empty( $parsed_url['path'] ) ) {
90 $gist_info['id'] = preg_replace( '/^\/([^\.]+)\./', '$1', $parsed_url['path'] );
91 // Overwrite $gist with our identifier to clean it up below.
92 $gist = $gist_info['id'];
93 }
94 }
95
96 // Not a URL nor an ID? Look for "username/id", "/username/id", or "id", and only keep the ID.
97 if ( preg_match( '#^/?(([a-z0-9_-]+/)?([a-z0-9]+))$#i', $gist, $matches ) ) {
98 $gist_info['id'] = $matches[3];
99 }
100
101 return $gist_info;
102 }
103
104 /**
105 * Callback for gist shortcode.
106 *
107 * @since 2.8.0
108 *
109 * @param array $atts Attributes found in the shortcode.
110 * @param string $content Content enclosed by the shortcode.
111 *
112 * @return string The gist HTML.
113 */
114 function github_gist_shortcode( $atts, $content = '' ) {
115
116 if ( empty( $atts[0] ) && empty( $content ) ) {
117 if ( current_user_can( 'edit_posts' ) ) {
118 return esc_html__( 'Please specify a Gist URL or ID.', 'jetpack' );
119 } else {
120 return '<!-- Missing Gist ID -->';
121 }
122 }
123
124 $id = ( ! empty( $content ) ) ? $content : $atts[0];
125
126 // Parse a URL to get an ID we can use.
127 $gist_info = jetpack_gist_get_shortcode_id( $id );
128 if ( empty( $gist_info['id'] ) ) {
129 if ( current_user_can( 'edit_posts' ) ) {
130 return esc_html__( 'The Gist ID you provided is not valid. Please try a different one.', 'jetpack' );
131 } else {
132 return '<!-- Invalid Gist ID -->';
133 }
134 } else {
135 // Add trailing .json to all unique gist identifiers.
136 $id = $gist_info['id'] . '.json';
137 }
138
139 // The file name can come from the URL passed, or from a shortcode attribute.
140 if ( ! empty( $gist_info['file'] ) ) {
141 $file = $gist_info['file'];
142 } elseif ( ! empty( $atts['file'] ) ) {
143 $file = $atts['file'];
144 } else {
145 $file = '';
146 }
147
148 // Replace - by . to get a real file name from slug.
149 if ( ! empty( $file ) ) {
150 // Find the last -.
151 $dash_position = strrpos( $file, '-' );
152 if ( false !== $dash_position ) {
153 // Replace the - by a period.
154 $file = substr_replace( $file, '.', $dash_position, 1 );
155 }
156
157 $file = rawurlencode( $file );
158 }
159
160 if (
161 class_exists( 'Jetpack_AMP_Support' )
162 && Jetpack_AMP_Support::is_amp_request()
163 ) {
164 /*
165 * According to <https://www.ampproject.org/docs/reference/components/amp-gist#height-(required)>:
166 *
167 * > Note: You must find the height of the gist by inspecting it with your browser (e.g., Chrome Developer Tools).
168 *
169 * However, this does not seem to be the case any longer. The actual height of the content does get set in the
170 * page after loading. So this is just the initial height.
171 * See <https://github.com/ampproject/amphtml/pull/17738>.
172 */
173 $height = 240;
174
175 $amp_tag = sprintf(
176 '<amp-gist layout="fixed-height" data-gistid="%s" height="%s"',
177 esc_attr( basename( $id, '.json' ) ),
178 esc_attr( $height )
179 );
180 if ( ! empty( $file ) ) {
181 $amp_tag .= sprintf( ' data-file="%s"', esc_attr( $file ) );
182 }
183 $amp_tag .= '></amp-gist>';
184 return $amp_tag;
185 }
186
187 // URL points to the entire gist, including the file name if there was one.
188 $id = ( ! empty( $file ) ? $id . '?file=' . $file : $id );
189
190 wp_enqueue_script(
191 'jetpack-gist-embed',
192 Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
193 array( 'jquery' ),
194 JETPACK__VERSION,
195 true
196 );
197
198 // inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables.
199 $return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>';
200
201 if (
202 // No need to check for a nonce here, that's already handled by Core further up.
203 // phpcs:disable WordPress.Security.NonceVerification.Missing
204 isset( $_POST['type'] )
205 && 'embed' === $_POST['type']
206 && isset( $_POST['action'] )
207 && 'parse-embed' === $_POST['action']
208 // phpcs:enable WordPress.Security.NonceVerification.Missing
209 ) {
210 return github_gist_simple_embed( $id );
211 }
212
213 return $return;
214 }
215
216 /**
217 * Use script tag to load shortcode in editor.
218 * Can't use wp_enqueue_script here.
219 *
220 * @since 3.9.0
221 *
222 * @param string $id The ID of the gist.
223 */
224 function github_gist_simple_embed( $id ) {
225 $id = str_replace( 'json', 'js', $id );
226 return '<script src="' . esc_url( "https://gist.github.com/$id" ) . '"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
227 }
228