diff options
Diffstat (limited to 'plugins/jetpack/modules/shortcodes/gist.php')
-rw-r--r-- | plugins/jetpack/modules/shortcodes/gist.php | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/plugins/jetpack/modules/shortcodes/gist.php b/plugins/jetpack/modules/shortcodes/gist.php index 7c558c46..009fc04c 100644 --- a/plugins/jetpack/modules/shortcodes/gist.php +++ b/plugins/jetpack/modules/shortcodes/gist.php @@ -15,11 +15,9 @@ * Gist ID with username: [gist jeherve/57cc50246aab776e110060926a2face2] * Gist private ID with username: [gist xknown/fc5891af153e2cf365c9] * - * @package Jetpack + * @package automattic/jetpack */ -use Automattic\Jetpack\Assets; - wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([a-zA-Z0-9/]+)(\#file\-[a-zA-Z0-9\_\-]+)?#', 'github_gist_embed_handler' ); add_shortcode( 'gist', 'github_gist_shortcode' ); @@ -201,22 +199,31 @@ function github_gist_shortcode( $atts, $content = '' ) { } // 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( - '<style>.gist table { margin-bottom: 0; }</style><div class="gist-oembed" data-gist="%1$s" data-ts="%2$d"></div>', - 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( + '#(\<div\s)+(id=\"gist[0-9]+\")+(\sclass=\"gist\"\>)?#', + 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( '<style>.gist table { margin-bottom: 0; }</style>%1$s', $gist ); + } if ( // No need to check for a nonce here, that's already handled by Core further up. |