-
Notifications
You must be signed in to change notification settings - Fork 834
Handles custom tab sizes for gist embeds and shortcodes #13807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2ab9cd1
55d9440
4ca5709
f36a598
217bdfa
4de9a1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -58,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 ) ) { | ||||
|
@@ -76,6 +77,7 @@ function jetpack_gist_get_shortcode_id( $gist = '' ) { | |||
return array( | ||||
'id' => '', | ||||
'file' => '', | ||||
'ts' => 8, | ||||
); | ||||
} | ||||
|
||||
|
@@ -86,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. | ||||
|
@@ -156,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'] ) ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to allow the attributes to override the query string to allow for theme filtering via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add some default attributes in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code already sets the defaults in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do indeed have defaults but we do not have the Something like this? jetpack/modules/shortcodes/recipe.php Line 175 in 89a9af9
That's not necessarily a blocker here. 👍 |
||||
$tab_size = absint( $atts['ts'] ); | ||||
} | ||||
|
||||
if ( | ||||
class_exists( 'Jetpack_AMP_Support' ) | ||||
&& Jetpack_AMP_Support::is_amp_request() | ||||
|
@@ -195,7 +212,11 @@ class_exists( 'Jetpack_AMP_Support' ) | |||
); | ||||
|
||||
// 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>', | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting that we embed this multiple times. May be a better way to do this. |
||||
esc_attr( $id ), | ||||
absint( $tab_size ) | ||||
); | ||||
|
||||
if ( | ||||
// No need to check for a nonce here, that's already handled by Core further up. | ||||
|
@@ -206,7 +227,7 @@ class_exists( 'Jetpack_AMP_Support' ) | |||
&& '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; | ||||
|
@@ -218,9 +239,11 @@ class_exists( 'Jetpack_AMP_Support' ) | |||
* | ||||
* @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 | ||||
} |
Uh oh!
There was an error while loading. Please reload this page.