Make WordPress Core

Ticket #31093: 31093.5.diff

File 31093.5.diff, 2.3 KB (added by birgire, 7 years ago)
  • src/wp-includes/shortcodes.php

    diff --git src/wp-includes/shortcodes.php src/wp-includes/shortcodes.php
    index c6df28b..565207e 100644
    function shortcode_exists( $tag ) { 
    127127}
    128128
    129129/**
    130  * Whether the passed content contains the specified shortcode
     130 * Whether the passed content contains any or specified shortcode.
    131131 *
    132132 * @since 3.6.0
    133133 *
    134134 * @global array $shortcode_tags
    135135 *
    136136 * @param string $content Content to search for shortcodes.
    137  * @param string $tag     Shortcode tag to check.
    138  * @return bool Whether the passed content contains the given shortcode.
     137 * @param string $tag     Shortcode tag to check. Default Empty string.
     138 * @return bool  $return  Whether the passed content contains any or specified shortcode.
    139139 */
    140 function has_shortcode( $content, $tag ) {
     140function has_shortcode( $content, $tag = '' ) {
    141141        if ( false === strpos( $content, '[' ) ) {
    142142                return false;
    143143        }
     144       
     145        if ( empty( $tag ) ) {
     146                return strip_shortcodes( $content ) !== $content;
     147        }
    144148
    145149        if ( shortcode_exists( $tag ) ) {
    146150                preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
  • tests/phpunit/tests/shortcode.php

    diff --git tests/phpunit/tests/shortcode.php tests/phpunit/tests/shortcode.php
    index 08ef1ac..8dba3f8 100644
    EOF; 
    582582        }
    583583
    584584        /**
     585         * Tests if has_shortcode() finds any shortcode, when tag input is empty
     586         *
     587         * @ticket 31093
     588         */
     589        public function test_has_shortcode_should_find_any_shortcode_with_empty_tag_input() {
     590                $content = 'This is a blob with [gallery] in it';
     591                $this->assertTrue( has_shortcode( $content ) );
     592
     593                $content = 'This is a blob without a shortcode in it';
     594                $this->assertFalse( has_shortcode( $content ) );
     595
     596                $content = 'This is a blob with an [foo] non-existing shortcode in it';
     597                $this->assertFalse( has_shortcode( $content ) );
     598
     599                $content_nested = 'This is a blob with [foo] [gallery] [/foo] in it';
     600                $this->assertTrue( has_shortcode( $content_nested ) );
     601
     602                add_shortcode( 'foo', '__return_empty_string' );
     603                $content = 'This is a blob with a [foo] shortcode in it';
     604                $this->assertTrue( has_shortcode( $content ) );
     605                remove_shortcode( 'foo' );
     606        }
     607       
     608        /**
    585609         * Make sure invalid shortcode names are not allowed.
    586610         *
    587611         * @dataProvider data_registration_bad