From 73a1aed2cdab89189639dd36733a396880d1bdbd Mon Sep 17 00:00:00 2001 From: "Aaron D. Campbell" Date: Sun, 23 Oct 2016 14:24:26 +0000 Subject: [PATCH] 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 --- src/wp-includes/shortcodes.php | 15 ++++++++++- tests/phpunit/tests/shortcode.php | 44 ++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/shortcodes.php b/src/wp-includes/shortcodes.php index f0523622cc..3af0e10f58 100644 --- a/src/wp-includes/shortcodes.php +++ b/src/wp-includes/shortcodes.php @@ -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; diff --git a/tests/phpunit/tests/shortcode.php b/tests/phpunit/tests/shortcode.php index e807f9539e..ec25f76fea 100644 --- a/tests/phpunit/tests/shortcode.php +++ b/tests/phpunit/tests/shortcode.php @@ -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 ) {