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

Embed block: fix embedding stories with plain permalinks #5561

Merged
merged 12 commits into from
Dec 9, 2020

Conversation

swissspidy
Copy link
Collaborator

Summary

Fixes embedding local stories with plain permalinks.

Relevant Technical Choices

  1. Encodes URLs for transimission
  2. Adds some custom logic to parse URLs because url_to_postid() does not recognize plain permalinks like https://example.com/?web-story=my-story.

To-do

User-facing changes

N/A

Testing Instructions

  1. Disable pretty permalinks in the WordPress settings
  2. Create a new blog post and insert a Web Stories embed block
  3. Paste the URL of a web story on the same site
  4. Verify that the embed is working

Fixes #5560

@swissspidy swissspidy added Type: Bug Something isn't working Group: WordPress Changes related to WordPress or Gutenberg integration Pod: WP & Infra Group: Blocks Issues related to the provided Gutenberg Blocks labels Dec 3, 2020
@google-cla google-cla bot added the cla: yes label Dec 3, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2020

Size Change: +89 B (0%)

Total Size: 1.5 MB

ℹ️ View Unchanged
Filename Size Change
assets/css/edit-story.css 876 B 0 B
assets/css/stories-dashboard.css 929 B 0 B
assets/css/web-stories-embed-block.css 550 B 0 B
assets/js/chunk-fonts-********************.js 44.1 kB 0 B
assets/js/chunk-web-stories-template-0-********************.js 10.7 kB 0 B
assets/js/chunk-web-stories-template-1-********************.js 11.2 kB 0 B
assets/js/chunk-web-stories-template-2-********************.js 11 kB 0 B
assets/js/chunk-web-stories-template-3-********************.js 11.2 kB 0 B
assets/js/chunk-web-stories-template-4-********************.js 12.6 kB 0 B
assets/js/chunk-web-stories-template-5-********************.js 7.07 kB 0 B
assets/js/chunk-web-stories-template-6-********************.js 10.2 kB 0 B
assets/js/chunk-web-stories-template-7-********************.js 10.3 kB 0 B
assets/js/chunk-web-stories-textset-0-********************.js 5.25 kB 0 B
assets/js/chunk-web-stories-textset-1-********************.js 6.54 kB 0 B
assets/js/chunk-web-stories-textset-2-********************.js 7.83 kB 0 B
assets/js/chunk-web-stories-textset-3-********************.js 15.2 kB 0 B
assets/js/chunk-web-stories-textset-4-********************.js 4.38 kB 0 B
assets/js/chunk-web-stories-textset-5-********************.js 5.68 kB 0 B
assets/js/chunk-web-stories-textset-6-********************.js 5.47 kB 0 B
assets/js/chunk-web-stories-textset-7-********************.js 10.3 kB 0 B
assets/js/edit-story.js 573 kB +65 B (0%)
assets/js/stories-dashboard.js 639 kB +20 B (0%)
assets/js/web-stories-activation-notice.js 74 kB 0 B
assets/js/web-stories-embed-block.js 17.4 kB +4 B (0%)

compressed-size-action

Copy link
Contributor

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

Unit tests do not pass.

@swissspidy
Copy link
Collaborator Author

@spacedmonkey I know, on it already

@codecov
Copy link

codecov bot commented Dec 3, 2020

Codecov Report

Merging #5561 (e0311dc) into main (4af6d49) will increase coverage by 0.25%.
The diff coverage is 92.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5561      +/-   ##
==========================================
+ Coverage   84.48%   84.74%   +0.25%     
==========================================
  Files        1002     1002              
  Lines       17647    17668      +21     
==========================================
+ Hits        14909    14972      +63     
+ Misses       2738     2696      -42     
Flag Coverage Δ
karmatests 73.14% <ø> (+0.01%) ⬆️
unittests 65.84% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
assets/src/story-embed-block/block/edit.js 2.85% <0.00%> (ø)
includes/REST_API/Embed_Controller.php 96.00% <95.83%> (+13.97%) ⬆️
assets/src/edit-story/app/api/apiProvider.js 52.83% <0.00%> (-1.17%) ⬇️
assets/src/edit-story/app/story/storyProvider.js 96.87% <0.00%> (+0.10%) ⬆️
...s/src/edit-story/app/story/effects/useLoadStory.js 92.59% <0.00%> (+0.28%) ⬆️
includes/Story_Post_Type.php 91.60% <0.00%> (+0.47%) ⬆️
...ibrary/panes/media/common/paginatedMediaGallery.js 93.02% <0.00%> (+2.32%) ⬆️
includes/REST_API/Stories_Controller.php 66.66% <0.00%> (+10.31%) ⬆️
...ry/components/library/panes/media/common/styles.js 100.00% <0.00%> (+16.66%) ⬆️
... and 1 more

}

if ( Story_Post_Type::POST_TYPE_SLUG !== $post->post_type ) {
return false;
if ( ! $post_id ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ( ! $post_id ) {
else {

Doesn't it make more sense to use an else here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The result is exactly the same, so just a matter of preference for coding style and readability.

Aside: PHPMD by default complains about using else branches, but we've disabled that.

$this->set_permalink_structure( '/%postname%/' );
$this->go_to( get_permalink( self::$story_id ) );
}

public function tearDown() {
$this->set_permalink_structure( '' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just some regular cleanup for these tests. The setUp calls $this->set_permalink_structure( '/%postname%/' );, so it's important to reset that again; otherwise it will affect other tests in other files.

@@ -76,6 +76,13 @@ public static function wpSetUpBeforeClass( $factory ) {
set_post_thumbnail( self::$story_id, $poster_attachment_id );
}

public function tearDown() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain this / add a comment here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just some regular cleanup for these tests. Some tests call $this->set_permalink_structure( '/%postname%/' );, so it's important to reset that again; otherwise it will affect other tests in other files.

$path = explode( '/', ltrim( $url_parts['path'], '/' ) );
$path = reset( $path );
$path = count( $path ) > 2 ? reset( $path ) : false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$path = count( $path ) > 2 ? reset( $path ) : false;
$path = count( $path ) > 2 ? reset( $path ) : '';

Why not default to an empty string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Doesn't matter too much, just needs to be falsey for the check below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Empty string is a default value of get_networks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, but $path is only used below when if ( $path && $network instanceof \WP_Network )` evaluates to true. So this won't make a difference.

I can change it though, no problem. Prevents accidental bugs in the future 👍 Just wanted to point that out :)

Copy link
Contributor

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

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

Approved with a couple of nitpicks.

Tested locally with multisite and seems to work.

@swissspidy swissspidy merged commit 9b710c7 into main Dec 9, 2020
@swissspidy swissspidy deleted the fix/story-block-plain-permalinks branch December 9, 2020 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Group: Blocks Issues related to the provided Gutenberg Blocks Group: WordPress Changes related to WordPress or Gutenberg integration Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Embed block: not working for non-pretty permalinks
2 participants