summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/shortcodes/gist.php')
-rw-r--r--plugins/jetpack/modules/shortcodes/gist.php49
1 files changed, 46 insertions, 3 deletions
diff --git a/plugins/jetpack/modules/shortcodes/gist.php b/plugins/jetpack/modules/shortcodes/gist.php
index 072014ef..968e9e75 100644
--- a/plugins/jetpack/modules/shortcodes/gist.php
+++ b/plugins/jetpack/modules/shortcodes/gist.php
@@ -54,6 +54,49 @@ function github_gist_shortcode( $atts, $content = '' ) {
return '<!-- Invalid Gist ID -->';
}
+ if ( Jetpack_AMP_Support::is_amp_request() ) {
+ /*
+ * According to <https://www.ampproject.org/docs/reference/components/amp-gist#height-(required)>:
+ *
+ * > Note: You must find the height of the gist by inspecting it with your browser (e.g., Chrome Developer Tools).
+ *
+ * However, this does not seem to be the case any longer. The actual height of the content does get set in the
+ * page after loading. So this is just the initial height.
+ * See <https://github.com/ampproject/amphtml/pull/17738>.
+ */
+ $height = 240;
+
+ $parsed_url = wp_parse_url( $id );
+
+ // Strip Github user name.
+ $id = preg_replace( '#^.*/(?=[a-z0-9]+)#', '', $parsed_url['path'] );
+
+ $file = null;
+ if ( ! empty( $parsed_url['query'] ) ) {
+ $query_args = wp_parse_args( $parsed_url['query'] );
+ if ( isset( $query_args['file'] ) ) {
+ $file = $query_args['file'];
+ }
+ }
+ if ( ! $file && ! empty( $parsed_url['fragment'] ) && preg_match( '#^file-(.+)#', $parsed_url['fragment'], $matches ) ) {
+ $file = $matches[1];
+
+ // Make best guess of file for fragment that was slugified.
+ $file = preg_replace( '/-(\w+)/', '.$1', $file );
+ }
+
+ $amp_tag = sprintf(
+ '<amp-gist layout="fixed-height" data-gistid="%s" height="%s"',
+ esc_attr( basename( $id, '.json' ) ),
+ esc_attr( $height )
+ );
+ if ( ! empty( $file ) ) {
+ $amp_tag .= sprintf( ' data-file="%s"', esc_attr( $file ) );
+ }
+ $amp_tag .= '></amp-gist>';
+ return $amp_tag;
+ }
+
wp_enqueue_script(
'jetpack-gist-embed',
Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
@@ -75,8 +118,8 @@ function github_gist_shortcode( $atts, $content = '' ) {
// inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables
$return = '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="' . esc_attr( $id ) . '"></div>';
- if ( isset( $_POST[ 'type' ] ) && 'embed' === $_POST[ 'type' ] &&
- isset( $_POST[ 'action' ] ) && 'parse-embed' === $_POST['action'] ) {
+ if ( isset( $_POST['type'] ) && 'embed' === $_POST['type'] &&
+ isset( $_POST['action'] ) && 'parse-embed' === $_POST['action'] ) {
return github_gist_simple_embed( $id );
}
@@ -95,4 +138,4 @@ function github_gist_shortcode( $atts, $content = '' ) {
function github_gist_simple_embed( $id ) {
$id = str_replace( 'json', 'js', $id );
return '<script type="text/javascript" src="https://gist.github.com/' . $id . '"></script>';
-} \ No newline at end of file
+}