-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compat: Upgrade admin notices to use Notices module at runtime (#11604)
* Notices: Normalize notice type to use WP prefix * Notices: Support Notice object as argument of createNotice * Edit Post: Upgrade admin notices to notice module * Notices: Extract default status to constant * Docs: Add unstable API convention to coding guidelines See: https://wordpress.slack.com/archives/C5UNMSU4R/p1541690775295000 (Registration: https://make.wordpress.org/chat/) * Notices: Support __unstableHTML action property * Components: Pass through __unstableHTML as Notice RawHTML * Edit Post: Pass through notice HTML from admin notices * Notices: Enforce string-y content by cast * Notices: Add speak option * Edit Post: Add missing AdminNotices classes * Edit Post: Derive all AdminNotices upgraded notice children * Edit Post: Fix AdminNotices missing text nodes content * Edit Post: Reverse order of AdminNotice upgraded notices * Notices: Mark as __unstableHTML via option Content is used for both * Edit Post: Limit upgraded notices to wpbody-content ID
- Loading branch information
Showing
15 changed files
with
294 additions
and
14 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
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 |
---|---|---|
|
@@ -126,6 +126,7 @@ | |
'wp-embed', | ||
'wp-i18n', | ||
'wp-keycodes', | ||
'wp-notices', | ||
'wp-nux', | ||
'wp-plugins', | ||
'wp-url', | ||
|
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
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
105 changes: 105 additions & 0 deletions
105
packages/edit-post/src/components/admin-notices/index.js
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,105 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Mapping of server-supported notice class names to an equivalent notices | ||
* module status. | ||
* | ||
* @type {Map} | ||
*/ | ||
const NOTICE_CLASS_STATUSES = { | ||
'notice-success': 'success', | ||
updated: 'success', | ||
'notice-warning': 'warning', | ||
'notice-error': 'error', | ||
error: 'error', | ||
'notice-info': 'info', | ||
}; | ||
|
||
/** | ||
* Returns an array of admin notice Elements. | ||
* | ||
* @return {Element[]} Admin notice elements. | ||
*/ | ||
function getAdminNotices() { | ||
// The order is reversed to match expectations of rendered order, since a | ||
// NoticesList is itself rendered in reverse order (newest to oldest). | ||
return [ ...document.querySelectorAll( '#wpbody-content > .notice' ) ].reverse(); | ||
} | ||
|
||
/** | ||
* Given an admin notice Element, returns the relevant notice content HTML. | ||
* | ||
* @param {Element} element Admin notice element. | ||
* | ||
* @return {Element} Upgraded notice HTML. | ||
*/ | ||
function getNoticeHTML( element ) { | ||
const fragments = []; | ||
|
||
for ( const child of element.childNodes ) { | ||
if ( child.nodeType !== window.Node.ELEMENT_NODE ) { | ||
const value = child.nodeValue.trim(); | ||
if ( value ) { | ||
fragments.push( child.nodeValue ); | ||
} | ||
} else if ( ! child.classList.contains( 'notice-dismiss' ) ) { | ||
fragments.push( child.outerHTML ); | ||
} | ||
} | ||
|
||
return fragments.join( '' ); | ||
} | ||
|
||
/** | ||
* Given an admin notice Element, returns the upgraded status type, or | ||
* undefined if one cannot be determined (i.e. one is not assigned). | ||
* | ||
* @param {Element} element Admin notice element. | ||
* | ||
* @return {?string} Upgraded status type. | ||
*/ | ||
function getNoticeStatus( element ) { | ||
for ( const className of element.classList ) { | ||
if ( NOTICE_CLASS_STATUSES.hasOwnProperty( className ) ) { | ||
return NOTICE_CLASS_STATUSES[ className ]; | ||
} | ||
} | ||
} | ||
|
||
export class AdminNotices extends Component { | ||
componentDidMount() { | ||
this.convertNotices(); | ||
} | ||
|
||
convertNotices() { | ||
const { createNotice } = this.props; | ||
getAdminNotices().forEach( ( element ) => { | ||
// Convert and create. | ||
const status = getNoticeStatus( element ); | ||
const content = getNoticeHTML( element ); | ||
const isDismissible = element.classList.contains( 'is-dismissible' ); | ||
createNotice( status, content, { | ||
speak: false, | ||
__unstableHTML: true, | ||
isDismissible, | ||
} ); | ||
|
||
// Remove (now-redundant) admin notice element. | ||
element.parentNode.removeChild( element ); | ||
} ); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default withDispatch( ( dispatch ) => { | ||
const { createNotice } = dispatch( 'core/notices' ); | ||
|
||
return { createNotice }; | ||
} )( AdminNotices ); |
61 changes: 61 additions & 0 deletions
61
packages/edit-post/src/components/admin-notices/test/index.js
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,61 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import renderer from 'react-test-renderer'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { AdminNotices } from '../'; | ||
|
||
describe( 'AdminNotices', () => { | ||
beforeEach( () => { | ||
// The superfluous whitespace is intentional in verifying expected | ||
// outputs of (a) non-element first child of the element (whitespace | ||
// text node) and (b) untrimmed content. | ||
document.body.innerHTML = ` | ||
<div id="wpbody-content"> | ||
<div class="notice updated is-dismissible"> | ||
<p>My <strong>notice</strong> text</p> | ||
<p>My second line of text</p> | ||
<button type="button" class="notice-dismiss"> | ||
<span class="screen-reader-text">Dismiss this notice.</span> | ||
</button> | ||
</div> | ||
<div class="notice notice-warning">Warning</div> | ||
<aside class="elsewhere"> | ||
<div class="notice">Ignore me</div> | ||
</aside> | ||
</div> | ||
`; | ||
} ); | ||
|
||
it( 'should upgrade notices', () => { | ||
const createNotice = jest.fn(); | ||
|
||
renderer.create( <AdminNotices createNotice={ createNotice } /> ); | ||
|
||
expect( createNotice ).toHaveBeenCalledTimes( 2 ); | ||
expect( createNotice.mock.calls[ 0 ] ).toEqual( [ | ||
'warning', | ||
'Warning', | ||
{ | ||
speak: false, | ||
__unstableHTML: true, | ||
isDismissible: false, | ||
}, | ||
] ); | ||
expect( createNotice.mock.calls[ 1 ] ).toEqual( [ | ||
'success', | ||
'<p>My <strong>notice</strong> text</p><p>My second line of text</p>', | ||
{ | ||
speak: false, | ||
__unstableHTML: true, | ||
isDismissible: true, | ||
}, | ||
] ); | ||
|
||
// Verify all but `<aside>` are removed. | ||
expect( document.getElementById( 'wpbody-content' ).childElementCount ).toBe( 1 ); | ||
} ); | ||
} ); |
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
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
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
Oops, something went wrong.