diff --git a/modules/shortcodes/gist.php b/modules/shortcodes/gist.php index 7c558c462173f..ba60f2399845e 100644 --- a/modules/shortcodes/gist.php +++ b/modules/shortcodes/gist.php @@ -201,22 +201,31 @@ class_exists( 'Jetpack_AMP_Support' ) } // URL points to the entire gist, including the file name if there was one. - $id = ( ! empty( $file ) ? $id . '?file=' . $file : $id ); - - wp_enqueue_script( - 'jetpack-gist-embed', - Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ), - array( 'jquery' ), - JETPACK__VERSION, - true - ); + $id = ( ! empty( $file ) ? $id . '?file=' . $file : $id ); + $return = false; - // inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables. - $return = sprintf( - '
', - esc_attr( $id ), - absint( $tab_size ) - ); + $request = wp_remote_get( esc_url_raw( 'https://gist.github.com/' . esc_attr( $id ) ) ); + $request_code = wp_remote_retrieve_response_code( $request ); + + if ( 200 === $request_code ) { + $request_body = wp_remote_retrieve_body( $request ); + $request_data = json_decode( $request_body ); + + wp_enqueue_style( 'jetpack-gist-styling', esc_url( $request_data->stylesheet ), array(), JETPACK__VERSION ); + + $gist = substr_replace( $request_data->div, sprintf( 'style="tab-size: %1$s" ', absint( $tab_size ) ), 5, 0 ); + + // Add inline styles for the tab style in the opening div of the gist. + $gist = preg_replace( + '#(\)?#', + sprintf( '$1style="tab-size: %1$s" $2$3', absint( $tab_size ) ), + $request_data->div, + 1 + ); + + // Add inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables. + $return = sprintf( '%1$s', $gist ); + } if ( // No need to check for a nonce here, that's already handled by Core further up. diff --git a/modules/shortcodes/js/gist.js b/modules/shortcodes/js/gist.js deleted file mode 100644 index 4b8b7c10301c5..0000000000000 --- a/modules/shortcodes/js/gist.js +++ /dev/null @@ -1,32 +0,0 @@ -( function ( $, undefined ) { - var gistStylesheetLoaded = false, - gistEmbed = function () { - $( '.gist-oembed' ).each( function ( i, el ) { - var url = 'https://gist.github.com/' + $( el ).data( 'gist' ), - ts = parseInt( $( el ).data( 'ts' ), 10 ); - - $.ajax( { - url: url, - dataType: 'jsonp', - } ).done( function ( response ) { - if ( ts && 8 !== ts ) { - $( el ).replaceWith( $( response.div ).css( 'tab-size', ts.toString() ) ); - } else { - $( el ).replaceWith( response.div ); - } - - if ( ! gistStylesheetLoaded ) { - var stylesheet = - ''; - - $( 'head' ).append( stylesheet ); - - gistStylesheetLoaded = true; - } - } ); - } ); - }; - - $( document ).ready( gistEmbed ); - $( 'body' ).on( 'post-load', gistEmbed ); -} )( jQuery ); diff --git a/tests/php/modules/shortcodes/test-class.gist.php b/tests/php/modules/shortcodes/test-class.gist.php index eb737051dd825..a48c018d2ac5c 100644 --- a/tests/php/modules/shortcodes/test-class.gist.php +++ b/tests/php/modules/shortcodes/test-class.gist.php @@ -89,7 +89,7 @@ public function test_shortcodes_gist_public_id_in_content() { // Test HTML version. $shortcode_content = do_shortcode( $content ); - $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $actual ); + $this->assertContains( '
', $actual ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $shortcode_content ); + $this->assertContains( '
', $actual ); + $this->assertContains( '