Skip to content

Commit

Permalink
Fallback to Twitter provider when embedding X URLs (#54876)
Browse files Browse the repository at this point in the history
* Fallback to Twitter provider when embedding X URLs

* Avoid processing empty urls when trying a different provider

* Update `react-native-editor` changelog
  • Loading branch information
fluiddot committed Sep 27, 2023
1 parent a3904c4 commit 5b286b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/block-library/src/embed/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { useBlockProps } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { View } from '@wordpress/primitives';
import { getAuthority } from '@wordpress/url';

const EmbedEdit = ( props ) => {
const {
Expand Down Expand Up @@ -137,6 +138,20 @@ const EmbedEdit = ( props ) => {
setAttributes( { url: newURL } );
}, [ preview?.html, attributesUrl, cannotEmbed, fetching ] );

// Try a different provider in case the embed url is not supported.
useEffect( () => {
if ( ! cannotEmbed || fetching || ! url ) {
return;
}

// Until X provider is supported in WordPress, as a workaround we use Twitter provider.
if ( getAuthority( url ) === 'x.com' ) {
const newURL = new URL( url );
newURL.host = 'twitter.com';
setAttributes( { url: newURL.toString() } );
}
}, [ url, cannotEmbed, fetching, setAttributes ] );

// Handle incoming preview.
useEffect( () => {
if ( preview && ! isEditingURL ) {
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/embed/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { View } from '@wordpress/primitives';
import { getAuthority } from '@wordpress/url';

// The inline preview feature will be released progressible, for this reason
// the embed will only be considered previewable for the following providers list.
Expand Down Expand Up @@ -160,6 +161,20 @@ const EmbedEdit = ( props ) => {
setAttributes( { url: newURL } );
}, [ preview?.html, url, cannotEmbed, fetching ] );

// Try a different provider in case the embed url is not supported.
useEffect( () => {
if ( ! cannotEmbed || fetching || ! url ) {
return;
}

// Until X provider is supported in WordPress, as a workaround we use Twitter provider.
if ( getAuthority( url ) === 'x.com' ) {
const newURL = new URL( url );
newURL.host = 'twitter.com';
setAttributes( { url: newURL.toString() } );
}
}, [ url, cannotEmbed, fetching, setAttributes ] );

// Handle incoming preview.
useEffect( () => {
if ( preview && ! isEditingURL ) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For each user feature we should also add a importance categorization label to i
## Unreleased
- [*] Limit inner blocks nesting depth to avoid call stack size exceeded crash [#54382]
- [*] Prevent crashes when setting an invalid media URL for Video or Audio blocks [#54834]
- [**] Fallback to Twitter provider when embedding X URLs [#54876]

## 1.104.0
- [*] Fix the obscurred "Insert from URL" input for media blocks when using a device in landscape orientation. [#54096]
Expand Down

0 comments on commit 5b286b7

Please sign in to comment.