-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Run wp.oldEditor.removep()
when transforming shortcodes
#8077
Changes from all commits
7a55d99
70790bd
b9429e9
7deaecd
e75c8ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { removep, autop } from '@wordpress/autop'; | ||
import { RawHTML } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { Dashicon } from '@wordpress/components'; | ||
|
@@ -46,7 +47,7 @@ export const settings = { | |
text: { | ||
type: 'string', | ||
shortcode: ( attrs, { content } ) => { | ||
return content; | ||
return removep( autop( content ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not evident why we're doing this. An inline code comment would have been appropriate. |
||
}, | ||
}, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/shortcode` block. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Performs wpautop() on the shortcode block content. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be improved to explain why we're Alternatively, it could be considered an implementation detail that the block's render applies autop, not necessarily appropriate for the callback function. In other words, this function should be documented along lines of "Renders the shortcode", and the implementation detail explained by an inline code comment. |
||
* | ||
* @param array $attributes The block attributes. | ||
* @param string $content The block content. | ||
* | ||
* @return string Returns the block content. | ||
*/ | ||
function render_block_core_shortcode( $attributes, $content ) { | ||
return wpautop( $content ); | ||
} | ||
|
||
/** | ||
* Registers the `core/shortcode` block on server. | ||
*/ | ||
function register_block_core_shortcode() { | ||
register_block_type( 'core/shortcode', array( | ||
'render_callback' => 'render_block_core_shortcode', | ||
) ); | ||
} | ||
|
||
add_action( 'init', 'register_block_core_shortcode' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,11 @@ import createSelector from 'rememo'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { serialize, getBlockType, getBlockTypes, hasBlockSupport, hasChildBlocks } from '@wordpress/blocks'; | ||
import { serialize, getBlockType, getBlockTypes, hasBlockSupport, hasChildBlocks, getUnknownTypeHandlerName } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { moment } from '@wordpress/date'; | ||
import deprecated from '@wordpress/deprecated'; | ||
import { removep } from '@wordpress/autop'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/*** | ||
* Module constants | ||
|
@@ -1352,7 +1353,13 @@ export const getEditedPostContent = createSelector( | |
return edits.content; | ||
} | ||
|
||
return serialize( getBlocks( state ) ); | ||
const blocks = getBlocks( state ); | ||
|
||
if ( blocks.length === 1 && blocks[ 0 ].name === getUnknownTypeHandlerName() ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not evident why we're doing this. An inline code comment would have been appropriate. |
||
return removep( serialize( blocks ) ); | ||
} | ||
|
||
return serialize( blocks ); | ||
}, | ||
( state ) => [ | ||
state.editor.present.edits.content, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,26 @@ function test_do_blocks_removes_comments() { | |
|
||
$this->assertEquals( $expected_html, $actual_html ); | ||
} | ||
|
||
/** | ||
* Test that shortcode blocks get the same HTML as shortcodes in Classic content. | ||
*/ | ||
function test_the_content() { | ||
add_shortcode( 'someshortcode', array( $this, 'handle_shortcode' ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not as familiar with PHP testing, but should we be tearing down anything we create, i.e. a complementing |
||
|
||
$classic_content = "Foo\n\n[someshortcode]\n\nBar\n\n[/someshortcode]\n\nBaz"; | ||
$block_content = "<!-- wp:core/paragraph -->\n<p>Foo</p>\n<!-- /wp:core/paragraph -->\n\n<!-- wp:core/shortcode -->[someshortcode]\n\nBar\n\n[/someshortcode]<!-- /wp:core/shortcode -->\n\n<!-- wp:core/paragraph -->\n<p>Baz</p>\n<!-- /wp:core/paragraph -->"; | ||
|
||
$classic_filtered_content = apply_filters( 'the_content', $classic_content ); | ||
$block_filtered_content = apply_filters( 'the_content', $block_content ); | ||
|
||
// Block rendering add some extra blank lines, but we're not worried about them. | ||
$block_filtered_content = preg_replace( "/\n{2,}/", "\n", $block_filtered_content ); | ||
|
||
$this->assertEquals( $classic_filtered_content, $block_filtered_content ); | ||
} | ||
|
||
function handle_shortcode( $atts, $content ) { | ||
return $content; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core-blocks
does not have a dependency defined forwp-autop
, so its existence is purely incidental and this could be prone to future breakage.