Skip to content

Commit 7ee5af1

Browse files
committed
Customize: Account for existing query params in the admin URL when wp_customize_url() adds the theme query param.
Query parameters may be inserted in the initial `customize.php` URL via the `admin_url` and `site_url` filters. Props xipasduarte, westonruter. Fixes #63632. git-svn-id: https://develop.svn.wordpress.org/trunk@60499 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a3b3207 commit 7ee5af1

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/wp-includes/theme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3771,7 +3771,7 @@ function _wp_customize_loader_settings() {
37713771
function wp_customize_url( $stylesheet = '' ) {
37723772
$url = admin_url( 'customize.php' );
37733773
if ( $stylesheet ) {
3774-
$url .= '?theme=' . urlencode( $stylesheet );
3774+
$url = add_query_arg( 'theme', urlencode( $stylesheet ), $url );
37753775
}
37763776
return esc_url( $url );
37773777
}

tests/phpunit/tests/theme/wpTheme.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,101 @@ public function test_get_files_nonexistent_theme() {
375375
$this->assertEmpty( $files );
376376
}
377377

378+
/**
379+
* Test wp_customize_url with no $stylesheet argument.
380+
*
381+
* @ticket 63632
382+
*
383+
* @covers ::wp_customize_url
384+
*/
385+
public function test_wp_customize_url_no_stylesheet() {
386+
$this->assertSame( esc_url( admin_url( 'customize.php' ) ), wp_customize_url() );
387+
}
388+
389+
/**
390+
* Test wp_customize_url with no query args.
391+
*
392+
* @ticket 63632
393+
*
394+
* @covers ::wp_customize_url
395+
*/
396+
public function test_wp_customize_url_without_query_args() {
397+
$this->assertSame( esc_url( admin_url( 'customize.php?theme=foo' ) ), wp_customize_url( 'foo' ) );
398+
}
399+
400+
/**
401+
* Test wp_customize_url with existing query args.
402+
*
403+
* @ticket 63632
404+
*
405+
* @covers ::wp_customize_url
406+
*/
407+
public function test_wp_customize_url_with_existing_query_args() {
408+
$clean_admin_url = admin_url( 'customize.php' );
409+
410+
// Ensure the existing query arg is present in the URL.
411+
add_filter(
412+
'admin_url',
413+
function ( $url ) {
414+
return add_query_arg( 'existing_arg', 'value', $url );
415+
}
416+
);
417+
$this->assertSame( esc_url( $clean_admin_url . '?existing_arg=value&theme=foo' ), wp_customize_url( 'foo' ) );
418+
}
419+
420+
/**
421+
* Test wp_customize_url with existing theme query arg.
422+
*
423+
* @ticket 63632
424+
*
425+
* @covers ::wp_customize_url
426+
*/
427+
public function test_wp_customize_url_with_existing_theme_query_arg() {
428+
$clean_admin_url = admin_url( 'customize.php' );
429+
430+
// Ensure the theme query arg is replaced with the new value.
431+
add_filter(
432+
'admin_url',
433+
function ( $url ) {
434+
return add_query_arg( 'theme', 'to-be-replaced', $url );
435+
}
436+
);
437+
$this->assertSame( esc_url( $clean_admin_url . '?theme=foo' ), wp_customize_url( 'foo' ) );
438+
}
439+
440+
/**
441+
* Test wp_customize_url with multiple theme query args in array syntax.
442+
*
443+
* @ticket 63632
444+
*
445+
* @covers ::wp_customize_url
446+
*/
447+
public function test_wp_customize_url_with_multiple_theme_query_args() {
448+
$clean_admin_url = admin_url( 'customize.php' );
449+
450+
// Ensure the theme query arg is replaced with the new value.
451+
add_filter(
452+
'admin_url',
453+
function ( $url ) {
454+
return add_query_arg( array( 'theme' => array( 'to-be-replaced-1', 'to-be-replaced-2' ) ), $url );
455+
}
456+
);
457+
$this->assertSame( esc_url( $clean_admin_url . '?theme=foo' ), wp_customize_url( 'foo' ) );
458+
}
459+
460+
/**
461+
* Test wp_customize_url with special characters in the theme name.
462+
*
463+
* @ticket 63632
464+
*
465+
* @covers ::wp_customize_url
466+
*/
467+
public function test_wp_customize_url_with_special_chars() {
468+
$stylesheet = 'foo!@-_ +';
469+
$expected = admin_url( 'customize.php?theme=' . urlencode( $stylesheet ) );
470+
$this->assertSame( esc_url( $expected ), wp_customize_url( $stylesheet ) );
471+
}
472+
378473
/**
379474
* Data provider.
380475
*

0 commit comments

Comments
 (0)