Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/wp-admin/includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1876,14 +1876,24 @@ function add_settings_error( $setting, $code, $message, $type = 'error' ) {
);
}

/**
* Stores settings errors for retrieval on the next Network Admin page load.
*
* @since x.x.x
*/
function set_network_settings_errors() {
set_site_transient( 'settings_errors', get_settings_errors(), 30 );
}

/**
* Fetches settings errors registered by add_settings_error().
*
* Checks the $wp_settings_errors array for any errors declared during the current
* pageload and returns them.
*
* If changes were just submitted ($_GET['settings-updated']) and settings errors were saved
* to the 'settings_errors' transient then those errors will be returned instead. This
* to the 'settings_errors' transient then those errors will be returned instead. In Network
* Admin, the same applies to $_GET['updated'] and the 'settings_errors' site transient. This
* is used to pass errors back across pageloads.
*
* Use the $sanitize argument to manually re-sanitize the option before returning errors.
Expand All @@ -1892,6 +1902,7 @@ function add_settings_error( $setting, $code, $message, $type = 'error' ) {
* action hook).
*
* @since 3.0.0
* @since x.x.x Added support for network settings errors stored in a site transient.
*
* @global array[] $wp_settings_errors Storage array of errors registered during this pageload
*
Expand Down Expand Up @@ -1924,10 +1935,13 @@ function get_settings_errors( $setting = '', $sanitize = false ) {
sanitize_option( $setting, get_option( $setting ) );
}

// If settings were passed back from options.php then use them.
// If settings were passed back from options.php or network/settings.php then use them.
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
$wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
delete_transient( 'settings_errors' );
} elseif ( is_network_admin() && isset( $_GET['updated'] ) && 'true' === $_GET['updated'] && get_site_transient( 'settings_errors' ) ) {
$wp_settings_errors = array_merge( (array) $wp_settings_errors, get_site_transient( 'settings_errors' ) );
delete_site_transient( 'settings_errors' );
}

// Check global in case errors have been added on this pageload.
Expand Down
24 changes: 16 additions & 8 deletions src/wp-admin/network/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,29 @@
*/
do_action( 'update_wpmu_options' );

set_network_settings_errors();

wp_redirect( add_query_arg( 'updated', 'true', network_admin_url( 'settings.php' ) ) );
exit;
}

require_once ABSPATH . 'wp-admin/admin-header.php';

if ( isset( $_GET['updated'] ) ) {
wp_admin_notice(
__( 'Settings saved.' ),
array(
'type' => 'success',
'dismissible' => true,
'id' => 'message',
)
);
$settings_errors = get_settings_errors();

if ( $settings_errors ) {
settings_errors();
} elseif ( 'true' === $_GET['updated'] ) {
wp_admin_notice(
__( 'Settings saved.' ),
array(
'type' => 'success',
'dismissible' => true,
'id' => 'message',
)
);
}
}
?>

Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/admin/includesTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,46 @@ public function test_get_settings_errors_sources() {
$wp_settings_errors = null;
}

/**
* @ticket 18088
* @covers ::get_settings_errors
* @covers ::set_network_settings_errors
* @global array $wp_settings_errors
*/
public function test_get_settings_errors_from_network_transient() {
global $current_screen, $wp_settings_errors;

$previous_screen = $current_screen;
$error = array(
'setting' => 'new_admin_email',
'code' => 'invalid_new_admin_email',
'message' => 'Invalid email address.',
'type' => 'error',
);

set_current_screen( 'dashboard-network' );
$wp_settings_errors = null;
add_settings_error( $error['setting'], $error['code'], $error['message'], $error['type'] );
set_network_settings_errors();

$_GET['updated'] = 'false';
$wp_settings_errors = null;
$errors_when_false = get_settings_errors();

$_GET['updated'] = 'true';
$wp_settings_errors = null;
$errors_when_true = get_settings_errors();
$transient = get_site_transient( 'settings_errors' );

unset( $_GET['updated'] );
$current_screen = $previous_screen;
$wp_settings_errors = null;

$this->assertSame( array(), $errors_when_false );
$this->assertSame( array( $error ), $errors_when_true );
$this->assertFalse( $transient );
}

/**
* @ticket 44941
* @covers ::settings_errors
Expand Down
Loading