Make WordPress Core

Changeset 38877


Ignore:
Timestamp:
10/23/2016 02:24:26 PM (8 years ago)
Author:
aaroncampbell
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/shortcodes.php

    r38713 r38877  
    606606    // Find all registered tag names in $content.
    607607    preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
    608     $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
     608
     609    $tags_to_remove = array_keys( $shortcode_tags );
     610
     611    /**
     612     * Filters the list of shortcode tags to remove from the content.
     613     *
     614     * @since 4.7.0
     615     *
     616     * @param array  $tag_array Array of shortcode tags to remove.
     617     * @param string $content   Content shortcodes are being removed from.
     618     */
     619    $tags_to_remove = apply_filters( 'strip_shortcodes_tagnames', $tags_to_remove, $content );
     620
     621    $tagnames = array_intersect( $tags_to_remove, $matches[1] );
    609622
    610623    if ( empty( $tagnames ) ) {
  • trunk/tests/phpunit/tests/shortcode.php

    r38713 r38877  
    341341    }
    342342
     343    function data_test_strip_shortcodes() {
     344        return array(
     345            array( 'before', 'before[gallery]' ),
     346            array( 'after', '[gallery]after' ),
     347            array( 'beforeafter', 'before[gallery]after' ),
     348            array( 'before[after', 'before[after' ),
     349            array( 'beforeafter', 'beforeafter' ),
     350            array( 'beforeafter', 'before[gallery id="123" size="medium"]after' ),
     351            array( 'before[unregistered_shortcode]after', 'before[unregistered_shortcode]after' ),
     352            array( 'beforeafter', 'before[footag]after' ),
     353            array( 'before  after', 'before [footag]content[/footag] after' ),
     354            array( 'before  after', 'before [footag foo="123"]content[/footag] after' ),
     355        );
     356    }
     357
    343358    /**
    344359     * @ticket 10326
    345      */
    346     function test_strip_shortcodes() {
    347         $this->assertEquals('before', strip_shortcodes('before[gallery]'));
    348         $this->assertEquals('after', strip_shortcodes('[gallery]after'));
    349         $this->assertEquals('beforeafter', strip_shortcodes('before[gallery]after'));
    350     }
    351 
     360     *
     361     * @dataProvider data_test_strip_shortcodes
     362     *
     363     * @param string $expected  Expected output.
     364     * @param string $content   Content to run strip_shortcodes() on.
     365     */
     366    function test_strip_shortcodes( $expected, $content ) {
     367        $this->assertEquals( $expected, strip_shortcodes( $content ) );
     368    }
     369
     370    /**
     371     * @ticket 37767
     372     */
     373    function test_strip_shortcodes_filter() {
     374        add_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
     375        $this->assertEquals( 'beforemiddle [footag]after', strip_shortcodes( 'before[gallery]middle [footag]after' ) );
     376        remove_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
     377    }
     378
     379    function _filter_strip_shortcodes_tagnames() {
     380        return array( 'gallery' );
     381    }
    352382
    353383    // Store passed in shortcode_atts_{$shortcode} args
Note: See TracChangeset for help on using the changeset viewer.