summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/shortcodes/gist.php')
-rw-r--r--plugins/jetpack/modules/shortcodes/gist.php39
1 files changed, 32 insertions, 7 deletions
diff --git a/plugins/jetpack/modules/shortcodes/gist.php b/plugins/jetpack/modules/shortcodes/gist.php
index eba0a1a3..7c558c46 100644
--- a/plugins/jetpack/modules/shortcodes/gist.php
+++ b/plugins/jetpack/modules/shortcodes/gist.php
@@ -18,6 +18,8 @@
* @package 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' );
@@ -56,6 +58,7 @@ function jetpack_gist_get_shortcode_id( $gist = '' ) {
$gist_info = array(
'id' => '',
'file' => '',
+ 'ts' => 8,
);
// Simple shortcode, with just an ID.
if ( ctype_alnum( $gist ) ) {
@@ -74,6 +77,7 @@ function jetpack_gist_get_shortcode_id( $gist = '' ) {
return array(
'id' => '',
'file' => '',
+ 'ts' => 8,
);
}
@@ -84,10 +88,19 @@ function jetpack_gist_get_shortcode_id( $gist = '' ) {
// Keep the unique identifier without any leading or trailing slashes.
if ( ! empty( $parsed_url['path'] ) ) {
- $gist_info['id'] = preg_replace( '/^\/([^\.]+)\./', '$1', $parsed_url['path'] );
+ $gist_info['id'] = trim( $parsed_url['path'], '/' );
// Overwrite $gist with our identifier to clean it up below.
$gist = $gist_info['id'];
}
+
+ // Parse the query args to obtain the tab spacing.
+ if ( ! empty( $parsed_url['query'] ) ) {
+ $query_args = array();
+ wp_parse_str( $parsed_url['query'], $query_args );
+ if ( ! empty( $query_args['ts'] ) ) {
+ $gist_info['ts'] = absint( $query_args['ts'] );
+ }
+ }
}
// Not a URL nor an ID? Look for "username/id", "/username/id", or "id", and only keep the ID.
@@ -154,6 +167,12 @@ function github_gist_shortcode( $atts, $content = '' ) {
$file = rawurlencode( $file );
}
+ // Set the tab size, allowing attributes to override the query string.
+ $tab_size = $gist_info['ts'];
+ if ( ! empty( $atts['ts'] ) ) {
+ $tab_size = absint( $atts['ts'] );
+ }
+
if (
class_exists( 'Jetpack_AMP_Support' )
&& Jetpack_AMP_Support::is_amp_request()
@@ -186,14 +205,18 @@ function github_gist_shortcode( $atts, $content = '' ) {
wp_enqueue_script(
'jetpack-gist-embed',
- Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
+ Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/gist.min.js', 'modules/shortcodes/js/gist.js' ),
array( 'jquery' ),
JETPACK__VERSION,
true
);
// 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>';
+ $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 )
+ );
if (
// No need to check for a nonce here, that's already handled by Core further up.
@@ -204,7 +227,7 @@ function github_gist_shortcode( $atts, $content = '' ) {
&& 'parse-embed' === $_POST['action']
// phpcs:enable WordPress.Security.NonceVerification.Missing
) {
- return github_gist_simple_embed( $id );
+ return github_gist_simple_embed( $id, $tab_size );
}
return $return;
@@ -216,9 +239,11 @@ function github_gist_shortcode( $atts, $content = '' ) {
*
* @since 3.9.0
*
- * @param string $id The ID of the gist.
+ * @param string $id The ID of the gist.
+ * @param int $tab_size The tab size of the gist.
+ * @return string The script tag of the gist.
*/
-function github_gist_simple_embed( $id ) {
+function github_gist_simple_embed( $id, $tab_size = 8 ) {
$id = str_replace( 'json', 'js', $id );
- return '<script src="' . esc_url( "https://gist.github.com/$id" ) . '"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
+ return '<script src="' . esc_url( "https://gist.github.com/$id?ts=$tab_size" ) . '"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}