Skip to content

Commit

Permalink
Embed: Add deprecation for the caption element (#45166)
Browse files Browse the repository at this point in the history
Co-authored-by: Glen Davies <glen.davies@automattic.com>
  • Loading branch information
2 people authored and ockham committed Oct 25, 2022
1 parent 5657c8b commit f270db0
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 26 deletions.
79 changes: 53 additions & 26 deletions packages/block-library/src/embed/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,63 @@ import metadata from './block.json';
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';
import { RichText, useBlockProps } from '@wordpress/block-editor';

const { attributes: blockAttributes } = metadata;

const deprecated = [
{
attributes: blockAttributes,
save( { attributes: { url, caption, type, providerNameSlug } } ) {
if ( ! url ) {
return null;
}

const embedClassName = classnames( 'wp-block-embed', {
[ `is-type-${ type }` ]: type,
[ `is-provider-${ providerNameSlug }` ]: providerNameSlug,
} );

return (
<figure className={ embedClassName }>
// In #41140 support was added to global styles for caption elements which added a `wp-element-caption` classname
// to the embed figcaption element.
const v2 = {
attributes: blockAttributes,
save( { attributes } ) {
const { url, caption, type, providerNameSlug } = attributes;

if ( ! url ) {
return null;
}

const className = classnames( 'wp-block-embed', {
[ `is-type-${ type }` ]: type,
[ `is-provider-${ providerNameSlug }` ]: providerNameSlug,
[ `wp-block-embed-${ providerNameSlug }` ]: providerNameSlug,
} );

return (
<figure { ...useBlockProps.save( { className } ) }>
<div className="wp-block-embed__wrapper">
{ `\n${ url }\n` /* URL needs to be on its own line. */ }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content
tagName="figcaption"
value={ caption }
/>
) }
</figure>
);
},
</div>
{ ! RichText.isEmpty( caption ) && (
<RichText.Content tagName="figcaption" value={ caption } />
) }
</figure>
);
},
};

const v1 = {
attributes: blockAttributes,
save( { attributes: { url, caption, type, providerNameSlug } } ) {
if ( ! url ) {
return null;
}

const embedClassName = classnames( 'wp-block-embed', {
[ `is-type-${ type }` ]: type,
[ `is-provider-${ providerNameSlug }` ]: providerNameSlug,
} );

return (
<figure className={ embedClassName }>
{ `\n${ url }\n` /* URL needs to be on its own line. */ }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content tagName="figcaption" value={ caption } />
) }
</figure>
);
},
];
};

const deprecated = [ v2, v1 ];

export default deprecated;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- wp:embed {"url":"https://example.com/"} -->
<figure class="wp-block-embed">
https://example.com/
<figcaption>Embedded content from an example URL</figcaption>
</figure>
<!-- /wp:embed -->
14 changes: 14 additions & 0 deletions test/integration/fixtures/blocks/core__embed__deprecated-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "core/embed",
"isValid": true,
"attributes": {
"url": "https://example.com/",
"caption": "Embedded content from an example URL",
"allowResponsive": true,
"responsive": false,
"previewable": true
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"blockName": "core/embed",
"attrs": {
"url": "https://example.com/"
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-embed\">\n https://example.com/\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-embed\">\n https://example.com/\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:embed {"url":"https://example.com/"} -->
<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
https://example.com/
</div><figcaption class="wp-element-caption">Embedded content from an example URL</figcaption></figure>
<!-- /wp:embed -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- wp:embed {"url":"https://example.com/"} -->
<figure class="wp-block-embed">
<div class="wp-block-embed__wrapper">
https://example.com/
</div>
<figcaption>Embedded content from an example URL</figcaption>
</figure>
<!-- /wp:embed -->
14 changes: 14 additions & 0 deletions test/integration/fixtures/blocks/core__embed__deprecated-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "core/embed",
"isValid": true,
"attributes": {
"url": "https://example.com/",
"caption": "Embedded content from an example URL",
"allowResponsive": true,
"responsive": false,
"previewable": true
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"blockName": "core/embed",
"attrs": {
"url": "https://example.com/"
},
"innerBlocks": [],
"innerHTML": "\n<figure class=\"wp-block-embed\">\n <div class=\"wp-block-embed__wrapper\">\n https://example.com/\n </div>\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>\n",
"innerContent": [
"\n<figure class=\"wp-block-embed\">\n <div class=\"wp-block-embed__wrapper\">\n https://example.com/\n </div>\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:embed {"url":"https://example.com/"} -->
<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
https://example.com/
</div><figcaption class="wp-element-caption">Embedded content from an example URL</figcaption></figure>
<!-- /wp:embed -->

0 comments on commit f270db0

Please sign in to comment.