Skip to content
Permalink
Browse files

Themes: In `get_theme_mod()`, only run the `sprintf()` replacement on…

… the default value if there's a string format pattern found in the value.

This prevents standalone percent symbols from being stripped out, e.g. in a default value like `100%`.

Props aristath, kuus, moonomo, westonruter, davetgreen, daviedR, katielgc, noisysocks, SergeyBiryukov.
Fixes #34290.

git-svn-id: https://develop.svn.wordpress.org/trunk@46395 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Oct 5, 2019
1 parent 1be6c41 commit 56b6b1e803bf1c6d45074fe04f597f0ed51144cf
Showing with 70 additions and 1 deletion.
  1. +4 −1 src/wp-includes/theme.php
  2. +66 −0 tests/phpunit/tests/option/themeMods.php
@@ -934,7 +934,10 @@ function get_theme_mod( $name, $default = false ) {
}
if ( is_string( $default ) ) {
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
// Only run the replacement if an sprintf() string format pattern was found.
if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default ) ) {
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
}
}
/** This filter is documented in wp-includes/theme.php */
@@ -32,4 +32,70 @@ function test_theme_mod_remove() {
$this->assertEquals( '', get_theme_mod( 'test_remove' ) );
}
/**
* @ticket 34290
*
* @dataProvider data_theme_mod_default_value_with_percent_symbols
*/
function test_theme_mod_default_value_with_percent_symbols( $default, $expected ) {
$this->assertEquals( $expected, get_theme_mod( 'test_name', $default ) );
}
function data_theme_mod_default_value_with_percent_symbols() {
return array(
array(
'100%',
'100%',
),
array(
'%s',
get_template_directory_uri(),
),
array(
'%s%s',
get_template_directory_uri() . get_stylesheet_directory_uri(),
),
array(
'%1$s%s',
get_template_directory_uri() . get_template_directory_uri(),
),
array(
'%2$s%s',
get_stylesheet_directory_uri() . get_template_directory_uri(),
),
array(
'%1$s%2$s',
get_template_directory_uri() . get_stylesheet_directory_uri(),
),
array(
'%40s%40s',
get_template_directory_uri() . get_stylesheet_directory_uri(),
),
array(
'%%1',
'%%1',
),
array(
'%1%',
'%1%',
),
array(
'1%%',
'1%%',
),
array(
'%%s',
'%%s',
),
array(
'%s%',
get_template_directory_uri(),
),
array(
's%%',
's%%',
),
);
}
}

0 comments on commit 56b6b1e

Please sign in to comment.
You can’t perform that action at this time.