Merge pull request #27172 from woocommerce/fix/27168

Let user stay in the same page after clicking to update WooCommerce
This commit is contained in:
Claudio Sanches 2020-07-30 15:48:27 -03:00 committed by GitHub
commit 608fc8620d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 2 deletions

View File

@ -111,7 +111,7 @@ class WC_Notes_Run_Db_Update {
private static function update_needed_notice( $note_id = null ) {
$update_url = html_entity_decode(
wp_nonce_url(
add_query_arg( 'do_update_woocommerce', 'true', admin_url( 'admin.php?page=wc-settings' ) ),
add_query_arg( 'do_update_woocommerce', 'true', wc_get_current_admin_url() ? wc_get_current_admin_url() : admin_url( 'admin.php?page=wc-settings' ) ),
'wc_db_update',
'wc_db_update_nonce'
)
@ -210,7 +210,7 @@ class WC_Notes_Run_Db_Update {
add_query_arg(
'wc-hide-notice',
'update',
admin_url( 'admin.php?page=wc-settings' )
wc_get_current_admin_url() ? wc_get_current_admin_url() : admin_url( 'admin.php?page=wc-settings' )
),
'woocommerce_hide_notices_nonce',
'_wc_notice_nonce'

View File

@ -474,3 +474,23 @@ function wc_render_invalid_variation_notice( $product_object ) {
<?php
}
}
/**
* Get current admin page URL.
*
* Returns an empty string if it cannot generate a URL.
*
* @internal
* @since 4.4.0
* @return string
*/
function wc_get_current_admin_url() {
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
$uri = preg_replace( '|^.*/wp-admin/|i', '', $uri );
if ( ! $uri ) {
return '';
}
return remove_query_arg( array( '_wpnonce', '_wc_notice_nonce', 'wc_db_update', 'wc_db_update_nonce', 'wc-hide-notice' ), admin_url( $uri ) );
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Unit tests for the WC_Admin_Functions_Test class
*
* @package WooCommerce\Tests\Admin
*/
/**
* Class WC_Admin_Functions_Test_Test
*/
class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
/**
* Load up the importer classes since they aren't loaded by default.
*/
public function setUp() {
parent::setUp();
$bootstrap = \WC_Unit_Tests_Bootstrap::instance();
require_once $bootstrap->plugin_dir . '/includes/admin/wc-admin-functions.php';
}
/**
* Test wc_get_current_admin_url() function.
*/
public function test_wc_get_current_admin_url() {
// Since REQUEST_URI is empty on unit tests it should return an empty string.
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
$this->assertEquals( '', wc_get_current_admin_url() );
}
// Test with REQUEST_URI.
$default_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$_SERVER['REQUEST_URI'] = '/wp-admin/admin.php?page=wc-admin&foo=bar';
$this->assertEquals( admin_url( 'admin.php?page=wc-admin&foo=bar' ), wc_get_current_admin_url() );
// Test if nonce gets removed.
$_SERVER['REQUEST_URI'] = '/wp-admin/admin.php?page=wc-admin&_wpnonce=xxxxxxxxxxxx';
$this->assertEquals( admin_url( 'admin.php?page=wc-admin' ), wc_get_current_admin_url() );
// Restore REQUEST_URI.
$_SERVER['REQUEST_URI'] = $default_uri;
}
}