-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block API: Add hook for deprecated raw HTML support
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, DangerousHTML } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
|
||
class DangerousHTMLWithWarning extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
// Disable reason: We're intentionally logging a console warning | ||
// advising the developer to upgrade usage. | ||
|
||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'Deprecated: String returns from block `save` is unsupported. ' + | ||
'Use `wp.element.DangerousHTML` component instead.\n\n' + | ||
'See: ' | ||
); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
|
||
return <DangerousHTML>{ children }</DangerousHTML>; | ||
} | ||
} | ||
|
||
/** | ||
* Override save element for a block, providing support for deprecated string | ||
* return value, logging a warning advising the developer to use the preferred | ||
* DangerousHTML component instead. | ||
* | ||
* @param {(string|Object)} element Original block save return. | ||
* | ||
* @returns {Object} Filtered props applied to save element. | ||
*/ | ||
export function shimDangerousHTML( element ) { | ||
if ( typeof element !== 'string' ) { | ||
return element; | ||
} | ||
|
||
return <DangerousHTMLWithWarning children={ element } />; | ||
} | ||
|
||
addFilter( 'blocks.getSaveContent.saveElement', 'core/deprecated/save-props', shimDangerousHTML ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters