Shortcodes: Add new strip_shortcodes_tagnames filter.

With the new `strip_shortcodes_tagnames` filter you can specify which shortcodes are stripped by `strip_shortcodes()`. The default is all registered shortcodes.

Props DylanAuty, orvils, swissspidy.
Fixes #37767.



git-svn-id: https://develop.svn.wordpress.org/trunk@38877 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Aaron D. Campbell 2016-10-23 14:24:26 +00:00
parent 3a97668d06
commit 73a1aed2cd
2 changed files with 51 additions and 8 deletions

View File

@ -605,7 +605,20 @@ function strip_shortcodes( $content ) {
// Find all registered tag names in $content.
preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
$tags_to_remove = array_keys( $shortcode_tags );
/**
* Filters the list of shortcode tags to remove from the content.
*
* @since 4.7.0
*
* @param array $tag_array Array of shortcode tags to remove.
* @param string $content Content shortcodes are being removed from.
*/
$tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );
$tagnames = array_intersect( $tags_to_remove, $matches[1] );
if ( empty( $tagnames ) ) {
return $content;

View File

@ -340,15 +340,45 @@ EOF;
$this->assertEquals( $test_string, shortcode_unautop( wpautop( $test_string ) ) );
}
/**
* @ticket 10326
*/
function test_strip_shortcodes() {
$this->assertEquals('before', strip_shortcodes('before[gallery]'));
$this->assertEquals('after', strip_shortcodes('[gallery]after'));
$this->assertEquals('beforeafter', strip_shortcodes('before[gallery]after'));
function data_test_strip_shortcodes() {
return array(
array( 'before', 'before[gallery]' ),
array( 'after', '[gallery]after' ),
array( 'beforeafter', 'before[gallery]after' ),
array( 'before[after', 'before[after' ),
array( 'beforeafter', 'beforeafter' ),
array( 'beforeafter', 'before[gallery id="123" size="medium"]after' ),
array( 'before[unregistered_shortcode]after', 'before[unregistered_shortcode]after' ),
array( 'beforeafter', 'before[footag]after' ),
array( 'before after', 'before [footag]content[/footag] after' ),
array( 'before after', 'before [footag foo="123"]content[/footag] after' ),
);
}
/**
* @ticket 10326
*
* @dataProvider data_test_strip_shortcodes
*
* @param string $expected Expected output.
* @param string $content Content to run strip_shortcodes() on.
*/
function test_strip_shortcodes( $expected, $content ) {
$this->assertEquals( $expected, strip_shortcodes( $content ) );
}
/**
* @ticket 37767
*/
function test_strip_shortcodes_filter() {
add_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
$this->assertEquals( 'beforemiddle [footag]after', strip_shortcodes( 'before[gallery]middle [footag]after' ) );
remove_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
}
function _filter_strip_shortcodes_tagnames() {
return array( 'gallery' );
}
// Store passed in shortcode_atts_{$shortcode} args
function _filter_atts( $out, $pairs, $atts ) {