WordPress.org

Make WordPress Core

Changeset 35483


Ignore:
Timestamp:
11/01/2015 06:39:50 AM (4 years ago)
Author:
westonruter
Message:

Customize: Return user to referring URL when leaving Customizer in absence of return query param.

When referring URL is not available, default returning user to frontend URL instead of admin URL. Themes page is updated to include the return path in Customizer links.

Props McGuive7, westonruter.
Fixes #32637.

Location:
trunk
Files:
4 edited

Legend:

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

    r34651 r35483  
    526526        }
    527527
     528        $customize_action = null;
     529        if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
     530            $customize_action = esc_url( add_query_arg(
     531                array(
     532                    'return' => urlencode( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ),
     533                ),
     534                wp_customize_url( $slug )
     535            ) );
     536        }
     537
    528538        $prepared_themes[ $slug ] = array(
    529539            'id'           => $slug,
     
    541551            'actions'      => array(
    542552                'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
    543                 'customize' => ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) ? wp_customize_url( $slug ) : null,
     553                'customize' => $customize_action,
    544554                'delete'   => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
    545555            ),
  • trunk/src/wp-admin/themes.php

    r35220 r35483  
    388388    <# if ( data.active ) { #>
    389389        <# if ( data.actions.customize ) { #>
    390             <a class="button button-primary customize load-customize hide-if-no-customize" href="{{ data.actions.customize }}"><?php _e( 'Customize' ); ?></a>
     390            <a class="button button-primary customize load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Customize' ); ?></a>
    391391        <# } #>
    392392    <# } else { #>
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r35391 r35483  
    14741474     */
    14751475    public function get_return_url() {
     1476        $referer = wp_get_referer();
    14761477        if ( $this->return_url ) {
    14771478            $return_url = $this->return_url;
     1479        } else if ( $referer ) {
     1480            $return_url = $referer;
    14781481        } else if ( $this->preview_url ) {
    14791482            $return_url = $this->preview_url;
    1480         } else if ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
    1481             $return_url = admin_url( 'themes.php' );
    14821483        } else {
    1483             $return_url = admin_url();
     1484            $return_url = home_url( '/' );
    14841485        }
    14851486        return $return_url;
  • trunk/tests/phpunit/tests/customize/manager.php

    r35307 r35483  
    253253    function test_return_url() {
    254254        wp_set_current_user( self::factory()->user->create( array( 'role' => 'author' ) ) );
    255         $this->assertEquals( get_admin_url(), $this->manager->get_return_url() );
     255        $this->assertEquals( home_url( '/' ), $this->manager->get_return_url() );
    256256
    257257        wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
    258258        $this->assertTrue( current_user_can( 'edit_theme_options' ) );
    259         $this->assertEquals( admin_url( 'themes.php' ), $this->manager->get_return_url() );
     259        $this->assertEquals( home_url( '/' ), $this->manager->get_return_url() );
    260260
    261261        $preview_url = home_url( '/foo/' );
    262262        $this->manager->set_preview_url( $preview_url );
     263        $this->assertEquals( $preview_url, $this->manager->get_return_url() );
     264
     265        $url = home_url( '/referred/' );
     266        $_SERVER['HTTP_REFERER'] = wp_slash( $url );
     267        $this->assertEquals( $url, $this->manager->get_return_url() );
     268
     269        $url = 'http://badreferer.example.com/';
     270        $_SERVER['HTTP_REFERER'] = wp_slash( $url );
     271        $this->assertNotEquals( $url, $this->manager->get_return_url() );
    263272        $this->assertEquals( $preview_url, $this->manager->get_return_url() );
    264273
Note: See TracChangeset for help on using the changeset viewer.