'',
'file' => '',
'ts' => 8,
);
// Simple shortcode, with just an ID.
if ( ctype_alnum( $gist ) ) {
$gist_info['id'] = $gist;
}
// Full URL? Only keep the relevant parts.
$parsed_url = wp_parse_url( $gist );
if (
! empty( $parsed_url )
&& is_array( $parsed_url )
&& isset( $parsed_url['scheme'] ) && isset( $parsed_url['host'] ) && isset( $parsed_url['path'] )
) {
// Not a Gist URL? Bail.
if ( 'gist.github.com' !== $parsed_url['host'] ) {
return array(
'id' => '',
'file' => '',
'ts' => 8,
);
}
// Keep the file name if there was one.
if ( ! empty( $parsed_url['fragment'] ) ) {
$gist_info['file'] = preg_replace( '/(?:file-)(.+)/', '$1', $parsed_url['fragment'] );
}
// Keep the unique identifier without any leading or trailing slashes.
if ( ! empty( $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.
if ( preg_match( '#^/?(([a-z0-9_-]+/)?([a-z0-9]+))$#i', $gist, $matches ) ) {
$gist_info['id'] = $matches[3];
}
return $gist_info;
}
/**
* Callback for gist shortcode.
*
* @since 2.8.0
*
* @param array $atts Attributes found in the shortcode.
* @param string $content Content enclosed by the shortcode.
*
* @return string The gist HTML.
*/
function github_gist_shortcode( $atts, $content = '' ) {
if ( empty( $atts[0] ) && empty( $content ) ) {
if ( current_user_can( 'edit_posts' ) ) {
return esc_html__( 'Please specify a Gist URL or ID.', 'jetpack' );
} else {
return '';
}
}
$id = ( ! empty( $content ) ) ? $content : $atts[0];
// Parse a URL to get an ID we can use.
$gist_info = jetpack_gist_get_shortcode_id( $id );
if ( empty( $gist_info['id'] ) ) {
if ( current_user_can( 'edit_posts' ) ) {
return esc_html__( 'The Gist ID you provided is not valid. Please try a different one.', 'jetpack' );
} else {
return '';
}
} else {
// Add trailing .json to all unique gist identifiers.
$id = $gist_info['id'] . '.json';
}
// The file name can come from the URL passed, or from a shortcode attribute.
if ( ! empty( $gist_info['file'] ) ) {
$file = $gist_info['file'];
} elseif ( ! empty( $atts['file'] ) ) {
$file = $atts['file'];
} else {
$file = '';
}
// Replace - by . to get a real file name from slug.
if ( ! empty( $file ) ) {
// Find the last -.
$dash_position = strrpos( $file, '-' );
if ( false !== $dash_position ) {
// Replace the - by a period.
$file = substr_replace( $file, '.', $dash_position, 1 );
}
$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()
) {
/*
* According to