Skip to content
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

Gist Shortcode: Remove jQuery Dependency #16700

Merged
merged 6 commits into from
Aug 24, 2020

Conversation

Aurorum
Copy link
Contributor

@Aurorum Aurorum commented Aug 4, 2020

Part of #16409

Changes proposed in this Pull Request:

  • Removes the use of jQuery for the Gist shortcode

Jetpack product discussion

  • N/A

Does this pull request change what data or activity we track or use?

Nope.

Testing instructions:

Try using a Gist shortcode with different ts parameters. For example:

[gist jeherve/57cc50246aab776e110060926a2face2] (ts defaults to 8)
[gist ts="2" jeherve/57cc50246aab776e110060926a2face2]

Verify that the Gist appears with the additional styling, and the tab-size style is also inserted.

Screenshot 2020-08-04 at 11 06 51

Proposed changelog entry for your changes:

  • Shortcodes: remove jQuery dependancy from Gist shortcode.

@jetpackbot
Copy link

jetpackbot commented Aug 4, 2020

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

E2E results is available here (for debugging purposes): https://jetpack-e2e-dashboard.herokuapp.com/pr-16700

Scheduled Jetpack release: September 1, 2020.
Scheduled code freeze: August 25, 2020

Generated by 🚫 dangerJS against 6314a9f

@jeherve jeherve added [Pri] Normal [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Focus] Performance [Feature] Shortcodes / Embeds [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Aug 5, 2020
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests appear to be failing right now. Could you take a look?
https://github.com/Automattic/jetpack/pull/16700/checks?check_run_id=944382555

@Aurorum
Copy link
Contributor Author

Aurorum commented Aug 5, 2020

I think that I've fixed the tests now! :)

@Aurorum Aurorum requested a review from jeherve August 5, 2020 16:15
@jeherve
Copy link
Member

jeherve commented Aug 5, 2020

Thank you. I'm a bit worried of us removing a CSS class that may be used by some to customize the look of the embed on their site. Maybe we should keep it around?

@Aurorum
Copy link
Contributor Author

Aurorum commented Aug 5, 2020

The gist-oembed class? That appears to only be used by the jQuery to replace the empty div with the actual Gist. It's replaced as soon as it loads, so there shouldn't be anyone using it for styling purposes!

Screenshot 2020-08-05 at 19 24 22

@jeherve jeherve added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Aug 10, 2020
@jeherve jeherve added this to the 8.9 milestone Aug 10, 2020
@jeherve
Copy link
Member

jeherve commented Aug 10, 2020

It may be worth testing to see if this also happens to fix #10161. 🤞

@Aurorum
Copy link
Contributor Author

Aurorum commented Aug 10, 2020

I must admit that I'm not sure that I fully understand that issue, but it appears to be triggering a console error still with no visible fixes. It's possible that I've misunderstood it though.

Screenshot 2020-08-10 at 20 37 46

Screenshot 2020-08-10 at 20 39 07

@jeherve jeherve changed the title Gist Shortcode: Remove jQuery Dependancy Gist Shortcode: Remove jQuery Dependency Aug 11, 2020
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests well for me. I would have a few suggestions though, see below.

Here is a diff incorporating the different changes I suggested. Let me know what you think about it!

diff --git a/modules/shortcodes/gist.php b/modules/shortcodes/gist.php
index ad51b5119..c8403257c 100644
--- a/modules/shortcodes/gist.php
+++ b/modules/shortcodes/gist.php
@@ -201,22 +201,29 @@ 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 );
+	$id     = ( ! empty( $file ) ? $id . '?file=' . $file : $id );
 	$return = false;
 
-	$request      = wp_remote_get( "https://gist.github.com/" . esc_attr( $id ) );
+	$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', $request_data->stylesheet );
+		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
+		);
 
-		// inline style to prevent the bottom margin to the embed that themes like TwentyTen, et al., add to tables.
+		// 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 (

modules/shortcodes/gist.php Outdated Show resolved Hide resolved
modules/shortcodes/gist.php Outdated Show resolved Hide resolved
modules/shortcodes/gist.php Show resolved Hide resolved
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Aug 11, 2020
@Aurorum
Copy link
Contributor Author

Aurorum commented Aug 11, 2020

Thank you - I've updated the PR to use that approach instead! :)

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but I'd appreciate an additional pair of eyes on this as I've contributed code to that PR.

@jeherve jeherve added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Aug 11, 2020
@jeherve jeherve self-assigned this Aug 11, 2020
@kraftbj kraftbj added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Aug 21, 2020
@jeherve jeherve merged commit 2d0bbe1 into Automattic:master Aug 24, 2020
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Aug 24, 2020
@jeherve
Copy link
Member

jeherve commented Aug 24, 2020

r212543-wpcom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Shortcodes / Embeds [Focus] Performance [Pri] Normal Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants