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

Embeds: Don't transform into specialized embed block variation if it's not registered #24559

Merged

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Aug 13, 2020

Description

Users can insert a generic version of the core/embed block from the block inserter, or choose a more specialized block variation (e.g. for Instagram, YouTube, etc).

Furthermore, if a user inserts a plain URL, we also trigger our embed block creation behavior: We first create a generic core/embed block from that URL, and then analyze if we have a more specialized block variation. If we do, we transform the generic block into that block variation.

A site admit might now choose to unregister certain block variations, in order to prevent users from inserting embeds of a certain type (e.g. they might choose to disallow WordPress embeds out of security concerns).

In that case however, the URL insertion behavior will still attempt to insert a embed block variation for that specific embed type, even though that block variation is not available.

We should thus check if a block variation of the core/embed is registered for the given embed type before attempting to transform the generic embed block into that variation.

Related conversations

#24090 (comment)
Automattic/wp-calypso#39935 (comment)

How has this been tested?

Verify that embeds still work.

The specific use case where a block variation isn't registered should be covered by the unit tests, but you can add some code to unregister a variation manually and insert a matching embed to verify that the generic embed block isn't tranformed to that variation.

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@ockham ockham added the [Block] Embed Affects the Embed Block label Aug 13, 2020
@ockham ockham self-assigned this Aug 13, 2020
@github-actions
Copy link

github-actions bot commented Aug 13, 2020

Size Change: +1.79 kB (0%)

Total Size: 1.16 MB

Filename Size Change
build/api-fetch/index.js 3.44 kB -1 B
build/block-editor/index.js 126 kB +4 B (0%)
build/block-library/index.js 133 kB +1.04 kB (0%)
build/block-library/style-rtl.css 7.55 kB +75 B (0%)
build/block-library/style.css 7.55 kB +73 B (0%)
build/blocks/index.js 47.7 kB -762 B (1%)
build/components/index.js 200 kB -1 B
build/core-data/index.js 11.8 kB -4 B (0%)
build/data/index.js 8.55 kB -6 B (0%)
build/dom/index.js 4.47 kB +1.24 kB (27%) 🚨
build/edit-navigation/index.js 11 kB +132 B (1%)
build/edit-post/index.js 304 kB +2 B (0%)
build/edit-post/style-rtl.css 5.61 kB -3 B (0%)
build/edit-post/style.css 5.61 kB -3 B (0%)
build/edit-site/index.js 17 kB +1 B
build/edit-widgets/index.js 11.8 kB +1 B
build/editor/index.js 45.3 kB -9 B (0%)
build/format-library/index.js 7.71 kB +1 B
build/media-utils/index.js 5.33 kB -1 B
build/primitives/index.js 1.41 kB +7 B (0%)
build/rich-text/index.js 13.9 kB -5 B (0%)
build/server-side-render/index.js 2.77 kB +1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.67 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 7.96 kB 0 B
build/block-directory/style-rtl.css 953 B 0 B
build/block-directory/style.css 952 B 0 B
build/block-editor/style-rtl.css 10.7 kB 0 B
build/block-editor/style.css 10.7 kB 0 B
build/block-library/editor-rtl.css 8.36 kB 0 B
build/block-library/editor.css 8.36 kB 0 B
build/block-library/theme-rtl.css 729 B 0 B
build/block-library/theme.css 730 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/components/style-rtl.css 15.7 kB 0 B
build/components/style.css 15.7 kB 0 B
build/compose/index.js 9.68 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/date/index.js 5.38 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/edit-navigation/style-rtl.css 1.12 kB 0 B
build/edit-navigation/style.css 1.12 kB 0 B
build/edit-site/style-rtl.css 3.06 kB 0 B
build/edit-site/style.css 3.06 kB 0 B
build/edit-widgets/style-rtl.css 2.45 kB 0 B
build/edit-widgets/style.css 2.45 kB 0 B
build/editor/editor-styles-rtl.css 537 B 0 B
build/editor/editor-styles.css 539 B 0 B
build/editor/style-rtl.css 3.8 kB 0 B
build/editor/style.css 3.79 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.11 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@ockham ockham force-pushed the fix/disable-wordpress-embeds-transform-if-variation-unavailable branch from b3eb47e to 86c4add Compare August 18, 2020 12:27
@ockham ockham force-pushed the fix/disable-wordpress-embeds-transform-if-variation-unavailable branch from 894b7d6 to c583191 Compare August 18, 2020 13:35
@ockham ockham marked this pull request as ready for review August 18, 2020 15:17
@ockham ockham requested a review from mcsf August 18, 2020 15:20
@ockham
Copy link
Contributor Author

