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: Add deprecation for the caption element #45166

Merged
merged 2 commits into from
Oct 21, 2022
Merged
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
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 -->