Shortcodes: prevent registration of invalid shortcode names.

Adds unit tests.

Props miqrogroove.
Fixes #34090.


git-svn-id: https://develop.svn.wordpress.org/trunk@34745 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-10-01 17:33:58 +00:00
parent 3c50d18785
commit 1b70f27b04
2 changed files with 90 additions and 0 deletions

View File

@ -88,6 +88,19 @@ $shortcode_tags = array();
*/
function add_shortcode($tag, $func) {
global $shortcode_tags;
if ( '' == trim( $tag ) ) {
$message = __( 'Invalid shortcode name. Empty name given.' );
_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
return;
}
if ( 0 !== preg_match( '@[<>&/\[\]\x00-\x20]@', $tag ) ) {
$message = sprintf( __( 'Invalid shortcode name: %s Do not use spaces or reserved chars: & / < > [ ]' ), $tag );
_doing_it_wrong( __FUNCTION__, $message, '4.4.0' );
return;
}
$shortcode_tags[ $tag ] = $func;
}

View File

@ -539,4 +539,81 @@ EOF;
$this->assertTrue( has_shortcode( $content_nested, 'gallery' ) );
remove_shortcode( 'foo' );
}
/**
* Make sure invalid shortcode names are not allowed.
*
* @dataProvider data_registration_bad
* @expectedIncorrectUsage add_shortcode
*/
function test_registration_bad( $input, $expected ) {
return $this->sub_registration( $input, $expected );
}
/**
* Make sure valid shortcode names are allowed.
*
* @dataProvider data_registration_good
*/
function test_registration_good( $input, $expected ) {
return $this->sub_registration( $input, $expected );
}
function sub_registration( $input, $expected ) {
add_shortcode( $input, '' );
$actual = shortcode_exists( $input );
$test = $this->assertEquals( $expected, $actual );
if ( $actual ) remove_shortcode( $input );
return $test;
}
function data_registration_bad() {
return array(
array(
'<html>',
false,
),
array(
'[shortcode]',
false,
),
array(
'bad/',
false,
),
array(
'/bad',
false,
),
array(
'bad space',
false,
),
array(
'&amp;',
false,
),
array(
'',
false,
),
);
}
function data_registration_good() {
return array(
array(
'good!',
true,
),
array(
'plain',
true,
),
array(
'unreserved!#$%()*+,-.;?@^_{|}~chars',
true,
),
);
}
}