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

Gutenberg: oembed api middleware #27437

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client/gutenberg/editor/api-middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* External dependencies
*/
import wpcomProxyRequest from 'wpcom-proxy-request';
import url from 'url';
import { stringify } from 'qs';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,6 +36,38 @@ export const pathRewriteMiddleware = ( options, next, siteSlug ) => {
return next( { ...options, path: wpcomPath } );
};

export const oembedMiddleware = ( options, next, siteSlug ) => {
// Updates https://public-api.wordpress.com/wp/v2/oembed/1.0/proxy?url=<source URL> to
// https://public-api.wordpress.com/rest/v1.1/sites/<site ID>/embeds/render?embed_url=<source URL>&force=wpcom
// This intentionally breaks the middleware chain if we match an embed.
if ( /oembed\/1.0\/proxy/.test( options.url ) ) {
const urlObject = url.parse( options.url, { parseQueryString: true } );
// Make authenticated calls using the WordPress.com REST Proxy
// bypassing the apiFetch call that uses window.fetch.
// This intentionally breaks the middleware chain.
return new Promise( ( resolve, reject ) => {
const query = {
force: 'wpcom',
embed_url: urlObject.query.url,
};
wpcomProxyRequest(
{
path: `/sites/${ siteSlug }/embeds/render?${ stringify( query ) }`,
apiVersion: '1.1',
},
( error, bodyOrData ) => {
if ( error ) {
return reject( error );
}

return resolve( bodyOrData );
}
);
} );
}
return next( options );
};

export const wpcomProxyMiddleware = parameters => {
// Make authenticated calls using the WordPress.com REST Proxy
// bypassing the apiFetch call that uses window.fetch.
Expand Down
4 changes: 4 additions & 0 deletions client/gutenberg/editor/api-middleware/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
pathRewriteMiddleware,
urlRewriteMiddleware,
wpcomProxyMiddleware,
oembedMiddleware,
} from './index';

export class WithAPIMiddleware extends Component {
Expand Down Expand Up @@ -44,6 +45,9 @@ export class WithAPIMiddleware extends Component {
// This call intentionally breaks the middleware chain.
apiFetch.use( options => wpcomProxyMiddleware( options ) );

// This call may break the middleware chain if we match an oembed
apiFetch.use( ( options, next ) => oembedMiddleware( options, next, siteSlug ) );

apiFetch.use( ( options, next ) => debugMiddleware( options, next ) );

apiFetch.use( ( options, next ) => urlRewriteMiddleware( options, next, siteSlug ) );
Expand Down