ockham commented Aug 19, 2020

Should be ready for review 🙂

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Looks good in principle, but I haven't tested. I'll defer to @ntsekouras for review and approval. :)

packages/block-library/src/embed/util.js Outdated Show resolved Hide resolved
Copy link
Contributor

@ntsekouras ntsekouras 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 👍 - I left some small comments though.

packages/block-library/src/embed/util.js Outdated Show resolved Hide resolved
packages/block-library/src/embed/util.js Outdated Show resolved Hide resolved
@ockham
Copy link
Contributor Author

ockham commented Aug 19, 2020

Thanks for the feedback! Should be ready for another look 🙂

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

I left a comment to change the hardcoded value wordpress but when this change happens let's land this! 👍

Thanks @ockham!

@@ -98,8 +104,7 @@ export const createUpgradedEmbedBlock = (
// WordPress blocks can work on multiple sites, and so don't have patterns,
// so if we're in a WordPress block, assume the user has chosen it for a WordPress URL.
const isCurrentBlockWP =
providerNameSlug === WP_VARIATION.attributes.providerNameSlug ||
type === WP_EMBED_TYPE;
providerNameSlug === 'wordpress' || type === WP_EMBED_TYPE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Change the hardcoded wordpress to WP_PROVIDER or something similar, put it in constants file like WP_EMBED_TYPE and get it from our variations.js file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH given my other comment plus the fact that these are currently two different fields, using a constant to compare them both to has the potential to make this more confusing IMO. Using the same constant kind of implies that both fields are somehow functionally equivalent, which they aren't. Using a string literal in both cases (which just happens to be identical) carries that notion much less.

(To clarify, my previous suggestion to drop providerNameSlug was solely meant for the variations definition JSON file -- I didn't mean to remove the provider name check altogether for embeds that we're fetching from a given URL -- at least not before I understand it better 😅 )

@@ -114,14 +119,24 @@ export const createUpgradedEmbedBlock = (
} );
}

const wpVariation = getBlockVariations( DEFAULT_EMBED_BLOCK )?.find(
( { name } ) => name === 'wordpress'
Copy link
Contributor

Choose a reason for hiding this comment

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

Some comment as above to retrieve hardcoded value. Since name and providerNameSlug are the same now, choose what fits more. For example if you choose only the providerName, change this find check to use the same property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH for the sake of future refactoring, I'd rather stick with name here, since it'll be one thing less to think about for future devs who'd like to phase out providerNameSlug. (Semantically, it also makes more sense to say that we're looking for the right block variation by its name, rather than its provider name.)

@ockham
Copy link
Contributor Author

ockham commented Aug 20, 2020

I left a comment to change the hardcoded value wordpress but when this change happens let's land this!

Thanks @ockham!

Thanks @ntsekouras! I replied to both comments, essentially suggesting to keep the wordpress literals around 😅 Curious to hear your thoughts.

@ntsekouras
Copy link
Contributor

Using the same constant kind of implies that both fields are somehow functionally equivalent, which they aren't. Using a string literal in both cases (which just happens to be identical) carries that notion much less.

essentially suggesting to keep the wordpress literals

I agree with that but still the name don't you think it should be retrieved from variations.js and not be hardcoded? Even with the removal of the providerSlugName, the name would still be there. Maybe one and one 😅 ? What do you think?

Either way this is just a small comment and that's why I had pre-approved :)

@ockham
Copy link
Contributor Author

ockham commented Aug 20, 2020

Using the same constant kind of implies that both fields are somehow functionally equivalent, which they aren't. Using a string literal in both cases (which just happens to be identical) carries that notion much less.

essentially suggesting to keep the wordpress literals

I agree with that but still the name don't you think it should be retrieved from variations.js and not be hardcoded? Even with the removal of the providerSlugName, the name would still be there. Maybe one and one 😅 ? What do you think?

Not sure I'm totally following 😅 -- we use the name to look up the wordpress variation object from the array in variations.js. We kind of needed something hard-coded for the lookup, don't we? 😅

@ntsekouras
Copy link
Contributor

ntsekouras commented Aug 20, 2020

You're right. Got a bit stuck there with that previous line in mind WP_VARIATION.attributes.providerNameSlug.

Land this 💯

@ockham
Copy link
Contributor Author

ockham commented Aug 21, 2020

Thanks a bunch @ntsekouras!

@ockham ockham merged commit da41e80 into master Aug 21, 2020
@ockham ockham deleted the fix/disable-wordpress-embeds-transform-if-variation-unavailable branch August 21, 2020 09:38
@github-actions github-actions bot added this to the Gutenberg 8.9 milestone Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Embed Affects the Embed Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants